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

318 lines
10 KiB
Elm
Raw Normal View History

2020-03-31 22:43:32 +03:00
module Examples.Tooltip exposing (example, State, Msg)
2019-08-17 01:26:51 +03:00
{-|
2020-03-31 22:43:32 +03:00
@docs example, State, Msg
2019-08-17 01:26:51 +03:00
-}
2020-09-03 20:26:48 +03:00
import Accessibility.Styled as Html exposing (Html)
import Accessibility.Styled.Key as Key
import Category exposing (Category(..))
2022-03-18 03:30:50 +03:00
import CommonControls
2019-08-17 01:26:51 +03:00
import Css
2020-09-03 20:33:31 +03:00
import Debug.Control as Control exposing (Control)
2021-10-28 18:57:25 +03:00
import Debug.Control.Extra as ControlExtra
2022-03-18 03:25:51 +03:00
import Debug.Control.View as ControlView
2020-03-31 23:20:03 +03:00
import Example exposing (Example)
2020-09-03 20:50:42 +03:00
import Html.Styled.Attributes as Attributes exposing (css, href)
2021-05-28 04:59:03 +03:00
import Nri.Ui.ClickableSvg.V2 as ClickableSvg
import Nri.Ui.ClickableText.V3 as ClickableText
2019-08-17 01:26:51 +03:00
import Nri.Ui.Heading.V2 as Heading
2020-09-03 20:50:42 +03:00
import Nri.Ui.Svg.V1 as Svg
2021-10-27 20:42:23 +03:00
import Nri.Ui.Text.V6 as Text
2020-09-03 18:44:48 +03:00
import Nri.Ui.Tooltip.V2 as Tooltip
2020-09-03 20:50:42 +03:00
import Nri.Ui.UiIcon.V1 as UiIcon
2019-08-17 01:26:51 +03:00
2020-09-03 20:26:48 +03:00
example : Example State Msg
example =
{ name = "Tooltip"
, version = 2
2021-11-12 01:54:56 +03:00
, categories = [ Messaging ]
2020-09-03 20:26:48 +03:00
, keyboardSupport = []
, state = init
, update = update
, subscriptions = \_ -> Sub.none
2021-11-06 00:17:14 +03:00
, preview =
[ Html.div
[ css
[ Css.marginTop (Css.px 60)
, Css.alignSelf Css.center
]
]
[ Tooltip.view
{ id = "preview-tooltip"
, trigger =
\attributes ->
ClickableSvg.button "example-preview-tooltip-icon"
UiIcon.gear
[ ClickableSvg.custom attributes
, ClickableSvg.small
, ClickableSvg.custom [ Key.tabbable False ]
2021-11-06 00:17:14 +03:00
]
}
[ Tooltip.plaintext "This is a tooltip."
, Tooltip.open True
, Tooltip.onTop
, Tooltip.smallPadding
, Tooltip.fitToContent
]
]
]
2020-09-03 20:26:48 +03:00
, view = view
}
2019-08-17 01:26:51 +03:00
type alias State =
{ openTooltip : Maybe TooltipType
2021-10-28 18:57:25 +03:00
, staticExampleSettings : Control (List (Tooltip.Attribute Never))
2019-08-17 01:26:51 +03:00
}
init : State
init =
2020-09-03 20:26:48 +03:00
{ openTooltip = Nothing
2020-09-03 20:33:31 +03:00
, staticExampleSettings = initStaticExampleSettings
2020-09-03 20:26:48 +03:00
}
type TooltipType
= PrimaryLabel
2020-09-03 20:26:48 +03:00
| AuxillaryDescription
| LearnMore
2019-08-17 01:26:51 +03:00
type Msg
= ToggleTooltip TooltipType Bool
2022-03-18 03:25:51 +03:00
| SetControl (Control (List (Tooltip.Attribute Never)))
2019-08-17 01:26:51 +03:00
2020-03-31 22:43:32 +03:00
update : Msg -> State -> ( State, Cmd Msg )
2019-08-17 01:26:51 +03:00
update msg model =
case msg of
ToggleTooltip type_ isOpen ->
if isOpen then
2020-03-31 22:43:32 +03:00
( { model | openTooltip = Just type_ }, Cmd.none )
2019-08-17 01:26:51 +03:00
else
2020-03-31 22:43:32 +03:00
( { model | openTooltip = Nothing }, Cmd.none )
2019-08-17 01:26:51 +03:00
2022-03-18 03:25:51 +03:00
SetControl settings ->
2020-09-03 20:33:31 +03:00
( { model | staticExampleSettings = settings }, Cmd.none )
2019-08-17 01:26:51 +03:00
2020-09-03 20:26:48 +03:00
view : State -> List (Html Msg)
view model =
[ Heading.h3 [] [ Html.text "Using the Tooltip module" ]
2021-10-27 21:54:14 +03:00
, Text.mediumBody
[ Text.html
[ Html.text "Label the Tooltip as either being the "
, viewPrimaryLabelTooltip model.openTooltip
, Html.text " or the "
, viewAuxillaryDescriptionToolip model.openTooltip
, Html.text " for the trigger content."
, viewToggleTip model.openTooltip
]
2020-09-03 20:26:48 +03:00
]
, viewCustomizableExample model.staticExampleSettings
]
viewPrimaryLabelTooltip : Maybe TooltipType -> Html Msg
viewPrimaryLabelTooltip openTooltip =
Tooltip.view
{ id = "tooltip__primaryLabel"
, trigger =
\eventHandlers ->
ClickableText.button "primaryLabel"
[ ClickableText.custom eventHandlers
]
2020-09-03 20:26:48 +03:00
}
[ Tooltip.html
[ Html.text "A primary label is used when the tooltip content serves as the main label for its trigger content"
, Html.br []
, Html.text "e.g. when the trigger content is an icon with no text."
]
2020-09-03 20:26:48 +03:00
, Tooltip.auxillaryDescription
, Tooltip.onHover (ToggleTooltip PrimaryLabel)
, Tooltip.open (openTooltip == Just PrimaryLabel)
, Tooltip.onBottom
2020-09-03 20:26:48 +03:00
]
viewAuxillaryDescriptionToolip : Maybe TooltipType -> Html Msg
viewAuxillaryDescriptionToolip openTooltip =
Tooltip.view
{ id = "tooltip__auxillaryDescription"
, trigger =
\eventHandlers ->
ClickableText.button "auxillaryDescription"
[ ClickableText.custom eventHandlers
]
}
[ Tooltip.html
[ Html.text "An auxillary description is used when the tooltip content provides supplementary information about its trigger content"
, Html.br []
, Html.text "e.g. when the trigger content is a word in the middle of a body of text that requires additional explanation."
2020-09-03 20:26:48 +03:00
]
, Tooltip.auxillaryDescription
, Tooltip.onHover (ToggleTooltip AuxillaryDescription)
, Tooltip.open (openTooltip == Just AuxillaryDescription)
, Tooltip.onBottom
2020-09-03 20:26:48 +03:00
]
viewToggleTip : Maybe TooltipType -> Html Msg
viewToggleTip openTooltip =
Tooltip.toggleTip { label = "tooltip__learn-more" }
[ Tooltip.html
[ Html.a
[ href "https://inclusive-components.design/tooltips-toggletips" ]
[ Html.text "Learn more" ]
2020-09-03 20:26:48 +03:00
]
, Tooltip.primaryLabel
, Tooltip.onHover (ToggleTooltip LearnMore)
, Tooltip.open (openTooltip == Just LearnMore)
, Tooltip.smallPadding
, Tooltip.fitToContent
2020-09-03 20:26:48 +03:00
]
2020-09-03 20:33:31 +03:00
2021-10-28 18:57:25 +03:00
initStaticExampleSettings : Control (List (Tooltip.Attribute Never))
2020-09-03 20:33:31 +03:00
initStaticExampleSettings =
2021-10-28 18:57:25 +03:00
ControlExtra.list
|> ControlExtra.listItem "content" controlContent
|> ControlExtra.listItem "direction" controlDirection
|> ControlExtra.listItem "alignment" controlAlignment
|> ControlExtra.listItem "withoutTail" controlTail
2021-10-28 18:57:25 +03:00
|> ControlExtra.listItem "width" controlWidth
|> ControlExtra.listItem "padding" controlPadding
2020-09-03 20:50:42 +03:00
controlContent : Control (Tooltip.Attribute Never)
controlContent =
2022-03-18 03:30:50 +03:00
CommonControls.content
{ moduleName = "Tooltip"
, plaintext = Tooltip.plaintext
, markdown = Nothing
, html = Tooltip.html
, httpError = Nothing
}
|> Control.map Tuple.second
2020-09-03 20:33:31 +03:00
2021-10-28 18:57:25 +03:00
controlTail : Control (Tooltip.Attribute Never)
2021-05-21 00:55:39 +03:00
controlTail =
2021-10-28 18:57:25 +03:00
Control.map
(\bool ->
2021-10-30 03:23:48 +03:00
if bool then
2021-10-28 18:57:25 +03:00
Tooltip.withoutTail
else
-- TODO: change `withoutTail` to take
-- a bool or expose a `withTail` from Tooltip.
Tooltip.css []
)
(Control.bool False)
2021-05-21 00:55:39 +03:00
2020-09-03 20:52:46 +03:00
controlDirection : Control (Tooltip.Attribute Never)
controlDirection =
Control.choice
[ ( "onTop", Control.value Tooltip.onTop )
, ( "onBottom", Control.value Tooltip.onBottom )
, ( "onLeft", Control.value Tooltip.onLeft )
, ( "onRight", Control.value Tooltip.onRight )
]
2020-09-04 01:28:58 +03:00
controlAlignment : Control (Tooltip.Attribute Never)
controlAlignment =
Control.choice
2020-09-05 01:09:13 +03:00
[ ( "alignMiddle (default)", Control.value Tooltip.alignMiddle )
, ( "alignStart", Control.map (Css.px >> Tooltip.alignStart) controlNumber )
, ( "alignEnd", Control.map (Css.px >> Tooltip.alignEnd) controlNumber )
2020-09-04 01:28:58 +03:00
]
controlNumber : Control Float
controlNumber =
Control.map (String.toFloat >> Maybe.withDefault 0) (Control.string "0")
2020-09-03 20:56:07 +03:00
controlWidth : Control (Tooltip.Attribute Never)
controlWidth =
Control.choice
[ ( "exactWidth 320 (default)", Control.value (Tooltip.exactWidth 320) )
2020-09-05 01:08:04 +03:00
, ( "exactWidth", Control.map (round >> Tooltip.exactWidth) controlNumber )
2020-09-03 20:56:07 +03:00
, ( "fitToContent", Control.value Tooltip.fitToContent )
]
2020-09-03 20:57:55 +03:00
controlPadding : Control (Tooltip.Attribute Never)
controlPadding =
Control.choice
[ ( "normalPadding (default)", Control.value Tooltip.normalPadding )
, ( "smallPadding", Control.value Tooltip.smallPadding )
2020-09-05 01:08:04 +03:00
, ( "customPadding", Control.map Tooltip.customPadding controlNumber )
2020-09-03 20:57:55 +03:00
]
2021-10-28 18:57:25 +03:00
viewCustomizableExample : Control (List (Tooltip.Attribute Never)) -> Html Msg
viewCustomizableExample controlSettings =
2020-09-03 20:33:31 +03:00
let
settings =
Control.currentValue controlSettings
2020-09-03 20:50:42 +03:00
attributes =
2021-10-28 18:57:25 +03:00
Tooltip.open True :: settings
2020-09-03 20:33:31 +03:00
in
Html.div []
2022-03-18 03:25:51 +03:00
[ ControlView.view
{ update = SetControl
, settings = controlSettings
, toExampleCode =
\controls ->
[ { sectionName = "Example"
, code =
String.join "\n"
[ "Tooltip.view"
, " { trigger ="
, " \\popupTriggerAttributes ->"
, " ClickableSvg.button \"Up\""
, " UiIcon.arrowTop"
, " [ ClickableSvg.custom popupTriggerAttributes"
, " ]"
, " , id = \"an-id-for-the-tooltip\""
, " }"
, " [ "
++ String.join "\n , "
("Tooltip.open True" :: List.map (\_ -> "TODO") controls)
, " ]"
]
}
]
}
2020-09-04 01:28:58 +03:00
, Html.div
[ css
[ Css.displayFlex
, Css.justifyContent Css.center
, Css.alignItems Css.center
, Css.height (Css.px 300)
]
]
2020-09-03 20:50:42 +03:00
[ Tooltip.view
{ trigger =
\eventHandlers ->
2022-03-18 03:25:51 +03:00
ClickableSvg.button "Up"
UiIcon.arrowTop
[ ClickableSvg.custom eventHandlers
]
2022-03-18 03:25:51 +03:00
, id = "an-id-for-the-tooltip"
2020-09-03 20:50:42 +03:00
}
attributes
|> Html.map never
]
2020-09-03 20:33:31 +03:00
]