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

160 lines
4.0 KiB
Elm
Raw Normal View History

2020-03-31 22:43:32 +03:00
module Examples.Slide exposing (Msg, State, example)
2019-04-11 22:54:14 +03:00
{-|
2020-03-31 22:43:32 +03:00
@docs Msg, State, example
2019-04-11 22:54:14 +03:00
-}
import Accessibility.Styled as Html
import Category exposing (Category(..))
2019-04-11 22:54:14 +03:00
import Css
2020-03-31 23:20:03 +03:00
import Example exposing (Example)
2019-04-11 22:54:14 +03:00
import Html.Styled.Attributes exposing (css)
import Html.Styled.Keyed as Keyed
2019-09-04 12:03:03 +03:00
import List.Zipper as Zipper exposing (Zipper)
2019-04-11 22:54:14 +03:00
import Nri.Ui.Button.V8 as Button
import Nri.Ui.Colors.V1 as Colors
import Nri.Ui.Slide.V1 as Slide
{-| -}
type Msg
= TriggerAnimation Slide.AnimationDirection
{-| -}
type alias State =
{ direction : Slide.AnimationDirection
2019-04-12 20:43:49 +03:00
, panels : Zipper Panel
2019-04-11 22:54:14 +03:00
, previous : Maybe Panel
}
{-| -}
2020-03-31 23:20:03 +03:00
example : Example State Msg
2020-03-31 22:43:32 +03:00
example =
2019-05-03 19:56:43 +03:00
{ name = "Nri.Ui.Slide.V1"
2020-03-31 22:43:32 +03:00
, categories = [ Animations ]
, state = init
, update = update
2020-03-31 22:48:26 +03:00
, subscriptions = \_ -> Sub.none
2020-03-31 22:43:32 +03:00
, view =
\state ->
[ Keyed.node "div"
[ css
[ Slide.withSlidingContents
, Css.border3 (Css.px 3) Css.solid Colors.gray75
, Css.padding (Css.px 20)
, Css.width (Css.px 600)
2019-04-11 22:54:14 +03:00
]
2020-03-31 22:43:32 +03:00
]
(case state.previous of
Just previousPanel ->
[ viewPanel previousPanel (Slide.animateOut state.direction)
, viewPanel (Zipper.current state.panels) (Slide.animateIn state.direction)
]
2019-04-11 22:54:14 +03:00
2020-03-31 22:43:32 +03:00
Nothing ->
[ viewPanel (Zipper.current state.panels) (Css.batch [])
]
)
, Html.div
[ css
[ Css.displayFlex
, Css.justifyContent Css.spaceBetween
, Css.marginTop (Css.px 20)
, Css.width (Css.px 300)
2019-04-11 22:54:14 +03:00
]
2020-03-31 22:43:32 +03:00
]
[ triggerAnimation Slide.FromLTR "Left-to-right"
, triggerAnimation Slide.FromRTL "Right-to-left"
2019-04-11 22:54:14 +03:00
]
]
}
{-| -}
init : State
init =
{ direction = Slide.FromRTL
2019-09-04 12:03:03 +03:00
, panels = Zipper.from [] One [ Two, Three ]
2019-04-11 22:54:14 +03:00
, previous = Nothing
}
{-| -}
update : Msg -> State -> ( State, Cmd Msg )
update msg state =
case msg of
TriggerAnimation direction ->
( { state
| direction = direction
2019-04-12 20:43:49 +03:00
, panels =
case direction of
Slide.FromRTL ->
Zipper.next state.panels
|> Maybe.withDefault (Zipper.first state.panels)
Slide.FromLTR ->
Zipper.previous state.panels
|> Maybe.withDefault (Zipper.last state.panels)
, previous = Just (Zipper.current state.panels)
2019-04-11 22:54:14 +03:00
}
, Cmd.none
)
-- INTERNAL
type Panel
= One
| Two
| Three
viewPanel : Panel -> Css.Style -> ( String, Html.Html msg )
viewPanel panel animation =
let
( color, text, key ) =
case panel of
One ->
2019-11-16 02:26:41 +03:00
( Colors.redDark, "Panel One", "panel-1" )
2019-04-11 22:54:14 +03:00
Two ->
2019-11-16 02:26:41 +03:00
( Colors.ochre, "Panel Two", "panel-2" )
2019-04-11 22:54:14 +03:00
Three ->
( Colors.green, "Panel Three", "panel-3" )
in
( key
, Html.div
[ css
[ Css.border3 (Css.px 2) Css.dashed color
, Css.color color
, Css.padding (Css.px 10)
, Css.width (Css.px 100)
, Css.textAlign Css.center
, animation
]
]
[ Html.text text
]
)
triggerAnimation : Slide.AnimationDirection -> String -> Html.Html Msg
triggerAnimation direction label =
Button.button
{ onClick = TriggerAnimation direction
, size = Button.Small
, style = Button.Secondary
, width = Button.WidthUnbounded
}
{ label = label
, state = Button.Enabled
, icon = Nothing
}