ToggleGroup unstable
A row of toggle buttons that share one value: the values of the pressed ones, such as text alignment in a toolbar.
Details
ToggleGroup is a headless component. It keeps which item holds the tab
stop, and moves focus between the items with the arrow keys through a
Command. The value is the consumer's: it comes in as the value prop,
and the group emits Toggled with the value the user asked for.
Anatomy:
| Part | Element |
|---|---|
root |
<div> |
item |
<button>, one for each item |
root takes the consumer's builder and returns it, so the consumer goes
on chaining and adds the items with .children(...). item takes
{ element, value }, the item's <button> and the value of one entry
in the items prop, and returns the builder. A value that isn't in
items fails the render. Neither part adds markup.
An item writes the same attributes as Toggle, so don't stack Toggle
on it. Naming an item is the consumer's job, as for Toggle: text inside
it, or an aria-label on the button.
Data attributes:
Each present with an empty value or absent, except data-orientation,
which holds the orientation:
root:data-orientation,data-disabledanddata-multipleitem:data-pressedanddata-disabled
Keyboard:
Only one item is in the Tab order: the one focused last, or else the first enabled item. A key handler on each item moves focus:
| Key | Action |
|---|---|
| Right, Left | next and previous item, horizontally |
| Down, Up | next and previous item, vertically |
| Home, End | first and last enabled item |
| Space, Enter | press the item, from the native button |
The arrow keys follow orientation: a horizontal group ignores Down and
Up, and a vertical one Right and Left. They skip disabled items, and with loopFocus they wrap from the last
item to the first and back. In a right-to-left layout, Left and Right
swap. A key held with Alt, Ctrl, Meta or Shift does nothing.
Usage:
The items prop lists every item in the order they are drawn, so the
group can move focus without reading the page. Draw one item for each
entry.
Inside a Toolbar, the group leaves the tab stop and the arrow keys to
the toolbar, and is disabled with it. Stack each item inside a toolbar
button with the same value:
toolbar.button({ element: toggleGroup.item({ element: h.button, value }), value }).
An item is disabled with its toolbar item, by the item's entry, its group
or the toolbar.
Example Text alignment, one choice at a time
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { ToggleGroup } from "effect/ui/kit"import { Effect, Schema } from "effect"
const EditorMessage = UiSchema.TaggedEnum({ ToggledAlign: { value: Schema.Array(Schema.String) } })
const alignments = ["left", "center", "right"]
const Editor = Html.component({ name: "Editor", model: Schema.Struct({ align: Schema.Array(Schema.String) }), message: EditorMessage}).make({ init: () => ({ align: ["left"] }), update: { ToggledAlign: (_, { value }) => ({ align: value }) }, view: (h, model) => ToggleGroup(h, { props: { value: model.align, items: alignments.map((value) => ({ value })) }, onEmit: ({ value }) => EditorMessage.ToggledAlign({ value }), view: (toggleGroup) => toggleGroup.root(h.div.class("alignment").attr("aria-label", "Text alignment")).children( alignments.map((value) => toggleGroup.item({ element: h.button.class("tool").attr("aria-label", "Align " + value), value }) ) ) })})
// The left item is pressed, and holds the one tab stop.const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Editor, {}))if (!/<div class="alignment"[^>]* role="group"/.test(html)) throw new Error("the root isn't a group")if (!/aria-label="Align left"[^>]* aria-pressed="true"[^>]* tabindex="0"/.test(html)) { throw new Error("the left item isn't pressed with the tab stop")}Components
makeToggleGroup
unstableCreates a toggle group whose selected values, items, item part and
Toggled event use the supplied string-valued schema.
Details
The schema must encode to a string. Create the component once at module
level: each call creates a new component. The existing ToggleGroup export
uses Schema.String.
Signature
declare function makeToggleGroup<V extends string>(options: { readonly value: Codec<V, string>;}): Headless<{ readonly disabled?: boolean; readonly items: readonly Array<{ readonly disabled?: boolean; value: V; }>; readonly loopFocus?: boolean; readonly multiple?: boolean; readonly orientation?: "horizontal" | "vertical"; readonly value: readonly Array<V>;}, { readonly _tag: "Toggled"; readonly value: readonly Array<V>;}, { disabled: boolean; item: <B extends Keyed<"button">>(__namedParameters: { readonly element: B; readonly value: V; }) => ReturnType<B["attr"]> & Pick<Part<"button", [value: V], never>, "~R">; multiple: boolean; orientation: "horizontal" | "vertical"; root: Part<"div", [], never>; value: readonly Array<V>;}, never, never, never, never, never, never, { readonly active: string | null; readonly rtl: boolean;}, { readonly _tag: "FocusedItem"; readonly value: string;} | { readonly _tag: "ClickedItem"; readonly value: string;} | { readonly _tag: "FoundDirection"; readonly rtl: boolean;} | { readonly _tag: "PressedKey"; readonly key: string; readonly value: string;}, QueryEntries<{}, {}>>Example A toggle group typed by a literal union
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { makeToggleGroup } from "effect/ui/kit"import { Effect, Schema } from "effect"
const Align = Schema.Literals(["left", "center", "right"])const AlignToggleGroup = makeToggleGroup({ value: Align })const items = Align.literals.map((value) => ({ value }))
const Message = UiSchema.TaggedEnum({ Aligned: { align: Schema.Array(Align) } })
const Editor = Html.component({ name: "Editor", model: Schema.Struct({ align: Schema.Array(Align) }), message: Message}).make({ init: () => ({ align: ["left" as const] }), update: { Aligned: ($, { align }) => $.evolve({ align }) }, view: (h, model) => AlignToggleGroup(h, { props: { value: model.align, items }, // `value` is ReadonlyArray<"left" | "center" | "right"> onEmit: ({ value }) => Message.Aligned({ align: value }), view: (toggleGroup) => toggleGroup.root(h.div.attr("aria-label", "Alignment")).children( items.map(({ value }) => toggleGroup.item({ element: h.button, value }).children(value) ) ) })})
const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Editor, {}))if (!html.includes('aria-pressed="true"')) throw new Error("no item is pressed")ToggleGroup
unstableA headless toggle group. Call it with the consumer's h, the props,
an onEmit for Toggled, and a view closure that draws the root with
toggleGroup.root(h.div) and, inside it, one
toggleGroup.item({ element: h.button, value }) for each item.
Details
The parts also return value, disabled, multiple and
orientation, resolved, for markup that depends on them.
The group's own attributes win over the ones the consumer set on the builder before the part, including the ones a state leaves absent:
root:role="group",data-orientation,data-disabledanddata-multipleitem:type="button",aria-pressed,disabled,aria-disabled, which it never sets,tabindex,data-pressedanddata-disabled
A part writes them when it is applied, so an attribute the consumer chains after the part is the consumer's to answer for.
An item handles click, focus and keydown on a layer of its own,
so the consumer's handlers for them still run. It cancels only the keys
it moves focus with.
The group follows the nearest DirectionProvider above it or, without
one, the root's CSS direction. Inside a
Toolbar, an item writes no tabindex and handles no focus or
keydown. The toolbar's disabled disables the group, and a disabled
toolbar item disables the group's item with the same value.
Signature
declare const ToggleGroup: Headless<{ readonly disabled?: boolean; readonly items: readonly Array<{ readonly disabled?: boolean; value: string; }>; readonly loopFocus?: boolean; readonly multiple?: boolean; readonly orientation?: "horizontal" | "vertical"; readonly value: readonly Array<string>;}, { readonly _tag: "Toggled"; readonly value: readonly Array<string>;}, { disabled: boolean; item: <B extends Keyed<"button">>(__namedParameters: { readonly element: B; readonly value: string; }) => ReturnType<B["attr"]> & Pick<Part<"button", [value: string], never>, "~R">; multiple: boolean; orientation: "horizontal" | "vertical"; root: Part<"div", [], never>; value: readonly Array<string>;}, never, never, never, never, never, never, { readonly active: string | null; readonly rtl: boolean;}, { readonly _tag: "FocusedItem"; readonly value: string;} | { readonly _tag: "ClickedItem"; readonly value: string;} | { readonly _tag: "FoundDirection"; readonly rtl: boolean;} | { readonly _tag: "PressedKey"; readonly key: string; readonly value: string;}, QueryEntries<{}, {}>>Models
What a ToggleGroup emits.
Signature
type ToggleGroupEvents<V extends string = string> = ReturnType<typeof eventsSchema>["Type"]One item of a toggle group.
Signature
type ToggleGroupItem<V extends string = string> = ReturnType<typeof itemSchema>["Type"]The direction a toggle group's items run in.
Signature
type ToggleGroupOrientation = typeof ToggleGroupOrientation.TypeThe props of a ToggleGroup.
Signature
type ToggleGroupProps<V extends string = string> = ReturnType<typeof propsSchema>["Type"]Schemas
ToggleGroupEvents
unstableWhat a toggle group emits.
Details
Toggled: asks for the group's value invalue. Pressing an item gives[value]and pressing the pressed one gives[]. Withmultiple, the item's value is added at the end or removed. Store it and pass it back as thevalueprop. A disabled group or item emits nothing.
Signature
declare const ToggleGroupEvents: TaggedEnum<{ Toggled: TaggedStruct<"Toggled", { readonly value: $Array<String>; }>;}>ToggleGroupItem
unstableOne item of a toggle group: the value its item part is drawn with,
and whether it is disabled.
Signature
declare const ToggleGroupItem: Struct<{ readonly disabled: optional<Boolean>; value: String;}>ToggleGroupOrientation
unstableThe direction a toggle group's items run in, which picks its arrow keys.
Signature
declare const ToggleGroupOrientation: Literals<readonly ["horizontal", "vertical"]>ToggleGroupProps
unstableProps for the state of a ToggleGroup.
Details
value: the values of the pressed items. Required: the consumer owns it.items: every item, in the order they are drawn. Required. A disabled item ignores clicks and is skipped by the arrow keys.disabled: disables every item.multiple: lets several items be pressed at once. Off by default, so pressing an item releases the others.orientation:"horizontal"by default, which moves with Right and Left, or"vertical", which moves with Down and Up.loopFocus: the arrow keys wrap from the last item to the first and back. On by default.
Signature
declare const ToggleGroupProps: Struct<{ readonly disabled: optional<Boolean>; readonly items: $Array<Struct<{ readonly disabled: optional<Boolean>; value: String; }>>; readonly loopFocus: optional<Boolean>; readonly multiple: optional<Boolean>; readonly orientation: optional<Literals<readonly ["horizontal", "vertical"]>>; readonly value: $Array<String>;}>