@barqjs/aria 0.0.0-stage → 0.2.0-next.0
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 +293 -2
- package/package.json +347 -4
- package/src/breadcrumbs.tsx +296 -0
- package/src/button.tsx +342 -0
- package/src/calendar.tsx +1389 -0
- package/src/checkbox.tsx +570 -0
- package/src/collections.ts +976 -0
- package/src/color.ts +562 -0
- package/src/colorpicker.tsx +1478 -0
- package/src/combobox.tsx +2481 -0
- package/src/command.tsx +1184 -0
- package/src/date.ts +652 -0
- package/src/datefield.tsx +951 -0
- package/src/datepicker.tsx +556 -0
- package/src/dialog.tsx +890 -0
- package/src/disclosure.tsx +659 -0
- package/src/dom.ts +706 -0
- package/src/drawer.ts +674 -0
- package/src/focus.ts +1370 -0
- package/src/form.tsx +94 -0
- package/src/gridlist.tsx +563 -0
- package/src/i18n.ts +823 -0
- package/src/index.ts +1270 -0
- package/src/interactions/description.ts +73 -0
- package/src/interactions/flags.ts +35 -0
- package/src/interactions/focus-events.ts +218 -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 +105 -0
- package/src/interactions/listeners.ts +78 -0
- package/src/interactions/long-press.ts +160 -0
- package/src/interactions/modality.ts +416 -0
- package/src/interactions/move.ts +257 -0
- package/src/interactions/open-link.ts +193 -0
- package/src/interactions/press.ts +898 -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 +128 -0
- package/src/link.tsx +381 -0
- package/src/listbox.tsx +721 -0
- package/src/live.ts +204 -0
- package/src/menu.tsx +2290 -0
- package/src/menubar.tsx +575 -0
- package/src/navigation.ts +735 -0
- package/src/numberfield.tsx +838 -0
- package/src/otp.ts +430 -0
- package/src/overlays.ts +1247 -0
- package/src/platform.ts +74 -0
- package/src/presence.ts +164 -0
- package/src/questionnaire.tsx +1712 -0
- package/src/radio.tsx +567 -0
- package/src/register-dom.ts +12 -0
- package/src/scroller.ts +1165 -0
- package/src/select.tsx +1665 -0
- package/src/selection.ts +1415 -0
- package/src/slider.tsx +1042 -0
- package/src/splitter.ts +1152 -0
- package/src/static-collection.ts +211 -0
- package/src/switch.tsx +186 -0
- package/src/table.tsx +0 -0
- package/src/tabs.tsx +1018 -0
- package/src/tag.tsx +452 -0
- package/src/textfield.tsx +417 -0
- package/src/toast.ts +658 -0
- package/src/toggle-input.tsx +48 -0
- package/src/toggle.ts +321 -0
- package/src/togglegroup.ts +365 -0
- package/src/toolbar.tsx +179 -0
- package/src/tooltip.tsx +657 -0
- package/src/utils.ts +758 -0
- package/src/validation.ts +447 -0
- package/src/virtualizer.tsx +674 -0
package/src/platform.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser and platform detection.
|
|
3
|
+
*
|
|
4
|
+
* Every quirk this package works around is tied to one engine or one operating
|
|
5
|
+
* system, and the workaround is wrong everywhere else: `user-select: none` on
|
|
6
|
+
* the document element is what iOS WebKit needs and what everything else pays
|
|
7
|
+
* for in style recalculation; the Meta-key keyup bug is macOS only.
|
|
8
|
+
*
|
|
9
|
+
* Each answer is computed once. `navigator` cannot change under a running
|
|
10
|
+
* page, and these are read inside event handlers on the hot path.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
function testUserAgent(re: RegExp): boolean {
|
|
14
|
+
if (typeof window === "undefined" || window.navigator === undefined) return false;
|
|
15
|
+
const brands = (
|
|
16
|
+
window.navigator as Navigator & {
|
|
17
|
+
userAgentData?: { brands?: { brand: string; version: string }[]; platform?: string };
|
|
18
|
+
}
|
|
19
|
+
).userAgentData?.brands;
|
|
20
|
+
if (Array.isArray(brands) && brands.some((b) => re.test(b.brand))) return true;
|
|
21
|
+
return re.test(window.navigator.userAgent);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function testPlatform(re: RegExp): boolean {
|
|
25
|
+
if (typeof window === "undefined" || window.navigator === undefined) return false;
|
|
26
|
+
const data = (window.navigator as Navigator & { userAgentData?: { platform?: string } })
|
|
27
|
+
.userAgentData;
|
|
28
|
+
return re.test(data?.platform ?? window.navigator.platform);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The answer, computed on the first call and kept.
|
|
33
|
+
*
|
|
34
|
+
* Not cached under a test runner: happy-dom's `navigator.userAgent` is
|
|
35
|
+
* writable, and a suite that pins a platform to exercise its branch would
|
|
36
|
+
* otherwise get whatever the first test that asked happened to see.
|
|
37
|
+
*/
|
|
38
|
+
function cached(fn: () => boolean): () => boolean {
|
|
39
|
+
if (typeof process !== "undefined" && process.env.NODE_ENV === "test") return fn;
|
|
40
|
+
|
|
41
|
+
let answer: boolean | null = null;
|
|
42
|
+
return (): boolean => {
|
|
43
|
+
if (answer === null) answer = fn();
|
|
44
|
+
return answer;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const isMac: () => boolean = cached(() => testPlatform(/^Mac/i));
|
|
49
|
+
|
|
50
|
+
export const isIPhone: () => boolean = cached(() => testPlatform(/^iPhone/i));
|
|
51
|
+
|
|
52
|
+
export const isIPad: () => boolean = cached(
|
|
53
|
+
() =>
|
|
54
|
+
testPlatform(/^iPad/i) ||
|
|
55
|
+
// iPadOS 13 and later report themselves as a Mac. Touch points are what
|
|
56
|
+
// separate the two.
|
|
57
|
+
(isMac() && navigator.maxTouchPoints > 1),
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
export const isIOS: () => boolean = cached(() => isIPhone() || isIPad());
|
|
61
|
+
|
|
62
|
+
export const isAppleDevice: () => boolean = cached(() => isMac() || isIOS());
|
|
63
|
+
|
|
64
|
+
export const isChrome: () => boolean = cached(() => testUserAgent(/Chrome|CriOS|CrMo/i));
|
|
65
|
+
|
|
66
|
+
export const isFirefox: () => boolean = cached(() => testUserAgent(/(Firefox|FxiOS)/i));
|
|
67
|
+
|
|
68
|
+
export const isAndroid: () => boolean = cached(() => testUserAgent(/Android/i));
|
|
69
|
+
|
|
70
|
+
export const isWebKit: () => boolean = cached(
|
|
71
|
+
() => testUserAgent(/AppleWebKit/i) && (isIOS() || !isChrome()),
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
export const isSafari: () => boolean = cached(() => isWebKit() && !isChrome() && !isFirefox());
|
package/src/presence.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeping something in the document long enough for it to leave.
|
|
3
|
+
*
|
|
4
|
+
* A conditional removes its content in the frame the condition turns false, so
|
|
5
|
+
* an exit animation has nothing left to animate: the enter direction plays and
|
|
6
|
+
* the exit direction is a disappearance. `presence` holds the content for
|
|
7
|
+
* exactly as long as the stylesheet is still drawing it, and marks it
|
|
8
|
+
* `data-closed` while it does, which is the hook the exit rules key off.
|
|
9
|
+
*
|
|
10
|
+
* The duration is READ, never configured. It belongs to whoever wrote the CSS,
|
|
11
|
+
* so a component library that hard-coded one would be wrong for every
|
|
12
|
+
* application that chose otherwise, and wrong under `prefers-reduced-motion`,
|
|
13
|
+
* where the answer is zero.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { type Accessor, createEffect, createSignal, untrack } from "@barqjs/core";
|
|
17
|
+
import { isServer } from "@barqjs/web";
|
|
18
|
+
|
|
19
|
+
import type { ElementRef } from "./interactions/press.ts";
|
|
20
|
+
import { access, type MaybeAccessor } from "./utils.ts";
|
|
21
|
+
|
|
22
|
+
/** A `<time>` from a computed style, in milliseconds. `"0.2s"` and `"200ms"` both. */
|
|
23
|
+
function milliseconds(value: string | undefined): number {
|
|
24
|
+
const text = value?.trim() ?? "";
|
|
25
|
+
const amount = Number.parseFloat(text);
|
|
26
|
+
if (Number.isNaN(amount)) return 0;
|
|
27
|
+
return text.endsWith("ms") ? amount : amount * 1000;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The longest of a comma-separated duration list, each with its own delay. */
|
|
31
|
+
function longest(durations: string, delays: string): number {
|
|
32
|
+
const times = durations.split(",");
|
|
33
|
+
const offsets = delays.split(",");
|
|
34
|
+
let max = 0;
|
|
35
|
+
for (let index = 0; index < times.length; index++) {
|
|
36
|
+
max = Math.max(max, milliseconds(times[index]) + milliseconds(offsets[index] ?? offsets[0]));
|
|
37
|
+
}
|
|
38
|
+
return max;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** What the cascade says about this element alone, whether or not anything has started. */
|
|
42
|
+
function declared(element: Element): number {
|
|
43
|
+
const view = element.ownerDocument.defaultView;
|
|
44
|
+
if (view === null) return 0;
|
|
45
|
+
const style = view.getComputedStyle(element);
|
|
46
|
+
return Math.max(
|
|
47
|
+
longest(style.transitionDuration, style.transitionDelay),
|
|
48
|
+
longest(style.animationDuration, style.animationDelay),
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface Animatable {
|
|
53
|
+
getAnimations?: (options?: { subtree?: boolean }) => Animation[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** What is actually running, anywhere under this element. */
|
|
57
|
+
function running(element: Element): number {
|
|
58
|
+
const source = element as Element & Animatable;
|
|
59
|
+
if (typeof source.getAnimations !== "function") return 0;
|
|
60
|
+
let max = 0;
|
|
61
|
+
for (const animation of source.getAnimations({ subtree: true })) {
|
|
62
|
+
const end = animation.effect?.getComputedTiming().endTime;
|
|
63
|
+
// An `endTime` of Infinity is a spinner somewhere in the subtree, not this
|
|
64
|
+
// element leaving. Waiting for one would keep the overlay forever.
|
|
65
|
+
if (typeof end !== "number" || !Number.isFinite(end)) continue;
|
|
66
|
+
max = Math.max(max, end);
|
|
67
|
+
}
|
|
68
|
+
return max;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* How long this element still has to be drawn for.
|
|
73
|
+
*
|
|
74
|
+
* Both measures, because neither alone is enough. The cascade covers a
|
|
75
|
+
* transition that is DECLARED but has not started yet, which is the disclosure
|
|
76
|
+
* panel's case. `getAnimations` covers an animation declared on a DESCENDANT,
|
|
77
|
+
* which is every menu and listbox: `@barqjs/aria` wraps them in a popover, so
|
|
78
|
+
* the element marked `data-closed` is the wrapper and the animation is on the
|
|
79
|
+
* list inside it. Reading either forces the pending style change, so the
|
|
80
|
+
* attribute set a line earlier is already in effect.
|
|
81
|
+
*
|
|
82
|
+
* Zero when nothing is animating, which is the answer for a plain overlay and
|
|
83
|
+
* the answer under `prefers-reduced-motion`.
|
|
84
|
+
*/
|
|
85
|
+
export function exitDuration(element: Element): number {
|
|
86
|
+
return Math.max(declared(element), running(element));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** What an exit rule selects on, written before the element is measured. */
|
|
90
|
+
const CLOSED = "data-closed";
|
|
91
|
+
|
|
92
|
+
export interface PresenceOptions {
|
|
93
|
+
isOpen: MaybeAccessor<boolean | undefined>;
|
|
94
|
+
/** The element whose own animations decide how long leaving takes. */
|
|
95
|
+
ref: ElementRef;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface PresenceResult {
|
|
99
|
+
/** Whether the content belongs in the document at all. */
|
|
100
|
+
isPresent: Accessor<boolean>;
|
|
101
|
+
/** True while it is on its way out. Write it as `data-closed`. */
|
|
102
|
+
isExiting: Accessor<boolean>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* `isPresent` stays true after `isOpen` turns false, until the element has
|
|
107
|
+
* finished animating out.
|
|
108
|
+
*
|
|
109
|
+
* `data-closed` is written STRAIGHT onto the element and the duration read back
|
|
110
|
+
* in the same tick, because that is what an exit rule is selected by: measuring
|
|
111
|
+
* before the attribute lands reads the enter animation, and measuring a frame
|
|
112
|
+
* later means every close without an animation costs a frame it should not.
|
|
113
|
+
* Reading a computed style flushes the pending change, so one tick is enough.
|
|
114
|
+
*/
|
|
115
|
+
export function presence(options: PresenceOptions): PresenceResult {
|
|
116
|
+
const open = (): boolean => access(options.isOpen) === true;
|
|
117
|
+
const [isPresent, setIsPresent] = createSignal(open());
|
|
118
|
+
const [isExiting, setIsExiting] = createSignal(false);
|
|
119
|
+
|
|
120
|
+
if (isServer) return { isPresent: open, isExiting: () => false };
|
|
121
|
+
|
|
122
|
+
// Not a signal read inside the effect: an effect that depended on what it
|
|
123
|
+
// sets would re-run itself, and this one has to fire on `isOpen` alone.
|
|
124
|
+
let mounted = open();
|
|
125
|
+
|
|
126
|
+
// The open state is read in the compute half, which the graph keeps
|
|
127
|
+
// consistent, rather than inside an effect's own first run: one opened from
|
|
128
|
+
// `onSettled` was read there a pass too early and never seen again.
|
|
129
|
+
createEffect(open, (isOpen) => {
|
|
130
|
+
if (isOpen) {
|
|
131
|
+
mounted = true;
|
|
132
|
+
setIsExiting(false);
|
|
133
|
+
setIsPresent(true);
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
if (!mounted) return undefined;
|
|
137
|
+
|
|
138
|
+
const leave = (): void => {
|
|
139
|
+
mounted = false;
|
|
140
|
+
setIsExiting(false);
|
|
141
|
+
setIsPresent(false);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const element = untrack(() => access(options.ref)) as Element | null;
|
|
145
|
+
if (element === null || element === undefined) {
|
|
146
|
+
leave();
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
element.setAttribute(CLOSED, "");
|
|
151
|
+
const duration = exitDuration(element);
|
|
152
|
+
if (duration <= 0) {
|
|
153
|
+
element.removeAttribute(CLOSED);
|
|
154
|
+
leave();
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
setIsExiting(true);
|
|
159
|
+
const timer = setTimeout(leave, duration);
|
|
160
|
+
return () => clearTimeout(timer);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
return { isPresent, isExiting };
|
|
164
|
+
}
|