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

243 lines
6.7 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-04-30 14:14:20 +03:00
-}
import Category exposing (Category(..))
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)
2018-08-18 00:37:26 +03:00
import Html.Styled exposing (..)
import Html.Styled.Attributes exposing (css, id)
import ModuleExample exposing (ModuleExample, ModuleMessages)
2020-01-29 04:09:32 +03:00
import Nri.Ui.Button.V10 as Button
import Nri.Ui.Heading.V2 as Heading
import Nri.Ui.Svg.V1 as Svg exposing (Svg)
import Nri.Ui.UiIcon.V1 as UiIcon
import Sort.Set as Set exposing (Set)
2018-04-30 14:14:20 +03:00
{-| -}
2019-07-12 01:18:49 +03:00
type State parentMsg
= State (Control (Model parentMsg))
2018-04-30 14:14:20 +03:00
{-| -}
type ButtonType
= Button
| Link
{-| -}
example :
2019-07-12 01:18:49 +03:00
(String -> ModuleMessages (Msg parentMsg) parentMsg)
-> State parentMsg
2018-04-30 14:14:20 +03:00
-> 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
2020-01-29 04:09:32 +03:00
{ name = "Nri.Ui.Button.V10"
, categories = Set.fromList Category.sorter <| List.singleton Buttons
2019-07-03 15:51:23 +03:00
, content = [ viewButtonExamples messages state ]
2018-04-30 14:24:03 +03:00
}
2018-04-30 14:14:20 +03:00
{-| -}
2019-07-12 01:18:49 +03:00
type Msg parentMsg
= SetState (State parentMsg)
| NoOp
{-| -}
2019-07-12 01:18:49 +03:00
update : Msg msg -> State msg -> ( State msg, Cmd (Msg msg) )
update msg state =
case msg of
SetState newState ->
( newState, Cmd.none )
NoOp ->
( state, Cmd.none )
-- INTERNAL
2019-07-12 01:18:49 +03:00
type alias Model msg =
{ label : String
, icon : Maybe Svg
, buttonType : ButtonType
2019-07-19 18:57:56 +03:00
, width : Button.Attribute msg
, state : Button.Attribute msg
}
2018-04-30 14:14:20 +03:00
{-| -}
init : State msg
init =
2018-04-30 14:14:20 +03:00
Control.record Model
|> Control.field "label" (Control.string "Label")
|> Control.field "icon" iconChoice
2019-07-12 01:18:49 +03:00
|> Control.field "button type"
(Control.choice
2019-07-30 22:55:55 +03:00
[ ( "button", Control.value Button )
, ( "link", Control.value Link )
2019-07-12 01:18:49 +03:00
]
)
2018-04-30 14:14:20 +03:00
|> Control.field "width"
(Control.choice
2019-07-30 22:55:55 +03:00
[ ( "exactWidth 120", Control.value (Button.exactWidth 120) )
, ( "exactWidth 70", Control.value (Button.exactWidth 70) )
, ( "unboundedWidth", Control.value Button.unboundedWidth )
, ( "fillContainerWidth", Control.value Button.fillContainerWidth )
]
2018-04-30 14:14:20 +03:00
)
|> Control.field "state (button only)"
(Control.choice
2019-07-30 22:55:55 +03:00
[ ( "enabled", Control.value Button.enabled )
, ( "disabled", Control.value Button.disabled )
, ( "error", Control.value Button.error )
, ( "unfulfilled", Control.value Button.unfulfilled )
, ( "loading", Control.value Button.loading )
, ( "success", Control.value Button.success )
]
2018-04-30 14:14:20 +03:00
)
|> State
iconChoice : Control.Control (Maybe Svg)
iconChoice =
Control.choice
[ ( "Nothing", Control.value Nothing )
, ( "Just Performance", Control.value (Just UiIcon.performance) )
, ( "Just Share", Control.value (Just UiIcon.share) )
, ( "Just Download", Control.value (Just UiIcon.download) )
]
2018-04-30 14:14:20 +03:00
viewButtonExamples :
2019-07-12 01:18:49 +03:00
ModuleMessages (Msg parentMsg) parentMsg
-> State parentMsg
-> 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"
}
2019-07-20 04:16:46 +03:00
, Button.link "linkExternalWithTracking"
[ Button.unboundedWidth
2019-07-19 18:57:56 +03:00
, Button.secondary
, Button.linkExternalWithTracking
{ url = "#"
, track = messages.showItWorked "linkExternalWithTracking clicked"
}
2019-07-19 18:57:56 +03:00
]
2018-04-30 14:24:03 +03:00
]
|> div []
2018-04-30 14:14:20 +03:00
buttons :
2019-07-12 01:18:49 +03:00
ModuleMessages (Msg parentMsg) parentMsg
-> Model parentMsg
-> Html parentMsg
2019-03-28 02:56:54 +03:00
buttons messages model =
2018-04-30 14:14:20 +03:00
let
sizes =
2019-07-12 00:29:10 +03:00
[ ( Button.small, "small" )
, ( Button.medium, "medium" )
, ( Button.large, "large" )
]
styles =
2019-07-12 00:29:10 +03:00
[ ( Button.primary, "primary" )
, ( Button.secondary, "secondary" )
, ( Button.danger, "danger" )
, ( Button.premium, "premium" )
]
exampleRow ( style, styleName ) =
2018-04-30 14:14:20 +03:00
List.concat
2018-08-18 02:06:31 +03:00
[ [ td
[ css
[ verticalAlign middle
]
]
[ text styleName ]
2018-04-30 14:14:20 +03:00
]
, List.map (Tuple.first >> exampleCell style) sizes
2018-04-30 14:14:20 +03:00
]
|> tr []
2019-07-19 18:57:56 +03:00
buttonOrLink =
case model.buttonType of
Link ->
Button.link
Button ->
Button.button
exampleCell setStyle setSize =
2019-07-20 04:16:46 +03:00
buttonOrLink model.label
2019-07-19 18:57:56 +03:00
[ setSize
, setStyle
, model.width
, model.state
2019-07-20 00:56:01 +03:00
, Button.custom [ Html.Styled.Attributes.class "styleguide-button" ]
2019-07-19 18:57:56 +03:00
, Button.onClick (messages.showItWorked "Button clicked!")
, case model.icon of
Just icon ->
Button.icon icon
Nothing ->
2019-07-20 00:56:01 +03:00
Button.custom []
2019-07-19 18:57:56 +03:00
]
2018-04-30 14:14:20 +03:00
|> 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 (\( _, sizeName ) -> th [] [ text sizeName ])
2018-04-30 14:24:03 +03:00
|> (\cells -> tr [] (th [] [] :: cells))
]
, List.map exampleRow styles
2018-04-30 14:24:03 +03:00
]
|> table []
2018-04-30 14:14:20 +03:00
2019-07-12 01:18:49 +03:00
toggleButtons : ModuleMessages (Msg parentMsg) parentMsg -> Html parentMsg
2018-04-30 14:14:20 +03:00
toggleButtons messages =
div []
[ Heading.h3 [] [ text "Button toggle" ]
2020-01-29 04:09:53 +03:00
, div [ css [ Css.displayFlex, Css.marginBottom (Css.px 20) ] ]
[ 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
}
]
2018-04-30 14:14:20 +03:00
]