@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,208 @@
1
+ /**
2
+ * A drop zone: an area things can be dropped on, or pasted into.
3
+ *
4
+ * The area itself is not focusable; a visually hidden button inside it is.
5
+ * That button is the drop target a keyboard or screen reader drag lands on,
6
+ * the element a paste is read from, and what a click on the area focuses.
7
+ */
8
+
9
+ import {
10
+ type Accessor,
11
+ type Child,
12
+ cells,
13
+ createContext,
14
+ createSignal,
15
+ getContext,
16
+ getOwner,
17
+ install,
18
+ onCleanup,
19
+ } from "@barqjs/core";
20
+ import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
21
+ import { button } from "./button.tsx";
22
+ import { clipboard, drop, type DropOptions } from "./dnd/index.ts";
23
+ import { contains, focusWithoutScrolling, isFocusable } from "./dom.ts";
24
+ import { focusRing } from "./focus.ts";
25
+ import { StringDictionary, stringFormatter } from "./i18n.ts";
26
+ import { hover, type HoverEvent } from "./interactions/hover.ts";
27
+ import { visuallyHidden } from "./live.ts";
28
+ import {
29
+ callback,
30
+ filterDOMProps,
31
+ fromProps,
32
+ id,
33
+ mergeProps,
34
+ type StyleProps,
35
+ styleProps,
36
+ } from "./utils.ts";
37
+
38
+ const strings = new StringDictionary<"dropzoneLabel">({
39
+ "en-US": { dropzoneLabel: "DropZone" },
40
+ });
41
+
42
+ interface DropZoneLabelSlot {
43
+ id: Accessor<string>;
44
+ name(has: boolean): void;
45
+ }
46
+
47
+ const DropZoneLabelContext = createContext<DropZoneLabelSlot | null>(null);
48
+
49
+ /**
50
+ * The id the enclosing drop zone's visible label must carry.
51
+ *
52
+ * Calling it is what makes the zone's button point at the label, and leaving
53
+ * undoes that, so the button never names an element that is not there.
54
+ */
55
+ export function useDropZoneLabel(): Accessor<string> | null {
56
+ const slot = getContext(DropZoneLabelContext);
57
+ if (slot === null || slot === undefined) return null;
58
+ slot.name(true);
59
+ onCleanup(() => slot.name(false));
60
+ return slot.id;
61
+ }
62
+
63
+ export interface DropZoneLabelProps extends StyleProps {
64
+ children?: Child;
65
+ }
66
+
67
+ /** The visible text that names a drop zone. */
68
+ export function DropZoneLabel(props: DropZoneLabelProps) {
69
+ const labelId = useDropZoneLabel();
70
+ return (
71
+ <span
72
+ {...filterDOMProps(fromProps(cells(props)), { global: true })}
73
+ {...styleProps(cells(props))}
74
+ id={labelId?.()}
75
+ data-slot={props["data-slot"] ?? "label"}
76
+ >
77
+ {props.children}
78
+ </span>
79
+ );
80
+ }
81
+
82
+ export interface DropZoneProps
83
+ extends
84
+ Omit<DropOptions, "getDropOperationForPoint" | "ref" | "hasDropButton" | "isDisabled">,
85
+ StyleProps {
86
+ children?: Child;
87
+ isDisabled?: boolean;
88
+ /** @default "DropZone" */
89
+ "aria-label"?: string;
90
+ "aria-labelledby"?: string;
91
+ onHoverStart?: (event: HoverEvent) => void;
92
+ onHoverEnd?: (event: HoverEvent) => void;
93
+ onHoverChange?: (isHovered: boolean) => void;
94
+ ref?: RefTarget<HTMLDivElement>;
95
+ }
96
+
97
+ /**
98
+ * ```tsx
99
+ * <DropZone onDrop={async (event) => upload(event.items)}>
100
+ * <DropZoneLabel>Drop files here</DropZoneLabel>
101
+ * </DropZone>
102
+ * ```
103
+ *
104
+ * `data-drop-target` while a drag is over it, and `data-hovered`,
105
+ * `data-focused`, `data-focus-visible` and `data-disabled` as usual. A paste
106
+ * while its button is focused is delivered to `onDrop` as a copy.
107
+ */
108
+ export function DropZone(props: DropZoneProps) {
109
+ const zoneRef = makeRef<HTMLDivElement>();
110
+ const buttonRef = makeRef<HTMLButtonElement>();
111
+ const options = fromProps(cells(props));
112
+ const isDisabled = (): boolean => props.isDisabled === true;
113
+ const message = stringFormatter(strings);
114
+
115
+ const { dropProps, dropButtonProps, isDropTarget } = drop({
116
+ ref: buttonRef,
117
+ hasDropButton: true,
118
+ isDisabled,
119
+ getDropOperation: (types, allowed) =>
120
+ callback(cells(props).getDropOperation)?.(types, allowed) ?? allowed[0] ?? "cancel",
121
+ onDropEnter: (event) => options.onDropEnter?.(event),
122
+ onDropMove: (event) => options.onDropMove?.(event),
123
+ onDropActivate: (event) => options.onDropActivate?.(event),
124
+ onDropExit: (event) => options.onDropExit?.(event),
125
+ onDrop: (event) => options.onDrop?.(event),
126
+ });
127
+
128
+ const textId = id();
129
+ const [isNamed, setNamed] = createSignal(false, { ownedWrite: true });
130
+ const owner = getOwner();
131
+ if (owner !== null) {
132
+ const slot: DropZoneLabelSlot = { id: textId, name: setNamed };
133
+ install(owner, DropZoneLabelContext, () => slot);
134
+ }
135
+
136
+ const buttonId = id();
137
+ const ariaLabel = (): string => props["aria-label"] ?? message().format("dropzoneLabel");
138
+ // The button names itself AND points at the visible label, so a screen
139
+ // reader reads both: "DropZone, Drop files here".
140
+ const ariaLabelledBy = (): string | undefined => {
141
+ const ids = [isNamed() ? textId() : undefined, props["aria-labelledby"]].filter(
142
+ (each): each is string => each !== undefined && each !== "",
143
+ );
144
+ return ids.length === 0 ? undefined : [buttonId(), ...ids].join(" ");
145
+ };
146
+
147
+ const { buttonProps } = button({ ...dropButtonProps, isDisabled }, buttonRef);
148
+ const { hoverProps, isHovered } = hover({
149
+ isDisabled,
150
+ onHoverStart: (event) => options.onHoverStart?.(event),
151
+ onHoverEnd: (event) => options.onHoverEnd?.(event),
152
+ onHoverChange: (value) => options.onHoverChange?.(value),
153
+ });
154
+ const { focusProps, isFocused, isFocusVisible } = focusRing();
155
+ const { clipboardProps } = clipboard({
156
+ isDisabled,
157
+ onPaste: (items) =>
158
+ options.onDrop?.({ type: "drop", items, x: 0, y: 0, dropOperation: "copy" }),
159
+ });
160
+ const { visuallyHiddenProps } = visuallyHidden();
161
+
162
+ // A click on the zone's own surface focuses its button; a click on
163
+ // something focusable inside it leaves focus there.
164
+ const onClick = (event: MouseEvent): void => {
165
+ const zone = zoneRef();
166
+ let target = event.target instanceof Element ? event.target : null;
167
+ while (target !== null && contains(zone, target)) {
168
+ if (isFocusable(target)) break;
169
+ const hidden = buttonRef();
170
+ if (target === zone && hidden !== null) {
171
+ focusWithoutScrolling(hidden);
172
+ break;
173
+ }
174
+ target = target.parentElement;
175
+ }
176
+ };
177
+
178
+ const elementProps = mergeProps(
179
+ filterDOMProps(options, { global: true, id: false }),
180
+ styleProps(cells(props)),
181
+ dropProps,
182
+ hoverProps,
183
+ {
184
+ onClick,
185
+ "data-hovered": isHovered,
186
+ "data-focused": isFocused,
187
+ "data-focus-visible": isFocusVisible,
188
+ "data-drop-target": isDropTarget,
189
+ "data-disabled": isDisabled,
190
+ "data-testid": () => props["data-testid"],
191
+ },
192
+ );
193
+
194
+ const hiddenButtonProps = mergeProps(buttonProps, focusProps, clipboardProps, {
195
+ id: buttonId,
196
+ "aria-label": ariaLabel,
197
+ "aria-labelledby": ariaLabelledBy,
198
+ });
199
+
200
+ return (
201
+ <div {...elementProps} ref={mergeRefs(zoneRef.set, props.ref)}>
202
+ <div {...visuallyHiddenProps}>
203
+ <button {...hiddenButtonProps} ref={buttonRef.set} />
204
+ </div>
205
+ {props.children}
206
+ </div>
207
+ );
208
+ }
@@ -0,0 +1,103 @@
1
+ /**
2
+ * A file trigger: any pressable control opens the platform's file picker.
3
+ *
4
+ * The picker can only be opened by a click on an `<input type="file">`, and
5
+ * that input is hard to style and harder to label. So the input is rendered
6
+ * hidden and the control the caller wrote is the one the user sees, presses
7
+ * and hears announced. A press on it clicks the input.
8
+ *
9
+ * The input is emptied before each click. Picking the same file twice in a row
10
+ * fires no `change` otherwise, and a user re-uploading a file they just edited
11
+ * would see nothing happen.
12
+ */
13
+
14
+ import { cells, type Child, createContext, getContext, provide } from "@barqjs/core";
15
+ import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
16
+ import {
17
+ filterDOMProps,
18
+ fromProps,
19
+ mergeProps,
20
+ provideTriggerSlot,
21
+ type StyleProps,
22
+ } from "./utils.ts";
23
+
24
+ export interface FileTriggerValue {
25
+ /** Open the file picker, as a press on the trigger does. */
26
+ open(): void;
27
+ }
28
+
29
+ const FileTriggerContext = createContext<FileTriggerValue | null>(null);
30
+
31
+ /**
32
+ * The enclosing {@link FileTrigger}, for a control this package does not
33
+ * provide: call `open()` from its own press handler.
34
+ */
35
+ export function useFileTrigger(): FileTriggerValue | null {
36
+ return getContext(FileTriggerContext) ?? null;
37
+ }
38
+
39
+ export interface FileTriggerComponentProps extends Omit<
40
+ StyleProps,
41
+ "class" | "className" | "style"
42
+ > {
43
+ /** The control the user presses. A `<Button>` takes the press on its own. */
44
+ children?: Child;
45
+ /** The MIME types or extensions the picker offers. */
46
+ acceptedFileTypes?: readonly string[];
47
+ allowsMultiple?: boolean;
48
+ /** Capture from a camera rather than the file system, on a device that has one. */
49
+ defaultCamera?: "user" | "environment";
50
+ /** Pick a directory rather than files. */
51
+ acceptDirectory?: boolean;
52
+ /** The hidden input, for a form or a test. */
53
+ ref?: RefTarget<HTMLInputElement>;
54
+ onSelect?: (files: FileList | null) => void;
55
+ }
56
+
57
+ /**
58
+ * ```tsx
59
+ * <FileTrigger acceptedFileTypes={["image/png"]} onSelect={(files) => upload(files)}>
60
+ * <Button>Upload</Button>
61
+ * </FileTrigger>
62
+ * ```
63
+ */
64
+ export function FileTrigger(props: FileTriggerComponentProps) {
65
+ const inputRef = makeRef<HTMLInputElement>();
66
+
67
+ const open = (): void => {
68
+ const input = inputRef();
69
+ if (input === null) return;
70
+ if (input.value !== "") input.value = "";
71
+ input.click();
72
+ };
73
+
74
+ const value: FileTriggerValue = { open };
75
+
76
+ const inputProps = mergeProps(filterDOMProps(fromProps(cells(props)), { global: true }), {
77
+ type: "file",
78
+ style: { display: "none" },
79
+ accept: () => props.acceptedFileTypes?.toString(),
80
+ multiple: () => props.allowsMultiple === true || undefined,
81
+ capture: () => props.defaultCamera,
82
+ webkitdirectory: () => (props.acceptDirectory === true ? "" : undefined),
83
+ "data-testid": () => props["data-testid"],
84
+ // The click this component makes must not bubble on as a click on whatever
85
+ // contains the trigger.
86
+ onClick: (event: MouseEvent) => event.stopPropagation(),
87
+ onChange: (event: Event) => props.onSelect?.((event.target as HTMLInputElement).files),
88
+ });
89
+
90
+ return (
91
+ <>
92
+ {provide(
93
+ FileTriggerContext,
94
+ () => value,
95
+ () => {
96
+ provideTriggerSlot({ props: { onPress: open } });
97
+ return props.children;
98
+ },
99
+ )}
100
+ <input {...inputProps} ref={mergeRefs(inputRef.set, props.ref)} />
101
+ </>
102
+ );
103
+ }
package/src/form.tsx CHANGED
@@ -16,7 +16,7 @@
16
16
  * handler AND navigates does both.
17
17
  */
18
18
 
19
- import { type Child, type Incoming } from "@barqjs/core";
19
+ import { cells, type Child } from "@barqjs/core";
20
20
  import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
21
21
  import {
22
22
  provideValidationBehavior,
@@ -54,14 +54,14 @@ export interface FormComponentProps extends StyleProps {
54
54
  * </Form>
55
55
  * ```
56
56
  */
57
- export function Form(props: Incoming<FormComponentProps>) {
57
+ export function Form(props: FormComponentProps) {
58
58
  const domRef = makeRef<HTMLFormElement>();
59
59
 
60
- provideValidationErrors(() => props.validationErrors?.() ?? {});
61
- provideValidationBehavior(() => props.validationBehavior?.());
60
+ provideValidationErrors(() => props.validationErrors ?? {});
61
+ provideValidationBehavior(() => props.validationBehavior);
62
62
 
63
63
  const onSubmit = (event: SubmitEvent): void => {
64
- const handler = props.onSubmit?.();
64
+ const handler = props.onSubmit;
65
65
  if (handler === undefined) return;
66
66
  // Both calling a handler and navigating is doing the thing twice.
67
67
  event.preventDefault();
@@ -69,25 +69,25 @@ export function Form(props: Incoming<FormComponentProps>) {
69
69
  };
70
70
 
71
71
  const elementProps = mergeProps(
72
- filterDOMProps(fromProps(props), { global: true }),
73
- styleProps(props),
72
+ filterDOMProps(fromProps(cells(props)), { global: true }),
73
+ styleProps(cells(props)),
74
74
  {
75
- action: () => props.action?.(),
76
- method: () => props.method?.(),
77
- enctype: () => props.encType?.(),
78
- target: () => props.target?.(),
79
- autocomplete: () => props.autoComplete?.(),
80
- novalidate: () => props.noValidate?.() === true || undefined,
81
- "aria-label": () => props["aria-label"]?.(),
82
- "aria-labelledby": () => props["aria-labelledby"]?.(),
83
- "data-testid": () => props["data-testid"]?.(),
75
+ action: () => props.action,
76
+ method: () => props.method,
77
+ enctype: () => props.encType,
78
+ target: () => props.target,
79
+ autocomplete: () => props.autoComplete,
80
+ novalidate: () => props.noValidate === true || undefined,
81
+ "aria-label": () => props["aria-label"],
82
+ "aria-labelledby": () => props["aria-labelledby"],
83
+ "data-testid": () => props["data-testid"],
84
84
  onSubmit,
85
- onReset: (event: Event) => props.onReset?.()?.(event),
85
+ onReset: (event: Event) => props.onReset?.(event),
86
86
  },
87
87
  );
88
88
 
89
89
  return (
90
- <form {...elementProps} ref={mergeRefs(domRef.set, props.ref?.())}>
90
+ <form {...elementProps} ref={mergeRefs(domRef.set, props.ref)}>
91
91
  {props.children}
92
92
  </form>
93
93
  );