@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,582 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A preview: a non-modal popover that opens on hover, keyboard focus or a long
|
|
3
|
+
* press, and unlike a tooltip may hold interactive content.
|
|
4
|
+
*
|
|
5
|
+
* A link to a profile that shows the profile card while the pointer rests on
|
|
6
|
+
* it is the case. Its timing is a tooltip's, warm-up and cool-down included,
|
|
7
|
+
* and shared with the page's tooltips: moving from one preview to the next is
|
|
8
|
+
* one gesture. Four things a tooltip does not need:
|
|
9
|
+
*
|
|
10
|
+
* - **The pointer has to reach it.** A tooltip may vanish as the pointer
|
|
11
|
+
* leaves the trigger; a preview with a button in it may not. The preview
|
|
12
|
+
* stays open while the pointer is anywhere in the SAFE AREA: the trigger,
|
|
13
|
+
* the popover, and the hull joining them, so a diagonal path across the gap
|
|
14
|
+
* does not close it, whatever side the popover is on.
|
|
15
|
+
* - **The keyboard has to reach it.** Keyboard focus opens it after the delay
|
|
16
|
+
* rather than at once, so tabbing quickly down a list of links does not open
|
|
17
|
+
* and close a popover per stop. Once it is open, Tab from the trigger goes
|
|
18
|
+
* into it, and Tab out of it continues after the trigger.
|
|
19
|
+
* - **A touch has no hover.** A long press opens it, and focus moves into the
|
|
20
|
+
* popover so a touch screen reader's cursor goes with it. The long press is
|
|
21
|
+
* described to assistive technology only while the user is on touch.
|
|
22
|
+
* - **Escape closes it and gives focus back** to the trigger, which must not
|
|
23
|
+
* then reopen it on the focus it was given.
|
|
24
|
+
*
|
|
25
|
+
* The popover is `role="dialog"`. Its PLACEMENT element is the `<Popover>`
|
|
26
|
+
* this renders; the dialog is the element inside it, which is the one that
|
|
27
|
+
* takes the caller's class, style and id, as every popover part here does.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import {
|
|
31
|
+
cells,
|
|
32
|
+
type Child,
|
|
33
|
+
createContext,
|
|
34
|
+
createEffect,
|
|
35
|
+
getContext,
|
|
36
|
+
provide,
|
|
37
|
+
untrack,
|
|
38
|
+
} from "@barqjs/core";
|
|
39
|
+
import { isServer } from "@barqjs/web";
|
|
40
|
+
import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
|
|
41
|
+
import { Popover, popoverStateProps, providePopoverTrigger } from "./dialog.tsx";
|
|
42
|
+
import {
|
|
43
|
+
activeElement,
|
|
44
|
+
contains,
|
|
45
|
+
focusWithoutScrolling,
|
|
46
|
+
ownerDocument,
|
|
47
|
+
ownerWindow,
|
|
48
|
+
} from "./dom.ts";
|
|
49
|
+
import { focusableWalker, focusScope } from "./focus.ts";
|
|
50
|
+
import { focusWithin } from "./interactions/focus-events.ts";
|
|
51
|
+
import { hover } from "./interactions/hover.ts";
|
|
52
|
+
import type { BaseEvent } from "./interactions/keyboard.ts";
|
|
53
|
+
import { longPress } from "./interactions/long-press.ts";
|
|
54
|
+
import { getInteractionModality, isFocusVisible, modality } from "./interactions/modality.ts";
|
|
55
|
+
import type { ElementRef } from "./interactions/press.ts";
|
|
56
|
+
import { dismissButtonProps, type Placement } from "./overlays.ts";
|
|
57
|
+
import {
|
|
58
|
+
tooltipTriggerState,
|
|
59
|
+
type TooltipTriggerState,
|
|
60
|
+
type TooltipTriggerStateOptions,
|
|
61
|
+
} from "./tooltip.tsx";
|
|
62
|
+
import {
|
|
63
|
+
access,
|
|
64
|
+
type DOMProps,
|
|
65
|
+
filterDOMProps,
|
|
66
|
+
fromProps,
|
|
67
|
+
id,
|
|
68
|
+
type MaybeAccessor,
|
|
69
|
+
mergeProps,
|
|
70
|
+
provideTriggerSlot,
|
|
71
|
+
type StyleProps,
|
|
72
|
+
styleProps,
|
|
73
|
+
} from "./utils.ts";
|
|
74
|
+
|
|
75
|
+
/** The event a focus scope sends before it puts focus back on an element. */
|
|
76
|
+
const RESTORE_EVENT = "barq-focus-scope-restore";
|
|
77
|
+
|
|
78
|
+
const LONG_PRESS_MESSAGE = "Long press to open preview";
|
|
79
|
+
|
|
80
|
+
// ---------------------------------------------------------------------------
|
|
81
|
+
// The safe area
|
|
82
|
+
// ---------------------------------------------------------------------------
|
|
83
|
+
|
|
84
|
+
interface Point {
|
|
85
|
+
x: number;
|
|
86
|
+
y: number;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Forgiveness around both boxes, for sub-pixel jitter and the gap between them. */
|
|
90
|
+
const PADDING = 8;
|
|
91
|
+
|
|
92
|
+
function rectContains(rect: DOMRect, point: Point): boolean {
|
|
93
|
+
return (
|
|
94
|
+
point.x >= rect.left - PADDING &&
|
|
95
|
+
point.x <= rect.right + PADDING &&
|
|
96
|
+
point.y >= rect.top - PADDING &&
|
|
97
|
+
point.y <= rect.bottom + PADDING
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function rectCorners(rect: DOMRect): Point[] {
|
|
102
|
+
const left = rect.left - PADDING;
|
|
103
|
+
const right = rect.right + PADDING;
|
|
104
|
+
const top = rect.top - PADDING;
|
|
105
|
+
const bottom = rect.bottom + PADDING;
|
|
106
|
+
return [
|
|
107
|
+
{ x: left, y: top },
|
|
108
|
+
{ x: right, y: top },
|
|
109
|
+
{ x: right, y: bottom },
|
|
110
|
+
{ x: left, y: bottom },
|
|
111
|
+
];
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function cross(origin: Point, a: Point, b: Point): number {
|
|
115
|
+
return (a.x - origin.x) * (b.y - origin.y) - (a.y - origin.y) * (b.x - origin.x);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The convex hull, by the monotone chain. */
|
|
119
|
+
function convexHull(points: Point[]): Point[] {
|
|
120
|
+
const sorted = points.toSorted((a, b) => a.x - b.x || a.y - b.y);
|
|
121
|
+
if (sorted.length < 3) return sorted;
|
|
122
|
+
|
|
123
|
+
const lower: Point[] = [];
|
|
124
|
+
for (const point of sorted) {
|
|
125
|
+
while (
|
|
126
|
+
lower.length >= 2 &&
|
|
127
|
+
cross(lower[lower.length - 2] as Point, lower[lower.length - 1] as Point, point) <= 0
|
|
128
|
+
) {
|
|
129
|
+
lower.pop();
|
|
130
|
+
}
|
|
131
|
+
lower.push(point);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const upper: Point[] = [];
|
|
135
|
+
for (const point of sorted.toReversed()) {
|
|
136
|
+
while (
|
|
137
|
+
upper.length >= 2 &&
|
|
138
|
+
cross(upper[upper.length - 2] as Point, upper[upper.length - 1] as Point, point) <= 0
|
|
139
|
+
) {
|
|
140
|
+
upper.pop();
|
|
141
|
+
}
|
|
142
|
+
upper.push(point);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
lower.pop();
|
|
146
|
+
upper.pop();
|
|
147
|
+
return [...lower, ...upper];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Ray casting. */
|
|
151
|
+
function isPointInPolygon(point: Point, polygon: Point[]): boolean {
|
|
152
|
+
let inside = false;
|
|
153
|
+
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
|
154
|
+
const a = polygon[i] as Point;
|
|
155
|
+
const b = polygon[j] as Point;
|
|
156
|
+
const crosses =
|
|
157
|
+
a.y > point.y !== b.y > point.y &&
|
|
158
|
+
point.x < ((b.x - a.x) * (point.y - a.y)) / (b.y - a.y) + a.x;
|
|
159
|
+
if (crosses) inside = !inside;
|
|
160
|
+
}
|
|
161
|
+
return inside;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Whether `point` is on the trigger, on the overlay, or in the hull joining the two. */
|
|
165
|
+
export function isPointInSafeArea(
|
|
166
|
+
point: Point,
|
|
167
|
+
triggerRect: DOMRect,
|
|
168
|
+
overlayRect: DOMRect | null,
|
|
169
|
+
): boolean {
|
|
170
|
+
if (rectContains(triggerRect, point)) return true;
|
|
171
|
+
if (overlayRect === null) return false;
|
|
172
|
+
if (rectContains(overlayRect, point)) return true;
|
|
173
|
+
const hull = convexHull([...rectCorners(triggerRect), ...rectCorners(overlayRect)]);
|
|
174
|
+
return hull.length >= 3 && isPointInPolygon(point, hull);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export interface SafeAreaOptions {
|
|
178
|
+
triggerRef: ElementRef;
|
|
179
|
+
overlayRef: ElementRef;
|
|
180
|
+
isOpen: MaybeAccessor<boolean | undefined>;
|
|
181
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
182
|
+
/** Told on every pointer move, and when the pointer leaves the document. */
|
|
183
|
+
onSafeAreaChange: (isInSafeArea: boolean) => void;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Track whether the pointer is in the region joining a trigger and its
|
|
188
|
+
* overlay, while the overlay is open.
|
|
189
|
+
*/
|
|
190
|
+
export function safeArea(options: SafeAreaOptions): void {
|
|
191
|
+
if (isServer) return;
|
|
192
|
+
createEffect(
|
|
193
|
+
() =>
|
|
194
|
+
access(options.isDisabled) === true || access(options.isOpen) !== true
|
|
195
|
+
? null
|
|
196
|
+
: ((access(options.triggerRef) as Element | null) ?? null),
|
|
197
|
+
(trigger) => {
|
|
198
|
+
if (trigger === null) return undefined;
|
|
199
|
+
const onPointerMove = (event: PointerEvent): void => {
|
|
200
|
+
if (event.pointerType === "touch") return;
|
|
201
|
+
const overlay = untrack(() => access(options.overlayRef)) as Element | null;
|
|
202
|
+
options.onSafeAreaChange(
|
|
203
|
+
isPointInSafeArea(
|
|
204
|
+
{ x: event.clientX, y: event.clientY },
|
|
205
|
+
trigger.getBoundingClientRect(),
|
|
206
|
+
overlay?.getBoundingClientRect() ?? null,
|
|
207
|
+
),
|
|
208
|
+
);
|
|
209
|
+
};
|
|
210
|
+
const onPointerLeave = (): void => options.onSafeAreaChange(false);
|
|
211
|
+
|
|
212
|
+
const view = ownerWindow(trigger);
|
|
213
|
+
const root = ownerDocument(trigger).documentElement;
|
|
214
|
+
view.addEventListener("pointermove", onPointerMove);
|
|
215
|
+
root.addEventListener("pointerleave", onPointerLeave);
|
|
216
|
+
return () => {
|
|
217
|
+
view.removeEventListener("pointermove", onPointerMove);
|
|
218
|
+
root.removeEventListener("pointerleave", onPointerLeave);
|
|
219
|
+
};
|
|
220
|
+
},
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// ---------------------------------------------------------------------------
|
|
225
|
+
// The hook
|
|
226
|
+
// ---------------------------------------------------------------------------
|
|
227
|
+
|
|
228
|
+
export interface PreviewTriggerOptions {
|
|
229
|
+
/** The trigger, usually a link. */
|
|
230
|
+
triggerRef: ElementRef;
|
|
231
|
+
/** The popover's dialog element. */
|
|
232
|
+
popoverRef: ElementRef;
|
|
233
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export interface PreviewTriggerResult {
|
|
237
|
+
triggerProps: DOMProps;
|
|
238
|
+
/** For the popover's dialog element. */
|
|
239
|
+
popoverProps: DOMProps;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function isTouchCapable(): boolean {
|
|
243
|
+
return typeof window !== "undefined" && "ontouchstart" in window;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function previewTrigger(
|
|
247
|
+
options: PreviewTriggerOptions,
|
|
248
|
+
state: TooltipTriggerState,
|
|
249
|
+
): PreviewTriggerResult {
|
|
250
|
+
const popoverId = id();
|
|
251
|
+
const isDisabled = (): boolean => access(options.isDisabled) === true;
|
|
252
|
+
const trigger = (): HTMLElement | null =>
|
|
253
|
+
(access(options.triggerRef) as HTMLElement | null) ?? null;
|
|
254
|
+
const popover = (): HTMLElement | null =>
|
|
255
|
+
(access(options.popoverRef) as HTMLElement | null) ?? null;
|
|
256
|
+
|
|
257
|
+
// The focus a focus scope gives back on close must not reopen the preview.
|
|
258
|
+
let ignoreFocus = false;
|
|
259
|
+
// Opened by a long press: focus follows into the popover, for a touch screen reader.
|
|
260
|
+
let shouldFocusOnOpen = false;
|
|
261
|
+
let pointerInSafeArea = false;
|
|
262
|
+
|
|
263
|
+
const keepOpen = (): void => state.open(true);
|
|
264
|
+
|
|
265
|
+
const checkClose = (): void => {
|
|
266
|
+
if (pointerInSafeArea) return;
|
|
267
|
+
const element = trigger();
|
|
268
|
+
const active = element === null ? null : activeElement(ownerDocument(element));
|
|
269
|
+
if (isFocusVisible() && (contains(element, active) || contains(popover(), active))) return;
|
|
270
|
+
state.close();
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
if (!isServer) {
|
|
274
|
+
createEffect(
|
|
275
|
+
() => (state.isOpen() ? popover() : null),
|
|
276
|
+
(element) => {
|
|
277
|
+
if (element === null || !shouldFocusOnOpen) return;
|
|
278
|
+
shouldFocusOnOpen = false;
|
|
279
|
+
focusWithoutScrolling(element);
|
|
280
|
+
},
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
createEffect(trigger, (element) => {
|
|
284
|
+
if (element === null) return undefined;
|
|
285
|
+
const onRestore = (event: Event): void => {
|
|
286
|
+
event.preventDefault();
|
|
287
|
+
// Only for the length of the call: focus events are dispatched inside
|
|
288
|
+
// it, and a flag left for the next event would swallow a real one
|
|
289
|
+
// whenever the trigger was already focused and none came.
|
|
290
|
+
ignoreFocus = true;
|
|
291
|
+
try {
|
|
292
|
+
element.focus();
|
|
293
|
+
} finally {
|
|
294
|
+
ignoreFocus = false;
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
element.addEventListener(RESTORE_EVENT, onRestore);
|
|
298
|
+
return () => element.removeEventListener(RESTORE_EVENT, onRestore);
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const { hoverProps } = hover({
|
|
303
|
+
isDisabled,
|
|
304
|
+
onHoverStart: () => {
|
|
305
|
+
// What `tooltip.tsx` decides too: a hover that began because something
|
|
306
|
+
// covering the trigger went away is not the pointer arriving.
|
|
307
|
+
const how = getInteractionModality();
|
|
308
|
+
if (how === "keyboard" || how === "virtual") return;
|
|
309
|
+
pointerInSafeArea = true;
|
|
310
|
+
state.open();
|
|
311
|
+
},
|
|
312
|
+
onHoverEnd: () => {
|
|
313
|
+
// Before it opens, leaving cancels the warm-up. After, the safe area
|
|
314
|
+
// decides, so the pointer can cross the gap to the popover.
|
|
315
|
+
if (state.isOpen()) return;
|
|
316
|
+
pointerInSafeArea = false;
|
|
317
|
+
state.close();
|
|
318
|
+
},
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
const onFocus = (event: FocusEvent): void => {
|
|
322
|
+
if (isDisabled()) return;
|
|
323
|
+
if (ignoreFocus) return;
|
|
324
|
+
// A long press focuses the link on release, after focus already went into
|
|
325
|
+
// the popover; it goes back there.
|
|
326
|
+
const element = popover();
|
|
327
|
+
if (state.isOpen() && element !== null && event.relatedTarget === element) {
|
|
328
|
+
focusWithoutScrolling(element);
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
// After the delay, not at once: tabbing quickly through a list of links
|
|
332
|
+
// must not open, and add the Tab stops of, a preview per link.
|
|
333
|
+
if (isFocusVisible()) state.open();
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
const onKeyDown = (event: KeyboardEvent): void => {
|
|
337
|
+
const element = popover();
|
|
338
|
+
if (event.key === "Tab" && !event.shiftKey && state.isOpen() && element !== null) {
|
|
339
|
+
const first = focusableWalker(element, { tabbable: true }).nextNode() as HTMLElement | null;
|
|
340
|
+
if (first !== null) {
|
|
341
|
+
event.preventDefault();
|
|
342
|
+
first.focus();
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
} else if (event.key === "Escape") {
|
|
346
|
+
const wasOpen = state.isOpen();
|
|
347
|
+
state.close(true);
|
|
348
|
+
// An Escape that closed the preview is spent; one that closed nothing
|
|
349
|
+
// belongs to whatever is around the trigger.
|
|
350
|
+
if (wasOpen) {
|
|
351
|
+
event.preventDefault();
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
(event as Partial<BaseEvent<KeyboardEvent>>).continuePropagation?.();
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
const how = modality();
|
|
359
|
+
const shouldLongPress = (): boolean => {
|
|
360
|
+
const current = how();
|
|
361
|
+
return (current === "pointer" || current === "virtual" || current === null) && isTouchCapable();
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
const { longPressProps } = longPress({
|
|
365
|
+
isDisabled,
|
|
366
|
+
pointerType: "touch",
|
|
367
|
+
accessibilityDescription: () => (shouldLongPress() ? LONG_PRESS_MESSAGE : undefined),
|
|
368
|
+
onLongPress: () => {
|
|
369
|
+
shouldFocusOnOpen = true;
|
|
370
|
+
state.open(true);
|
|
371
|
+
},
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
safeArea({
|
|
375
|
+
triggerRef: options.triggerRef,
|
|
376
|
+
overlayRef: options.popoverRef,
|
|
377
|
+
isOpen: state.isOpen,
|
|
378
|
+
isDisabled,
|
|
379
|
+
onSafeAreaChange: (inside) => {
|
|
380
|
+
if (inside === pointerInSafeArea) return;
|
|
381
|
+
pointerInSafeArea = inside;
|
|
382
|
+
if (inside) keepOpen();
|
|
383
|
+
else checkClose();
|
|
384
|
+
},
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
const { focusWithinProps } = focusWithin({
|
|
388
|
+
onFocusWithin: keepOpen,
|
|
389
|
+
onBlurWithin: checkClose,
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
const longPressDescription = longPressProps["aria-describedby"] as
|
|
393
|
+
| MaybeAccessor<string | undefined>
|
|
394
|
+
| undefined;
|
|
395
|
+
|
|
396
|
+
return {
|
|
397
|
+
triggerProps: mergeProps(hoverProps, longPressProps, {
|
|
398
|
+
onFocus,
|
|
399
|
+
onBlur: checkClose,
|
|
400
|
+
onKeyDown,
|
|
401
|
+
"aria-haspopup": "dialog",
|
|
402
|
+
"aria-expanded": () => state.isOpen(),
|
|
403
|
+
"aria-controls": () => (state.isOpen() ? popoverId() : undefined),
|
|
404
|
+
"aria-describedby": () =>
|
|
405
|
+
[access(longPressDescription), state.isOpen() ? popoverId() : undefined]
|
|
406
|
+
.filter((each) => each !== undefined && each !== "")
|
|
407
|
+
.join(" ") || undefined,
|
|
408
|
+
}),
|
|
409
|
+
popoverProps: mergeProps(focusWithinProps, { id: popoverId }),
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// ---------------------------------------------------------------------------
|
|
414
|
+
// Components
|
|
415
|
+
// ---------------------------------------------------------------------------
|
|
416
|
+
|
|
417
|
+
export interface PreviewTriggerValue {
|
|
418
|
+
state: TooltipTriggerState;
|
|
419
|
+
triggerRef: ReturnType<typeof makeRef<HTMLElement>>;
|
|
420
|
+
popoverRef: ReturnType<typeof makeRef<HTMLElement>>;
|
|
421
|
+
/** For a trigger this package does not provide. A `<Button>` takes them itself. */
|
|
422
|
+
triggerProps: DOMProps;
|
|
423
|
+
popoverProps: DOMProps;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const PreviewTriggerContext = createContext<PreviewTriggerValue | null>(null);
|
|
427
|
+
|
|
428
|
+
/** The enclosing {@link PreviewTrigger}. */
|
|
429
|
+
export function usePreviewTrigger(): PreviewTriggerValue {
|
|
430
|
+
const value = getContext(PreviewTriggerContext);
|
|
431
|
+
if (value === null || value === undefined) {
|
|
432
|
+
throw new Error("This must be rendered inside a PreviewTrigger.");
|
|
433
|
+
}
|
|
434
|
+
return value;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
export interface PreviewTriggerComponentProps {
|
|
438
|
+
/** The trigger, and a {@link PreviewPopover}. */
|
|
439
|
+
children?: Child;
|
|
440
|
+
isOpen?: boolean;
|
|
441
|
+
defaultOpen?: boolean;
|
|
442
|
+
isDisabled?: boolean;
|
|
443
|
+
/** @default 600 */
|
|
444
|
+
delay?: number;
|
|
445
|
+
/** @default 200 */
|
|
446
|
+
closeDelay?: number;
|
|
447
|
+
onOpenChange?: (isOpen: boolean) => void;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* ```tsx
|
|
452
|
+
* <PreviewTrigger>
|
|
453
|
+
* <Link href="/people/salama">Salama</Link>
|
|
454
|
+
* <PreviewPopover aria-label="Salama's profile">
|
|
455
|
+
* <ProfileCard />
|
|
456
|
+
* </PreviewPopover>
|
|
457
|
+
* </PreviewTrigger>
|
|
458
|
+
* ```
|
|
459
|
+
*
|
|
460
|
+
* The trigger takes its props through the trigger slot, as a tooltip's does.
|
|
461
|
+
* A control that does not read the slot spreads `usePreviewTrigger().triggerProps`
|
|
462
|
+
* and sets `triggerRef` itself.
|
|
463
|
+
*/
|
|
464
|
+
export function PreviewTrigger(props: PreviewTriggerComponentProps) {
|
|
465
|
+
const triggerRef = makeRef<HTMLElement>();
|
|
466
|
+
const popoverRef = makeRef<HTMLElement>();
|
|
467
|
+
const options = fromProps(cells(props));
|
|
468
|
+
const state = tooltipTriggerState({
|
|
469
|
+
...(options as TooltipTriggerStateOptions),
|
|
470
|
+
delay: () => props.delay ?? 600,
|
|
471
|
+
closeDelay: () => props.closeDelay ?? 200,
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
const { triggerProps, popoverProps } = previewTrigger(
|
|
475
|
+
{ triggerRef, popoverRef, isDisabled: () => props.isDisabled },
|
|
476
|
+
state,
|
|
477
|
+
);
|
|
478
|
+
|
|
479
|
+
const value: PreviewTriggerValue = {
|
|
480
|
+
state,
|
|
481
|
+
triggerRef,
|
|
482
|
+
popoverRef,
|
|
483
|
+
triggerProps,
|
|
484
|
+
popoverProps,
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
const setOpen = (open: boolean): void => (open ? state.open(true) : state.close(true));
|
|
488
|
+
|
|
489
|
+
return (
|
|
490
|
+
<>
|
|
491
|
+
{provide(
|
|
492
|
+
PreviewTriggerContext,
|
|
493
|
+
() => value,
|
|
494
|
+
() => {
|
|
495
|
+
provideTriggerSlot({ props: triggerProps, ref: triggerRef.set });
|
|
496
|
+
return providePopoverTrigger(
|
|
497
|
+
{
|
|
498
|
+
triggerRef,
|
|
499
|
+
state: {
|
|
500
|
+
isOpen: state.isOpen,
|
|
501
|
+
setOpen,
|
|
502
|
+
open: () => setOpen(true),
|
|
503
|
+
close: () => setOpen(false),
|
|
504
|
+
toggle: () => setOpen(!state.isOpen()),
|
|
505
|
+
},
|
|
506
|
+
shouldContainFocus: false,
|
|
507
|
+
},
|
|
508
|
+
() => props.children,
|
|
509
|
+
);
|
|
510
|
+
},
|
|
511
|
+
)}
|
|
512
|
+
</>
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
export interface PreviewPopoverComponentProps extends StyleProps {
|
|
517
|
+
children?: Child;
|
|
518
|
+
/** @default "bottom" */
|
|
519
|
+
placement?: Placement;
|
|
520
|
+
/** @default 8 */
|
|
521
|
+
offset?: number;
|
|
522
|
+
crossOffset?: number;
|
|
523
|
+
shouldFlip?: boolean;
|
|
524
|
+
containerPadding?: number;
|
|
525
|
+
"aria-label"?: string;
|
|
526
|
+
"aria-labelledby"?: string;
|
|
527
|
+
"aria-describedby"?: string;
|
|
528
|
+
/** The dialog element. */
|
|
529
|
+
ref?: RefTarget<HTMLDivElement>;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/** The preview itself, mounted while it is open. */
|
|
533
|
+
export function PreviewPopover(props: PreviewPopoverComponentProps) {
|
|
534
|
+
return (
|
|
535
|
+
<Popover
|
|
536
|
+
placement={props.placement}
|
|
537
|
+
offset={props.offset}
|
|
538
|
+
crossOffset={props.crossOffset}
|
|
539
|
+
shouldFlip={props.shouldFlip}
|
|
540
|
+
containerPadding={props.containerPadding}
|
|
541
|
+
>
|
|
542
|
+
<PreviewBody {...props} />
|
|
543
|
+
</Popover>
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Inside the popover's `<Show>`, so the focus scope comes and goes with the
|
|
549
|
+
* content: disposing it is what gives focus back to the trigger.
|
|
550
|
+
*/
|
|
551
|
+
function PreviewBody(props: PreviewPopoverComponentProps) {
|
|
552
|
+
const preview = usePreviewTrigger();
|
|
553
|
+
const state = preview.state;
|
|
554
|
+
// Focus is given back on close, and Tab out of the preview continues after
|
|
555
|
+
// the trigger, but nothing holds focus in and nothing is focused on opening:
|
|
556
|
+
// the preview is not modal, and a hover must not take the keyboard.
|
|
557
|
+
const scope = focusScope({ restoreFocus: true });
|
|
558
|
+
|
|
559
|
+
const elementProps = mergeProps(
|
|
560
|
+
preview.popoverProps,
|
|
561
|
+
popoverStateProps(),
|
|
562
|
+
filterDOMProps(fromProps(cells(props)), { global: true, labelable: true }),
|
|
563
|
+
styleProps(cells(props)),
|
|
564
|
+
{
|
|
565
|
+
role: "dialog",
|
|
566
|
+
tabIndex: -1,
|
|
567
|
+
"data-skip-animation": state.shouldSkipAnimation,
|
|
568
|
+
"data-testid": () => props["data-testid"],
|
|
569
|
+
},
|
|
570
|
+
);
|
|
571
|
+
|
|
572
|
+
return (
|
|
573
|
+
<>
|
|
574
|
+
<span hidden ref={scope.startRef} />
|
|
575
|
+
<div {...elementProps} ref={mergeRefs(preview.popoverRef.set, props.ref)}>
|
|
576
|
+
{props.children}
|
|
577
|
+
<button type="button" {...dismissButtonProps(() => state.close(true))} />
|
|
578
|
+
</div>
|
|
579
|
+
<span hidden ref={scope.endRef} />
|
|
580
|
+
</>
|
|
581
|
+
);
|
|
582
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createContext } from "@barqjs/core";
|
|
2
|
+
import type { MaybeAccessor } from "./utils.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What a component wrapping a progress bar needs it to carry. A pending
|
|
6
|
+
* `<Button>` names itself with its progress bar, so it hands down the id.
|
|
7
|
+
*
|
|
8
|
+
* In a module of its own so that `button.tsx` can provide it without bringing
|
|
9
|
+
* the progress bar, and everything beside it, into the button's entry.
|
|
10
|
+
*/
|
|
11
|
+
export interface ProgressBarContextValue {
|
|
12
|
+
id?: MaybeAccessor<string | undefined>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const ProgressBarContext = createContext<ProgressBarContextValue | null>(null);
|