@barqjs/aria 0.0.0-stage → 0.2.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +293 -2
  3. package/package.json +347 -4
  4. package/src/breadcrumbs.tsx +296 -0
  5. package/src/button.tsx +342 -0
  6. package/src/calendar.tsx +1389 -0
  7. package/src/checkbox.tsx +570 -0
  8. package/src/collections.ts +976 -0
  9. package/src/color.ts +562 -0
  10. package/src/colorpicker.tsx +1478 -0
  11. package/src/combobox.tsx +2481 -0
  12. package/src/command.tsx +1184 -0
  13. package/src/date.ts +652 -0
  14. package/src/datefield.tsx +951 -0
  15. package/src/datepicker.tsx +556 -0
  16. package/src/dialog.tsx +890 -0
  17. package/src/disclosure.tsx +659 -0
  18. package/src/dom.ts +706 -0
  19. package/src/drawer.ts +674 -0
  20. package/src/focus.ts +1370 -0
  21. package/src/form.tsx +94 -0
  22. package/src/gridlist.tsx +563 -0
  23. package/src/i18n.ts +823 -0
  24. package/src/index.ts +1270 -0
  25. package/src/interactions/description.ts +73 -0
  26. package/src/interactions/flags.ts +35 -0
  27. package/src/interactions/focus-events.ts +218 -0
  28. package/src/interactions/focusable.ts +86 -0
  29. package/src/interactions/hover.ts +213 -0
  30. package/src/interactions/interact-outside.ts +123 -0
  31. package/src/interactions/keyboard.ts +105 -0
  32. package/src/interactions/listeners.ts +78 -0
  33. package/src/interactions/long-press.ts +160 -0
  34. package/src/interactions/modality.ts +416 -0
  35. package/src/interactions/move.ts +257 -0
  36. package/src/interactions/open-link.ts +193 -0
  37. package/src/interactions/press.ts +898 -0
  38. package/src/interactions/prevent-focus.ts +102 -0
  39. package/src/interactions/scroll-wheel.ts +43 -0
  40. package/src/interactions/text-selection.ts +77 -0
  41. package/src/interactions.ts +81 -0
  42. package/src/label.ts +128 -0
  43. package/src/link.tsx +381 -0
  44. package/src/listbox.tsx +721 -0
  45. package/src/live.ts +204 -0
  46. package/src/menu.tsx +2290 -0
  47. package/src/menubar.tsx +575 -0
  48. package/src/navigation.ts +735 -0
  49. package/src/numberfield.tsx +838 -0
  50. package/src/otp.ts +430 -0
  51. package/src/overlays.ts +1247 -0
  52. package/src/platform.ts +74 -0
  53. package/src/presence.ts +164 -0
  54. package/src/questionnaire.tsx +1712 -0
  55. package/src/radio.tsx +567 -0
  56. package/src/register-dom.ts +12 -0
  57. package/src/scroller.ts +1165 -0
  58. package/src/select.tsx +1665 -0
  59. package/src/selection.ts +1415 -0
  60. package/src/slider.tsx +1042 -0
  61. package/src/splitter.ts +1152 -0
  62. package/src/static-collection.ts +211 -0
  63. package/src/switch.tsx +186 -0
  64. package/src/table.tsx +0 -0
  65. package/src/tabs.tsx +1018 -0
  66. package/src/tag.tsx +452 -0
  67. package/src/textfield.tsx +417 -0
  68. package/src/toast.ts +658 -0
  69. package/src/toggle-input.tsx +48 -0
  70. package/src/toggle.ts +321 -0
  71. package/src/togglegroup.ts +365 -0
  72. package/src/toolbar.tsx +179 -0
  73. package/src/tooltip.tsx +657 -0
  74. package/src/utils.ts +758 -0
  75. package/src/validation.ts +447 -0
  76. package/src/virtualizer.tsx +674 -0
@@ -0,0 +1,296 @@
1
+ /**
2
+ * Breadcrumbs: where you are, and every step back to the top.
3
+ *
4
+ * A `<nav>` with a name, holding an ordered list of links. Three details are
5
+ * what separate it from a row of links:
6
+ *
7
+ * - **The current page is still a LINK, marked `aria-current="page"`.** That
8
+ * is what the authoring practices have, and the alternative is worse: an
9
+ * `<a>` with no `href` has no role at all, so it is announced as text while
10
+ * looking like something to press.
11
+ * - **The separator is not content.** A "/" between the items belongs to the
12
+ * stylesheet, or to an element marked `aria-hidden`; announced, it is read
13
+ * as "slash" between every pair.
14
+ * - **The list is ordered.** `<ol>` says the sequence matters, which is the
15
+ * whole point of a trail.
16
+ */
17
+
18
+ import {
19
+ type Child,
20
+ For,
21
+ createContext,
22
+ getContext,
23
+ getOwner,
24
+ type Incoming,
25
+ install,
26
+ } from "@barqjs/core";
27
+ import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
28
+ import type { ItemAccessors, Key, Node } from "./collections.ts";
29
+ import { buildCollection, type ListCollection } from "./collections.ts";
30
+ import { focusRing } from "./focus.ts";
31
+ import { hover } from "./interactions/hover.ts";
32
+ import type { ElementRef, PressEvent } from "./interactions/press.ts";
33
+ import { link, type LinkOptions } from "./link.tsx";
34
+ import {
35
+ access,
36
+ type DOMProps,
37
+ filterDOMProps,
38
+ fromProps,
39
+ type MaybeAccessor,
40
+ mergeProps,
41
+ type StyleProps,
42
+ styleProps,
43
+ } from "./utils.ts";
44
+
45
+ // ---------------------------------------------------------------------------
46
+ // Hooks
47
+ // ---------------------------------------------------------------------------
48
+
49
+ export interface BreadcrumbsOptions {
50
+ "aria-label"?: MaybeAccessor<string | undefined>;
51
+ "aria-labelledby"?: MaybeAccessor<string | undefined>;
52
+ }
53
+
54
+ export interface BreadcrumbsResult {
55
+ navProps: DOMProps;
56
+ }
57
+
58
+ export function breadcrumbs(options: BreadcrumbsOptions = {}): BreadcrumbsResult {
59
+ return {
60
+ navProps: mergeProps(filterDOMProps(options, { labelable: true }), {
61
+ // Named, because a page with more than one landmark of a kind needs
62
+ // telling apart, and "Breadcrumbs" is what a user of one calls it.
63
+ "aria-label": () => access(options["aria-label"]) ?? "Breadcrumbs",
64
+ }),
65
+ };
66
+ }
67
+
68
+ export interface BreadcrumbItemOptions extends Omit<LinkOptions, "elementType"> {
69
+ /** Where you are now. Still a link, and says so with `aria-current`. */
70
+ isCurrent?: MaybeAccessor<boolean | undefined>;
71
+ /** What kind of current. @default "page" */
72
+ "aria-current"?: MaybeAccessor<
73
+ "page" | "step" | "location" | "date" | "time" | boolean | undefined
74
+ >;
75
+ /** @default "a" */
76
+ elementType?: MaybeAccessor<string | undefined>;
77
+ }
78
+
79
+ export interface BreadcrumbItemResult {
80
+ itemProps: DOMProps;
81
+ isPressed: ReturnType<typeof link>["isPressed"];
82
+ }
83
+
84
+ export function breadcrumbItem(
85
+ options: BreadcrumbItemOptions,
86
+ ref?: ElementRef,
87
+ ): BreadcrumbItemResult {
88
+ const isCurrent = (): boolean => access(options.isCurrent) === true;
89
+ const elementType = (): string => access(options.elementType) ?? "a";
90
+ const isHeading = (): boolean => /^h[1-6]$/.test(elementType());
91
+
92
+ const { linkProps, isPressed } = link({ ...options, elementType }, ref);
93
+
94
+ return {
95
+ isPressed,
96
+ itemProps: mergeProps(
97
+ // A heading is not a link at all, so it takes none of the link's
98
+ // behaviour: `<h1>Current page</h1>` is a perfectly good last crumb.
99
+ isHeading() ? {} : linkProps,
100
+ {
101
+ "aria-disabled": () => access(options.isDisabled) === true || undefined,
102
+ // The current crumb stays a LINK, as the authoring practices have it:
103
+ // `aria-current="page"` is what says you are already there, and a link
104
+ // with no `href` has no role at all — it is a span that looks like one.
105
+ "aria-current": () =>
106
+ isCurrent() ? (access(options["aria-current"]) ?? "page") : undefined,
107
+ },
108
+ ),
109
+ };
110
+ }
111
+
112
+ // ---------------------------------------------------------------------------
113
+ // Components
114
+ // ---------------------------------------------------------------------------
115
+
116
+ interface BreadcrumbsContextValue {
117
+ isDisabled: () => boolean;
118
+ onAction: () => ((key: Key) => void) | undefined;
119
+ }
120
+
121
+ const BreadcrumbsContext = createContext<BreadcrumbsContextValue | null>(null);
122
+ const CrumbNodeContext = createContext<{ node: Node<unknown>; isLast: boolean } | null>(null);
123
+
124
+ export function useBreadcrumbs(): BreadcrumbsContextValue {
125
+ const value = getContext(BreadcrumbsContext);
126
+ if (value === null || value === undefined) {
127
+ throw new Error("A Breadcrumb must be rendered inside Breadcrumbs.");
128
+ }
129
+ return value;
130
+ }
131
+
132
+ /** The collection node the crumb being built is for, and whether it is last. */
133
+ export function useCrumb(): { node: Node<unknown>; isLast: boolean } {
134
+ const value = getContext(CrumbNodeContext);
135
+ if (value === null || value === undefined) {
136
+ throw new Error("A Breadcrumb must be rendered inside Breadcrumbs' item callback.");
137
+ }
138
+ return value;
139
+ }
140
+
141
+ export interface BreadcrumbsComponentProps<T> extends StyleProps, ItemAccessors<T> {
142
+ /** The trail, from the top down. The last one is where you are. */
143
+ items: Iterable<T>;
144
+ /** How one crumb renders. Return a `<Breadcrumb>`. */
145
+ children: (item: T) => Child;
146
+ isDisabled?: boolean;
147
+ "aria-label"?: string;
148
+ "aria-labelledby"?: string;
149
+ ref?: RefTarget<HTMLElement>;
150
+ onAction?: (key: Key) => void;
151
+ }
152
+
153
+ /**
154
+ * ```tsx
155
+ * <Breadcrumbs items={trail()} onAction={(key) => go(key)}>
156
+ * {(crumb) => <Breadcrumb>{crumb.name}</Breadcrumb>}
157
+ * </Breadcrumbs>
158
+ * ```
159
+ */
160
+ export function Breadcrumbs<T>(props: Incoming<BreadcrumbsComponentProps<T>>) {
161
+ const options = fromProps(props as unknown as Incoming<Record<string, unknown>>);
162
+ const { navProps } = breadcrumbs(options);
163
+
164
+ const collection = (): ListCollection<T> =>
165
+ buildCollection(props.items(), props as unknown as ItemAccessors<T>);
166
+
167
+ const owner = getOwner();
168
+ if (owner !== null) {
169
+ install(owner, BreadcrumbsContext, () => ({
170
+ isDisabled: () => props.isDisabled?.() === true,
171
+ onAction: () => props.onAction?.(),
172
+ }));
173
+ }
174
+
175
+ const elementProps = mergeProps(
176
+ navProps,
177
+ filterDOMProps(fromProps(props), { global: true }),
178
+ styleProps(props),
179
+ {
180
+ "data-testid": () => props["data-testid"]?.(),
181
+ },
182
+ );
183
+
184
+ const render = props.children;
185
+
186
+ return (
187
+ <nav {...elementProps} ref={mergeRefs(props.ref?.())}>
188
+ <ol>
189
+ <For each={() => [...collection()]}>
190
+ {(node: Node<T>) => {
191
+ const rowOwner = getOwner();
192
+ if (rowOwner !== null) {
193
+ install(rowOwner, CrumbNodeContext, () => ({
194
+ node: node as Node<unknown>,
195
+ isLast: node.nextKey === undefined,
196
+ }));
197
+ }
198
+ return render(node.value as T);
199
+ }}
200
+ </For>
201
+ </ol>
202
+ </nav>
203
+ );
204
+ }
205
+
206
+ export interface BreadcrumbComponentProps extends StyleProps {
207
+ children?: Child;
208
+ href?: string;
209
+ target?: string;
210
+ rel?: string;
211
+ isDisabled?: boolean;
212
+ /** Override which crumb is the current one. @default the last */
213
+ isCurrent?: boolean;
214
+ ref?: RefTarget<HTMLAnchorElement>;
215
+ onPress?: (event: PressEvent) => void;
216
+ }
217
+
218
+ /**
219
+ * One crumb.
220
+ *
221
+ * The last is the current page, marked `aria-current="page"`. A crumb with no
222
+ * `href` renders as a `<span>`: an anchor without one is not a link, and
223
+ * dressing it as one is a promise the markup does not keep.
224
+ */
225
+ export function Breadcrumb(props: Incoming<BreadcrumbComponentProps>) {
226
+ const domRef = makeRef<HTMLAnchorElement>();
227
+ const trail = useBreadcrumbs();
228
+ const { node, isLast } = useCrumb();
229
+
230
+ const isCurrent = (): boolean => props.isCurrent?.() ?? isLast;
231
+ const href = (): string | undefined => props.href?.() ?? (node.props?.href as string | undefined);
232
+
233
+ const isDisabled = (): boolean => props.isDisabled?.() === true || trail.isDisabled();
234
+
235
+ /**
236
+ * Whether this crumb is a link at all.
237
+ *
238
+ * A disabled one is not: `link` takes the `href` off it, and an `<a>` with
239
+ * no `href` has no role — so it would be announced as text while still
240
+ * looking like something to press.
241
+ */
242
+ const isLink = (): boolean => href() !== undefined && !isDisabled();
243
+
244
+ const { itemProps, isPressed } = breadcrumbItem(
245
+ {
246
+ isCurrent,
247
+ isDisabled,
248
+ href,
249
+ target: () => props.target?.(),
250
+ rel: () => props.rel?.(),
251
+ onPress: (event) => {
252
+ props.onPress?.()?.(event);
253
+ trail.onAction()?.(node.key);
254
+ },
255
+ },
256
+ domRef,
257
+ );
258
+
259
+ const { hoverProps, isHovered } = hover({ isDisabled });
260
+ const { focusProps, isFocusVisible } = focusRing();
261
+
262
+ const elementProps = mergeProps(
263
+ itemProps,
264
+ hoverProps,
265
+ focusProps,
266
+ filterDOMProps(fromProps(props), { global: true }),
267
+ styleProps(props),
268
+ {
269
+ "data-current": isCurrent,
270
+ "data-pressed": isPressed,
271
+ "data-hovered": isHovered,
272
+ "data-focus-visible": isFocusVisible,
273
+ "data-disabled": isDisabled,
274
+ "data-testid": () => props["data-testid"]?.(),
275
+ },
276
+ );
277
+
278
+ return (
279
+ <li>
280
+ {() =>
281
+ !isLink() ? (
282
+ <span
283
+ {...elementProps}
284
+ ref={mergeRefs(domRef.set as unknown as RefTarget<HTMLSpanElement>)}
285
+ >
286
+ {props.children}
287
+ </span>
288
+ ) : (
289
+ <a {...elementProps} ref={mergeRefs(domRef.set, props.ref?.())}>
290
+ {props.children}
291
+ </a>
292
+ )
293
+ }
294
+ </li>
295
+ );
296
+ }
package/src/button.tsx ADDED
@@ -0,0 +1,342 @@
1
+ /**
2
+ * Buttons.
3
+ *
4
+ * A `<button>` gets most of this from the platform. The reason the hook exists
5
+ * is everything that is not a `<button>`: a `<div role="button">` has no
6
+ * keyboard activation, no disabled state that assistive technology can see,
7
+ * and no press state to style. And even a real button needs the press
8
+ * handling, because `onClick` cannot report the press beginning, cannot be
9
+ * cancelled by dragging off, and arrives late on touch.
10
+ */
11
+
12
+ import { type Accessor, type Child, type Incoming } from "@barqjs/core";
13
+ import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
14
+ import { focusRing } from "./focus.ts";
15
+ import { focusable, type FocusableOptions } from "./interactions/focusable.ts";
16
+ import { hover } from "./interactions/hover.ts";
17
+ import { press, type ElementRef, type PressEvent } from "./interactions/press.ts";
18
+ import { controllable } from "./utils.ts";
19
+ import {
20
+ access,
21
+ filterDOMProps,
22
+ fromProps,
23
+ mergeProps,
24
+ styleProps,
25
+ triggerSlot,
26
+ type DOMProps,
27
+ type MaybeAccessor,
28
+ type StyleProps,
29
+ } from "./utils.ts";
30
+
31
+ export interface ButtonOptions extends FocusableOptions {
32
+ isDisabled?: MaybeAccessor<boolean | undefined>;
33
+ /**
34
+ * What the element actually is.
35
+ *
36
+ * Anything other than `button` gets `role="button"` and the keyboard
37
+ * handling the platform would otherwise not provide.
38
+ *
39
+ * @default "button"
40
+ */
41
+ elementType?: MaybeAccessor<string | undefined>;
42
+ /** @default "button" */
43
+ type?: MaybeAccessor<"button" | "submit" | "reset" | undefined>;
44
+ /** Keep focus where it is. Only safe where another control owns the keyboard. */
45
+ preventFocusOnPress?: MaybeAccessor<boolean | undefined>;
46
+ href?: MaybeAccessor<string | undefined>;
47
+ target?: MaybeAccessor<string | undefined>;
48
+ rel?: MaybeAccessor<string | undefined>;
49
+ form?: MaybeAccessor<string | undefined>;
50
+ name?: MaybeAccessor<string | undefined>;
51
+ value?: MaybeAccessor<string | undefined>;
52
+ "aria-label"?: MaybeAccessor<string | undefined>;
53
+ "aria-labelledby"?: MaybeAccessor<string | undefined>;
54
+ "aria-describedby"?: MaybeAccessor<string | undefined>;
55
+ "aria-expanded"?: MaybeAccessor<boolean | undefined>;
56
+ "aria-haspopup"?: MaybeAccessor<boolean | string | undefined>;
57
+ "aria-controls"?: MaybeAccessor<string | undefined>;
58
+ "aria-pressed"?: MaybeAccessor<boolean | "mixed" | undefined>;
59
+ "aria-current"?: MaybeAccessor<boolean | string | undefined>;
60
+ /**
61
+ * A button standing in for a form control carries these too.
62
+ *
63
+ * A select's trigger is a `<button>`, and it is the field: it has to be able
64
+ * to say it is required and that what it holds is wrong.
65
+ */
66
+ "aria-invalid"?: MaybeAccessor<boolean | undefined>;
67
+ "aria-required"?: MaybeAccessor<boolean | undefined>;
68
+ onPress?: (event: PressEvent) => void;
69
+ onPressStart?: (event: PressEvent) => void;
70
+ onPressEnd?: (event: PressEvent) => void;
71
+ onPressUp?: (event: PressEvent) => void;
72
+ onPressChange?: (isPressed: boolean) => void;
73
+ onClick?: (event: MouseEvent) => void;
74
+ }
75
+
76
+ export interface ButtonResult {
77
+ buttonProps: DOMProps;
78
+ isPressed: Accessor<boolean>;
79
+ }
80
+
81
+ /**
82
+ * ```tsx
83
+ * const domRef = ref<HTMLButtonElement>();
84
+ * const { buttonProps, isPressed } = button({ onPress: () => count.update((n) => n + 1) }, domRef);
85
+ * <button {...buttonProps} ref={domRef.set} data-pressed={isPressed}>Add</button>
86
+ * ```
87
+ */
88
+ export function button(options: ButtonOptions, ref?: ElementRef): ButtonResult {
89
+ const elementType = (): string => access(options.elementType) ?? "button";
90
+ const isDisabled = (): boolean => access(options.isDisabled) === true;
91
+
92
+ const { pressProps, isPressed } = press({ ...options, ref });
93
+ const { focusableProps } = focusable(options, ref);
94
+
95
+ const nativeProps: DOMProps = {
96
+ type: () =>
97
+ elementType() === "button" || elementType() === "input"
98
+ ? (access(options.type) ?? "button")
99
+ : undefined,
100
+ // A native button expresses "disabled" through the platform; anything else
101
+ // has to say so in ARIA, because `disabled` means nothing on a `<div>`.
102
+ disabled: () =>
103
+ elementType() === "button" || elementType() === "input" ? isDisabled() : undefined,
104
+ role: () => (elementType() === "button" ? undefined : "button"),
105
+ "aria-disabled": () =>
106
+ elementType() === "button" || elementType() === "input"
107
+ ? undefined
108
+ : isDisabled() || undefined,
109
+ // A disabled link must not navigate, and `disabled` does not exist on `<a>`.
110
+ href: () => (elementType() === "a" && !isDisabled() ? access(options.href) : undefined),
111
+ target: () => (elementType() === "a" ? access(options.target) : undefined),
112
+ rel: () => (elementType() === "a" ? access(options.rel) : undefined),
113
+ form: () => access(options.form),
114
+ name: () => access(options.name),
115
+ value: () => access(options.value),
116
+ };
117
+
118
+ return {
119
+ isPressed,
120
+ buttonProps: mergeProps(nativeProps, focusableProps, pressProps, {
121
+ "aria-label": () => access(options["aria-label"]),
122
+ "aria-labelledby": () => access(options["aria-labelledby"]),
123
+ "aria-describedby": () => access(options["aria-describedby"]),
124
+ "aria-haspopup": () => access(options["aria-haspopup"]),
125
+ "aria-expanded": () => access(options["aria-expanded"]),
126
+ "aria-controls": () => access(options["aria-controls"]),
127
+ "aria-pressed": () => access(options["aria-pressed"]),
128
+ "aria-current": () => access(options["aria-current"]),
129
+ "aria-invalid": () => access(options["aria-invalid"]) || undefined,
130
+ "aria-required": () => access(options["aria-required"]) || undefined,
131
+ }),
132
+ };
133
+ }
134
+
135
+ // ---------------------------------------------------------------------------
136
+ // Toggle button
137
+ // ---------------------------------------------------------------------------
138
+
139
+ export interface ToggleState {
140
+ isSelected: Accessor<boolean>;
141
+ setSelected(isSelected: boolean): void;
142
+ toggle(): void;
143
+ }
144
+
145
+ export interface ToggleStateOptions {
146
+ isSelected?: MaybeAccessor<boolean | undefined>;
147
+ defaultSelected?: MaybeAccessor<boolean | undefined>;
148
+ isReadOnly?: MaybeAccessor<boolean | undefined>;
149
+ onChange?: (isSelected: boolean) => void;
150
+ }
151
+
152
+ /** On or off, controlled or not. Shared by checkbox, switch and toggle button. */
153
+ export function toggleState(options: ToggleStateOptions = {}): ToggleState {
154
+ const [isSelected, setSelected] = controllable<boolean>(
155
+ () => access(options.isSelected),
156
+ () => access(options.defaultSelected) ?? false,
157
+ options.onChange,
158
+ );
159
+
160
+ const set = (value: boolean): void => {
161
+ // Read-only means the value is shown but not the user's to change.
162
+ if (access(options.isReadOnly) === true) return;
163
+ setSelected(value);
164
+ };
165
+
166
+ return {
167
+ isSelected,
168
+ setSelected: set,
169
+ toggle: () => set(!isSelected()),
170
+ };
171
+ }
172
+
173
+ export interface ToggleButtonOptions extends Omit<ButtonOptions, "aria-pressed"> {
174
+ isSelected?: MaybeAccessor<boolean | undefined>;
175
+ defaultSelected?: MaybeAccessor<boolean | undefined>;
176
+ onChange?: (isSelected: boolean) => void;
177
+ }
178
+
179
+ export interface ToggleButtonResult extends ButtonResult {
180
+ isSelected: Accessor<boolean>;
181
+ }
182
+
183
+ /**
184
+ * A button with two states, e.g. "bold" in a text editor.
185
+ *
186
+ * `aria-pressed` is what makes it a toggle rather than a button that happens
187
+ * to look different; without it a screen reader announces no state at all.
188
+ */
189
+ export function toggleButton(
190
+ options: ToggleButtonOptions,
191
+ state: ToggleState,
192
+ ref?: ElementRef,
193
+ ): ToggleButtonResult {
194
+ const { buttonProps, isPressed } = button(
195
+ {
196
+ ...options,
197
+ onPress: (event) => {
198
+ state.toggle();
199
+ options.onPress?.(event);
200
+ },
201
+ "aria-pressed": () => state.isSelected(),
202
+ },
203
+ ref,
204
+ );
205
+
206
+ return { buttonProps, isPressed, isSelected: state.isSelected };
207
+ }
208
+
209
+ // ---------------------------------------------------------------------------
210
+ // Components
211
+ // ---------------------------------------------------------------------------
212
+
213
+ export interface ButtonComponentProps extends StyleProps {
214
+ children?: Child;
215
+ isDisabled?: boolean;
216
+ type?: "button" | "submit" | "reset";
217
+ autoFocus?: boolean;
218
+ excludeFromTabOrder?: boolean;
219
+ form?: string;
220
+ name?: string;
221
+ value?: string;
222
+ "aria-label"?: string;
223
+ "aria-labelledby"?: string;
224
+ "aria-describedby"?: string;
225
+ "aria-expanded"?: boolean;
226
+ "aria-haspopup"?: boolean | string;
227
+ "aria-controls"?: string;
228
+ ref?: RefTarget<HTMLButtonElement>;
229
+ onPress?: (event: PressEvent) => void;
230
+ onPressStart?: (event: PressEvent) => void;
231
+ onPressEnd?: (event: PressEvent) => void;
232
+ onPressChange?: (isPressed: boolean) => void;
233
+ onClick?: (event: MouseEvent) => void;
234
+ }
235
+
236
+ /**
237
+ * `press` already delivers `onClick`, including the one a keyboard or a touch
238
+ * activation would have produced. Bound on the element as well, a caller's
239
+ * handler runs twice for every mouse click.
240
+ */
241
+ function withoutClick(props: DOMProps): DOMProps {
242
+ const { onClick: _, ...rest } = props;
243
+ return rest;
244
+ }
245
+
246
+ /**
247
+ * A button, with the interaction states as data attributes.
248
+ *
249
+ * `data-pressed`, `data-hovered`, `data-focused` and `data-focus-visible` are
250
+ * written as their presence, so `[data-pressed] { … }` is the selector. There
251
+ * is no class name and no style: what it looks like is the caller's.
252
+ *
253
+ * ```tsx
254
+ * <Button onPress={() => save()}>Save</Button>
255
+ * ```
256
+ */
257
+ export function Button(props: Incoming<ButtonComponentProps>) {
258
+ const domRef = makeRef<HTMLButtonElement>();
259
+ // What a tooltip, a menu or a dialog wrapped around this button wants on it,
260
+ // merged into the button's OPTIONS rather than onto its element.
261
+ //
262
+ // Onto the element it could not work, and did not: `onPress` is not a DOM
263
+ // event, so a trigger's press handler became `addEventListener("press")` and
264
+ // never fired; and `aria-haspopup` lost to the button's own accessor for the
265
+ // same key, which yields `undefined` and is still a function, so `mergeProps`
266
+ // saw a value and kept it. Through the options both arrive where `button()`
267
+ // already knows what to do with them, and `mergeProps` chains the two
268
+ // `onPress` handlers instead of one replacing the other.
269
+ const options = mergeProps(fromProps(props), triggerSlot().props) as ButtonOptions;
270
+ const slot = triggerSlot();
271
+
272
+ const { buttonProps, isPressed } = button(options, domRef);
273
+ const { hoverProps, isHovered } = hover({ isDisabled: options.isDisabled });
274
+ const { focusProps, isFocused, isFocusVisible } = focusRing();
275
+
276
+ const elementProps = mergeProps(
277
+ buttonProps,
278
+ hoverProps,
279
+ focusProps,
280
+ withoutClick(filterDOMProps(options, { global: true })),
281
+ styleProps(props),
282
+ {
283
+ "data-pressed": isPressed,
284
+ "data-hovered": isHovered,
285
+ "data-focused": isFocused,
286
+ "data-focus-visible": isFocusVisible,
287
+ "data-disabled": () => props.isDisabled?.() === true,
288
+ },
289
+ );
290
+
291
+ return (
292
+ <button {...elementProps} ref={mergeRefs(domRef.set, slot.ref, props.ref?.())}>
293
+ {props.children}
294
+ </button>
295
+ );
296
+ }
297
+
298
+ export interface ToggleButtonComponentProps extends Omit<ButtonComponentProps, "aria-pressed"> {
299
+ isSelected?: boolean;
300
+ defaultSelected?: boolean;
301
+ onChange?: (isSelected: boolean) => void;
302
+ }
303
+
304
+ /**
305
+ * ```tsx
306
+ * <ToggleButton defaultSelected onChange={(on) => bold.set(on)}>Bold</ToggleButton>
307
+ * ```
308
+ */
309
+ export function ToggleButton(props: Incoming<ToggleButtonComponentProps>) {
310
+ const domRef = makeRef<HTMLButtonElement>();
311
+ // See `Button` for why the slot goes into the options and not onto the
312
+ // element.
313
+ const options = mergeProps(fromProps(props), triggerSlot().props) as ToggleButtonOptions;
314
+ const slot = triggerSlot();
315
+
316
+ const state = toggleState(options);
317
+ const { buttonProps, isPressed } = toggleButton(options, state, domRef);
318
+ const { hoverProps, isHovered } = hover({ isDisabled: options.isDisabled });
319
+ const { focusProps, isFocused, isFocusVisible } = focusRing();
320
+
321
+ const elementProps = mergeProps(
322
+ buttonProps,
323
+ hoverProps,
324
+ focusProps,
325
+ withoutClick(filterDOMProps(options, { global: true })),
326
+ styleProps(props),
327
+ {
328
+ "data-pressed": isPressed,
329
+ "data-hovered": isHovered,
330
+ "data-focused": isFocused,
331
+ "data-focus-visible": isFocusVisible,
332
+ "data-selected": state.isSelected,
333
+ "data-disabled": () => props.isDisabled?.() === true,
334
+ },
335
+ );
336
+
337
+ return (
338
+ <button {...elementProps} ref={mergeRefs(domRef.set, slot.ref, props.ref?.())}>
339
+ {props.children}
340
+ </button>
341
+ );
342
+ }