2018-11-12 23:25:37 +03:00
|
|
|
module Examples.Tabs exposing
|
|
|
|
( example
|
2020-03-31 22:43:32 +03:00
|
|
|
, State, Msg
|
2018-11-12 23:25:37 +03:00
|
|
|
)
|
2018-11-10 01:01:11 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
|
|
|
|
@docs example
|
2020-03-31 22:43:32 +03:00
|
|
|
@docs State, Msg
|
2018-11-10 01:01:11 +03:00
|
|
|
|
|
|
|
-}
|
|
|
|
|
2020-06-10 03:56:10 +03:00
|
|
|
import Browser.Dom as Dom
|
2020-03-24 03:33:42 +03:00
|
|
|
import Category exposing (Category(..))
|
2020-06-10 01:28:12 +03:00
|
|
|
import Css
|
2020-06-10 00:51:00 +03:00
|
|
|
import Debug.Control as Control exposing (Control)
|
2020-03-31 23:20:03 +03:00
|
|
|
import Example exposing (Example)
|
2020-06-10 00:51:00 +03:00
|
|
|
import Html.Styled as Html exposing (Html, fromUnstyled)
|
2020-06-10 01:28:12 +03:00
|
|
|
import Html.Styled.Attributes exposing (css)
|
2020-06-20 00:45:32 +03:00
|
|
|
import KeyboardSupport exposing (Key(..))
|
2020-06-10 01:12:30 +03:00
|
|
|
import List.Zipper exposing (Zipper)
|
2020-06-10 01:28:12 +03:00
|
|
|
import Nri.Ui.Svg.V1 as Svg
|
2020-09-05 01:53:19 +03:00
|
|
|
import Nri.Ui.Tabs.V7 as Tabs exposing (Alignment(..), Tab)
|
2020-09-05 01:26:57 +03:00
|
|
|
import Nri.Ui.Tooltip.V2 as Tooltip
|
2020-06-10 01:28:12 +03:00
|
|
|
import Nri.Ui.UiIcon.V1 as UiIcon
|
2020-06-10 03:56:10 +03:00
|
|
|
import Task
|
2018-11-10 01:01:11 +03:00
|
|
|
|
|
|
|
|
2020-06-10 00:51:00 +03:00
|
|
|
type alias State =
|
|
|
|
{ selected : Id
|
2020-06-10 00:55:39 +03:00
|
|
|
, settings : Control Settings
|
2020-09-08 21:55:09 +03:00
|
|
|
, openTooltip : Maybe Id
|
2020-06-10 00:51:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
init : State
|
|
|
|
init =
|
|
|
|
{ selected = First
|
2020-06-10 00:55:39 +03:00
|
|
|
, settings = initSettings
|
2020-09-08 21:55:09 +03:00
|
|
|
, openTooltip = Nothing
|
2020-06-10 00:55:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type alias Settings =
|
|
|
|
{ title : Maybe String
|
|
|
|
, alignment : Alignment
|
2020-06-10 04:08:33 +03:00
|
|
|
, customSpacing : Maybe Float
|
2021-05-26 16:29:54 +03:00
|
|
|
, labelledBy : Maybe String
|
2020-06-10 00:51:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-10 00:55:39 +03:00
|
|
|
initSettings : Control Settings
|
|
|
|
initSettings =
|
|
|
|
Control.record Settings
|
|
|
|
|> Control.field "title" (Control.maybe False (Control.string "Title"))
|
|
|
|
|> Control.field "alignment"
|
|
|
|
(Control.choice
|
|
|
|
[ ( "Left", Control.value Left )
|
|
|
|
, ( "Center", Control.value Center )
|
|
|
|
, ( "Right", Control.value Right )
|
|
|
|
]
|
|
|
|
)
|
2020-06-10 04:08:33 +03:00
|
|
|
|> Control.field "customSpacing"
|
|
|
|
(Control.maybe False
|
|
|
|
(Control.choice
|
|
|
|
[ ( "2", Control.value 2 )
|
|
|
|
, ( "3", Control.value 3 )
|
|
|
|
, ( "4", Control.value 4 )
|
|
|
|
, ( "8", Control.value 8 )
|
|
|
|
, ( "16", Control.value 16 )
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2021-05-26 16:29:54 +03:00
|
|
|
|> Control.field "labelledBy" (Control.maybe False (Control.string "someId"))
|
2020-06-10 00:55:39 +03:00
|
|
|
|
|
|
|
|
2020-06-10 00:51:00 +03:00
|
|
|
type Id
|
2018-11-10 01:01:11 +03:00
|
|
|
= First
|
|
|
|
| Second
|
2020-06-10 01:12:30 +03:00
|
|
|
| Third
|
|
|
|
| Fourth
|
2018-11-10 01:01:11 +03:00
|
|
|
|
|
|
|
|
2020-06-10 00:51:00 +03:00
|
|
|
type Msg
|
2020-09-09 00:35:36 +03:00
|
|
|
= FocusAndSelectTab { select : Id, focus : Maybe String }
|
2020-06-10 03:56:10 +03:00
|
|
|
| Focused (Result Dom.Error ())
|
2020-06-10 00:55:39 +03:00
|
|
|
| SetSettings (Control Settings)
|
2020-09-08 21:55:09 +03:00
|
|
|
| ToggleTooltip Id Bool
|
2020-06-10 00:51:00 +03:00
|
|
|
|
|
|
|
|
2020-06-10 03:43:54 +03:00
|
|
|
update : Msg -> State -> ( State, Cmd Msg )
|
2020-06-10 00:51:00 +03:00
|
|
|
update msg model =
|
|
|
|
case msg of
|
2020-09-09 00:35:36 +03:00
|
|
|
FocusAndSelectTab { select, focus } ->
|
|
|
|
( { model | selected = select }
|
|
|
|
, focus
|
|
|
|
|> Maybe.map (Dom.focus >> Task.attempt Focused)
|
|
|
|
|> Maybe.withDefault Cmd.none
|
|
|
|
)
|
2020-06-10 03:56:10 +03:00
|
|
|
|
|
|
|
Focused error ->
|
|
|
|
( model, Cmd.none )
|
|
|
|
|
2020-06-10 00:55:39 +03:00
|
|
|
SetSettings settings ->
|
2020-06-10 03:43:54 +03:00
|
|
|
( { model | settings = settings }, Cmd.none )
|
2020-03-31 22:43:32 +03:00
|
|
|
|
2020-09-08 21:55:09 +03:00
|
|
|
ToggleTooltip id openTooltip ->
|
|
|
|
( { model
|
|
|
|
| openTooltip =
|
|
|
|
if openTooltip then
|
|
|
|
Just id
|
|
|
|
|
|
|
|
else
|
|
|
|
Nothing
|
|
|
|
}
|
|
|
|
, Cmd.none
|
|
|
|
)
|
2020-09-05 01:26:57 +03:00
|
|
|
|
2018-11-12 23:25:37 +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 = "Tabs"
|
2020-09-11 00:47:33 +03:00
|
|
|
, version = 7
|
2020-03-31 22:43:32 +03:00
|
|
|
, categories = [ Layout ]
|
2020-06-20 00:50:47 +03:00
|
|
|
, keyboardSupport =
|
|
|
|
[ { keys = [ KeyboardSupport.Tab ]
|
2020-08-06 01:58:40 +03:00
|
|
|
, result = "Move focus to the currently-selected Tab's tab panel"
|
2020-06-20 00:50:47 +03:00
|
|
|
}
|
|
|
|
, { keys = [ Arrow KeyboardSupport.Left ]
|
|
|
|
, result = "Select the tab to the left of the currently-selected Tab"
|
|
|
|
}
|
|
|
|
, { keys = [ Arrow KeyboardSupport.Right ]
|
|
|
|
, result = "Select the tab to the right of the currently-selected Tab"
|
|
|
|
}
|
|
|
|
]
|
2020-06-10 00:51:00 +03:00
|
|
|
, state = init
|
2020-06-10 03:43:54 +03:00
|
|
|
, update = update
|
2020-03-31 22:48:26 +03:00
|
|
|
, subscriptions = \_ -> Sub.none
|
2021-11-05 21:19:08 +03:00
|
|
|
, preview = []
|
2020-03-31 22:43:32 +03:00
|
|
|
, view =
|
2020-06-10 00:51:00 +03:00
|
|
|
\model ->
|
2020-06-10 00:55:39 +03:00
|
|
|
let
|
|
|
|
settings =
|
|
|
|
Control.currentValue model.settings
|
|
|
|
in
|
|
|
|
[ Control.view SetSettings model.settings |> fromUnstyled
|
2020-06-10 00:51:00 +03:00
|
|
|
, Tabs.view
|
2020-06-10 00:55:39 +03:00
|
|
|
{ title = settings.title
|
2020-06-10 02:23:15 +03:00
|
|
|
, alignment = settings.alignment
|
2020-06-10 04:08:33 +03:00
|
|
|
, customSpacing = settings.customSpacing
|
2020-09-09 00:35:36 +03:00
|
|
|
, focusAndSelect = FocusAndSelectTab
|
2020-06-10 01:53:46 +03:00
|
|
|
, selected = model.selected
|
2021-05-26 16:29:54 +03:00
|
|
|
, tabs = allTabs model.openTooltip settings.labelledBy
|
2020-03-31 22:43:32 +03:00
|
|
|
}
|
|
|
|
]
|
2018-11-10 01:01:11 +03:00
|
|
|
}
|
2020-06-10 01:12:30 +03:00
|
|
|
|
|
|
|
|
2021-05-26 16:29:54 +03:00
|
|
|
allTabs : Maybe Id -> Maybe String -> List (Tab Id Msg)
|
|
|
|
allTabs openTooltipId labelledBy =
|
2020-09-08 21:00:29 +03:00
|
|
|
let
|
|
|
|
bulbIcon =
|
|
|
|
UiIcon.bulb
|
|
|
|
|> Svg.withWidth (Css.px 40)
|
2020-09-08 21:22:17 +03:00
|
|
|
|> Svg.withHeight (Css.px 45)
|
2020-09-09 21:11:14 +03:00
|
|
|
|> Svg.withLabel "Bulb"
|
2020-09-08 21:00:29 +03:00
|
|
|
|> Svg.withCss [ Css.padding2 Css.zero (Css.px 6) ]
|
|
|
|
|> Svg.toHtml
|
|
|
|
in
|
2020-09-08 20:56:29 +03:00
|
|
|
[ Tabs.build { id = First, idString = "tab-0" }
|
2021-05-26 16:29:54 +03:00
|
|
|
([ Tabs.spaHref "/#/doodad/Tabs"
|
|
|
|
, Tabs.tabString "1"
|
|
|
|
, Tabs.withTooltip
|
2020-09-08 21:55:09 +03:00
|
|
|
[ Tooltip.plaintext "Link Example"
|
|
|
|
, Tooltip.onHover (ToggleTooltip First)
|
|
|
|
, Tooltip.alignStart (Css.px 75)
|
|
|
|
, Tooltip.primaryLabel
|
|
|
|
, Tooltip.open (openTooltipId == Just First)
|
|
|
|
]
|
2021-05-26 16:29:54 +03:00
|
|
|
, Tabs.panelHtml (Html.text "First Panel")
|
|
|
|
]
|
|
|
|
++ (case labelledBy of
|
|
|
|
Nothing ->
|
|
|
|
[]
|
|
|
|
|
|
|
|
Just labelledById ->
|
|
|
|
[ Tabs.labelledBy labelledById ]
|
|
|
|
)
|
|
|
|
)
|
2020-09-08 20:56:29 +03:00
|
|
|
, Tabs.build { id = Second, idString = "tab-1" }
|
2021-05-26 00:25:05 +03:00
|
|
|
[ Tabs.tabString "Second Tab (disabled)"
|
|
|
|
, Tabs.disabled True
|
2020-09-08 20:56:29 +03:00
|
|
|
, Tabs.panelHtml (Html.text "Second Panel")
|
|
|
|
]
|
|
|
|
, Tabs.build { id = Third, idString = "tab-2" }
|
2020-09-08 21:00:29 +03:00
|
|
|
[ Tabs.tabHtml bulbIcon
|
|
|
|
, Tabs.withTooltip
|
|
|
|
[ Tooltip.plaintext "The Electrifying Third Tab"
|
2020-09-08 21:55:09 +03:00
|
|
|
, Tooltip.onHover (ToggleTooltip Third)
|
2020-09-08 21:00:29 +03:00
|
|
|
, Tooltip.primaryLabel
|
2020-09-08 21:55:09 +03:00
|
|
|
, Tooltip.open (openTooltipId == Just Third)
|
2020-09-08 21:00:29 +03:00
|
|
|
]
|
2020-09-08 20:56:29 +03:00
|
|
|
, Tabs.panelHtml (Html.text "Third Panel")
|
|
|
|
]
|
|
|
|
, Tabs.build { id = Fourth, idString = "tab-3" }
|
|
|
|
[ Tabs.tabString "Fourth Tab"
|
|
|
|
, Tabs.panelHtml (Html.text "Fourth Panel")
|
|
|
|
]
|
2020-06-10 02:23:15 +03:00
|
|
|
]
|