noredink-ui/styleguide-app/Examples/Confetti.elm

86 lines
1.8 KiB
Elm
Raw Normal View History

2020-05-15 20:15:35 +03:00
module Examples.Confetti exposing (example, State, Msg)
{-|
@docs example, State, Msg
-}
import Browser.Events
2020-05-15 20:15:35 +03:00
import Category exposing (Category(..))
import Css
import Example exposing (Example)
import Html.Styled as Html
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
{-| -}
example : Example State Msg
example =
{ name = "Nri.Ui.Confetti.V1"
, categories = [ Animations ]
, state = init
, update = update
, subscriptions =
\state ->
Sub.batch
[ Browser.Events.onResize WindowResized
, Confetti.subscriptions ConfettiMsg state.confetti
]
2020-05-15 20:15:35 +03:00
, view =
\state ->
[ Button.button "Launch confetti!"
[ Button.onClick LaunchConfetti
, Button.small
, Button.secondary
]
, Confetti.view state.confetti
2020-05-15 20:15:35 +03:00
]
}
{-| -}
type alias State =
{ confetti : Confetti.System
2020-05-15 20:15:35 +03:00
}
init : State
init =
{ confetti = Confetti.init 400
2020-05-15 20:15:35 +03:00
}
{-| -}
type Msg
= LaunchConfetti
| ConfettiMsg Confetti.Msg
| WindowResized Int Int
2020-05-15 20:15:35 +03:00
{-| -}
update : Msg -> State -> ( State, Cmd Msg )
update msg state =
let
words =
[ { color = Colors.azure, text = "Hello!" } ]
in
case msg of
LaunchConfetti ->
( { state | confetti = Confetti.burst words state.confetti }
2020-05-15 20:15:35 +03:00
, Cmd.none
)
ConfettiMsg confettiMsg ->
( { 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
)