@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,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared elements: one element that appears to move from one parent to another.
|
|
3
|
+
*
|
|
4
|
+
* A selection indicator under the chosen tab is the case. Every tab renders
|
|
5
|
+
* its own indicator and only the selected one is shown; when the selection
|
|
6
|
+
* moves, the old indicator goes and a new one appears somewhere else. Drawn as
|
|
7
|
+
* that, it blinks. Drawn as one bar sliding across, it reads as the selection
|
|
8
|
+
* moving, which is what happened.
|
|
9
|
+
*
|
|
10
|
+
* The technique is FLIP. When a shared element leaves, its box and the current
|
|
11
|
+
* value of every property it transitions are recorded under its name. When one
|
|
12
|
+
* of the same name arrives, it is put where the old one was, with the old
|
|
13
|
+
* values, for one frame, and then let go: the stylesheet's own transitions
|
|
14
|
+
* carry it to where it now belongs. Nothing here animates anything, so the
|
|
15
|
+
* motion, its duration and `prefers-reduced-motion` all stay the stylesheet's.
|
|
16
|
+
*
|
|
17
|
+
* An element with nothing to continue from ENTERS: `data-entering` for one
|
|
18
|
+
* frame. One that leaves with nothing to take over from it EXITS:
|
|
19
|
+
* `data-exiting`, held until its exit transition has finished.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import {
|
|
23
|
+
cells,
|
|
24
|
+
type Accessor,
|
|
25
|
+
type Child,
|
|
26
|
+
createContext,
|
|
27
|
+
createEffect,
|
|
28
|
+
createRenderEffect,
|
|
29
|
+
createSignal,
|
|
30
|
+
getContext,
|
|
31
|
+
type Incoming,
|
|
32
|
+
onCleanup,
|
|
33
|
+
provide,
|
|
34
|
+
Show,
|
|
35
|
+
untrack,
|
|
36
|
+
} from "@barqjs/core";
|
|
37
|
+
import { isServer } from "@barqjs/web";
|
|
38
|
+
import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
|
|
39
|
+
import { exitDuration } from "./presence.ts";
|
|
40
|
+
import {
|
|
41
|
+
access,
|
|
42
|
+
type DOMProps,
|
|
43
|
+
filterDOMProps,
|
|
44
|
+
fromProps,
|
|
45
|
+
type MaybeAccessor,
|
|
46
|
+
mergeProps,
|
|
47
|
+
type StyleProps,
|
|
48
|
+
styleProps,
|
|
49
|
+
} from "./utils.ts";
|
|
50
|
+
|
|
51
|
+
interface Snapshot {
|
|
52
|
+
rect: DOMRect;
|
|
53
|
+
style: [property: string, value: string][];
|
|
54
|
+
/** The element it was taken from. */
|
|
55
|
+
source: HTMLElement;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface LiveElement {
|
|
59
|
+
name: Accessor<string>;
|
|
60
|
+
element: Accessor<HTMLElement | null>;
|
|
61
|
+
isVisible: Accessor<boolean>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** What a {@link SharedElementTransition} holds for the shared elements inside it. */
|
|
65
|
+
export interface SharedElementScope {
|
|
66
|
+
/** The boxes of elements that have left, by name, until one of the same name arrives. */
|
|
67
|
+
snapshots: Map<string, Snapshot>;
|
|
68
|
+
/** Every shared element currently mounted in the scope. */
|
|
69
|
+
live: Set<LiveElement>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const SharedElementContext = createContext<SharedElementScope | null>(null);
|
|
73
|
+
|
|
74
|
+
export interface SharedElementTransitionComponentProps {
|
|
75
|
+
children?: Child;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The scope a shared element's name is unique in.
|
|
80
|
+
*
|
|
81
|
+
* ```tsx
|
|
82
|
+
* <SharedElementTransition>
|
|
83
|
+
* <For each={tabs()}>{(tab) => <Tab>{tab.name}<Show when={selected() === tab.id}><SharedElement name="underline" /></Show></Tab>}</For>
|
|
84
|
+
* </SharedElementTransition>
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export function SharedElementTransition(props: Incoming<SharedElementTransitionComponentProps>) {
|
|
88
|
+
const scope: SharedElementScope = { snapshots: new Map(), live: new Set() };
|
|
89
|
+
return provide(
|
|
90
|
+
SharedElementContext,
|
|
91
|
+
() => scope,
|
|
92
|
+
() => props.children as unknown,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export type SharedElementPhase = "visible" | "hidden" | "entering" | "exiting";
|
|
97
|
+
|
|
98
|
+
export interface SharedElementOptions {
|
|
99
|
+
name: MaybeAccessor<string>;
|
|
100
|
+
/** @default true */
|
|
101
|
+
isVisible?: MaybeAccessor<boolean | undefined>;
|
|
102
|
+
ref: Accessor<HTMLElement | null>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface SharedElementResult {
|
|
106
|
+
/** Whether the element belongs in the document: shown, or still leaving. */
|
|
107
|
+
isRendered: Accessor<boolean>;
|
|
108
|
+
phase: Accessor<SharedElementPhase>;
|
|
109
|
+
elementProps: DOMProps;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
type StyleRecord = Record<string, string>;
|
|
113
|
+
|
|
114
|
+
function animationsOf(element: HTMLElement): Animation[] {
|
|
115
|
+
const source = element as HTMLElement & { getAnimations?: () => Animation[] };
|
|
116
|
+
return typeof source.getAnimations === "function" ? source.getAnimations() : [];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function snapshotOf(element: HTMLElement): Snapshot | null {
|
|
120
|
+
const style = element.ownerDocument.defaultView?.getComputedStyle(element);
|
|
121
|
+
if (style === undefined) return null;
|
|
122
|
+
const transitioned = style.transitionProperty.trim();
|
|
123
|
+
if (transitioned === "" || transitioned === "none") return null;
|
|
124
|
+
const values = style as unknown as StyleRecord;
|
|
125
|
+
return {
|
|
126
|
+
rect: element.getBoundingClientRect(),
|
|
127
|
+
style: transitioned.split(/\s*,\s*/).map((property) => [property, values[property] ?? ""]),
|
|
128
|
+
source: element,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function sharedElement(
|
|
133
|
+
options: SharedElementOptions,
|
|
134
|
+
scope: SharedElementScope,
|
|
135
|
+
): SharedElementResult {
|
|
136
|
+
const name = (): string => access(options.name);
|
|
137
|
+
const isVisible = (): boolean => access(options.isVisible) !== false;
|
|
138
|
+
const [phase, setPhase] = createSignal<SharedElementPhase>(
|
|
139
|
+
untrack(isVisible) ? "visible" : "hidden",
|
|
140
|
+
{ ownedWrite: true },
|
|
141
|
+
);
|
|
142
|
+
const self: LiveElement = { name, element: options.ref, isVisible };
|
|
143
|
+
|
|
144
|
+
const record = (element: HTMLElement | null): void => {
|
|
145
|
+
if (element === null || !element.isConnected || element.hasAttribute("data-exiting")) return;
|
|
146
|
+
const snapshot = snapshotOf(element);
|
|
147
|
+
if (snapshot !== null) scope.snapshots.set(untrack(name), snapshot);
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/** Whether the element a snapshot came from is still mounted and still shown. */
|
|
151
|
+
const stays = (source: HTMLElement): boolean => {
|
|
152
|
+
for (const other of scope.live) {
|
|
153
|
+
if (other !== self && untrack(other.element) === source) return untrack(other.isVisible);
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
if (!isServer) {
|
|
159
|
+
// An element whose PARENT unmounts it is gone from the document by the
|
|
160
|
+
// time its own cleanup runs, too late to measure. One arriving in the same
|
|
161
|
+
// flush is built while the other is still in place, so it measures it.
|
|
162
|
+
if (untrack(isVisible) && !scope.snapshots.has(untrack(name))) {
|
|
163
|
+
for (const other of scope.live) {
|
|
164
|
+
if (untrack(other.name) !== untrack(name)) continue;
|
|
165
|
+
const element = untrack(other.element);
|
|
166
|
+
if (element !== null && element.isConnected) {
|
|
167
|
+
record(element);
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
scope.live.add(self);
|
|
173
|
+
onCleanup(() => scope.live.delete(self));
|
|
174
|
+
|
|
175
|
+
// In the render phase, so the leaving element's box is on record before
|
|
176
|
+
// any arriving one, built in the same flush, looks for it.
|
|
177
|
+
createRenderEffect(isVisible, (visible, previous) => {
|
|
178
|
+
if (previous === true && !visible) record(untrack(options.ref));
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
createEffect(
|
|
182
|
+
() => ({ visible: isVisible(), element: options.ref(), key: name() }),
|
|
183
|
+
({ visible, element, key }) => {
|
|
184
|
+
if (element === null) return undefined;
|
|
185
|
+
let frame: number | null = null;
|
|
186
|
+
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
187
|
+
let cancelled = false;
|
|
188
|
+
|
|
189
|
+
let previous = scope.snapshots.get(key);
|
|
190
|
+
if (visible && previous !== undefined) {
|
|
191
|
+
scope.snapshots.delete(key);
|
|
192
|
+
// Taken from one that has not left after all: two of one name shown
|
|
193
|
+
// at once, which is nothing to move between.
|
|
194
|
+
if (stays(previous.source)) previous = undefined;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (visible && previous !== undefined) {
|
|
198
|
+
const from = previous;
|
|
199
|
+
setPhase("visible");
|
|
200
|
+
const running = animationsOf(element);
|
|
201
|
+
const inline = element.style as unknown as StyleRecord;
|
|
202
|
+
const restore = from.style.map(([property, value]): [string, string] => {
|
|
203
|
+
const current = inline[property] ?? "";
|
|
204
|
+
if (property === "translate") {
|
|
205
|
+
const now = element.getBoundingClientRect();
|
|
206
|
+
element.style.setProperty(
|
|
207
|
+
"translate",
|
|
208
|
+
`${from.rect.left - now.left}px ${from.rect.top - now.top}px`,
|
|
209
|
+
);
|
|
210
|
+
} else {
|
|
211
|
+
inline[property] = value;
|
|
212
|
+
}
|
|
213
|
+
return [property, current];
|
|
214
|
+
});
|
|
215
|
+
// Moving it back to the old values must not itself animate: only the
|
|
216
|
+
// release next frame should.
|
|
217
|
+
for (const animation of animationsOf(element)) {
|
|
218
|
+
if (!running.includes(animation)) animation.cancel();
|
|
219
|
+
}
|
|
220
|
+
frame = requestAnimationFrame(() => {
|
|
221
|
+
frame = null;
|
|
222
|
+
for (const [property, value] of restore) inline[property] = value;
|
|
223
|
+
});
|
|
224
|
+
} else if (visible) {
|
|
225
|
+
queueMicrotask(() => {
|
|
226
|
+
if (!cancelled) setPhase("entering");
|
|
227
|
+
});
|
|
228
|
+
frame = requestAnimationFrame(() => {
|
|
229
|
+
frame = null;
|
|
230
|
+
setPhase("visible");
|
|
231
|
+
});
|
|
232
|
+
} else {
|
|
233
|
+
// A microtask later, once any element arriving in the same flush has
|
|
234
|
+
// had its chance to take the snapshot over.
|
|
235
|
+
queueMicrotask(() => {
|
|
236
|
+
if (cancelled) return;
|
|
237
|
+
if (!scope.snapshots.has(key)) {
|
|
238
|
+
setPhase("hidden");
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
scope.snapshots.delete(key);
|
|
242
|
+
element.setAttribute("data-exiting", "");
|
|
243
|
+
setPhase("exiting");
|
|
244
|
+
const duration = exitDuration(element);
|
|
245
|
+
if (duration <= 0) {
|
|
246
|
+
setPhase("hidden");
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
timer = setTimeout(() => {
|
|
250
|
+
timer = null;
|
|
251
|
+
setPhase("hidden");
|
|
252
|
+
}, duration);
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return () => {
|
|
257
|
+
cancelled = true;
|
|
258
|
+
if (frame !== null) cancelAnimationFrame(frame);
|
|
259
|
+
if (timer !== null) clearTimeout(timer);
|
|
260
|
+
};
|
|
261
|
+
},
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return {
|
|
266
|
+
phase,
|
|
267
|
+
isRendered: () => isVisible() || phase() !== "hidden",
|
|
268
|
+
elementProps: {
|
|
269
|
+
"data-entering": () => phase() === "entering",
|
|
270
|
+
"data-exiting": () => phase() === "exiting",
|
|
271
|
+
},
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** The enclosing {@link SharedElementTransition}'s scope. */
|
|
276
|
+
export function useSharedElementScope(): SharedElementScope {
|
|
277
|
+
const scope = getContext(SharedElementContext);
|
|
278
|
+
if (scope === null || scope === undefined) {
|
|
279
|
+
throw new Error("A SharedElement must be rendered inside a SharedElementTransition.");
|
|
280
|
+
}
|
|
281
|
+
return scope;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export interface SharedElementComponentProps extends StyleProps {
|
|
285
|
+
/** Elements of one name, in one scope, are one element moving. */
|
|
286
|
+
name: string;
|
|
287
|
+
/** @default true */
|
|
288
|
+
isVisible?: boolean;
|
|
289
|
+
children?: Child;
|
|
290
|
+
ref?: RefTarget<HTMLDivElement>;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** An element that moves, rather than reappears, when it changes parent. */
|
|
294
|
+
export function SharedElement(props: SharedElementComponentProps) {
|
|
295
|
+
const scope = useSharedElementScope();
|
|
296
|
+
const domRef = makeRef<HTMLDivElement>();
|
|
297
|
+
const { isRendered, elementProps } = sharedElement(
|
|
298
|
+
{ name: () => props.name, isVisible: () => props.isVisible, ref: domRef },
|
|
299
|
+
scope,
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
const forwarded = mergeProps(
|
|
303
|
+
filterDOMProps(fromProps(cells(props)), { global: true, labelable: true }),
|
|
304
|
+
styleProps(cells(props)),
|
|
305
|
+
elementProps,
|
|
306
|
+
{ "data-testid": () => props["data-testid"] },
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
return (
|
|
310
|
+
<Show when={isRendered()}>
|
|
311
|
+
<div {...forwarded} ref={mergeRefs(domRef.set, props.ref)}>
|
|
312
|
+
{props.children}
|
|
313
|
+
</div>
|
|
314
|
+
</Show>
|
|
315
|
+
);
|
|
316
|
+
}
|