@barqjs/aria 0.0.0-stage → 0.2.0-next.1
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.
- package/LICENSE +21 -0
- package/README.md +304 -2
- package/package.json +437 -4
- package/src/autocomplete.tsx +581 -0
- package/src/breadcrumbs.tsx +297 -0
- package/src/button.tsx +468 -0
- package/src/calendar.tsx +2316 -0
- package/src/checkbox.tsx +732 -0
- package/src/collections.ts +1059 -0
- package/src/color.ts +2119 -0
- package/src/colorpicker.tsx +2023 -0
- package/src/colorswatchpicker.tsx +240 -0
- package/src/combobox.tsx +3003 -0
- package/src/command.tsx +1203 -0
- package/src/date.ts +674 -0
- package/src/datefield.tsx +2066 -0
- package/src/datepicker.tsx +907 -0
- package/src/dialog.tsx +1077 -0
- package/src/disclosure.tsx +711 -0
- package/src/dnd/clipboard.ts +125 -0
- package/src/dnd/collection.ts +1265 -0
- package/src/dnd/data.ts +409 -0
- package/src/dnd/drag-drop.ts +801 -0
- package/src/dnd/index.ts +133 -0
- package/src/dnd/manager.ts +639 -0
- package/src/dnd/strings.ts +1279 -0
- package/src/dnd/targets.ts +726 -0
- package/src/dnd/types.ts +212 -0
- package/src/dom.ts +703 -0
- package/src/drag-and-drop.tsx +669 -0
- package/src/drawer.ts +674 -0
- package/src/dropzone.tsx +208 -0
- package/src/file-trigger.tsx +103 -0
- package/src/focus.ts +1370 -0
- package/src/form.tsx +94 -0
- package/src/gridlist.tsx +1354 -0
- package/src/group.tsx +146 -0
- package/src/i18n.ts +823 -0
- package/src/index.ts +1672 -0
- package/src/interactions/description.ts +73 -0
- package/src/interactions/flags.ts +35 -0
- package/src/interactions/focus-events.ts +226 -0
- package/src/interactions/focusable.ts +86 -0
- package/src/interactions/hover.ts +213 -0
- package/src/interactions/interact-outside.ts +123 -0
- package/src/interactions/keyboard.ts +298 -0
- package/src/interactions/listeners.ts +78 -0
- package/src/interactions/long-press.ts +160 -0
- package/src/interactions/modality.ts +428 -0
- package/src/interactions/move.ts +257 -0
- package/src/interactions/open-link.ts +226 -0
- package/src/interactions/press.ts +915 -0
- package/src/interactions/prevent-focus.ts +102 -0
- package/src/interactions/scroll-wheel.ts +43 -0
- package/src/interactions/text-selection.ts +77 -0
- package/src/interactions.ts +81 -0
- package/src/label.ts +140 -0
- package/src/link.tsx +477 -0
- package/src/listbox.tsx +1247 -0
- package/src/live.ts +208 -0
- package/src/menu.tsx +2383 -0
- package/src/menubar.tsx +579 -0
- package/src/navigation-tree.tsx +357 -0
- package/src/navigation.ts +735 -0
- package/src/numberfield.tsx +942 -0
- package/src/otp.ts +430 -0
- package/src/overlay-arrow.tsx +157 -0
- package/src/overlays.ts +1549 -0
- package/src/platform.ts +75 -0
- package/src/presence.ts +164 -0
- package/src/preview-trigger.tsx +582 -0
- package/src/progress-bar-context.ts +15 -0
- package/src/questionnaire.tsx +1717 -0
- package/src/radio.tsx +641 -0
- package/src/register-dom.ts +12 -0
- package/src/scroller.ts +1165 -0
- package/src/select.tsx +1745 -0
- package/src/selection-indicator.tsx +74 -0
- package/src/selection.ts +1472 -0
- package/src/shared-element-transition.tsx +316 -0
- package/src/sheet.tsx +1461 -0
- package/src/slider.tsx +1137 -0
- package/src/splitter.ts +1152 -0
- package/src/static-collection.ts +211 -0
- package/src/switch.tsx +212 -0
- package/src/table.tsx +2218 -0
- package/src/tabs.tsx +1127 -0
- package/src/tag.tsx +637 -0
- package/src/textarea.tsx +186 -0
- package/src/textfield.tsx +483 -0
- package/src/toast-components.tsx +589 -0
- package/src/toast.ts +658 -0
- package/src/toggle-button-group.tsx +175 -0
- package/src/toggle-input.tsx +86 -0
- package/src/toggle.ts +384 -0
- package/src/togglegroup.ts +365 -0
- package/src/tokenfield.tsx +1673 -0
- package/src/toolbar.tsx +184 -0
- package/src/tooltip.tsx +674 -0
- package/src/tree.tsx +2075 -0
- package/src/utils.ts +787 -0
- package/src/validation.ts +504 -0
- package/src/virtualizer.tsx +837 -0
package/src/button.tsx
ADDED
|
@@ -0,0 +1,468 @@
|
|
|
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 { cells, createEffect, provide, type Accessor, type Child } 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, type HoverEvent } from "./interactions/hover.ts";
|
|
17
|
+
import { press, type ElementRef, type PressEvent } from "./interactions/press.ts";
|
|
18
|
+
import { ProgressBarContext } from "./progress-bar-context.ts";
|
|
19
|
+
import { announce } from "./live.ts";
|
|
20
|
+
import { SelectionIndicatorProvider } from "./selection-indicator.tsx";
|
|
21
|
+
import { controllable } from "./utils.ts";
|
|
22
|
+
import {
|
|
23
|
+
access,
|
|
24
|
+
filterDOMProps,
|
|
25
|
+
fromProps,
|
|
26
|
+
id,
|
|
27
|
+
mergeProps,
|
|
28
|
+
styleProps,
|
|
29
|
+
triggerSlot,
|
|
30
|
+
type DOMProps,
|
|
31
|
+
type MaybeAccessor,
|
|
32
|
+
type StyleProps,
|
|
33
|
+
} from "./utils.ts";
|
|
34
|
+
|
|
35
|
+
export interface ButtonOptions extends FocusableOptions {
|
|
36
|
+
id?: MaybeAccessor<string | undefined>;
|
|
37
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
38
|
+
/**
|
|
39
|
+
* What the element actually is.
|
|
40
|
+
*
|
|
41
|
+
* Anything other than `button` gets `role="button"` and the keyboard
|
|
42
|
+
* handling the platform would otherwise not provide.
|
|
43
|
+
*
|
|
44
|
+
* @default "button"
|
|
45
|
+
*/
|
|
46
|
+
elementType?: MaybeAccessor<string | undefined>;
|
|
47
|
+
/** @default "button" */
|
|
48
|
+
type?: MaybeAccessor<"button" | "submit" | "reset" | undefined>;
|
|
49
|
+
/** Keep focus where it is. Only safe where another control owns the keyboard. */
|
|
50
|
+
preventFocusOnPress?: MaybeAccessor<boolean | undefined>;
|
|
51
|
+
href?: MaybeAccessor<string | undefined>;
|
|
52
|
+
target?: MaybeAccessor<string | undefined>;
|
|
53
|
+
rel?: MaybeAccessor<string | undefined>;
|
|
54
|
+
form?: MaybeAccessor<string | undefined>;
|
|
55
|
+
/** Where a submit button sends the form, in place of the form's own `action`. */
|
|
56
|
+
formAction?: MaybeAccessor<string | undefined>;
|
|
57
|
+
formEncType?: MaybeAccessor<string | undefined>;
|
|
58
|
+
formMethod?: MaybeAccessor<string | undefined>;
|
|
59
|
+
formNoValidate?: MaybeAccessor<boolean | undefined>;
|
|
60
|
+
formTarget?: MaybeAccessor<string | undefined>;
|
|
61
|
+
name?: MaybeAccessor<string | undefined>;
|
|
62
|
+
value?: MaybeAccessor<string | undefined>;
|
|
63
|
+
/**
|
|
64
|
+
* Keep a `tabindex` of -1 while disabled, so a control that moves focus to
|
|
65
|
+
* the button itself (a number field's stepper) can still do so.
|
|
66
|
+
*/
|
|
67
|
+
allowFocusWhenDisabled?: MaybeAccessor<boolean | undefined>;
|
|
68
|
+
"aria-disabled"?: MaybeAccessor<boolean | "true" | "false" | undefined>;
|
|
69
|
+
"aria-label"?: MaybeAccessor<string | undefined>;
|
|
70
|
+
"aria-labelledby"?: MaybeAccessor<string | undefined>;
|
|
71
|
+
"aria-describedby"?: MaybeAccessor<string | undefined>;
|
|
72
|
+
"aria-expanded"?: MaybeAccessor<boolean | undefined>;
|
|
73
|
+
"aria-haspopup"?: MaybeAccessor<boolean | string | undefined>;
|
|
74
|
+
"aria-controls"?: MaybeAccessor<string | undefined>;
|
|
75
|
+
"aria-pressed"?: MaybeAccessor<boolean | "mixed" | undefined>;
|
|
76
|
+
"aria-current"?: MaybeAccessor<boolean | string | undefined>;
|
|
77
|
+
/**
|
|
78
|
+
* A button standing in for a form control carries these too.
|
|
79
|
+
*
|
|
80
|
+
* A select's trigger is a `<button>`, and it is the field: it has to be able
|
|
81
|
+
* to say it is required and that what it holds is wrong.
|
|
82
|
+
*/
|
|
83
|
+
"aria-invalid"?: MaybeAccessor<boolean | undefined>;
|
|
84
|
+
"aria-required"?: MaybeAccessor<boolean | undefined>;
|
|
85
|
+
onPress?: (event: PressEvent) => void;
|
|
86
|
+
onPressStart?: (event: PressEvent) => void;
|
|
87
|
+
onPressEnd?: (event: PressEvent) => void;
|
|
88
|
+
onPressUp?: (event: PressEvent) => void;
|
|
89
|
+
onPressChange?: (isPressed: boolean) => void;
|
|
90
|
+
onClick?: (event: MouseEvent) => void;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface ButtonResult {
|
|
94
|
+
buttonProps: DOMProps;
|
|
95
|
+
isPressed: Accessor<boolean>;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* ```tsx
|
|
100
|
+
* const domRef = ref<HTMLButtonElement>();
|
|
101
|
+
* const { buttonProps, isPressed } = button({ onPress: () => count.update((n) => n + 1) }, domRef);
|
|
102
|
+
* <button {...buttonProps} ref={domRef.set} data-pressed={isPressed}>Add</button>
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export function button(options: ButtonOptions, ref?: ElementRef): ButtonResult {
|
|
106
|
+
const elementType = (): string => access(options.elementType) ?? "button";
|
|
107
|
+
const isDisabled = (): boolean => access(options.isDisabled) === true;
|
|
108
|
+
|
|
109
|
+
const { pressProps, isPressed } = press({ ...options, ref });
|
|
110
|
+
const { focusableProps: focusableBase } = focusable(options, ref);
|
|
111
|
+
const focusableProps = mergeProps(focusableBase, {
|
|
112
|
+
tabIndex: () =>
|
|
113
|
+
access(options.allowFocusWhenDisabled) === true && isDisabled()
|
|
114
|
+
? -1
|
|
115
|
+
: access(focusableBase.tabIndex as MaybeAccessor<number | undefined>),
|
|
116
|
+
});
|
|
117
|
+
const isFormControl = (): boolean => elementType() === "button" || elementType() === "input";
|
|
118
|
+
|
|
119
|
+
const nativeProps: DOMProps = {
|
|
120
|
+
type: () =>
|
|
121
|
+
elementType() === "button" || elementType() === "input"
|
|
122
|
+
? (access(options.type) ?? "button")
|
|
123
|
+
: undefined,
|
|
124
|
+
// A native button expresses "disabled" through the platform; anything else
|
|
125
|
+
// has to say so in ARIA, because `disabled` means nothing on a `<div>`.
|
|
126
|
+
disabled: () =>
|
|
127
|
+
elementType() === "button" || elementType() === "input" ? isDisabled() : undefined,
|
|
128
|
+
role: () => (elementType() === "button" ? undefined : "button"),
|
|
129
|
+
"aria-disabled": () =>
|
|
130
|
+
access(options["aria-disabled"]) ??
|
|
131
|
+
(elementType() === "button" || elementType() === "input"
|
|
132
|
+
? undefined
|
|
133
|
+
: isDisabled() || undefined),
|
|
134
|
+
// A disabled link must not navigate, and `disabled` does not exist on `<a>`.
|
|
135
|
+
href: () => (elementType() === "a" && !isDisabled() ? access(options.href) : undefined),
|
|
136
|
+
target: () => (elementType() === "a" ? access(options.target) : undefined),
|
|
137
|
+
rel: () => (elementType() === "a" ? access(options.rel) : undefined),
|
|
138
|
+
form: () => access(options.form),
|
|
139
|
+
formAction: () => (isFormControl() ? access(options.formAction) : undefined),
|
|
140
|
+
formEncType: () => (isFormControl() ? access(options.formEncType) : undefined),
|
|
141
|
+
formMethod: () => (isFormControl() ? access(options.formMethod) : undefined),
|
|
142
|
+
formNoValidate: () => (isFormControl() ? access(options.formNoValidate) : undefined),
|
|
143
|
+
formTarget: () => (isFormControl() ? access(options.formTarget) : undefined),
|
|
144
|
+
name: () => access(options.name),
|
|
145
|
+
value: () => access(options.value),
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
isPressed,
|
|
150
|
+
buttonProps: mergeProps(nativeProps, focusableProps, pressProps, {
|
|
151
|
+
"aria-label": () => access(options["aria-label"]),
|
|
152
|
+
"aria-labelledby": () => access(options["aria-labelledby"]),
|
|
153
|
+
"aria-describedby": () => access(options["aria-describedby"]),
|
|
154
|
+
"aria-haspopup": () => access(options["aria-haspopup"]),
|
|
155
|
+
"aria-expanded": () => access(options["aria-expanded"]),
|
|
156
|
+
"aria-controls": () => access(options["aria-controls"]),
|
|
157
|
+
"aria-pressed": () => access(options["aria-pressed"]),
|
|
158
|
+
"aria-current": () => access(options["aria-current"]),
|
|
159
|
+
"aria-invalid": () => access(options["aria-invalid"]) || undefined,
|
|
160
|
+
"aria-required": () => access(options["aria-required"]) || undefined,
|
|
161
|
+
}),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// ---------------------------------------------------------------------------
|
|
166
|
+
// Toggle button
|
|
167
|
+
// ---------------------------------------------------------------------------
|
|
168
|
+
|
|
169
|
+
export interface ToggleState {
|
|
170
|
+
isSelected: Accessor<boolean>;
|
|
171
|
+
setSelected(isSelected: boolean): void;
|
|
172
|
+
toggle(): void;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export interface ToggleStateOptions {
|
|
176
|
+
isSelected?: MaybeAccessor<boolean | undefined>;
|
|
177
|
+
defaultSelected?: MaybeAccessor<boolean | undefined>;
|
|
178
|
+
isReadOnly?: MaybeAccessor<boolean | undefined>;
|
|
179
|
+
onChange?: (isSelected: boolean) => void;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** On or off, controlled or not. Shared by checkbox, switch and toggle button. */
|
|
183
|
+
export function toggleState(options: ToggleStateOptions = {}): ToggleState {
|
|
184
|
+
const [isSelected, setSelected] = controllable<boolean>(
|
|
185
|
+
() => access(options.isSelected),
|
|
186
|
+
() => access(options.defaultSelected) ?? false,
|
|
187
|
+
options.onChange,
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
const set = (value: boolean): void => {
|
|
191
|
+
// Read-only means the value is shown but not the user's to change.
|
|
192
|
+
if (access(options.isReadOnly) === true) return;
|
|
193
|
+
setSelected(value);
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
isSelected,
|
|
198
|
+
setSelected: set,
|
|
199
|
+
toggle: () => set(!isSelected()),
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export interface ToggleButtonOptions extends Omit<ButtonOptions, "aria-pressed"> {
|
|
204
|
+
isSelected?: MaybeAccessor<boolean | undefined>;
|
|
205
|
+
defaultSelected?: MaybeAccessor<boolean | undefined>;
|
|
206
|
+
onChange?: (isSelected: boolean) => void;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export interface ToggleButtonResult extends ButtonResult {
|
|
210
|
+
isSelected: Accessor<boolean>;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* A button with two states, e.g. "bold" in a text editor.
|
|
215
|
+
*
|
|
216
|
+
* `aria-pressed` is what makes it a toggle rather than a button that happens
|
|
217
|
+
* to look different; without it a screen reader announces no state at all.
|
|
218
|
+
*/
|
|
219
|
+
export function toggleButton(
|
|
220
|
+
options: ToggleButtonOptions,
|
|
221
|
+
state: ToggleState,
|
|
222
|
+
ref?: ElementRef,
|
|
223
|
+
): ToggleButtonResult {
|
|
224
|
+
const { buttonProps, isPressed } = button(
|
|
225
|
+
{
|
|
226
|
+
...options,
|
|
227
|
+
onPress: (event) => {
|
|
228
|
+
state.toggle();
|
|
229
|
+
options.onPress?.(event);
|
|
230
|
+
},
|
|
231
|
+
"aria-pressed": () => state.isSelected(),
|
|
232
|
+
},
|
|
233
|
+
ref,
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
return { buttonProps, isPressed, isSelected: state.isSelected };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// ---------------------------------------------------------------------------
|
|
240
|
+
// Components
|
|
241
|
+
// ---------------------------------------------------------------------------
|
|
242
|
+
|
|
243
|
+
export interface ButtonComponentProps extends StyleProps {
|
|
244
|
+
children?: Child;
|
|
245
|
+
isDisabled?: boolean;
|
|
246
|
+
/**
|
|
247
|
+
* Waiting on the action the button started. Presses and hover are ignored,
|
|
248
|
+
* focus stays, a submit button stops submitting, and a focused button
|
|
249
|
+
* announces the change. A `<ProgressBar>` inside joins the button's name.
|
|
250
|
+
*/
|
|
251
|
+
isPending?: boolean;
|
|
252
|
+
type?: "button" | "submit" | "reset";
|
|
253
|
+
autoFocus?: boolean;
|
|
254
|
+
excludeFromTabOrder?: boolean;
|
|
255
|
+
id?: string;
|
|
256
|
+
form?: string;
|
|
257
|
+
formAction?: string;
|
|
258
|
+
formEncType?: string;
|
|
259
|
+
formMethod?: string;
|
|
260
|
+
formNoValidate?: boolean;
|
|
261
|
+
formTarget?: string;
|
|
262
|
+
name?: string;
|
|
263
|
+
value?: string;
|
|
264
|
+
"aria-disabled"?: boolean | "true" | "false";
|
|
265
|
+
"aria-label"?: string;
|
|
266
|
+
"aria-labelledby"?: string;
|
|
267
|
+
"aria-describedby"?: string;
|
|
268
|
+
"aria-expanded"?: boolean;
|
|
269
|
+
"aria-haspopup"?: boolean | string;
|
|
270
|
+
"aria-controls"?: string;
|
|
271
|
+
ref?: RefTarget<HTMLButtonElement>;
|
|
272
|
+
onPress?: (event: PressEvent) => void;
|
|
273
|
+
onPressStart?: (event: PressEvent) => void;
|
|
274
|
+
onPressEnd?: (event: PressEvent) => void;
|
|
275
|
+
onPressUp?: (event: PressEvent) => void;
|
|
276
|
+
onPressChange?: (isPressed: boolean) => void;
|
|
277
|
+
onHoverStart?: (event: HoverEvent) => void;
|
|
278
|
+
onHoverEnd?: (event: HoverEvent) => void;
|
|
279
|
+
onHoverChange?: (isHovered: boolean) => void;
|
|
280
|
+
onFocus?: (event: FocusEvent) => void;
|
|
281
|
+
onBlur?: (event: FocusEvent) => void;
|
|
282
|
+
onClick?: (event: MouseEvent) => void;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* `press` already delivers `onClick`, including the one a keyboard or a touch
|
|
287
|
+
* activation would have produced, and `focusable` delivers `onFocus` and
|
|
288
|
+
* `onBlur`. Bound on the element as well, a caller's handler runs twice.
|
|
289
|
+
*/
|
|
290
|
+
function withoutClick(props: DOMProps): DOMProps {
|
|
291
|
+
const { onClick: _click, onFocus: _focus, onBlur: _blur, ...rest } = props;
|
|
292
|
+
return rest;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** What a pending button still answers: focus, and what a tooltip listens to. */
|
|
296
|
+
const LIVE_WHILE_PENDING =
|
|
297
|
+
/Focus|Blur|Hover|Pointer(Enter|Leave|Over|Out)|Mouse(Enter|Leave|Over|Out)/;
|
|
298
|
+
|
|
299
|
+
function inertWhilePending(props: DOMProps, isPending: () => boolean): DOMProps {
|
|
300
|
+
const result: DOMProps = {};
|
|
301
|
+
for (const key in props) {
|
|
302
|
+
const value = props[key];
|
|
303
|
+
if (key.startsWith("on") && typeof value === "function" && !LIVE_WHILE_PENDING.test(key)) {
|
|
304
|
+
const handler = value as (event: Event) => void;
|
|
305
|
+
result[key] = (event: Event): void => {
|
|
306
|
+
if (!isPending()) handler(event);
|
|
307
|
+
};
|
|
308
|
+
} else {
|
|
309
|
+
result[key] = value;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return result;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* A button, with the interaction states as data attributes.
|
|
317
|
+
*
|
|
318
|
+
* `data-pressed`, `data-hovered`, `data-focused` and `data-focus-visible` are
|
|
319
|
+
* written as their presence, so `[data-pressed] { … }` is the selector. There
|
|
320
|
+
* is no class name and no style: what it looks like is the caller's.
|
|
321
|
+
*
|
|
322
|
+
* ```tsx
|
|
323
|
+
* <Button onPress={() => save()}>Save</Button>
|
|
324
|
+
* ```
|
|
325
|
+
*/
|
|
326
|
+
export function Button(props: ButtonComponentProps) {
|
|
327
|
+
const domRef = makeRef<HTMLButtonElement>();
|
|
328
|
+
// What a tooltip, a menu or a dialog wrapped around this button wants on it,
|
|
329
|
+
// merged into the button's OPTIONS rather than onto its element.
|
|
330
|
+
//
|
|
331
|
+
// Onto the element it could not work, and did not: `onPress` is not a DOM
|
|
332
|
+
// event, so a trigger's press handler became `addEventListener("press")` and
|
|
333
|
+
// never fired; and `aria-haspopup` lost to the button's own accessor for the
|
|
334
|
+
// same key, which yields `undefined` and is still a function, so `mergeProps`
|
|
335
|
+
// saw a value and kept it. Through the options both arrive where `button()`
|
|
336
|
+
// already knows what to do with them, and `mergeProps` chains the two
|
|
337
|
+
// `onPress` handlers instead of one replacing the other.
|
|
338
|
+
const options = mergeProps(fromProps(cells(props)), triggerSlot().props) as ButtonOptions;
|
|
339
|
+
const slot = triggerSlot();
|
|
340
|
+
|
|
341
|
+
const isPending = (): boolean => props.isPending === true;
|
|
342
|
+
|
|
343
|
+
const { buttonProps, isPressed } = button(options, domRef);
|
|
344
|
+
const { hoverProps, isHovered } = hover({
|
|
345
|
+
isDisabled: () => access(options.isDisabled) === true || isPending(),
|
|
346
|
+
onHoverStart: (event) => props.onHoverStart?.(event),
|
|
347
|
+
onHoverEnd: (event) => props.onHoverEnd?.(event),
|
|
348
|
+
onHoverChange: (hovered) => props.onHoverChange?.(hovered),
|
|
349
|
+
});
|
|
350
|
+
const { focusProps, isFocused, isFocusVisible } = focusRing();
|
|
351
|
+
|
|
352
|
+
const buttonId = id(options.id);
|
|
353
|
+
const progressId = id();
|
|
354
|
+
// `aria-labelledby` outranks `aria-label` in the name computation, so a
|
|
355
|
+
// labelled button that wants its progress bar read has to name itself.
|
|
356
|
+
const labelledBy = (): string | undefined => {
|
|
357
|
+
const given = access(options["aria-labelledby"]);
|
|
358
|
+
if (!isPending()) return given;
|
|
359
|
+
if (given) return `${given} ${progressId()}`;
|
|
360
|
+
if (access(options["aria-label"])) {
|
|
361
|
+
return `${buttonId()} ${progressId()}`;
|
|
362
|
+
}
|
|
363
|
+
return given;
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
let wasPending = isPending();
|
|
367
|
+
createEffect(
|
|
368
|
+
() => ({ pending: isPending(), focused: isFocused(), by: labelledBy() || buttonId() }),
|
|
369
|
+
({ pending, focused, by }) => {
|
|
370
|
+
if (focused && pending !== wasPending) announce({ "aria-labelledby": by }, "assertive");
|
|
371
|
+
wasPending = pending;
|
|
372
|
+
},
|
|
373
|
+
);
|
|
374
|
+
|
|
375
|
+
const elementProps = mergeProps(
|
|
376
|
+
inertWhilePending(buttonProps, isPending),
|
|
377
|
+
hoverProps,
|
|
378
|
+
focusProps,
|
|
379
|
+
withoutClick(filterDOMProps(options, { global: true })),
|
|
380
|
+
styleProps(cells(props)),
|
|
381
|
+
{
|
|
382
|
+
id: buttonId,
|
|
383
|
+
// A pending submit button must not submit, implicitly from a text field
|
|
384
|
+
// on Enter included.
|
|
385
|
+
type: () => {
|
|
386
|
+
const type = access(buttonProps.type as MaybeAccessor<string | undefined>);
|
|
387
|
+
return type === "submit" && isPending() ? "button" : type;
|
|
388
|
+
},
|
|
389
|
+
"aria-labelledby": labelledBy,
|
|
390
|
+
"aria-disabled": () =>
|
|
391
|
+
isPending()
|
|
392
|
+
? "true"
|
|
393
|
+
: access(buttonProps["aria-disabled"] as MaybeAccessor<string | boolean | undefined>),
|
|
394
|
+
"data-pressed": () => isPressed() && !isPending(),
|
|
395
|
+
"data-hovered": isHovered,
|
|
396
|
+
"data-focused": isFocused,
|
|
397
|
+
"data-focus-visible": isFocusVisible,
|
|
398
|
+
"data-disabled": () => props.isDisabled === true,
|
|
399
|
+
"data-pending": isPending,
|
|
400
|
+
},
|
|
401
|
+
);
|
|
402
|
+
|
|
403
|
+
return (
|
|
404
|
+
<button {...elementProps} ref={mergeRefs(domRef.set, slot.ref, props.ref)}>
|
|
405
|
+
{provide(
|
|
406
|
+
ProgressBarContext,
|
|
407
|
+
() => ({ id: progressId }),
|
|
408
|
+
() => props.children as unknown,
|
|
409
|
+
)}
|
|
410
|
+
</button>
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
export interface ToggleButtonComponentProps extends Omit<
|
|
415
|
+
ButtonComponentProps,
|
|
416
|
+
"aria-pressed" | "isPending"
|
|
417
|
+
> {
|
|
418
|
+
isSelected?: boolean;
|
|
419
|
+
defaultSelected?: boolean;
|
|
420
|
+
onChange?: (isSelected: boolean) => void;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* ```tsx
|
|
425
|
+
* <ToggleButton defaultSelected onChange={(on) => bold.set(on)}>Bold</ToggleButton>
|
|
426
|
+
* ```
|
|
427
|
+
*/
|
|
428
|
+
export function ToggleButton(props: ToggleButtonComponentProps) {
|
|
429
|
+
const domRef = makeRef<HTMLButtonElement>();
|
|
430
|
+
// See `Button` for why the slot goes into the options and not onto the
|
|
431
|
+
// element.
|
|
432
|
+
const options = mergeProps(fromProps(cells(props)), triggerSlot().props) as ToggleButtonOptions;
|
|
433
|
+
const slot = triggerSlot();
|
|
434
|
+
|
|
435
|
+
const state = toggleState(options);
|
|
436
|
+
const { buttonProps, isPressed } = toggleButton(options, state, domRef);
|
|
437
|
+
const { hoverProps, isHovered } = hover({
|
|
438
|
+
isDisabled: options.isDisabled,
|
|
439
|
+
onHoverStart: (event) => props.onHoverStart?.(event),
|
|
440
|
+
onHoverEnd: (event) => props.onHoverEnd?.(event),
|
|
441
|
+
onHoverChange: (hovered) => props.onHoverChange?.(hovered),
|
|
442
|
+
});
|
|
443
|
+
const { focusProps, isFocused, isFocusVisible } = focusRing();
|
|
444
|
+
|
|
445
|
+
const elementProps = mergeProps(
|
|
446
|
+
buttonProps,
|
|
447
|
+
hoverProps,
|
|
448
|
+
focusProps,
|
|
449
|
+
withoutClick(filterDOMProps(options, { global: true })),
|
|
450
|
+
styleProps(cells(props)),
|
|
451
|
+
{
|
|
452
|
+
"data-pressed": isPressed,
|
|
453
|
+
"data-hovered": isHovered,
|
|
454
|
+
"data-focused": isFocused,
|
|
455
|
+
"data-focus-visible": isFocusVisible,
|
|
456
|
+
"data-selected": state.isSelected,
|
|
457
|
+
"data-disabled": () => props.isDisabled === true,
|
|
458
|
+
},
|
|
459
|
+
);
|
|
460
|
+
|
|
461
|
+
return (
|
|
462
|
+
<button {...elementProps} ref={mergeRefs(domRef.set, slot.ref, props.ref)}>
|
|
463
|
+
<SelectionIndicatorProvider isSelected={state.isSelected()}>
|
|
464
|
+
{props.children}
|
|
465
|
+
</SelectionIndicatorProvider>
|
|
466
|
+
</button>
|
|
467
|
+
);
|
|
468
|
+
}
|