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

276 lines
9.0 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)
2020-06-19 23:41:28 +03:00
import AtomicDesignType exposing (AtomicDesignType(..))
import Category exposing (Category(..))
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)
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)
import KeyboardSupport exposing (Direction(..), Key(..))
2020-09-03 20:50:42 +03:00
import Nri.Ui.Colors.V1 as Colors
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
2019-12-03 21:43:24 +03:00
import Nri.Ui.Text.V4 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 = "Nri.Ui.Tooltip.V2"
, categories = [ Widgets ]
, atomicDesignType = Molecule
, keyboardSupport = []
, state = init
, update = update
, subscriptions = \_ -> Sub.none
, view = view
}
2019-08-17 01:26:51 +03:00
type alias State =
{ openTooltip : Maybe TooltipType
2020-09-03 20:33:31 +03:00
, staticExampleSettings : Control StaticExampleSettings
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
= PrimaryLabelOnClick
| PrimaryLabelOnHover
| AuxillaryDescription
| ToggleTipTop
| ToggleTipRight
| ToggleTipBottom
| ToggleTipLeft
2019-08-17 01:26:51 +03:00
type Msg
= ToggleTooltip TooltipType Bool
2020-09-03 20:33:31 +03:00
| SetStaticExampleSettings (Control StaticExampleSettings)
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
2020-09-03 20:33:31 +03:00
SetStaticExampleSettings settings ->
( { 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 =
2020-09-03 20:33:31 +03:00
[ Heading.h2 [] [ Html.text "Static Examples" ]
, viewStaticExamples model.staticExampleSettings
, Heading.h2 [] [ Html.text "Interactive Examples" ]
, Text.mediumBody [ Html.text "These tooltips look similar, but serve different purposes when reading them via a screen-reader." ]
2020-09-03 20:26:48 +03:00
, Heading.h3 [] [ Html.text "primaryLabel" ]
, Text.smallBody
[ 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."
]
, Tooltip.view
{ triggerHtml = Html.text "Primary Label - OnClick Trigger"
, id = "primary label tooltip"
}
2020-09-03 21:58:11 +03:00
[ Tooltip.html [ Html.text "Tooltip ", Html.a [ href "/" ] [ Html.text "Links work!" ] ]
2020-09-03 20:26:48 +03:00
, Tooltip.primaryLabel
, Tooltip.onClick (ToggleTooltip PrimaryLabelOnClick)
, Tooltip.open (model.openTooltip == Just PrimaryLabelOnClick)
]
, Html.br [ css [ Css.marginBottom (Css.px 20) ] ]
, Tooltip.view
{ triggerHtml = Html.text "Primary Label - OnHover Trigger"
, id = "primary label tooltip"
}
2020-09-03 21:58:11 +03:00
[ Tooltip.html [ Html.text "Tooltip ", Html.a [ href "/" ] [ Html.text "Links work!" ] ]
2020-09-03 20:26:48 +03:00
, Tooltip.primaryLabel
, Tooltip.onHover (ToggleTooltip PrimaryLabelOnHover)
, Tooltip.open (model.openTooltip == Just PrimaryLabelOnHover)
]
, Html.br [ css [ Css.marginBottom (Css.px 20) ] ]
, Heading.h3 [] [ Html.text "auxillaryDescription" ]
, Text.smallBody
[ 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."
]
, Tooltip.view
{ triggerHtml = Html.text "Auxillary Description Trigger"
, id = "Auxillary description"
}
[ Tooltip.plaintext "Tooltip"
, Tooltip.onClick (ToggleTooltip AuxillaryDescription)
, Tooltip.auxillaryDescription
, Tooltip.open (model.openTooltip == Just AuxillaryDescription)
]
, Html.br [ css [ Css.marginBottom (Css.px 20) ] ]
, Heading.h3 [] [ Html.text "toggleTip" ]
, Text.smallBody [ Html.text "A Toggle Tip is triggered by the \"?\" icon and provides supplemental information for the page." ]
, Html.div [ css [ Css.displayFlex, Css.alignItems Css.center ] ]
[ Tooltip.toggleTip
{ label = "More info"
}
[ Tooltip.html
[ Html.text "Tooltip On Top! "
, Html.a [ href "/" ] [ Html.text "Links work!" ]
2020-09-03 20:18:17 +03:00
]
2020-09-03 20:26:48 +03:00
, Tooltip.onHover (ToggleTooltip ToggleTipTop)
, Tooltip.open (model.openTooltip == Just ToggleTipTop)
]
, Text.mediumBody
[ Html.text "This toggletip will open on top"
]
]
, Html.div [ css [ Css.displayFlex, Css.alignItems Css.center ] ]
[ Tooltip.toggleTip
{ label = "More info"
}
[ Tooltip.html
[ Html.text "Tooltip On Left! "
, Html.a [ href "/" ] [ Html.text "Links work!" ]
2019-12-03 21:43:24 +03:00
]
2020-09-03 20:26:48 +03:00
, Tooltip.onLeft
, Tooltip.onHover (ToggleTooltip ToggleTipLeft)
, Tooltip.open (model.openTooltip == Just ToggleTipLeft)
2019-12-03 03:13:14 +03:00
]
2020-09-03 20:26:48 +03:00
, Text.mediumBody
[ Html.text "This toggletip will open on the left"
]
]
]
2020-09-03 20:33:31 +03:00
type alias StaticExampleSettings =
2020-09-03 20:50:42 +03:00
{ content : Tooltip.Attribute Never
2020-09-03 20:52:46 +03:00
, direction : Tooltip.Attribute Never
2020-09-03 20:56:07 +03:00
, width : Tooltip.Attribute Never
2020-09-03 20:57:55 +03:00
, padding : Tooltip.Attribute Never
2020-09-03 20:50:42 +03:00
}
2020-09-03 20:33:31 +03:00
initStaticExampleSettings : Control StaticExampleSettings
initStaticExampleSettings =
Control.record StaticExampleSettings
2020-09-03 20:50:42 +03:00
|> Control.field "content" controlContent
2020-09-03 20:52:46 +03:00
|> Control.field "direction" controlDirection
2020-09-03 20:56:07 +03:00
|> Control.field "width" controlWidth
2020-09-03 20:57:55 +03:00
|> Control.field "padding" controlPadding
2020-09-03 20:50:42 +03:00
controlContent : Control (Tooltip.Attribute Never)
controlContent =
Control.choice
[ ( "plaintext"
, "Song lyrics are literature."
|> Control.string
|> Control.map Tooltip.plaintext
)
, ( "HTML (short)"
, [ Html.code [] [ Html.text "git status" ]
, Html.text " "
, Html.em [] [ Html.text "tries again" ]
]
|> Tooltip.html
|> Control.value
)
, ( "HTML"
, [ Html.text "Click "
, Html.a [ href "http://www.noredink.com", Attributes.target "_blank" ]
[ Html.text "here, yes, HERE, right here on this long tooltip. "
, Html.div
[ css
[ Css.display Css.inlineBlock
, Css.width (Css.px 20)
]
]
[ Svg.toHtml UiIcon.gear ]
]
, Html.text " to check out NoRedInk."
]
|> Tooltip.html
|> Control.value
)
]
2020-09-03 20:33:31 +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-03 20:56:07 +03:00
controlWidth : Control (Tooltip.Attribute Never)
controlWidth =
Control.choice
[ ( "exactWidth 320 (default)", Control.value (Tooltip.exactWidth 320) )
, ( "exactWidth 100", Control.value (Tooltip.exactWidth 100) )
, ( "exactWidth 400", Control.value (Tooltip.exactWidth 400) )
, ( "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 )
, ( "customPadding 40", Control.value (Tooltip.customPadding 40) )
]
2020-09-03 20:33:31 +03:00
viewStaticExamples : Control StaticExampleSettings -> Html Msg
viewStaticExamples controlSettings =
let
settings =
Control.currentValue controlSettings
2020-09-03 20:50:42 +03:00
attributes =
[ Tooltip.open True
, settings.content
2020-09-03 20:52:46 +03:00
, settings.direction
2020-09-03 20:56:07 +03:00
, settings.width
2020-09-03 20:57:55 +03:00
, settings.padding
2020-09-03 20:50:42 +03:00
]
2020-09-03 20:33:31 +03:00
in
Html.div []
[ Control.view SetStaticExampleSettings controlSettings
|> Html.fromUnstyled
2020-09-03 20:52:46 +03:00
, Html.div [ css [ Css.margin (Css.px 150) ] ]
2020-09-03 20:50:42 +03:00
[ Tooltip.view
{ triggerHtml =
UiIcon.arrowTop
|> Svg.withColor Colors.azure
|> Svg.withWidth (Css.px 20)
|> Svg.toHtml
, id = "my-top-tooltip"
}
attributes
|> Html.map never
]
2020-09-03 20:33:31 +03:00
]