@barqjs/aria 0.2.0-next.0 → 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.
Files changed (78) hide show
  1. package/README.md +24 -13
  2. package/package.json +127 -37
  3. package/src/autocomplete.tsx +581 -0
  4. package/src/breadcrumbs.tsx +21 -20
  5. package/src/button.tsx +151 -25
  6. package/src/calendar.tsx +1019 -92
  7. package/src/checkbox.tsx +214 -52
  8. package/src/collections.ts +88 -5
  9. package/src/color.ts +1580 -23
  10. package/src/colorpicker.tsx +698 -153
  11. package/src/colorswatchpicker.tsx +240 -0
  12. package/src/combobox.tsx +635 -113
  13. package/src/command.tsx +78 -59
  14. package/src/date.ts +31 -9
  15. package/src/datefield.tsx +1451 -336
  16. package/src/datepicker.tsx +465 -114
  17. package/src/dialog.tsx +302 -115
  18. package/src/disclosure.tsx +81 -29
  19. package/src/dnd/clipboard.ts +125 -0
  20. package/src/dnd/collection.ts +1265 -0
  21. package/src/dnd/data.ts +409 -0
  22. package/src/dnd/drag-drop.ts +801 -0
  23. package/src/dnd/index.ts +133 -0
  24. package/src/dnd/manager.ts +639 -0
  25. package/src/dnd/strings.ts +1279 -0
  26. package/src/dnd/targets.ts +726 -0
  27. package/src/dnd/types.ts +212 -0
  28. package/src/dom.ts +3 -6
  29. package/src/drag-and-drop.tsx +669 -0
  30. package/src/dropzone.tsx +208 -0
  31. package/src/file-trigger.tsx +103 -0
  32. package/src/form.tsx +18 -18
  33. package/src/gridlist.tsx +912 -121
  34. package/src/group.tsx +146 -0
  35. package/src/index.ts +402 -0
  36. package/src/interactions/focus-events.ts +13 -5
  37. package/src/interactions/keyboard.ts +195 -2
  38. package/src/interactions/modality.ts +18 -6
  39. package/src/interactions/open-link.ts +46 -13
  40. package/src/interactions/press.ts +35 -18
  41. package/src/label.ts +18 -6
  42. package/src/link.tsx +146 -50
  43. package/src/listbox.tsx +615 -89
  44. package/src/live.ts +4 -0
  45. package/src/menu.tsx +182 -89
  46. package/src/menubar.tsx +21 -17
  47. package/src/navigation-tree.tsx +357 -0
  48. package/src/numberfield.tsx +164 -60
  49. package/src/overlay-arrow.tsx +157 -0
  50. package/src/overlays.ts +364 -62
  51. package/src/platform.ts +2 -1
  52. package/src/preview-trigger.tsx +582 -0
  53. package/src/progress-bar-context.ts +15 -0
  54. package/src/questionnaire.tsx +109 -104
  55. package/src/radio.tsx +101 -27
  56. package/src/select.tsx +225 -145
  57. package/src/selection-indicator.tsx +74 -0
  58. package/src/selection.ts +73 -16
  59. package/src/shared-element-transition.tsx +316 -0
  60. package/src/sheet.tsx +1461 -0
  61. package/src/slider.tsx +139 -44
  62. package/src/switch.tsx +47 -21
  63. package/src/table.tsx +0 -0
  64. package/src/tabs.tsx +198 -89
  65. package/src/tag.tsx +217 -32
  66. package/src/textarea.tsx +186 -0
  67. package/src/textfield.tsx +103 -37
  68. package/src/toast-components.tsx +589 -0
  69. package/src/toggle-button-group.tsx +175 -0
  70. package/src/toggle-input.tsx +38 -0
  71. package/src/toggle.ts +68 -5
  72. package/src/tokenfield.tsx +1673 -0
  73. package/src/toolbar.tsx +15 -10
  74. package/src/tooltip.tsx +46 -29
  75. package/src/tree.tsx +2075 -0
  76. package/src/utils.ts +30 -1
  77. package/src/validation.ts +79 -22
  78. package/src/virtualizer.tsx +186 -23
@@ -0,0 +1,175 @@
1
+ /**
2
+ * A toggle button group: the component over `togglegroup.ts`.
3
+ *
4
+ * The state and the keyboard are that module's, and so is the one decision
5
+ * that sets this apart from React Aria's component: a `single` group stays a
6
+ * `group` of `aria-pressed` buttons rather than becoming a `radiogroup` of
7
+ * `radio`s, because pressing the pressed item releases it and a radio promises
8
+ * a choice that cannot be undone. `disallowEmptySelection` is the way to make
9
+ * one that cannot be emptied.
10
+ *
11
+ * The items sit in a {@link SharedElementTransition} and offer their pressed
12
+ * state to a {@link SelectionIndicator}, so a pill drawn behind the pressed
13
+ * item slides to the next one.
14
+ */
15
+
16
+ import { cells, type Child, createContext, getContext, omit, provide } from "@barqjs/core";
17
+ import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
18
+ import { ToggleButton } from "./button.tsx";
19
+ import type { PressEvent } from "./interactions/press.ts";
20
+ import type { Orientation } from "./selection.ts";
21
+ import { SharedElementTransition } from "./shared-element-transition.tsx";
22
+ import {
23
+ toggleGroup,
24
+ toggleGroupItem,
25
+ toggleGroupState,
26
+ type ToggleGroupSelectionMode,
27
+ type ToggleGroupState,
28
+ } from "./togglegroup.ts";
29
+ import { filterDOMProps, fromProps, mergeProps, type StyleProps, styleProps } from "./utils.ts";
30
+
31
+ interface ToggleButtonGroupValue {
32
+ state: ToggleGroupState;
33
+ disallowEmptySelection: () => boolean;
34
+ }
35
+
36
+ const ToggleButtonGroupContext = createContext<ToggleButtonGroupValue | null>(null);
37
+
38
+ /** The enclosing {@link ToggleButtonGroup}'s state, for an item of the caller's own. */
39
+ export function useToggleButtonGroup(): ToggleGroupState | null {
40
+ return getContext(ToggleButtonGroupContext)?.state ?? null;
41
+ }
42
+
43
+ export interface ToggleButtonGroupComponentProps extends StyleProps {
44
+ children?: Child;
45
+ /** @default "single" */
46
+ selectionMode?: ToggleGroupSelectionMode;
47
+ selectedKeys?: readonly string[];
48
+ defaultSelectedKeys?: readonly string[];
49
+ /** Pressing the last pressed item does not release it. */
50
+ disallowEmptySelection?: boolean;
51
+ isDisabled?: boolean;
52
+ /** @default "horizontal" */
53
+ orientation?: Orientation;
54
+ /** One tab stop, with the arrows moving inside. @default true */
55
+ rovingFocus?: boolean;
56
+ /** Whether the arrows wrap from the last item to the first. @default true */
57
+ loop?: boolean;
58
+ "aria-label"?: string;
59
+ "aria-labelledby"?: string;
60
+ "aria-describedby"?: string;
61
+ ref?: RefTarget<HTMLDivElement>;
62
+ onSelectionChange?: (keys: string[]) => void;
63
+ }
64
+
65
+ /**
66
+ * ```tsx
67
+ * <ToggleButtonGroup aria-label="Alignment" defaultSelectedKeys={["left"]}>
68
+ * <ToggleButtonGroupItem id="left">Left</ToggleButtonGroupItem>
69
+ * <ToggleButtonGroupItem id="center">Center</ToggleButtonGroupItem>
70
+ * </ToggleButtonGroup>
71
+ * ```
72
+ */
73
+ export function ToggleButtonGroup(props: ToggleButtonGroupComponentProps) {
74
+ const domRef = makeRef<HTMLDivElement>();
75
+ const state = toggleGroupState({
76
+ selectionMode: () => props.selectionMode,
77
+ selectedKeys: () => props.selectedKeys,
78
+ defaultSelectedKeys: () => props.defaultSelectedKeys,
79
+ isDisabled: () => props.isDisabled,
80
+ rovingFocus: () => props.rovingFocus,
81
+ onSelectionChange: (keys) => props.onSelectionChange?.(keys),
82
+ });
83
+ const { groupProps } = toggleGroup(
84
+ {
85
+ ref: domRef,
86
+ orientation: () => props.orientation ?? "horizontal",
87
+ direction: () => (props.dir === "auto" ? undefined : props.dir),
88
+ loop: () => props.loop,
89
+ "aria-label": () => props["aria-label"],
90
+ "aria-labelledby": () => props["aria-labelledby"],
91
+ "aria-describedby": () => props["aria-describedby"],
92
+ },
93
+ state,
94
+ );
95
+
96
+ const value: ToggleButtonGroupValue = {
97
+ state,
98
+ disallowEmptySelection: () => props.disallowEmptySelection === true,
99
+ };
100
+
101
+ const elementProps = mergeProps(
102
+ groupProps,
103
+ filterDOMProps(fromProps(cells(props)), { global: true }),
104
+ styleProps(cells(props)),
105
+ {
106
+ "data-orientation": () => props.orientation ?? "horizontal",
107
+ "data-disabled": state.isDisabled,
108
+ "data-testid": () => props["data-testid"],
109
+ },
110
+ );
111
+
112
+ return (
113
+ <div {...elementProps} ref={mergeRefs(domRef.set, props.ref)}>
114
+ <GroupProvider value={value}>
115
+ <SharedElementTransition>{props.children}</SharedElementTransition>
116
+ </GroupProvider>
117
+ </div>
118
+ );
119
+ }
120
+
121
+ function GroupProvider(props: { value: ToggleButtonGroupValue; children?: Child }) {
122
+ return provide(
123
+ ToggleButtonGroupContext,
124
+ () => props.value,
125
+ () => props.children as unknown,
126
+ );
127
+ }
128
+
129
+ export interface ToggleButtonGroupItemComponentProps extends StyleProps {
130
+ /** What this item contributes to the group's `selectedKeys`. */
131
+ id: string;
132
+ children?: Child;
133
+ isDisabled?: boolean;
134
+ autoFocus?: boolean;
135
+ "aria-label"?: string;
136
+ "aria-labelledby"?: string;
137
+ "aria-describedby"?: string;
138
+ ref?: RefTarget<HTMLButtonElement>;
139
+ onPress?: (event: PressEvent) => void;
140
+ onPressStart?: (event: PressEvent) => void;
141
+ onPressEnd?: (event: PressEvent) => void;
142
+ onPressChange?: (isPressed: boolean) => void;
143
+ }
144
+
145
+ /** One item: a {@link ToggleButton} whose pressed state is the group's. */
146
+ export function ToggleButtonGroupItem(props: ToggleButtonGroupItemComponentProps) {
147
+ const group = getContext(ToggleButtonGroupContext);
148
+ if (group === null || group === undefined) {
149
+ throw new Error("A ToggleButtonGroupItem must be rendered inside a ToggleButtonGroup.");
150
+ }
151
+ const domRef = makeRef<HTMLButtonElement>();
152
+ const item = toggleGroupItem(
153
+ { value: () => props.id, isDisabled: () => props.isDisabled, ref: domRef },
154
+ group.state,
155
+ );
156
+
157
+ const onChange = (next: boolean): void => {
158
+ // Releasing the only pressed item would leave the group empty.
159
+ if (!next && group.disallowEmptySelection() && group.state.selectedKeys().length <= 1) return;
160
+ item.onChange(next);
161
+ };
162
+
163
+ return (
164
+ <ToggleButton
165
+ {...omit(props, "id", "children", "isDisabled", "ref")}
166
+ isSelected={item.isSelected()}
167
+ isDisabled={item.isDisabled()}
168
+ excludeFromTabOrder={item.excludeFromTabOrder()}
169
+ onChange={onChange}
170
+ ref={mergeRefs(domRef.set, props.ref)}
171
+ >
172
+ {props.children}
173
+ </ToggleButton>
174
+ );
175
+ }
@@ -46,3 +46,41 @@ export function toggleInput(parts: {
46
46
  </label>
47
47
  );
48
48
  }
49
+
50
+ function present(value: unknown): boolean {
51
+ return value !== undefined && value !== null && value !== false && value !== "";
52
+ }
53
+
54
+ /**
55
+ * A toggle with its help text and its error after it.
56
+ *
57
+ * After, not inside: the label's content is the control's name, and text in it
58
+ * would be read once as the name and again as the description. Nothing is
59
+ * rendered for either while it has nothing to say.
60
+ */
61
+ export function withHelpText(
62
+ control: Child,
63
+ parts: {
64
+ descriptionProps: DOMProps;
65
+ errorMessageProps: DOMProps;
66
+ description: Accessor<unknown>;
67
+ /** What the error says, or nothing while the control is valid. */
68
+ errorMessage: Accessor<unknown>;
69
+ },
70
+ ) {
71
+ return (
72
+ <>
73
+ {control}
74
+ {() =>
75
+ present(parts.description()) ? (
76
+ <span {...parts.descriptionProps}>{parts.description() as Child}</span>
77
+ ) : null
78
+ }
79
+ {() =>
80
+ present(parts.errorMessage()) ? (
81
+ <span {...parts.errorMessageProps}>{parts.errorMessage() as Child}</span>
82
+ ) : null
83
+ }
84
+ </>
85
+ );
86
+ }
package/src/toggle.ts CHANGED
@@ -25,7 +25,14 @@ import {
25
25
  type ValidateFunction,
26
26
  type ValidationBehavior,
27
27
  } from "./validation.ts";
28
- import { access, filterDOMProps, mergeProps, type DOMProps, type MaybeAccessor } from "./utils.ts";
28
+ import {
29
+ access,
30
+ filterDOMProps,
31
+ id,
32
+ mergeProps,
33
+ type DOMProps,
34
+ type MaybeAccessor,
35
+ } from "./utils.ts";
29
36
 
30
37
  export { toggleState } from "./button.tsx";
31
38
  export type { ToggleState, ToggleStateOptions } from "./button.tsx";
@@ -47,7 +54,9 @@ export function formReset<T>(
47
54
  const form = element?.form;
48
55
  if (form === null || form === undefined) return undefined;
49
56
 
50
- const handle = (): void => onReset(access(initial) as T);
57
+ const handle = (event: Event): void => {
58
+ if (!event.defaultPrevented) onReset(access(initial) as T);
59
+ };
51
60
  form.addEventListener("reset", handle);
52
61
  return () => form.removeEventListener("reset", handle);
53
62
  },
@@ -65,6 +74,13 @@ export interface ToggleOptions extends FocusableOptions {
65
74
  validationBehavior?: MaybeAccessor<ValidationBehavior | undefined>;
66
75
  /** The caller's own message, which wins over anything validation found. */
67
76
  errorMessage?: MaybeAccessor<unknown>;
77
+ /** Help text for the control, which describes it when there is any. */
78
+ description?: MaybeAccessor<unknown>;
79
+ /**
80
+ * The validity to show, when it belongs to something bigger than this one
81
+ * control: a checkbox in a group is valid or not with its group.
82
+ */
83
+ validationState?: FormValidationState;
68
84
  /** The form value when selected. */
69
85
  value?: MaybeAccessor<string | undefined>;
70
86
  name?: MaybeAccessor<string | undefined>;
@@ -72,6 +88,7 @@ export interface ToggleOptions extends FocusableOptions {
72
88
  /** Whether there is a visible label. Without one, `aria-label` is required. */
73
89
  children?: MaybeAccessor<unknown>;
74
90
  "aria-label"?: MaybeAccessor<string | undefined>;
91
+ "aria-required"?: MaybeAccessor<boolean | undefined>;
75
92
  "aria-labelledby"?: MaybeAccessor<string | undefined>;
76
93
  "aria-describedby"?: MaybeAccessor<string | undefined>;
77
94
  "aria-controls"?: MaybeAccessor<string | undefined>;
@@ -88,6 +105,10 @@ export interface ToggleOptions extends FocusableOptions {
88
105
  export interface ToggleResult {
89
106
  labelProps: DOMProps;
90
107
  inputProps: DOMProps;
108
+ /** For the help text, outside the label so it is not read as the name. */
109
+ descriptionProps: DOMProps;
110
+ /** For the error, outside the label for the same reason. */
111
+ errorMessageProps: DOMProps;
91
112
  isSelected: Accessor<boolean>;
92
113
  isPressed: Accessor<boolean>;
93
114
  isDisabled: Accessor<boolean>;
@@ -125,7 +146,21 @@ export function toggle(
125
146
  isInvalid: options.isInvalid,
126
147
  errorMessage: options.errorMessage,
127
148
  name: options.name,
149
+ state: options.validationState,
128
150
  });
151
+ const isNative = (): boolean => behavior() === "native";
152
+ const isRequired = (): boolean => access(options.isRequired) === true;
153
+
154
+ const descriptionId = id();
155
+ const errorMessageId = id();
156
+ const describedBy = (): string | undefined =>
157
+ [
158
+ present(access(options.description)) ? descriptionId() : undefined,
159
+ isInvalid() && present(errorMessage()) ? errorMessageId() : undefined,
160
+ access(options["aria-describedby"]),
161
+ ]
162
+ .filter((part): part is string => part !== undefined)
163
+ .join(" ") || undefined;
129
164
 
130
165
  formValidation(
131
166
  {
@@ -185,6 +220,7 @@ export function toggle(
185
220
  return;
186
221
  }
187
222
  options.onPress?.(event);
223
+ focusInput(ref);
188
224
  },
189
225
  });
190
226
 
@@ -207,6 +243,8 @@ export function toggle(
207
243
  };
208
244
 
209
245
  return {
246
+ descriptionProps: { id: descriptionId },
247
+ errorMessageProps: { id: errorMessageId },
210
248
  // The label's own click is what toggles the input, so it is left alone.
211
249
  // `mousedown` is not: without this the label takes focus, and the ring
212
250
  // appears around the text rather than the control.
@@ -215,19 +253,28 @@ export function toggle(
215
253
  }),
216
254
 
217
255
  inputProps: mergeProps(
218
- filterDOMProps(options, { labelable: true }),
256
+ // Its own description first, then the caller's, which is the order they
257
+ // are read in; `describedBy` puts the caller's back at the end.
258
+ filterDOMProps({ ...options, "aria-describedby": undefined }, { labelable: true }),
219
259
  pressProps,
220
260
  focusableProps,
221
261
  {
222
262
  type: "checkbox",
223
263
  checked: () => state.isSelected(),
224
264
  disabled: isDisabled,
225
- required: () => access(options.isRequired) === true,
265
+ // `required` is the browser's, and the browser refuses the submit for
266
+ // it, so only when the page asked for that. Otherwise the form still
267
+ // submits and assistive technology is told instead.
268
+ required: () => isRequired() && isNative(),
226
269
  name: () => access(options.name),
227
270
  form: () => access(options.form),
228
271
  value: () => access(options.value),
229
- "aria-required": () => access(options.isRequired) === true || undefined,
272
+ // The caller's own word wins: a kit whose control states `required`
273
+ // both ways, as Radix's switch does, says so here.
274
+ "aria-required": () =>
275
+ access(options["aria-required"]) ?? ((isRequired() && !isNative()) || undefined),
230
276
  "aria-invalid": () => isInvalid() || undefined,
277
+ "aria-describedby": describedBy,
231
278
  "aria-errormessage": () => access(options["aria-errormessage"]),
232
279
  "aria-controls": () => access(options["aria-controls"]),
233
280
  "aria-readonly": () => isReadOnly() || undefined,
@@ -246,6 +293,22 @@ export function toggle(
246
293
  };
247
294
  }
248
295
 
296
+ function present(value: unknown): boolean {
297
+ return value !== undefined && value !== null && value !== false && value !== "";
298
+ }
299
+
300
+ /**
301
+ * Focus the input a press on its label was for.
302
+ *
303
+ * WebKit does not focus a checkbox or a radio for a mouse click, its label's
304
+ * included, so without this the press leaves focus wherever it was and the
305
+ * next Space toggles something else.
306
+ */
307
+ export function focusInput(ref: ElementRef<HTMLInputElement>): void {
308
+ const input = access(ref) as HTMLInputElement | null;
309
+ if (input !== null && input.ownerDocument.activeElement !== input) input.focus();
310
+ }
311
+
249
312
  /** The element a toggle's visual is drawn on. */
250
313
  export type ToggleElementType = "label" | "span";
251
314