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

252 lines
6.8 KiB
Elm
Raw Normal View History

2018-04-30 14:14:20 +03:00
module Examples.Button exposing (Msg, State, example, init, update)
{- \
@docs Msg, State, example, init, update,
-}
2018-08-18 02:06:31 +03:00
import Css exposing (middle, verticalAlign)
2018-04-30 14:14:20 +03:00
import Debug.Control as Control exposing (Control)
import Headings
2018-08-18 00:37:26 +03:00
import Html.Styled exposing (..)
import Html.Styled.Attributes exposing (css, id)
2018-04-30 14:14:20 +03:00
import ModuleExample as ModuleExample exposing (Category(..), ModuleExample, ModuleMessages)
2018-04-30 14:24:03 +03:00
import Nri.Ui.AssetPath exposing (Asset)
2019-03-28 02:32:47 +03:00
import Nri.Ui.Button.V8 as Button
2018-12-13 20:09:32 +03:00
import Nri.Ui.Icon.V4 as Icon
2019-03-28 02:56:54 +03:00
import Nri.Ui.Svg.V1 as NriSvg exposing (Svg)
import Nri.Ui.Text.V2 as Text
2018-04-30 14:14:20 +03:00
{-| -}
type Msg
= SetState State
{-| -}
type State
= State (Control Model)
{-| -}
type ButtonType
= Button
| Link
{-| -}
example :
2019-03-28 02:56:54 +03:00
(String -> ModuleMessages Msg parentMsg)
2018-04-30 14:14:20 +03:00
-> State
-> ModuleExample parentMsg
2019-03-28 02:56:54 +03:00
example unnamedMessages state =
2018-04-30 14:14:20 +03:00
let
messages =
unnamedMessages "ButtonExample"
in
2019-03-28 02:32:47 +03:00
{ filename = "Nri.Ui.Button.V8"
2018-04-30 14:24:03 +03:00
, category = Buttons
, content =
2019-03-28 02:56:54 +03:00
[ viewButtonExamples messages state ]
2018-04-30 14:24:03 +03:00
}
2018-04-30 14:14:20 +03:00
{-| -}
init : { r | performance : String, lock : String } -> State
init assets =
Control.record Model
|> Control.field "label" (Control.string "Button")
2019-03-28 02:32:47 +03:00
|> Control.field "icon"
2018-04-30 14:14:20 +03:00
(Control.maybe False <|
Control.choice
2019-03-28 02:56:54 +03:00
( "Performance"
, Icon.performance assets
|> Icon.decorativeIcon
|> NriSvg.fromHtml
|> Control.value
)
[ ( "Lock"
, Icon.lock assets
|> Icon.decorativeIcon
|> NriSvg.fromHtml
|> Control.value
)
2018-04-30 14:14:20 +03:00
]
)
|> Control.field "width"
(Control.choice
2018-12-13 20:09:32 +03:00
( "Nri.Ui.Button.V7.WidthExact 120", Control.value <| Button.WidthExact 120 )
[ ( "Nri.Ui.Button.V7.WidthExact 70", Control.value <| Button.WidthExact 70 )
, ( "Nri.Ui.Button.V7.WidthUnbounded", Control.value <| Button.WidthUnbounded )
2018-12-20 21:53:09 +03:00
, ( "Nri.Ui.Button.V7.WidthFillContainer", Control.value <| Button.WidthFillContainer )
]
2018-04-30 14:14:20 +03:00
)
|> Control.field "button type"
(Control.choice
2018-12-13 20:09:32 +03:00
( "Nri.Ui.Button.V7.button", Control.value Button )
[ ( "Nri.Ui.Button.V7.link", Control.value Link )
2018-04-30 14:14:20 +03:00
]
)
|> Control.field "state (button only)"
(Control.choice
( Debug.toString Button.Enabled, Control.value Button.Enabled )
(List.map (\x -> ( Debug.toString x, Control.value x ))
[ Button.Disabled
2018-04-30 14:14:20 +03:00
, Button.Error
, Button.Unfulfilled
, Button.Loading
, Button.Success
]
)
2018-04-30 14:14:20 +03:00
)
|> 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 02:56:54 +03:00
, icon : Maybe Svg
, width : Button.ButtonWidth
2018-04-30 14:14:20 +03:00
, buttonType : ButtonType
, state : Button.ButtonState
}
viewButtonExamples :
2019-03-28 02:56:54 +03:00
ModuleMessages Msg parentMsg
-> State
-> Html parentMsg
2019-03-28 02:56:54 +03:00
viewButtonExamples messages (State control) =
2018-04-30 14:14:20 +03:00
let
model =
Control.currentValue control
in
2018-04-30 14:24:03 +03:00
[ Control.view (State >> SetState >> messages.wrapper) control
2018-08-18 00:37:26 +03:00
|> fromUnstyled
2019-03-28 02:56:54 +03:00
, buttons messages model
2018-04-30 14:24:03 +03:00
, toggleButtons messages
, Button.delete
2018-04-30 14:24:03 +03:00
{ label = "Delete Something"
, onClick = messages.showItWorked "delete"
}
, Button.linkExternalWithTracking
(messages.showItWorked "linkExternalWithTracking clicked")
{ size = Button.Medium
, style = Button.Secondary
, width = Button.WidthUnbounded
, label = "linkExternalWithTracking"
, icon = Nothing
, url = "#"
}
2018-04-30 14:24:03 +03:00
]
|> div []
2018-04-30 14:14:20 +03:00
sizes : List Button.ButtonSize
sizes =
[ Button.Small
, Button.Medium
, Button.Large
]
allStyles : List Button.ButtonStyle
allStyles =
[ Button.Primary
, Button.Secondary
, Button.Danger
, Button.Premium
]
buttons :
2019-03-28 02:56:54 +03:00
ModuleMessages Msg parentMsg
-> Model
-> Html parentMsg
2019-03-28 02:56:54 +03:00
buttons messages model =
2018-04-30 14:14:20 +03:00
let
exampleRow style =
2018-04-30 14:14:20 +03:00
List.concat
2018-08-18 02:06:31 +03:00
[ [ td
[ css
[ verticalAlign middle
]
]
[ text <| Debug.toString style ]
2018-04-30 14:14:20 +03:00
]
, sizes
|> List.map (exampleCell style)
2018-04-30 14:14:20 +03:00
]
|> tr []
exampleCell style size =
2018-04-30 14:14:20 +03:00
(case model.buttonType of
Link ->
Button.link
{ size = size
, style = style
, label = model.label
, icon = model.icon
, url = ""
, width = model.width
}
Button ->
Button.button
{ size = size
, style = style
, onClick = messages.showItWorked (Debug.toString ( style, size ))
2018-04-30 14:14:20 +03:00
, width = model.width
}
{ label = model.label
, icon = model.icon
, state = model.state
}
)
|> List.singleton
2018-12-20 21:53:09 +03:00
|> td
[ css
[ verticalAlign middle
, Css.width (Css.px 200)
]
]
2018-04-30 14:14:20 +03:00
in
2018-04-30 14:24:03 +03:00
List.concat
[ [ sizes
|> List.map (\size -> th [] [ text <| Debug.toString size ])
2018-04-30 14:24:03 +03:00
|> (\cells -> tr [] (th [] [] :: cells))
]
, allStyles
|> List.map exampleRow
2018-04-30 14:24:03 +03:00
]
|> table []
2018-04-30 14:14:20 +03:00
toggleButtons : ModuleMessages Msg parentMsg -> Html parentMsg
toggleButtons messages =
div []
[ Headings.h3 [ text "Button toggle" ]
2018-04-30 14:14:20 +03:00
, Button.toggleButton
{ onDeselect = messages.showItWorked "onDeselect"
, onSelect = messages.showItWorked "onSelect"
, label = "5"
, pressed = False
}
, Button.toggleButton
{ onDeselect = messages.showItWorked "onDeselect"
, onSelect = messages.showItWorked "onSelect"
, label = "5"
, pressed = True
}
]