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

150 lines
4.0 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)
2021-10-22 03:20:29 +03:00
import Debug.Control.Extra as ControlExtra
import Example exposing (Example)
2022-03-15 21:06:13 +03:00
import Html.Styled exposing (Html, fromUnstyled, text)
2021-09-29 00:39:13 +03:00
import Html.Styled.Attributes exposing (css)
import Nri.Ui.Balloon.V1 as Balloon
{-| -}
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
2021-11-05 21:59:39 +03:00
, preview =
[ Balloon.balloon
[ Balloon.onTop
, Balloon.navy
2021-11-05 22:04:46 +03:00
, Balloon.paddingPx 15
2021-11-05 21:59:39 +03:00
]
(text "This is a balloon.")
]
, view = view
}
2021-09-29 00:26:37 +03:00
{-| -}
type alias State =
2021-10-28 18:40:23 +03:00
{ copy : Control String
, attributes : Control (List ( String, Balloon.Attribute ))
}
2021-09-29 00:26:37 +03:00
2021-10-28 18:40:23 +03:00
init : State
init =
{ copy = Control.string "Hello, world!"
, attributes = controlAttributes
2021-09-29 00:26:37 +03:00
}
2021-10-28 18:40:23 +03:00
controlAttributes : Control (List ( String, Balloon.Attribute ))
controlAttributes =
ControlExtra.list
|> ControlExtra.optionalListItem "theme" themeOptions
|> ControlExtra.optionalListItem "position" positionOptions
|> ControlExtra.optionalListItem "width" widthOptions
|> ControlExtra.optionalListItem "padding" 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 ))
2021-10-22 03:20:29 +03:00
(ControlExtra.float 50)
2021-09-29 01:09:32 +03:00
)
, ( "%"
, Control.map
(\w -> ( "Balloon.widthPct " ++ String.fromFloat w, Balloon.widthPct w ))
2021-10-22 03:20:29 +03:00
(ControlExtra.float 50)
2021-09-29 01:09:32 +03:00
)
]
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 ))
2021-10-22 03:20:29 +03:00
(ControlExtra.float 10)
2021-09-29 01:09:32 +03:00
2021-09-29 00:26:37 +03:00
{-| -}
type Msg
2021-10-28 18:40:23 +03:00
= SetCopy (Control String)
| SetAttributes (Control (List ( String, Balloon.Attribute )))
2021-09-29 00:26:37 +03:00
update : Msg -> State -> ( State, Cmd Msg )
update msg state =
case msg of
2021-10-28 18:40:23 +03:00
SetCopy copy ->
( { state | copy = copy }
, Cmd.none
)
SetAttributes attributes ->
( { state | attributes = attributes }
2021-09-29 00:26:37 +03:00
, Cmd.none
)
view : State -> List (Html Msg)
view state =
let
2021-10-28 18:40:23 +03:00
copy =
Control.currentValue state.copy
2021-09-29 00:39:13 +03:00
attributes =
2021-10-28 18:40:23 +03:00
Control.currentValue state.attributes
in
2021-10-28 18:40:23 +03:00
[ Control.view SetCopy state.copy |> fromUnstyled
, Control.view SetAttributes state.attributes |> 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)
++ " ] "
++ "\""
2021-10-28 18:40:23 +03:00
++ copy
2021-09-29 00:39:13 +03:00
++ "\""
]
2021-10-28 18:40:23 +03:00
, Balloon.balloon (List.map Tuple.second attributes) (text copy)
]