@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
|
@@ -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
|
+
}
|