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

165 lines
4.2 KiB
Elm
Raw Normal View History

2020-03-31 22:43:32 +03:00
module Examples.ClickableText exposing (Msg, State, example)
{-|
2020-03-31 22:43:32 +03:00
@docs Msg, State, example
-}
2020-06-19 23:41:28 +03:00
import AtomicDesignType exposing (AtomicDesignType(..))
import Category exposing (Category(..))
import Css exposing (middle, verticalAlign)
import Debug.Control as Control exposing (Control)
2020-03-31 23:20:03 +03:00
import Example exposing (Example)
import Html.Styled exposing (..)
import Html.Styled.Attributes exposing (css, id)
import KeyboardSupport exposing (Direction(..), Key(..))
2019-07-30 22:13:09 +03:00
import Nri.Ui.ClickableText.V3 as ClickableText
import Nri.Ui.Svg.V1 as Svg exposing (Svg)
2019-07-23 17:09:07 +03:00
import Nri.Ui.Text.V4 as Text
import Nri.Ui.UiIcon.V1 as UiIcon
{-| -}
type State
= State (Control Model)
{-| -}
2020-03-31 23:20:03 +03:00
example : Example State Msg
2020-03-31 22:43:32 +03:00
example =
2020-09-09 21:43:10 +03:00
{ name = "ClickableText"
, version = 3
2020-03-31 22:43:32 +03:00
, 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 -> [ viewExamples state ]
, categories = [ Buttons ]
2020-06-20 00:16:10 +03:00
, atomicDesignType = Molecule
, keyboardSupport = []
}
{-| -}
init : State
init =
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
[ ( "Preview", Control.value UiIcon.preview )
, ( "Performance", Control.value UiIcon.performance )
, ( "Edit", Control.value UiIcon.edit )
]
)
|> State
2020-03-31 22:43:32 +03:00
{-| -}
type Msg
= SetState State
| ShowItWorked String String
{-| -}
update : Msg -> State -> ( State, Cmd Msg )
update msg state =
case msg of
SetState newState ->
( newState, Cmd.none )
2020-03-31 22:43:32 +03:00
ShowItWorked group message ->
let
_ =
Debug.log group message
in
( state, Cmd.none )
-- INTERNAL
type alias Model =
{ label : String
2019-03-28 22:37:44 +03:00
, icon : Maybe Svg
}
2020-03-31 22:43:32 +03:00
viewExamples : State -> Html Msg
viewExamples (State control) =
let
model =
Control.currentValue control
in
2020-03-31 22:43:32 +03:00
[ Control.view (State >> SetState) control
|> fromUnstyled
2020-03-31 22:43:32 +03:00
, buttons 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
2020-03-31 22:43:32 +03:00
, ClickableText.onClick (ShowItWorked "ClickableText" "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" )
]
2020-03-31 22:43:32 +03:00
buttons : Model -> Html Msg
buttons 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
2020-03-31 22:43:32 +03:00
, ClickableText.onClick (ShowItWorked "ClickableText" 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 ]