2020-03-31 22:43:32 +03:00
|
|
|
module Examples.Button exposing (Msg, State, example)
|
2018-04-30 14:14:20 +03:00
|
|
|
|
2019-07-12 00:23:03 +03:00
|
|
|
{-|
|
|
|
|
|
2020-03-31 22:43:32 +03:00
|
|
|
@docs Msg, State, example
|
2019-07-12 00:23:03 +03:00
|
|
|
|
2018-04-30 14:14:20 +03:00
|
|
|
-}
|
|
|
|
|
2020-03-24 03:33:42 +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)
|
2020-03-31 23:20:03 +03:00
|
|
|
import Example exposing (Example)
|
2018-08-18 00:37:26 +03:00
|
|
|
import Html.Styled exposing (..)
|
2018-12-13 23:59:35 +03:00
|
|
|
import Html.Styled.Attributes exposing (css, id)
|
2020-06-20 00:45:32 +03:00
|
|
|
import KeyboardSupport exposing (Direction(..), Key(..))
|
2020-01-29 04:09:32 +03:00
|
|
|
import Nri.Ui.Button.V10 as Button
|
2019-07-23 17:58:23 +03:00
|
|
|
import Nri.Ui.Heading.V2 as Heading
|
2019-10-24 20:47:11 +03:00
|
|
|
import Nri.Ui.Svg.V1 as Svg exposing (Svg)
|
|
|
|
import Nri.Ui.UiIcon.V1 as UiIcon
|
2021-09-02 21:35:18 +03:00
|
|
|
import Set exposing (Set)
|
2018-04-30 14:14:20 +03:00
|
|
|
|
|
|
|
|
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 = "Button"
|
|
|
|
, version = 10
|
2020-03-31 22:05:56 +03:00
|
|
|
, state = init
|
|
|
|
, update = update
|
2020-03-31 22:48:26 +03:00
|
|
|
, subscriptions = \_ -> Sub.none
|
2020-03-31 22:05:56 +03:00
|
|
|
, view = \state -> [ viewButtonExamples state ]
|
|
|
|
, categories = [ Buttons ]
|
2020-06-20 00:45:32 +03:00
|
|
|
, keyboardSupport = []
|
2020-03-31 22:05:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-30 14:14:20 +03:00
|
|
|
{-| -}
|
2021-09-02 20:55:19 +03:00
|
|
|
type alias State =
|
|
|
|
{ debugControlsState : Control Model
|
2021-09-02 21:35:18 +03:00
|
|
|
, pressedToggleButtons : Set Int
|
2021-09-02 20:55:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
init : State
|
|
|
|
init =
|
|
|
|
{ debugControlsState = initDebugControls
|
2021-09-02 21:35:18 +03:00
|
|
|
, pressedToggleButtons = Set.singleton 1
|
2021-09-02 20:55:19 +03:00
|
|
|
}
|
2018-04-30 14:14:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
type ButtonType
|
|
|
|
= Button
|
|
|
|
| Link
|
|
|
|
|
|
|
|
|
2019-07-03 19:39:06 +03:00
|
|
|
{-| -}
|
2020-03-31 22:05:56 +03:00
|
|
|
type Msg
|
2021-09-02 20:55:19 +03:00
|
|
|
= SetDebugControlsState (Control Model)
|
2020-03-31 22:05:56 +03:00
|
|
|
| ShowItWorked String String
|
2021-09-02 23:05:50 +03:00
|
|
|
| ToggleToggleButton Int
|
2019-07-03 19:39:06 +03:00
|
|
|
| NoOp
|
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
2020-03-31 22:05:56 +03:00
|
|
|
update : Msg -> State -> ( State, Cmd Msg )
|
2019-07-03 19:39:06 +03:00
|
|
|
update msg state =
|
|
|
|
case msg of
|
2021-09-02 20:55:19 +03:00
|
|
|
SetDebugControlsState newDebugControlsState ->
|
|
|
|
( { state | debugControlsState = newDebugControlsState }
|
|
|
|
, Cmd.none
|
|
|
|
)
|
2019-07-03 19:39:06 +03:00
|
|
|
|
2020-03-31 22:05:56 +03:00
|
|
|
ShowItWorked group message ->
|
|
|
|
let
|
|
|
|
_ =
|
|
|
|
Debug.log group message
|
|
|
|
in
|
|
|
|
( state, Cmd.none )
|
|
|
|
|
2021-09-02 23:05:50 +03:00
|
|
|
ToggleToggleButton id ->
|
2021-09-03 00:33:25 +03:00
|
|
|
( { state
|
|
|
|
| pressedToggleButtons =
|
|
|
|
if Set.member id state.pressedToggleButtons then
|
|
|
|
Set.remove id state.pressedToggleButtons
|
|
|
|
|
|
|
|
else
|
|
|
|
Set.insert id state.pressedToggleButtons
|
|
|
|
}
|
2021-09-02 23:05:50 +03:00
|
|
|
, Cmd.none
|
|
|
|
)
|
|
|
|
|
2019-07-03 19:39:06 +03:00
|
|
|
NoOp ->
|
|
|
|
( state, Cmd.none )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- INTERNAL
|
|
|
|
|
|
|
|
|
2020-03-31 22:05:56 +03:00
|
|
|
type alias Model =
|
2019-07-03 19:39:06 +03:00
|
|
|
{ label : String
|
|
|
|
, icon : Maybe Svg
|
|
|
|
, buttonType : ButtonType
|
2020-03-31 22:05:56 +03:00
|
|
|
, width : Button.Attribute Msg
|
|
|
|
, state : Button.Attribute Msg
|
2019-07-03 19:39:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-30 14:14:20 +03:00
|
|
|
{-| -}
|
2021-09-02 20:55:19 +03:00
|
|
|
initDebugControls : Control Model
|
|
|
|
initDebugControls =
|
2018-04-30 14:14:20 +03:00
|
|
|
Control.record Model
|
2019-07-03 19:39:06 +03:00
|
|
|
|> Control.field "label" (Control.string "Label")
|
2019-10-24 20:47:11 +03:00
|
|
|
|> 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"
|
2018-10-02 15:06:29 +03:00
|
|
|
(Control.choice
|
2019-07-30 22:55:55 +03:00
|
|
|
[ ( "exactWidth 120", Control.value (Button.exactWidth 120) )
|
|
|
|
, ( "exactWidth 70", Control.value (Button.exactWidth 70) )
|
2020-10-27 00:53:43 +03:00
|
|
|
, ( "boundedWidth 100 180", Control.value (Button.boundedWidth { min = 100, max = 180 }) )
|
2019-07-03 19:39:06 +03:00
|
|
|
, ( "unboundedWidth", Control.value Button.unboundedWidth )
|
|
|
|
, ( "fillContainerWidth", Control.value Button.fillContainerWidth )
|
2018-10-02 15:06:29 +03:00
|
|
|
]
|
2018-04-30 14:14:20 +03:00
|
|
|
)
|
2018-10-01 17:12:21 +03:00
|
|
|
|> Control.field "state (button only)"
|
2018-12-05 01:36:15 +03:00
|
|
|
(Control.choice
|
2019-07-30 22:55:55 +03:00
|
|
|
[ ( "enabled", Control.value Button.enabled )
|
|
|
|
, ( "disabled", Control.value Button.disabled )
|
2019-07-12 00:23:03 +03:00
|
|
|
, ( "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
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-10-24 20:47:11 +03:00
|
|
|
iconChoice : Control.Control (Maybe Svg)
|
|
|
|
iconChoice =
|
2019-07-03 19:39:06 +03:00
|
|
|
Control.choice
|
2020-10-28 19:38:38 +03:00
|
|
|
[ ( "none", Control.value Nothing )
|
|
|
|
, ( "preview", Control.value (Just UiIcon.preview) )
|
|
|
|
, ( "arrowLeft", Control.value (Just UiIcon.arrowLeft) )
|
|
|
|
, ( "performance", Control.value (Just UiIcon.performance) )
|
|
|
|
, ( "share", Control.value (Just UiIcon.share) )
|
|
|
|
, ( "download", Control.value (Just UiIcon.download) )
|
2019-07-03 19:39:06 +03:00
|
|
|
]
|
2018-04-30 14:14:20 +03:00
|
|
|
|
|
|
|
|
2020-03-31 22:05:56 +03:00
|
|
|
viewButtonExamples : State -> Html Msg
|
2021-09-02 20:55:19 +03:00
|
|
|
viewButtonExamples state =
|
2018-04-30 14:14:20 +03:00
|
|
|
let
|
|
|
|
model =
|
2021-09-02 20:55:19 +03:00
|
|
|
Control.currentValue state.debugControlsState
|
2018-04-30 14:14:20 +03:00
|
|
|
in
|
2021-09-02 20:55:19 +03:00
|
|
|
[ Control.view SetDebugControlsState state.debugControlsState
|
2018-08-18 00:37:26 +03:00
|
|
|
|> fromUnstyled
|
2020-03-31 22:05:56 +03:00
|
|
|
, buttons model
|
2021-09-02 21:35:18 +03:00
|
|
|
, toggleButtons state.pressedToggleButtons
|
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
|
2019-07-20 04:27:17 +03:00
|
|
|
{ url = "#"
|
2020-03-31 22:05:56 +03:00
|
|
|
, track = ShowItWorked "ButtonExample" "linkExternalWithTracking clicked"
|
2019-07-20 04:27:17 +03:00
|
|
|
}
|
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
|
|
|
|
|
|
|
|
2020-03-31 22:05:56 +03:00
|
|
|
buttons : Model -> Html Msg
|
|
|
|
buttons model =
|
2018-04-30 14:14:20 +03:00
|
|
|
let
|
2019-07-03 19:39:06 +03:00
|
|
|
sizes =
|
2019-07-12 00:29:10 +03:00
|
|
|
[ ( Button.small, "small" )
|
|
|
|
, ( Button.medium, "medium" )
|
|
|
|
, ( Button.large, "large" )
|
2019-07-03 19:39:06 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
styles =
|
2019-07-12 00:29:10 +03:00
|
|
|
[ ( Button.primary, "primary" )
|
|
|
|
, ( Button.secondary, "secondary" )
|
|
|
|
, ( Button.danger, "danger" )
|
|
|
|
, ( Button.premium, "premium" )
|
2019-07-03 19:39:06 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
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
|
|
|
|
]
|
|
|
|
]
|
2019-07-03 19:39:06 +03:00
|
|
|
[ text styleName ]
|
2018-04-30 14:14:20 +03:00
|
|
|
]
|
2019-07-03 19:39:06 +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
|
2019-07-03 19:39:06 +03:00
|
|
|
|
|
|
|
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" ]
|
2020-03-31 22:05:56 +03:00
|
|
|
, Button.onClick (ShowItWorked "ButtonExample" "Button clicked!")
|
2019-07-19 19:07:42 +03:00
|
|
|
, 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
|
2019-07-03 19:39:06 +03:00
|
|
|
|> List.map (\( _, sizeName ) -> th [] [ text sizeName ])
|
2018-04-30 14:24:03 +03:00
|
|
|
|> (\cells -> tr [] (th [] [] :: cells))
|
|
|
|
]
|
2019-07-03 19:39:06 +03:00
|
|
|
, List.map exampleRow styles
|
2018-04-30 14:24:03 +03:00
|
|
|
]
|
|
|
|
|> table []
|
2018-04-30 14:14:20 +03:00
|
|
|
|
|
|
|
|
2021-09-02 21:35:18 +03:00
|
|
|
toggleButtons : Set Int -> Html Msg
|
|
|
|
toggleButtons pressedToggleButtons =
|
2018-04-30 14:14:20 +03:00
|
|
|
div []
|
2019-07-23 17:58:23 +03:00
|
|
|
[ Heading.h3 [] [ text "Button toggle" ]
|
2020-01-29 04:09:53 +03:00
|
|
|
, div [ css [ Css.displayFlex, Css.marginBottom (Css.px 20) ] ]
|
|
|
|
[ Button.toggleButton
|
2021-09-02 23:05:50 +03:00
|
|
|
{ onDeselect = ToggleToggleButton 0
|
|
|
|
, onSelect = ToggleToggleButton 0
|
2020-01-29 04:09:53 +03:00
|
|
|
, label = "5"
|
2021-09-02 21:35:18 +03:00
|
|
|
, pressed = Set.member 0 pressedToggleButtons
|
2020-01-29 04:09:53 +03:00
|
|
|
}
|
|
|
|
, Button.toggleButton
|
2021-09-02 23:05:50 +03:00
|
|
|
{ onDeselect = ToggleToggleButton 1
|
|
|
|
, onSelect = ToggleToggleButton 1
|
|
|
|
, label = "Kindergarten"
|
2021-09-02 21:35:18 +03:00
|
|
|
, pressed = Set.member 1 pressedToggleButtons
|
2020-01-29 04:09:53 +03:00
|
|
|
}
|
|
|
|
]
|
2018-04-30 14:14:20 +03:00
|
|
|
]
|