2020-05-15 20:15:35 +03:00
|
|
|
module Examples.Confetti exposing (example, State, Msg)
|
|
|
|
|
|
|
|
{-|
|
|
|
|
|
|
|
|
@docs example, State, Msg
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
2020-05-15 20:33:38 +03:00
|
|
|
import Browser.Events
|
2020-05-15 20:15:35 +03:00
|
|
|
import Category exposing (Category(..))
|
2020-05-15 20:51:21 +03:00
|
|
|
import Css exposing (Color)
|
|
|
|
import Dict exposing (Dict)
|
2020-05-15 20:15:35 +03:00
|
|
|
import Example exposing (Example)
|
2020-05-15 20:51:21 +03:00
|
|
|
import Html.Styled as Html exposing (Html)
|
2020-05-15 20:15:35 +03:00
|
|
|
import Html.Styled.Attributes as Attributes exposing (css)
|
|
|
|
import Nri.Ui.Button.V10 as Button
|
|
|
|
import Nri.Ui.Colors.V1 as Colors
|
|
|
|
import Nri.Ui.Confetti.V1 as Confetti
|
2020-05-15 20:51:21 +03:00
|
|
|
import Nri.Ui.TextArea.V4 as TextArea
|
2020-05-15 20:15:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
example : Example State Msg
|
|
|
|
example =
|
|
|
|
{ name = "Nri.Ui.Confetti.V1"
|
|
|
|
, categories = [ Animations ]
|
|
|
|
, state = init
|
|
|
|
, update = update
|
2020-05-15 20:33:38 +03:00
|
|
|
, subscriptions =
|
|
|
|
\state ->
|
|
|
|
Sub.batch
|
|
|
|
[ Browser.Events.onResize WindowResized
|
|
|
|
, Confetti.subscriptions ConfettiMsg state.confetti
|
|
|
|
]
|
2020-05-15 20:15:35 +03:00
|
|
|
, view =
|
|
|
|
\state ->
|
2020-05-15 20:51:21 +03:00
|
|
|
[ confettiWordsInput state.confettiWords
|
|
|
|
, Button.button "Launch confetti!"
|
2020-05-15 20:15:35 +03:00
|
|
|
[ Button.onClick LaunchConfetti
|
|
|
|
, Button.small
|
|
|
|
, Button.secondary
|
|
|
|
]
|
2020-05-15 20:33:38 +03:00
|
|
|
, Confetti.view state.confetti
|
2020-05-15 20:15:35 +03:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-15 20:51:21 +03:00
|
|
|
confettiWordsInput : String -> Html Msg
|
|
|
|
confettiWordsInput confettiWords =
|
|
|
|
Html.div [ css [ Css.width (Css.px 600), Css.marginBottom (Css.px 10) ] ]
|
|
|
|
[ TextArea.writing
|
|
|
|
{ value = confettiWords
|
|
|
|
, autofocus = False
|
|
|
|
, onInput = OnInput
|
|
|
|
, onBlur = Nothing
|
|
|
|
, isInError = False
|
|
|
|
, label = "Confetti Words"
|
|
|
|
, height = TextArea.Fixed
|
|
|
|
, placeholder = ""
|
|
|
|
, showLabel = True
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-05-15 20:15:35 +03:00
|
|
|
{-| -}
|
|
|
|
type alias State =
|
2020-05-15 20:54:43 +03:00
|
|
|
{ confetti : Confetti.Model
|
2020-05-15 20:51:21 +03:00
|
|
|
, confettiWords : String
|
2020-05-15 20:15:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
init : State
|
|
|
|
init =
|
2020-05-15 20:51:21 +03:00
|
|
|
{ confetti = Confetti.init 700
|
|
|
|
, confettiWords = "lemonade iced tea coca-cola pepsi"
|
2020-05-15 20:15:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
type Msg
|
|
|
|
= LaunchConfetti
|
|
|
|
| ConfettiMsg Confetti.Msg
|
2020-05-15 20:33:38 +03:00
|
|
|
| WindowResized Int Int
|
2020-05-15 20:51:21 +03:00
|
|
|
| OnInput String
|
2020-05-15 20:15:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
update : Msg -> State -> ( State, Cmd Msg )
|
|
|
|
update msg state =
|
|
|
|
let
|
|
|
|
words =
|
2020-05-15 20:51:21 +03:00
|
|
|
List.indexedMap
|
|
|
|
(\index word -> { color = getColor index, text = word })
|
|
|
|
(String.words state.confettiWords)
|
2020-05-15 20:15:35 +03:00
|
|
|
in
|
|
|
|
case msg of
|
|
|
|
LaunchConfetti ->
|
2020-05-15 20:33:38 +03:00
|
|
|
( { state | confetti = Confetti.burst words state.confetti }
|
2020-05-15 20:15:35 +03:00
|
|
|
, Cmd.none
|
|
|
|
)
|
|
|
|
|
|
|
|
ConfettiMsg confettiMsg ->
|
2020-05-15 20:33:38 +03:00
|
|
|
( { state | confetti = Confetti.update confettiMsg state.confetti }
|
|
|
|
, Cmd.none
|
|
|
|
)
|
|
|
|
|
|
|
|
WindowResized width _ ->
|
|
|
|
( { state | confetti = Confetti.updateCenter (toFloat (width // 2)) state.confetti }
|
2020-05-15 20:15:35 +03:00
|
|
|
, Cmd.none
|
|
|
|
)
|
2020-05-15 20:51:21 +03:00
|
|
|
|
|
|
|
OnInput confettiWords ->
|
|
|
|
( { state | confettiWords = confettiWords }
|
|
|
|
, Cmd.none
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
getColor : Int -> Color
|
|
|
|
getColor key =
|
|
|
|
let
|
|
|
|
dict =
|
|
|
|
List.indexedMap (\i c -> ( i, c ))
|
|
|
|
[ Colors.highlightBlue
|
|
|
|
, Colors.highlightBlueDark
|
|
|
|
, Colors.highlightCyan
|
|
|
|
, Colors.highlightCyanDark
|
|
|
|
, Colors.highlightGreen
|
|
|
|
, Colors.highlightGreenDark
|
|
|
|
, Colors.highlightMagenta
|
|
|
|
, Colors.highlightMagentaDark
|
|
|
|
, Colors.highlightYellow
|
|
|
|
, Colors.highlightYellowDark
|
|
|
|
]
|
|
|
|
|> Dict.fromList
|
|
|
|
in
|
|
|
|
Maybe.withDefault Colors.highlightYellow (Dict.get key dict)
|