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

149 lines
4.1 KiB
Elm
Raw Normal View History

module Examples.Balloon exposing (example, State, Msg)
{-|
@docs example, State, Msg
-}
import Category exposing (Category(..))
import Css
2021-09-29 00:26:37 +03:00
import Debug.Control as Control exposing (Control)
import Example exposing (Example)
import Examples.IconExamples as IconExamples
2021-09-29 00:26:37 +03:00
import Html.Styled exposing (Html, div, fromUnstyled, text)
2021-09-29 00:39:13 +03:00
import Html.Styled.Attributes exposing (css)
import KeyboardSupport exposing (Direction(..), Key(..))
import Nri.Ui.Balloon.V1 as Balloon
import Nri.Ui.Colors.V1 as Colors
{-| -}
example : Example State Msg
example =
{ name = "Balloon"
, version = 1
, categories = [ Messaging ]
, keyboardSupport = []
2021-09-29 00:26:37 +03:00
, state = init
, update = update
, subscriptions = \_ -> Sub.none
, view = view
}
2021-09-29 00:26:37 +03:00
{-| -}
type alias State =
Control Settings
type alias Settings =
{ copy : String
2021-09-29 01:13:54 +03:00
, theme : Maybe ( String, Balloon.Attribute )
, position : Maybe ( String, Balloon.Attribute )
, width : Maybe ( String, Balloon.Attribute )
2021-09-29 01:15:41 +03:00
, padding : Maybe ( String, Balloon.Attribute )
2021-09-29 00:26:37 +03:00
}
2021-09-29 00:39:13 +03:00
init : Control Settings
2021-09-29 00:26:37 +03:00
init =
Control.record Settings
2021-09-29 00:39:13 +03:00
|> Control.field "copy" (Control.string "Hello, world!")
|> Control.field "theme" (Control.maybe False themeOptions)
|> Control.field "position" (Control.maybe False positionOptions)
2021-09-29 01:09:32 +03:00
|> Control.field "width" (Control.maybe False widthOptions)
2021-09-29 01:15:41 +03:00
|> Control.field "padding" (Control.maybe False paddingOptions)
2021-09-29 00:39:13 +03:00
2021-09-29 01:13:54 +03:00
themeOptions : Control ( String, Balloon.Attribute )
2021-09-29 00:39:13 +03:00
themeOptions =
Control.choice
[ ( "green", Control.value ( "Balloon.green", Balloon.green ) )
, ( "purple", Control.value ( "Balloon.purple", Balloon.purple ) )
, ( "orange", Control.value ( "Balloon.orange", Balloon.orange ) )
, ( "white", Control.value ( "Balloon.white", Balloon.white ) )
, ( "navy", Control.value ( "Balloon.navy", Balloon.navy ) )
]
2021-09-29 01:13:54 +03:00
positionOptions : Control ( String, Balloon.Attribute )
2021-09-29 00:39:13 +03:00
positionOptions =
Control.choice
[ ( "onBottom", Control.value ( "Balloon.onBottom", Balloon.onBottom ) )
, ( "onLeft", Control.value ( "Balloon.onLeft", Balloon.onLeft ) )
, ( "onRight", Control.value ( "Balloon.onRight", Balloon.onRight ) )
, ( "onTop", Control.value ( "Balloon.onTop", Balloon.onTop ) )
]
2021-09-29 00:26:37 +03:00
2021-09-29 01:13:54 +03:00
widthOptions : Control ( String, Balloon.Attribute )
2021-09-29 01:09:32 +03:00
widthOptions =
Control.choice
[ ( "px"
, Control.map
(\w -> ( "Balloon.widthPx " ++ String.fromFloat w, Balloon.widthPx w ))
(controlFloat 50)
)
, ( "%"
, Control.map
(\w -> ( "Balloon.widthPct " ++ String.fromFloat w, Balloon.widthPct w ))
(controlFloat 50)
)
]
2021-09-29 01:15:41 +03:00
paddingOptions : Control ( String, Balloon.Attribute )
paddingOptions =
Control.map
(\w -> ( "Balloon.paddingPx " ++ String.fromFloat w, Balloon.paddingPx w ))
(controlFloat 10)
2021-09-29 01:09:32 +03:00
controlFloat : Float -> Control Float
controlFloat default =
Control.map (String.toFloat >> Maybe.withDefault default)
(Control.string (String.fromFloat default))
2021-09-29 00:26:37 +03:00
{-| -}
type Msg
= SetDebugControlsState (Control Settings)
update : Msg -> State -> ( State, Cmd Msg )
update msg state =
case msg of
SetDebugControlsState newDebugControlsState ->
( newDebugControlsState
, Cmd.none
)
view : State -> List (Html Msg)
view state =
let
2021-09-29 00:26:37 +03:00
settings =
Control.currentValue state
2021-09-29 00:39:13 +03:00
attributes =
2021-09-29 01:09:32 +03:00
List.filterMap identity
[ settings.theme
, settings.position
, settings.width
2021-09-29 01:15:41 +03:00
, settings.padding
2021-09-29 01:09:32 +03:00
]
in
2021-09-29 00:26:37 +03:00
[ Control.view SetDebugControlsState state |> fromUnstyled
2021-09-29 01:16:33 +03:00
, Html.Styled.code [ css [ Css.display Css.block, Css.margin2 (Css.px 20) Css.zero ] ]
2021-09-29 00:39:13 +03:00
[ text <|
"Balloon.balloon [ "
++ String.join ", " (List.map Tuple.first attributes)
++ " ] "
++ "\""
++ settings.copy
++ "\""
]
, Balloon.balloon (List.map Tuple.second attributes) (text settings.copy)
]