@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,240 @@
1
+ /**
2
+ * A colour swatch picker: a set of swatches, one of which is the value.
3
+ *
4
+ * It is a listbox with single selection that cannot be emptied, keyed by each
5
+ * swatch's colour, so everything a listbox does a screen reader already knows
6
+ * how to announce: "vibrant red, 1 of 8, selected". Two choices are its own:
7
+ *
8
+ * - **A swatch is named by its colour.** The option's name is a description a
9
+ * person would use ("dark vibrant blue"), and typing its first letters finds
10
+ * it. Naming one by its hex code reads out six characters nobody can picture.
11
+ * - **The swatches are a grid by default.** Left and right step through them in
12
+ * order, and up and down move to the swatch above or below, measured from
13
+ * where the swatches are drawn. `layout="stack"` makes it a single column.
14
+ *
15
+ * The value is a colour, not a key: a value that matches no swatch selects
16
+ * nothing, and choosing a swatch reports the colour it was given.
17
+ */
18
+
19
+ import {
20
+ cells,
21
+ type Accessor,
22
+ type Child,
23
+ createContext,
24
+ createMemo,
25
+ getContext,
26
+ isBlock,
27
+ omit,
28
+ onCleanup,
29
+ provide,
30
+ untrack,
31
+ } from "@barqjs/core";
32
+ import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
33
+ import { listState, type Key } from "./collections.ts";
34
+ import { type Color, defaultColor, parseColor } from "./color.ts";
35
+ import { collator, useLocale } from "./i18n.ts";
36
+ import { listBox, ListBoxProvider, Option, type ListBoxContextValue } from "./listbox.tsx";
37
+ import { ListKeyboardDelegate } from "./selection.ts";
38
+ import { staticAccessors, staticCollection, type StaticEntry } from "./static-collection.ts";
39
+ import {
40
+ controllable,
41
+ filterDOMProps,
42
+ fromProps,
43
+ mergeProps,
44
+ type StyleProps,
45
+ styleProps,
46
+ } from "./utils.ts";
47
+
48
+ // ---------------------------------------------------------------------------
49
+ // Components
50
+ // ---------------------------------------------------------------------------
51
+
52
+ function asColor(value: Color | string | null | undefined): Color | undefined {
53
+ if (value === null || value === undefined) return undefined;
54
+ return typeof value === "string" ? parseColor(value) : value;
55
+ }
56
+
57
+ /** The key a swatch is selected by. */
58
+ function keyOf(color: Color): string {
59
+ return color.toString("hexa");
60
+ }
61
+
62
+ interface PickerValue {
63
+ /** Every swatch's colour, by its key, so a selected key becomes a colour again. */
64
+ colors: Map<string, Color>;
65
+ }
66
+
67
+ const PickerContext = createContext<PickerValue | null>(null);
68
+ const SwatchColorContext = createContext<Accessor<Color> | null>(null);
69
+
70
+ /** The colour of the swatch being built, for a swatch element of the caller's own. */
71
+ export function useColorSwatchPickerItem(): Accessor<Color> | null {
72
+ return getContext(SwatchColorContext) ?? null;
73
+ }
74
+
75
+ export type ColorSwatchPickerLayout = "grid" | "stack";
76
+
77
+ export interface ColorSwatchPickerComponentProps extends StyleProps {
78
+ /** The {@link ColorSwatchPickerItem}s. */
79
+ children?: Child;
80
+ value?: Color | string;
81
+ defaultValue?: Color | string;
82
+ /** @default "grid" */
83
+ layout?: ColorSwatchPickerLayout;
84
+ /** @default "Color swatches", unless `aria-labelledby` names it */
85
+ "aria-label"?: string;
86
+ "aria-labelledby"?: string;
87
+ "aria-describedby"?: string;
88
+ ref?: RefTarget<HTMLUListElement>;
89
+ onChange?: (value: Color) => void;
90
+ }
91
+
92
+ /**
93
+ * ```tsx
94
+ * <ColorSwatchPicker defaultValue="#f00" onChange={(colour) => brand.set(colour)}>
95
+ * <ColorSwatchPickerItem color="#f00">{(colour) => <ColorSwatch color={colour} />}</ColorSwatchPickerItem>
96
+ * <ColorSwatchPickerItem color="#0f0">{(colour) => <ColorSwatch color={colour} />}</ColorSwatchPickerItem>
97
+ * </ColorSwatchPicker>
98
+ * ```
99
+ */
100
+ export function ColorSwatchPicker(props: ColorSwatchPickerComponentProps) {
101
+ const domRef = makeRef<HTMLUListElement>();
102
+ const rendered = staticCollection();
103
+ const colors = new Map<string, Color>();
104
+ const locale = useLocale();
105
+ const search = collator({ usage: "search", sensitivity: "base" });
106
+
107
+ const [value, setValue] = controllable<Color>(
108
+ () => asColor(props.value),
109
+ () => asColor(props.defaultValue) ?? defaultColor(),
110
+ (next) => props.onChange?.(next),
111
+ );
112
+
113
+ const state = listState<StaticEntry>({
114
+ items: rendered.entries,
115
+ ...staticAccessors,
116
+ selectionMode: "single",
117
+ disallowEmptySelection: true,
118
+ selectedKeys: () => [keyOf(value())],
119
+ onSelectionChange: (keys) => {
120
+ if (keys === "all") return;
121
+ const [key] = keys;
122
+ const chosen = key === undefined ? undefined : colors.get(String(key));
123
+ if (chosen !== undefined) setValue(chosen);
124
+ },
125
+ });
126
+
127
+ const delegate = new ListKeyboardDelegate({
128
+ collection: state.collection,
129
+ ref: domRef,
130
+ disabledKeys: state.disabledKeys,
131
+ disabledBehavior: () => state.selectionManager().disabledBehavior,
132
+ collator: search,
133
+ layout: untrack(() => props.layout) === "stack" ? "stack" : "grid",
134
+ orientation: "vertical",
135
+ direction: () => locale().direction,
136
+ });
137
+
138
+ const { listBoxProps, baseId } = listBox(
139
+ {
140
+ ref: domRef,
141
+ keyboardDelegate: delegate,
142
+ disallowEmptySelection: true,
143
+ "aria-label": () =>
144
+ props["aria-label"] ??
145
+ (props["aria-labelledby"] === undefined ? "Color swatches" : undefined),
146
+ "aria-labelledby": () => props["aria-labelledby"],
147
+ "aria-describedby": () => props["aria-describedby"],
148
+ },
149
+ state,
150
+ );
151
+
152
+ const list: ListBoxContextValue = {
153
+ state,
154
+ baseId,
155
+ shouldSelectOnPressUp: () => undefined,
156
+ shouldUseVirtualFocus: () => undefined,
157
+ staticItems: rendered,
158
+ };
159
+ const picker: PickerValue = { colors };
160
+
161
+ const elementProps = mergeProps(
162
+ listBoxProps,
163
+ filterDOMProps(fromProps(cells(props)), { global: true }),
164
+ styleProps(cells(props)),
165
+ {
166
+ "data-layout": () => props.layout ?? "grid",
167
+ "data-empty": () => state.collection().size === 0,
168
+ "data-testid": () => props["data-testid"],
169
+ },
170
+ );
171
+
172
+ return (
173
+ <ul {...elementProps} ref={mergeRefs(domRef.set, props.ref)}>
174
+ <ListBoxProvider value={list}>
175
+ {provide(
176
+ PickerContext,
177
+ () => picker,
178
+ () => props.children,
179
+ )}
180
+ </ListBoxProvider>
181
+ </ul>
182
+ );
183
+ }
184
+
185
+ type SwatchRender = (color: Color) => Child;
186
+
187
+ /** The callback among the children, when they are one. */
188
+ function swatchRender(children: unknown): SwatchRender | null {
189
+ if (typeof children !== "function") return null;
190
+ if (children.length > 0) return children as SwatchRender;
191
+ if (isBlock(children)) return null;
192
+ const inner: unknown = (children as () => unknown)();
193
+ return typeof inner === "function" && inner.length > 0 ? (inner as SwatchRender) : null;
194
+ }
195
+
196
+ export interface ColorSwatchPickerItemComponentProps extends StyleProps {
197
+ color: Color | string;
198
+ /** What is drawn: a `<ColorSwatch>`, or a function given the colour that returns one. */
199
+ children?: Child | SwatchRender;
200
+ isDisabled?: boolean;
201
+ /** @default the colour's name */
202
+ "aria-label"?: string;
203
+ ref?: RefTarget<HTMLLIElement>;
204
+ }
205
+
206
+ /** One swatch. Its key is its colour as first given, as an option's `value` is. */
207
+ export function ColorSwatchPickerItem(props: ColorSwatchPickerItemComponentProps) {
208
+ const picker = getContext(PickerContext);
209
+ if (picker === null || picker === undefined) {
210
+ throw new Error("A ColorSwatchPickerItem must be rendered inside a ColorSwatchPicker.");
211
+ }
212
+ const color = createMemo(() => asColor(props.color) ?? parseColor("#0000"));
213
+ const key: Key = untrack(() => keyOf(color()));
214
+ const held = untrack(color);
215
+ picker.colors.set(key, held);
216
+ onCleanup(() => {
217
+ if (picker.colors.get(key) === held) picker.colors.delete(key);
218
+ });
219
+
220
+ const locale = useLocale();
221
+ const render = swatchRender(cells(props).children);
222
+ const name = (): string => color().getColorName(locale().locale);
223
+
224
+ return (
225
+ <Option
226
+ {...omit(props, "color", "children", "isDisabled", "aria-label", "ref")}
227
+ value={key}
228
+ textValue={name()}
229
+ aria-label={props["aria-label"] ?? name()}
230
+ isDisabled={props.isDisabled}
231
+ ref={mergeRefs(props.ref)}
232
+ >
233
+ {provide(
234
+ SwatchColorContext,
235
+ () => color,
236
+ () => (render === null ? props.children : render(color())),
237
+ )}
238
+ </Option>
239
+ );
240
+ }