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

177 lines
4.7 KiB
Elm
Raw Normal View History

module Examples.ClickableText exposing (Msg, State, example, init, update)
{-|
@docs Msg, State, example, init, update
-}
import Css exposing (middle, verticalAlign)
import Debug.Control as Control exposing (Control)
import Html.Styled exposing (..)
import Html.Styled.Attributes exposing (css, id)
import ModuleExample as ModuleExample exposing (Category(..), ModuleExample, ModuleMessages)
2019-07-30 22:13:09 +03:00
import Nri.Ui.ClickableText.V3 as ClickableText
import Nri.Ui.Icon.V5 as Icon
2019-03-28 22:37:44 +03:00
import Nri.Ui.Svg.V1 as NriSvg exposing (Svg)
2019-07-23 17:09:07 +03:00
import Nri.Ui.Text.V4 as Text
{-| -}
type Msg
= SetState State
{-| -}
type State
= State (Control Model)
{-| -}
example :
(String -> ModuleMessages Msg parentMsg)
-> State
-> ModuleExample parentMsg
example unnamedMessages state =
let
messages =
unnamedMessages "ClickableTextExample"
in
2019-07-30 21:47:25 +03:00
{ name = "Nri.Ui.ClickableText.V3"
, category = Buttons
, content =
[ viewExamples messages state ]
}
{-| -}
init : { r | performance : String, lock : String, help : String } -> State
init assets =
Control.record Model
|> Control.field "label" (Control.string "Clickable Text")
|> Control.field "icon"
2019-03-28 22:37:44 +03:00
(Control.maybe True <|
Control.choice
( "Help"
, Icon.helpSvg assets
2019-03-28 22:37:44 +03:00
|> Icon.decorativeIcon
|> NriSvg.fromHtml
|> Control.value
)
[ ( "Performance"
, Icon.performance assets
|> Icon.decorativeIcon
|> NriSvg.fromHtml
|> Control.value
)
, ( "Lock"
2019-03-28 22:37:44 +03:00
, Icon.lock assets
|> Icon.decorativeIcon
|> NriSvg.fromHtml
|> Control.value
)
]
)
|> State
{-| -}
update : Msg -> State -> ( State, Cmd Msg )
update msg state =
case msg of
SetState newState ->
( newState, Cmd.none )
-- INTERNAL
type alias Model =
{ label : String
2019-03-28 22:37:44 +03:00
, icon : Maybe Svg
}
viewExamples :
ModuleMessages Msg parentMsg
-> State
-> Html parentMsg
viewExamples messages (State control) =
let
model =
Control.currentValue control
in
[ Control.view (State >> SetState >> messages.wrapper) control
|> fromUnstyled
, buttons messages model
, Text.smallBody
[ text "Sometimes, we'll want our clickable links: "
, ClickableText.link model.label
[ ClickableText.small
, Maybe.map ClickableText.icon model.icon
|> Maybe.withDefault (ClickableText.custom [])
]
, text " and clickable buttons: "
, ClickableText.button model.label
[ ClickableText.small
, ClickableText.onClick (messages.showItWorked "in-line button")
, Maybe.map ClickableText.icon model.icon
|> Maybe.withDefault (ClickableText.custom [])
]
, text " to show up in-line."
]
]
|> div []
2019-07-30 22:13:09 +03:00
sizes : List ( ClickableText.Attribute msg, String )
sizes =
2019-07-30 22:13:09 +03:00
[ ( ClickableText.small, "small" )
, ( ClickableText.medium, "medium" )
, ( ClickableText.large, "large" )
]
buttons :
ModuleMessages Msg parentMsg
-> Model
-> Html parentMsg
buttons messages model =
let
sizeRow label render =
row label (List.map render sizes)
in
table []
[ sizeRow "" (\( size, sizeLabel ) -> th [] [ text sizeLabel ])
, sizeRow ".link"
(\( size, sizeLabel ) ->
ClickableText.link model.label
[ size
, Maybe.map ClickableText.icon model.icon
|> Maybe.withDefault (ClickableText.custom [])
]
|> exampleCell
)
, sizeRow ".button"
(\( size, sizeLabel ) ->
ClickableText.button model.label
[ size
, ClickableText.onClick (messages.showItWorked sizeLabel)
, Maybe.map ClickableText.icon model.icon
|> Maybe.withDefault (ClickableText.custom [])
]
|> exampleCell
)
]
row : String -> List (Html msg) -> Html msg
row label tds =
tr [] (th [] [ td [] [ text label ] ] :: tds)
2019-07-30 22:13:09 +03:00
exampleCell : Html msg -> Html msg
exampleCell view =
td [ css [ verticalAlign middle, Css.width (Css.px 200) ] ] [ view ]