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

170 lines
4.5 KiB
Elm
Raw Normal View History

module Examples.Balloon exposing (example, State, Msg)
{-|
@docs example, State, Msg
-}
import Category exposing (Category(..))
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
2022-03-22 02:50:45 +03:00
import Debug.Control.View as ControlView
import EllieLink
import Example exposing (Example)
2022-03-15 21:06:13 +03:00
import Html.Styled exposing (Html, fromUnstyled, text)
import Nri.Ui.Balloon.V1 as Balloon
2022-03-24 20:03:47 +03:00
moduleName : String
moduleName =
"Balloon"
version : Int
version =
1
{-| -}
example : Example State Msg
example =
2022-03-24 20:03:47 +03:00
{ name = moduleName
, version = version
, 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 : EllieLink.Config -> State -> List (Html Msg)
view ellieLinkConfig state =
let
2021-10-28 18:40:23 +03:00
copy =
Control.currentValue state.copy
in
2021-10-28 18:40:23 +03:00
[ Control.view SetCopy state.copy |> fromUnstyled
2022-03-22 02:50:45 +03:00
, ControlView.view
{ ellieLinkConfig = ellieLinkConfig
, name = moduleName
2022-03-24 20:03:47 +03:00
, version = version
, update = SetAttributes
2022-03-22 02:50:45 +03:00
, settings = state.attributes
, mainType = "RootHtml.Html msg"
2022-04-13 03:32:46 +03:00
, extraImports = []
2022-03-22 02:50:45 +03:00
, toExampleCode =
\attrs ->
[ { sectionName = "Balloon"
, code =
"Balloon.balloon\n [ "
++ String.join "\n , " (List.map Tuple.first attrs)
++ "\n ] "
++ "\n (text \""
2022-03-22 02:50:45 +03:00
++ copy
2022-03-22 02:51:25 +03:00
++ "\")"
2022-03-22 02:50:45 +03:00
}
]
}
, Balloon.balloon
(List.map Tuple.second (Control.currentValue state.attributes))
(text copy)
]