Make the Switch component preview non-focusable

This commit is contained in:
Tessa Kelly 2021-11-05 14:42:34 -07:00
parent 52a58c4924
commit fe0e3f2e13
2 changed files with 25 additions and 12 deletions

View File

@ -1,8 +1,8 @@
module Nri.Ui.Switch.V1 exposing (view, Attribute, onSwitch, disabled, id, label) module Nri.Ui.Switch.V1 exposing (view, Attribute, onSwitch, disabled, id, label, custom)
{-| {-|
@docs view, Attribute, onSwitch, disabled, id, label @docs view, Attribute, onSwitch, disabled, id, label, custom
-} -}
@ -27,6 +27,7 @@ type Attribute msg
| Id String | Id String
| Label (Html msg) | Label (Html msg)
| Disabled | Disabled
| Custom (List (Html.Attribute Never))
{-| Specify what happens when the switch is toggled. {-| Specify what happens when the switch is toggled.
@ -63,10 +64,18 @@ label =
Label Label
{-| Pass custom attributes through to be attached to the underlying input.
-}
custom : List (Html.Attribute Never) -> Attribute msg
custom =
Custom
type alias Config msg = type alias Config msg =
{ onSwitch : Maybe (Bool -> msg) { onSwitch : Maybe (Bool -> msg)
, id : String , id : String
, label : Maybe (Html msg) , label : Maybe (Html msg)
, attributes : List (Html.Attribute Never)
} }
@ -75,6 +84,7 @@ defaultConfig =
{ onSwitch = Nothing { onSwitch = Nothing
, id = "nri-ui-switch-with-default-id" , id = "nri-ui-switch-with-default-id"
, label = Nothing , label = Nothing
, attributes = []
} }
@ -93,6 +103,9 @@ customize attr config =
Label label_ -> Label label_ ->
{ config | label = Just label_ } { config | label = Just label_ }
Custom custom_ ->
{ config | attributes = custom_ }
{-| Render a switch. The boolean here indicates whether the switch is on {-| Render a switch. The boolean here indicates whether the switch is on
or not. or not.
@ -133,6 +146,7 @@ view attrs isOn =
{ id = config.id { id = config.id
, onCheck = config.onSwitch , onCheck = config.onSwitch
, checked = isOn , checked = isOn
, attributes = config.attributes
} }
, Nri.Ui.Svg.V1.toHtml , Nri.Ui.Svg.V1.toHtml
(viewSwitch (viewSwitch
@ -162,26 +176,29 @@ viewCheckbox :
{ id : String { id : String
, onCheck : Maybe (Bool -> msg) , onCheck : Maybe (Bool -> msg)
, checked : Bool , checked : Bool
, attributes : List (Html.Attribute Never)
} }
-> Html msg -> Html msg
viewCheckbox config = viewCheckbox config =
Html.checkbox config.id Html.checkbox config.id
(Just config.checked) (Just config.checked)
[ Attributes.id config.id ([ Attributes.id config.id
, Attributes.css , Attributes.css
[ Css.position Css.absolute [ Css.position Css.absolute
, Css.top (Css.px 10) , Css.top (Css.px 10)
, Css.left (Css.px 10) , Css.left (Css.px 10)
, Css.zIndex (Css.int 0) , Css.zIndex (Css.int 0)
, Css.opacity (Css.num 0) , Css.opacity (Css.num 0)
] ]
, case config.onCheck of , case config.onCheck of
Just onCheck -> Just onCheck ->
Events.onCheck onCheck Events.onCheck onCheck
Nothing -> Nothing ->
Widget.disabled True Widget.disabled True
] ]
++ List.map (Attributes.map never) config.attributes
)
viewSwitch : viewSwitch :

View File

@ -34,16 +34,12 @@ example =
, preview = , preview =
[ Switch.view [ Switch.view
[ Switch.label (Html.text "Toggle On") [ Switch.label (Html.text "Toggle On")
, Switch.custom [ Key.tabbable False ]
-- TODO
-- Switch.custom [ Key.tabbable False ]
] ]
False False
, Switch.view , Switch.view
[ Switch.label (Html.text "Toggle Off") [ Switch.label (Html.text "Toggle Off")
, Switch.custom [ Key.tabbable False ]
-- TODO
-- Switch.custom [ Key.tabbable False ]
] ]
True True
] ]