Toolbar unstable
A row or column of controls, such as buttons, toggle groups, menus and a search input, that takes one stop in the Tab order and moves focus between its controls with the arrow keys.
Toolbar is a headless component. It keeps which item holds the tab stop,
and moves focus between the items through a Command. It emits nothing: each
control inside reports its own press, toggle or choice.
Anatomy
| Part | Element |
|---|---|
root |
<div> |
group |
<div>, around some of the items |
button |
<button>, one for each button item |
link |
<a>, one for each link item |
input |
<input>, one for each input item |
separator |
<div>, between items |
root and separator take the consumer's builder and return it, so the
consumer goes on chaining and adds children with .children(...). group,
button, link and input take { element, value }: the builder and the
value of one entry in the groups or items prop. A value that isn't
there fails the render. No part adds markup.
An item part is the element of another kit call too. Stack it inside a
trigger or a toggle group item, and pass the result to Button:
toolbar.button({ element: menu.trigger(h.button), value }), and the same for the trigger of aSelect,Popover,DialogorAlertDialogtoolbar.button({ element: toggleGroup.item({ element: h.button, value }), value })tooltip.trigger(toolbar.button({ element: h.button, value }))Button(h, { element: toolbar.button({ element: h.button, value }), onPress })
A ToggleGroup inside a toolbar leaves the tab stop and the arrow keys to
the toolbar, and each of its items is disabled with the toolbar item of
the same value. In a vertical toolbar, a Menu or Select trigger
leaves Down and Up to the toolbar, and opens with Enter or Space.
Data attributes
Each present with an empty value or absent, except data-orientation,
which holds an orientation:
root,group:data-orientationanddata-disabledbutton,input:data-orientation,data-disabledanddata-focusable, present while the item stays focusable when disabledlink:data-orientationseparator:data-orientation, across the toolbar's:"vertical"in a horizontal toolbar
Keyboard
Only one item is in the Tab order: the one focused last, or else the first item that can take focus. 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 item, from a button or link |
The arrow keys follow orientation, and with loopFocus they wrap from
the last item to the first and back. A disabled item stays in the order
unless its entry sets focusableWhenDisabled: false. In a right-to-left
layout, Left and Right swap. A key held with Alt, Ctrl, Meta or Shift does
nothing.
In a text input, the arrow keys move the caret, and leave the input only from the end of the text, or from its start for the previous item. Home, End and a selection stay with the input. An input reached with the arrow keys has its text selected.
Usage
The items prop lists every item in the order they are drawn, so the
toolbar can move focus without reading the page. Draw one item part for
each entry. Left and Right also move the caret, so put at most one text
input in a horizontal toolbar, as its last item.
A disabled item cancels the clicks the consumer handles on it, and those of
a Button stacked outside the part. A component stacked inside, such as a
Menu trigger or a Select, handles clicks on its own layer: pass it
disabled: toolbar.isDisabled(value) too. A ToggleGroup needs nothing.
Example A formatting toolbar
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { Button, Toolbar } from "effect/ui/kit"import { Effect, Schema } from "effect"
const EditorMessage = UiSchema.TaggedEnum({ PressedBold: {}, PressedItalic: {} })
const Editor = Html.component({ name: "Editor", model: Schema.Null, message: EditorMessage}).make({ init: () => null, update: ($) => $.model, view: (h) => Toolbar(h, { props: { items: [{ value: "bold" }, { value: "italic" }, { value: "help" }] }, view: (toolbar) => toolbar.root(h.div.class("toolbar").attr("aria-label", "Formatting")).children( Button(h, { element: toolbar.button({ element: h.button, value: "bold" }), onPress: EditorMessage.PressedBold() }) .children("Bold"), Button(h, { element: toolbar.button({ element: h.button, value: "italic" }), onPress: EditorMessage.PressedItalic() }).children("Italic"), toolbar.separator(h.div), toolbar.link({ element: h.a.attr("href", "/help"), value: "help" }).children("Help") ) })})
const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Editor, {}))if (!/<div class="toolbar"[^>]* role="toolbar"/.test(html)) throw new Error("the root isn't a toolbar")if (!/tabindex="0"[^>]*>Bold</.test(html) || !/tabindex="-1"[^>]*>Italic</.test(html)) { throw new Error("the first item doesn't hold the only tab stop")}Components
A headless toolbar. Call it with the consumer's h, the props, and a view
closure that draws the root with toolbar.root(h.div) and, inside it, one
toolbar.button, toolbar.link or toolbar.input for each item.
Details
The parts also return disabled and orientation, resolved, and
isDisabled(value), whether an item is disabled by its entry, its group or
the toolbar.
The toolbar'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="toolbar",aria-orientation,data-orientationanddata-disabledgroup:role="group",data-orientationanddata-disabledbutton:type="button",tabindex,disabled,aria-disabled,data-orientation,data-disabledanddata-focusableinput: the same buttype, andreadonlywhile it is disabled and focusable, which keeps the consumer's otherwiselink:tabindexanddata-orientationseparator:role="separator",aria-orientationanddata-orientation
A part writes them when it is applied. A button or input writes
disabled, aria-disabled and data-disabled again on the finished
element, over a Button stacked outside it.
Each item handles focus and keydown on a layer of its own, so the
consumer's handlers for them still run. A button or link cancels only the
keys it moves focus with, and an input none. A disabled, focusable button
cancels its clicks, and replaces a click handler set outside its layer.
The toolbar shares { disabled, orientation, disabledItems } with the
components in its view closure, disabledItems holding the values of
the disabled items. A ToggleGroup inside reads all three, and a Menu
or Select its orientation. It follows the nearest
DirectionProvider above it or, without one, the root's CSS direction.
Signature
declare const Toolbar: Headless<{ readonly disabled?: boolean; readonly groups?: readonly Array<{ readonly disabled?: boolean; readonly value: string; }>; readonly items: readonly Array<{ readonly disabled?: boolean; readonly focusableWhenDisabled?: boolean; readonly group?: string; readonly value: string; }>; readonly loopFocus?: boolean; readonly orientation?: "horizontal" | "vertical";}, never, { button: <B extends Keyed<"button">>(__namedParameters: { readonly element: B; readonly value: string; }) => ReturnType<B["attr"]> & Pick<Part<"button", [value: string, off: boolean, focusable: boolean], never>, "~R">; disabled: boolean; group: <B extends PartTarget<"div">>(__namedParameters: { readonly element: B; readonly value: string; }) => ReturnType<B["attr"]> & Pick<Part<"div", [value: string], never>, "~R">; input: <B extends Keyed<"input">>(__namedParameters: { readonly element: B; readonly value: string; }) => ReturnType<B["attr"]> & Pick<Part<"input", [value: string], never>, "~R">; isDisabled: (value: string) => boolean; link: <B extends Keyed<"a">>(__namedParameters: { readonly element: B; readonly value: string; }) => ReturnType<B["attr"]> & Pick<Part<"a", [value: string], never>, "~R">; orientation: "horizontal" | "vertical"; root: Part<"div", [], never>; separator: Part<"div", [], never>;}, never, never, Shared<"Toolbar">, { readonly disabled: boolean; readonly disabledItems: readonly Array<string>; readonly orientation: "horizontal" | "vertical";}, never, SharedKey<"Toolbar", { readonly disabled: boolean; readonly disabledItems: readonly Array<string>; readonly orientation: "horizontal" | "vertical";}, never, never, Shared<"Toolbar">>, { readonly active: string | null; readonly rtl: boolean;}, { readonly _tag: "FocusedItem"; readonly value: string;} | { readonly _tag: "PressedInputKey"; readonly caret: number | null; readonly key: string; readonly value: string;} | { readonly _tag: "FoundDirection"; readonly rtl: boolean;} | { readonly _tag: "PressedItemKey"; readonly key: string; readonly value: string;}, QueryEntries<{}, {}>>Models
One group of a toolbar.
Signature
type ToolbarGroup = typeof ToolbarGroup.TypeOne item of a toolbar.
Signature
type ToolbarItem = typeof ToolbarItem.TypeThe direction a toolbar's items run in.
Signature
type ToolbarOrientation = typeof ToolbarOrientation.TypeThe props of a Toolbar.
Signature
type ToolbarProps = typeof ToolbarProps.TypeSchemas
ToolbarGroup
unstableOne group of a toolbar: the value its group part is drawn with, and
whether it disables the items inside.
Signature
declare const ToolbarGroup: Struct<{ readonly disabled: optional<Boolean>; readonly value: String;}>ToolbarItem
unstableOne item of a toolbar: the value its part is drawn with, and its state.
Details
disabled: the item is disabled.focusableWhenDisabled: a disabled item stays focusable and in the arrow keys' order. On by default.
A link is never disabled, so leave both off a link's entry.
group: thevalueof the entry ingroupswhosegrouppart is drawn around the item, so a disabled group disables it.
Signature
declare const ToolbarItem: Struct<{ readonly disabled: optional<Boolean>; readonly focusableWhenDisabled: optional<Boolean>; readonly group: optional<String>; readonly value: String;}>ToolbarOrientation
unstableThe direction a toolbar's items run in, which picks its arrow keys.
Signature
declare const ToolbarOrientation: Literals<readonly ["horizontal", "vertical"]>ToolbarProps
unstableProps for the state of a Toolbar.
Details
items: every button, link and input, in the order they are drawn. Required.groups: every group drawn with thegrouppart. None by default.disabled: disables every button and input.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 ToolbarProps: Struct<{ readonly disabled: optional<Boolean>; readonly groups: optional<$Array<Struct<{ readonly disabled: optional<Boolean>; readonly value: String; }>>>; readonly items: $Array<Struct<{ readonly disabled: optional<Boolean>; readonly focusableWhenDisabled: optional<Boolean>; readonly group: optional<String>; readonly value: String; }>>; readonly loopFocus: optional<Boolean>; readonly orientation: optional<Literals<readonly ["horizontal", "vertical"]>>;}>