The default. The chosen item is filled with the main color.
A row of buttons where exactly one is chosen, like a segmented control.
Mirrors ToggleGroup from @digdir/designsystemet-react.
type Layout
= List
| Grid
layoutToString : Layout -> String
layoutToString layout =
case layout of
List ->
"list"
Grid ->
"grid"
ToggleGroup.controlled
{ name = "layout"
, label = "Visning"
, toValue = layoutToString
, selected = model.layout
, onSelect = LayoutSelected
}
[]
[ ToggleGroup.item List [ Html.text "Liste" ]
, ToggleGroup.item Grid [ Html.text "Rutenett" ]
]It's a <fieldset> of radio buttons, so forms and screen readers treat it like
any radio group. designsystemet-web makes the arrow keys move focus without
choosing, and lets Enter choose.
uncontrolled: the browser tracks the chosen item, like
any radio group. Use it in a plain <form> that gets submitted, or when
nothing else on the page depends on the choice.controlled: your model says which item is chosen, and
clicking an item sends a message instead of choosing it.<ToggleGroup aria-label="…" defaultValue="a"> → ToggleGroup.uncontrolled { label = "…", initial = A, … } []<ToggleGroup value={v} onChange={f}> → ToggleGroup.controlled { selected = v, onSelect = F, … } []<ToggleGroup.Item value="a">A</ToggleGroup.Item> → ToggleGroup.item A [ Html.text "A" ]<Tooltip> → ToggleGroup.iconItem { value = A, label = "…", icon = … }<ToggleGroup.Item disabled> → ToggleGroup.item A […] |> ToggleGroup.disabled<ToggleGroup.Item aria-disabled="true"> → ToggleGroup.item A […] |> ToggleGroup.disabledFocusable<ToggleGroup.Item readOnly> → ToggleGroup.item A […] |> ToggleGroup.readOnlyvariant="secondary" → ToggleGroup.secondaryaria-label, so
it is named before designsystemet-web turns the tooltip into the item's
aria-label.name is required. React generates one with useId.Never set these yourself: role on the fieldset and the radios, and
tabindex on the radios (the focusgroup polyfill moves it around).
A toggle group your model tracks. Clicking an item (or pressing Enter on it)
sends onSelect with its value. The chosen item changes only when selected
does.
The fields are as in uncontrolled, with selected and
onSelect instead of initial.
One choice in the group.
An item with visible content, usually text.
ToggleGroup.item Archived [ Html.text "Arkiverte" ]An item showing only an icon. The label is required, since an icon means nothing to a screen reader or to someone who doesn't recognise it. It names the radio button and shows as a tooltip on hover and focus.
ToggleGroup.iconItem { value = Grid, label = "Rutenett", icon = gridIcon }An item that can't be chosen right now. It's dimmed and the keyboard skips it.
ToggleGroup.item Map [ Html.text "Kart" ]
|> ToggleGroup.disabledA dimmed item the keyboard still reaches, so screen reader users find it
and hear that it's unavailable (aria-disabled).
In a controlled group it can't be chosen. In an
uncontrolled group it's only marked unavailable, as in
React, and the browser still lets the user choose it.
An item that is shown but can't be switched to, for a value fixed in this
context. The keyboard still reaches it, and designsystemet-web stops clicks
on it.
The default. The chosen item is filled with the main color.
A quieter look: a tinted track with the chosen item raised on it.
A toggle group the browser tracks.
initialis the item chosen on first render.nameis the radio buttons'name, which the value is submitted under in a form. Unique per form.labelis the group's accessible name (aria-label). To name it with a visible heading, also passAttr.attribute "aria-labelledby" headingId, which takes precedence.toValueturns a value into the radio button'svalue.If
initialchanges later, that item is chosen. Otherwise Elm leaves the choice to the browser.