@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
package/src/drawer.ts
ADDED
|
@@ -0,0 +1,674 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A drawer's drag: how far it has been pulled, and whether letting go closes it.
|
|
3
|
+
*
|
|
4
|
+
* This is the piece `@barqjs/ui`'s `<Drawer>` needed. shadcn's is the `vaul`
|
|
5
|
+
* package, which is React hooks over Radix, so the engine is written here and
|
|
6
|
+
* only the behaviour is upstream's.
|
|
7
|
+
*
|
|
8
|
+
* A drawer is a modal like any other until a pointer touches it. What makes it
|
|
9
|
+
* a drawer is that the pointer can take it away, and four things about that are
|
|
10
|
+
* decisions rather than details.
|
|
11
|
+
*
|
|
12
|
+
* **The threshold is a fraction of the drawer, not a distance.** A 100px pull
|
|
13
|
+
* on a 200px drawer is halfway and means it; the same pull on a full-height one
|
|
14
|
+
* is a nudge. A constant would make tall drawers impossible to close and short
|
|
15
|
+
* ones impossible to keep.
|
|
16
|
+
*
|
|
17
|
+
* **A flick closes it however far it got.** Someone who throws the drawer down
|
|
18
|
+
* has already decided, and making them cross a distance first turns a gesture
|
|
19
|
+
* into a haul. That is why the speed is measured and not only the offset.
|
|
20
|
+
*
|
|
21
|
+
* **Pulling the wrong way is damped rather than blocked.** A drawer that will
|
|
22
|
+
* not move at all reads as broken; one that moves an eighth as far says "this
|
|
23
|
+
* is as open as it gets" and springs back.
|
|
24
|
+
*
|
|
25
|
+
* **A drag that starts inside something scrolled is a scroll.** This is the
|
|
26
|
+
* bug every hand-written sheet has: the list inside scrolls to the middle, the
|
|
27
|
+
* next pull down takes the whole drawer with it, and the content the person was
|
|
28
|
+
* reading leaves the screen. `canDragFrom` is what stops it.
|
|
29
|
+
*
|
|
30
|
+
* **A snap point is how much of the SCREEN the drawer covers.** `0.5` is half
|
|
31
|
+
* the viewport and `"148px"` is that many pixels, which is Vaul's meaning. What
|
|
32
|
+
* differs is a drawer shorter than its snap point: Vaul positions against the
|
|
33
|
+
* viewport alone and pushes such a drawer part way off screen, and this one is
|
|
34
|
+
* simply as open as it gets.
|
|
35
|
+
*
|
|
36
|
+
* **Letting go between snap points reads the whole gesture, not its last
|
|
37
|
+
* sample.** A flick's final few milliseconds are noise, and "which point did
|
|
38
|
+
* they mean" is a question about the pull from start to finish. Closing a
|
|
39
|
+
* drawer that has no snap points still reads the last sample, because there a
|
|
40
|
+
* pull back at the end is a change of mind.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
import { createSignal, type Accessor } from "@barqjs/core";
|
|
44
|
+
|
|
45
|
+
import { access, type MaybeAccessor } from "./utils.ts";
|
|
46
|
+
|
|
47
|
+
export type DrawerDirection = "top" | "bottom" | "left" | "right";
|
|
48
|
+
|
|
49
|
+
/** A fraction of the viewport, or a length in pixels written `"148px"`. */
|
|
50
|
+
export type DrawerSnapPoint = number | string;
|
|
51
|
+
|
|
52
|
+
/** How much of the drawer has to be pulled away before letting go closes it. */
|
|
53
|
+
const THRESHOLD = 0.25;
|
|
54
|
+
/** px per ms past which a flick closes it whatever the offset. */
|
|
55
|
+
const VELOCITY = 0.4;
|
|
56
|
+
/** How much of a pull the wrong way is kept. */
|
|
57
|
+
const DAMPING = 8;
|
|
58
|
+
/** px per ms past which a throw skips every snap point in between. */
|
|
59
|
+
const THROW = 2;
|
|
60
|
+
/** The share of the viewport a pull stays under to count as a flick between points. */
|
|
61
|
+
const FLICK_REACH = 0.4;
|
|
62
|
+
/** How much of the viewport shows around the page while a drawer is over it. */
|
|
63
|
+
const WINDOW_OFFSET = 26;
|
|
64
|
+
const BACKGROUND_RADIUS = 8;
|
|
65
|
+
const BACKGROUND_SHIFT = 14;
|
|
66
|
+
/** How far a drawer steps back when another opens over it. */
|
|
67
|
+
const NESTED_DISPLACEMENT = 16;
|
|
68
|
+
const SETTLE_EASE = "cubic-bezier(0.32, 0.72, 0, 1)";
|
|
69
|
+
|
|
70
|
+
/** How much of the screen a snap point shows, in px. */
|
|
71
|
+
export function snapPointExtent(point: DrawerSnapPoint, viewport: number): number {
|
|
72
|
+
if (typeof point === "string") return Number.parseInt(point, 10) || 0;
|
|
73
|
+
return point * viewport;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* How far from fully open a drawer of `size` rests at a snap point.
|
|
78
|
+
*
|
|
79
|
+
* Never negative: a drawer shorter than what the snap point asks for cannot
|
|
80
|
+
* show more than itself.
|
|
81
|
+
*/
|
|
82
|
+
export function snapPointOffset(point: DrawerSnapPoint, viewport: number, size: number): number {
|
|
83
|
+
return Math.max(0, size - snapPointExtent(point, viewport));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface DrawerDragOptions {
|
|
87
|
+
/** Which edge the drawer is attached to. @default "bottom" */
|
|
88
|
+
direction?: MaybeAccessor<DrawerDirection | undefined>;
|
|
89
|
+
/** The fraction that closes it. @default 0.25 */
|
|
90
|
+
threshold?: MaybeAccessor<number | undefined>;
|
|
91
|
+
/** The speed that closes it, in px/ms. @default 0.4 */
|
|
92
|
+
velocity?: MaybeAccessor<number | undefined>;
|
|
93
|
+
/**
|
|
94
|
+
* Whether a pull can close it. @default true
|
|
95
|
+
*
|
|
96
|
+
* Without snap points a drawer that cannot be dismissed does not move at
|
|
97
|
+
* all; with them it moves between the points and stops at the first.
|
|
98
|
+
*/
|
|
99
|
+
dismissible?: MaybeAccessor<boolean | undefined>;
|
|
100
|
+
/** Where it may rest, smallest first. */
|
|
101
|
+
snapPoints?: MaybeAccessor<readonly DrawerSnapPoint[] | undefined>;
|
|
102
|
+
/** The snap point it rests at, when a caller holds it. */
|
|
103
|
+
activeSnapPoint?: MaybeAccessor<DrawerSnapPoint | null | undefined>;
|
|
104
|
+
onSnapPointChange?: (point: DrawerSnapPoint | null) => void;
|
|
105
|
+
/**
|
|
106
|
+
* The snap point from which the page behind is fully dimmed.
|
|
107
|
+
* @default the last one
|
|
108
|
+
*/
|
|
109
|
+
fadeFromIndex?: MaybeAccessor<number | undefined>;
|
|
110
|
+
/** A throw moves one snap point at a time instead of to the end. */
|
|
111
|
+
snapToSequentialPoint?: MaybeAccessor<boolean | undefined>;
|
|
112
|
+
/** The viewport's extent along the drawer's axis. @default the window's */
|
|
113
|
+
viewport?: () => number;
|
|
114
|
+
/** @default Date.now */
|
|
115
|
+
now?: () => number;
|
|
116
|
+
/** Called on every move with how much of the drawer has been pulled away, 0 to 1. */
|
|
117
|
+
onDrag?: (progress: number) => void;
|
|
118
|
+
/** Called on letting go, with whether the drawer stayed open. */
|
|
119
|
+
onRelease?: (open: boolean) => void;
|
|
120
|
+
onClose?: () => void;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface DrawerDrag {
|
|
124
|
+
/** How far it has been pulled toward its edge, in px. Never negative. */
|
|
125
|
+
readonly offset: Accessor<number>;
|
|
126
|
+
readonly isDragging: Accessor<boolean>;
|
|
127
|
+
/** `size` is the drawer's extent along the axis, which the threshold is of. */
|
|
128
|
+
start(point: { readonly x: number; readonly y: number }, size: number): void;
|
|
129
|
+
move(point: { readonly x: number; readonly y: number }): void;
|
|
130
|
+
/** Ends the drag, and answers whether it closed the drawer. */
|
|
131
|
+
end(): boolean;
|
|
132
|
+
/** Ends it where it started: a cancelled pointer took no decision. */
|
|
133
|
+
cancel(): void;
|
|
134
|
+
/** Tell it the drawer's extent along its axis, which a snap point is placed against. */
|
|
135
|
+
measure(size: number): void;
|
|
136
|
+
readonly activeSnapPoint: Accessor<DrawerSnapPoint | null>;
|
|
137
|
+
readonly activeSnapIndex: Accessor<number | null>;
|
|
138
|
+
setActiveSnapPoint(point: DrawerSnapPoint | null): void;
|
|
139
|
+
/** How far from fully open it rests, in px: the active snap point's offset. */
|
|
140
|
+
readonly rest: Accessor<number>;
|
|
141
|
+
/** How far from fully open it is right now: `rest` plus the pull. */
|
|
142
|
+
readonly position: Accessor<number>;
|
|
143
|
+
/** How much of the drawer is off screen, 0 to 1. */
|
|
144
|
+
readonly progress: Accessor<number>;
|
|
145
|
+
/** How dim the page behind should be, 0 to 1. */
|
|
146
|
+
readonly overlayOpacity: Accessor<number>;
|
|
147
|
+
/** What pressing the handle does: the next snap point, or closing after the last. */
|
|
148
|
+
cycle(): void;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Along which axis the drawer moves, and which way counts as closing. */
|
|
152
|
+
function axisOf(direction: DrawerDirection): { vertical: boolean; sign: number } {
|
|
153
|
+
return {
|
|
154
|
+
vertical: direction === "top" || direction === "bottom",
|
|
155
|
+
// A bottom drawer closes downward, so a growing y is a growing offset.
|
|
156
|
+
sign: direction === "bottom" || direction === "right" ? 1 : -1,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function drawerDrag(options: DrawerDragOptions = {}): DrawerDrag {
|
|
161
|
+
const [trackOffset, setOffset] = createSignal(0);
|
|
162
|
+
const [dragging, setDragging] = createSignal(false);
|
|
163
|
+
const [trackSize, setSize] = createSignal(0);
|
|
164
|
+
const [trackSnap, setSnap] = createSignal<DrawerSnapPoint | null>(null);
|
|
165
|
+
// The gesture's own copy of each. A pointer stream is read in the same tick
|
|
166
|
+
// it is written, and a signal write is not published until the flush, so the
|
|
167
|
+
// move after a `start` would find the drag not yet begun.
|
|
168
|
+
let isDragging = false;
|
|
169
|
+
let from = { x: 0, y: 0 };
|
|
170
|
+
let size = 0;
|
|
171
|
+
let speed = 0;
|
|
172
|
+
let lastAt = 0;
|
|
173
|
+
let startedAt = 0;
|
|
174
|
+
let lastOffset = 0;
|
|
175
|
+
let chosen: DrawerSnapPoint | null | undefined;
|
|
176
|
+
|
|
177
|
+
const now = (): number => (options.now ?? Date.now)();
|
|
178
|
+
const direction = (): DrawerDirection => access(options.direction) ?? "bottom";
|
|
179
|
+
const dismissible = (): boolean => access(options.dismissible) !== false;
|
|
180
|
+
const points = (): readonly DrawerSnapPoint[] => access(options.snapPoints) ?? [];
|
|
181
|
+
|
|
182
|
+
const viewport = (): number => {
|
|
183
|
+
if (options.viewport !== undefined) return options.viewport();
|
|
184
|
+
if (typeof window === "undefined") return 0;
|
|
185
|
+
return axisOf(direction()).vertical ? window.innerHeight : window.innerWidth;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// The offset is where the pointer IS, and the gesture paints from it in the
|
|
189
|
+
// same handler that moved it, so a read answers from the gesture's copy and
|
|
190
|
+
// the signal is only what tells a reader to look again.
|
|
191
|
+
const offset = (): number => {
|
|
192
|
+
trackOffset();
|
|
193
|
+
return lastOffset;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const activeSnapPoint = (): DrawerSnapPoint | null => {
|
|
197
|
+
trackSnap();
|
|
198
|
+
const all = points();
|
|
199
|
+
if (all.length === 0) return null;
|
|
200
|
+
const held = access(options.activeSnapPoint);
|
|
201
|
+
if (held !== undefined) return held;
|
|
202
|
+
if (chosen !== undefined && (chosen === null || all.includes(chosen))) return chosen;
|
|
203
|
+
return all[0] ?? null;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const activeSnapIndex = (): number | null => {
|
|
207
|
+
const active = activeSnapPoint();
|
|
208
|
+
if (active === null) return null;
|
|
209
|
+
const index = points().indexOf(active);
|
|
210
|
+
return index < 0 ? null : index;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const offsets = (): number[] => {
|
|
214
|
+
trackSize();
|
|
215
|
+
const extent = viewport();
|
|
216
|
+
return points().map((point) => snapPointOffset(point, extent, size));
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const rest = (): number => {
|
|
220
|
+
trackSize();
|
|
221
|
+
const active = activeSnapPoint();
|
|
222
|
+
return active === null ? 0 : snapPointOffset(active, viewport(), size);
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const position = (): number => rest() + offset();
|
|
226
|
+
|
|
227
|
+
const progress = (): number => {
|
|
228
|
+
trackSize();
|
|
229
|
+
if (size <= 0) return 0;
|
|
230
|
+
return Math.min(1, Math.max(0, position() / size));
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const overlayOpacity = (): number => {
|
|
234
|
+
const all = offsets();
|
|
235
|
+
if (all.length === 0) return 1 - progress();
|
|
236
|
+
const fadeFrom = access(options.fadeFromIndex) ?? all.length - 1;
|
|
237
|
+
const opaque = all[fadeFrom] ?? 0;
|
|
238
|
+
const clear = fadeFrom > 0 ? (all[fadeFrom - 1] ?? size) : size;
|
|
239
|
+
if (clear <= opaque) return position() <= opaque ? 1 : 0;
|
|
240
|
+
return Math.min(1, Math.max(0, (clear - position()) / (clear - opaque)));
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const setActiveSnapPoint = (point: DrawerSnapPoint | null): void => {
|
|
244
|
+
if (point === activeSnapPoint()) return;
|
|
245
|
+
chosen = point;
|
|
246
|
+
setSnap(point);
|
|
247
|
+
options.onSnapPointChange?.(point);
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
const raw = (point: { x: number; y: number }): number => {
|
|
251
|
+
const { vertical, sign } = axisOf(direction());
|
|
252
|
+
const moved = vertical ? point.y - from.y : point.x - from.x;
|
|
253
|
+
return moved * sign;
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
/** A pull toward open: free while a larger snap point is ahead, damped past the last. */
|
|
257
|
+
const towardOpen = (moved: number): number => {
|
|
258
|
+
const all = offsets();
|
|
259
|
+
if (all.length === 0) return moved / DAMPING;
|
|
260
|
+
const room = rest() - Math.min(...all);
|
|
261
|
+
return -moved <= room ? moved : -room + (moved + room) / DAMPING;
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const settle = (): void => {
|
|
265
|
+
isDragging = false;
|
|
266
|
+
lastOffset = 0;
|
|
267
|
+
setDragging(false);
|
|
268
|
+
setOffset(0);
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
const close = (): boolean => {
|
|
272
|
+
settle();
|
|
273
|
+
options.onRelease?.(false);
|
|
274
|
+
options.onClose?.();
|
|
275
|
+
return true;
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
const stay = (point?: DrawerSnapPoint): boolean => {
|
|
279
|
+
settle();
|
|
280
|
+
if (point !== undefined) setActiveSnapPoint(point);
|
|
281
|
+
options.onRelease?.(true);
|
|
282
|
+
return false;
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
/** Which snap point a release between them means, or `null` for closing. */
|
|
286
|
+
const landing = (): DrawerSnapPoint | null => {
|
|
287
|
+
const all = points();
|
|
288
|
+
const at = offsets();
|
|
289
|
+
const index = activeSnapIndex();
|
|
290
|
+
const last = all.length - 1;
|
|
291
|
+
const elapsed = now() - startedAt;
|
|
292
|
+
const velocity = elapsed > 0 ? Math.abs(lastOffset) / elapsed : 0;
|
|
293
|
+
const opening = lastOffset < 0;
|
|
294
|
+
const sequential = access(options.snapToSequentialPoint) === true;
|
|
295
|
+
|
|
296
|
+
if (!sequential && velocity > THROW) {
|
|
297
|
+
if (opening) return all[last] as DrawerSnapPoint;
|
|
298
|
+
return dismissible() ? null : (all[0] as DrawerSnapPoint);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (velocity > VELOCITY && Math.abs(lastOffset) < viewport() * FLICK_REACH && index !== null) {
|
|
302
|
+
if (opening) return all[Math.min(index + 1, last)] as DrawerSnapPoint;
|
|
303
|
+
if (index === 0) return dismissible() ? null : (all[0] as DrawerSnapPoint);
|
|
304
|
+
return all[index - 1] as DrawerSnapPoint;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const here = position();
|
|
308
|
+
let nearest = 0;
|
|
309
|
+
for (let i = 1; i < at.length; i++) {
|
|
310
|
+
if (Math.abs((at[i] as number) - here) < Math.abs((at[nearest] as number) - here)) {
|
|
311
|
+
nearest = i;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return all[nearest] as DrawerSnapPoint;
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
return {
|
|
318
|
+
offset,
|
|
319
|
+
isDragging: dragging,
|
|
320
|
+
activeSnapPoint,
|
|
321
|
+
activeSnapIndex,
|
|
322
|
+
setActiveSnapPoint,
|
|
323
|
+
rest,
|
|
324
|
+
position,
|
|
325
|
+
progress,
|
|
326
|
+
overlayOpacity,
|
|
327
|
+
measure(extent) {
|
|
328
|
+
if (extent === size) return;
|
|
329
|
+
size = extent;
|
|
330
|
+
setSize(extent);
|
|
331
|
+
},
|
|
332
|
+
start(point, extent) {
|
|
333
|
+
if (!dismissible() && points().length === 0) return;
|
|
334
|
+
from = { x: point.x, y: point.y };
|
|
335
|
+
size = extent;
|
|
336
|
+
speed = 0;
|
|
337
|
+
lastAt = now();
|
|
338
|
+
startedAt = lastAt;
|
|
339
|
+
lastOffset = 0;
|
|
340
|
+
isDragging = true;
|
|
341
|
+
setSize(extent);
|
|
342
|
+
setOffset(0);
|
|
343
|
+
setDragging(true);
|
|
344
|
+
},
|
|
345
|
+
move(point) {
|
|
346
|
+
if (!isDragging) return;
|
|
347
|
+
const moved = raw(point);
|
|
348
|
+
// Past the open position the drawer still moves, an eighth as far, so
|
|
349
|
+
// the gesture is answered without the drawer leaving its edge.
|
|
350
|
+
let next = moved >= 0 ? moved : towardOpen(moved);
|
|
351
|
+
// The first snap point is the floor of a drawer that cannot be closed.
|
|
352
|
+
if (next > 0 && !dismissible() && activeSnapIndex() === 0) next = 0;
|
|
353
|
+
const at = now();
|
|
354
|
+
const since = at - lastAt;
|
|
355
|
+
// Two moves in the same millisecond are one move, so the speed keeps
|
|
356
|
+
// its last value. The baseline still moves: leaving it behind measures
|
|
357
|
+
// the next sample from an offset the drawer no longer has, and a pull
|
|
358
|
+
// back then reads as a flick forward.
|
|
359
|
+
if (since > 0) {
|
|
360
|
+
speed = (next - lastOffset) / since;
|
|
361
|
+
lastAt = at;
|
|
362
|
+
}
|
|
363
|
+
lastOffset = next;
|
|
364
|
+
setOffset(next);
|
|
365
|
+
options.onDrag?.(progress());
|
|
366
|
+
},
|
|
367
|
+
end() {
|
|
368
|
+
if (!isDragging) return false;
|
|
369
|
+
if (points().length > 0) {
|
|
370
|
+
const target = landing();
|
|
371
|
+
return target === null ? close() : stay(target);
|
|
372
|
+
}
|
|
373
|
+
const far = lastOffset > size * (access(options.threshold) ?? THRESHOLD);
|
|
374
|
+
const limit = access(options.velocity) ?? VELOCITY;
|
|
375
|
+
// Two measures, and a flick passes both. The last move says the hand was
|
|
376
|
+
// still going toward closed, which a pull that turned back was not. The
|
|
377
|
+
// whole gesture says how fast it was, which is vaul's measure and the
|
|
378
|
+
// only one that falls while a hand rests on the drawer before letting go.
|
|
379
|
+
const elapsed = now() - startedAt;
|
|
380
|
+
const overall = elapsed > 0 ? lastOffset / elapsed : speed;
|
|
381
|
+
const fast = speed > limit && overall > limit;
|
|
382
|
+
return far || fast ? close() : stay();
|
|
383
|
+
},
|
|
384
|
+
cancel() {
|
|
385
|
+
settle();
|
|
386
|
+
},
|
|
387
|
+
cycle() {
|
|
388
|
+
if (isDragging) return;
|
|
389
|
+
const all = points();
|
|
390
|
+
if (all.length === 0) {
|
|
391
|
+
if (dismissible()) options.onClose?.();
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
const index = activeSnapIndex();
|
|
395
|
+
if (index === null) return;
|
|
396
|
+
if (index === all.length - 1) {
|
|
397
|
+
if (dismissible()) options.onClose?.();
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
setActiveSnapPoint(all[index + 1] as DrawerSnapPoint);
|
|
401
|
+
},
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Whether a drag starting at `target` belongs to the drawer or to a scroller
|
|
407
|
+
* inside it.
|
|
408
|
+
*
|
|
409
|
+
* Walking up from the target to the drawer, the first element that can scroll
|
|
410
|
+
* along the drag's axis takes the gesture unless it is already at the edge the
|
|
411
|
+
* drag would take it past. A list scrolled to the middle keeps the pull; the
|
|
412
|
+
* same list at its top hands it over, which is what makes a drawer with a long
|
|
413
|
+
* body still closable by pulling its content.
|
|
414
|
+
*/
|
|
415
|
+
export function canDragFrom(
|
|
416
|
+
target: Element | null,
|
|
417
|
+
content: Element,
|
|
418
|
+
direction: DrawerDirection,
|
|
419
|
+
): boolean {
|
|
420
|
+
const { vertical, sign } = axisOf(direction);
|
|
421
|
+
let at: Element | null = target;
|
|
422
|
+
|
|
423
|
+
while (at !== null && at !== content) {
|
|
424
|
+
const scrolled = vertical ? at.scrollTop : at.scrollLeft;
|
|
425
|
+
const extent = vertical ? at.scrollHeight - at.clientHeight : at.scrollWidth - at.clientWidth;
|
|
426
|
+
if (extent > 0) {
|
|
427
|
+
// A bottom drawer is closed by dragging down, which scrolls the list up,
|
|
428
|
+
// so the list only gives up the gesture at its own start.
|
|
429
|
+
return sign > 0 ? scrolled <= 0 : scrolled >= extent;
|
|
430
|
+
}
|
|
431
|
+
at = at.parentElement;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
return true;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** A number as CSS writes it: no float noise, no trailing zeros. */
|
|
438
|
+
function tidy(value: number): string {
|
|
439
|
+
return String(Math.round(value * 10000) / 10000);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function along(direction: DrawerDirection, distance: string): string {
|
|
443
|
+
return axisOf(direction).vertical
|
|
444
|
+
? `translate3d(0, ${distance}, 0)`
|
|
445
|
+
: `translate3d(${distance}, 0, 0)`;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* What the page behind a drawer looks like, `progress` of the way to closed.
|
|
450
|
+
*
|
|
451
|
+
* Scaled down so a strip of the viewport shows around it and its corners
|
|
452
|
+
* round, which is what makes the drawer read as a sheet laid over a card
|
|
453
|
+
* rather than a panel on a page.
|
|
454
|
+
*/
|
|
455
|
+
export function drawerBackgroundStyle(
|
|
456
|
+
direction: DrawerDirection,
|
|
457
|
+
viewportWidth: number,
|
|
458
|
+
progress: number,
|
|
459
|
+
): { transform: string; borderRadius: string } {
|
|
460
|
+
const resting = viewportWidth > 0 ? (viewportWidth - WINDOW_OFFSET) / viewportWidth : 1;
|
|
461
|
+
const scale = Math.min(resting + progress * (1 - resting), 1);
|
|
462
|
+
const shift = Math.max(0, BACKGROUND_SHIFT - progress * BACKGROUND_SHIFT);
|
|
463
|
+
return {
|
|
464
|
+
transform: `scale(${tidy(scale)}) ${along(direction, `${tidy(shift)}px`)}`,
|
|
465
|
+
borderRadius: `${tidy(BACKGROUND_RADIUS - progress * BACKGROUND_RADIUS)}px`,
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* The transform of a drawer that has another open over it, with the upper one
|
|
471
|
+
* `progress` of the way to closed.
|
|
472
|
+
*/
|
|
473
|
+
export function nestedDrawerTransform(
|
|
474
|
+
direction: DrawerDirection,
|
|
475
|
+
viewportWidth: number,
|
|
476
|
+
progress: number,
|
|
477
|
+
): string {
|
|
478
|
+
const resting = viewportWidth > 0 ? (viewportWidth - NESTED_DISPLACEMENT) / viewportWidth : 1;
|
|
479
|
+
const scale = resting + progress * (1 - resting);
|
|
480
|
+
const shift = -NESTED_DISPLACEMENT + progress * NESTED_DISPLACEMENT;
|
|
481
|
+
return `scale(${tidy(scale)}) ${along(direction, `${tidy(shift)}px`)}`;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/** The element an application marked as the page a drawer scales. */
|
|
485
|
+
const WRAPPER = "[data-drawer-wrapper], [data-vaul-drawer-wrapper], [vaul-drawer-wrapper]";
|
|
486
|
+
|
|
487
|
+
export interface DrawerBackgroundOptions {
|
|
488
|
+
direction?: MaybeAccessor<DrawerDirection | undefined>;
|
|
489
|
+
/** Paint the body black behind the scaled page. @default true */
|
|
490
|
+
setBackgroundColor?: MaybeAccessor<boolean | undefined>;
|
|
491
|
+
/** How long the page takes to scale, in ms. @default 500 */
|
|
492
|
+
duration?: number;
|
|
493
|
+
/** The viewport's width. @default the window's */
|
|
494
|
+
viewport?: () => number;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
export interface DrawerBackground {
|
|
498
|
+
/** Scale the page down. A second call while it is scaled does nothing. */
|
|
499
|
+
open(): void;
|
|
500
|
+
/** Follow a pull, `progress` of the way to closed, with no transition. */
|
|
501
|
+
drag(progress: number): void;
|
|
502
|
+
/** Go back to the open look after a pull that did not close. */
|
|
503
|
+
settle(): void;
|
|
504
|
+
/**
|
|
505
|
+
* Scale the page back, then put it exactly as it was found.
|
|
506
|
+
*
|
|
507
|
+
* The transform goes at once and everything else after the transition: the
|
|
508
|
+
* transition is what carries the page back, and the black behind it must
|
|
509
|
+
* outlast the corners that are still rounding out.
|
|
510
|
+
*/
|
|
511
|
+
close(): void;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* The page behind a drawer, scaled while the drawer is open.
|
|
516
|
+
*
|
|
517
|
+
* The page is whichever element carries `data-drawer-wrapper` (Vaul's
|
|
518
|
+
* `data-vaul-drawer-wrapper` is read too), found when the drawer opens rather
|
|
519
|
+
* than when this is called, because the wrapper is usually an ancestor that
|
|
520
|
+
* does not exist yet. With none on the page every method is a no-op.
|
|
521
|
+
*/
|
|
522
|
+
export function drawerBackground(options: DrawerBackgroundOptions = {}): DrawerBackground {
|
|
523
|
+
const KEYS = [
|
|
524
|
+
"transform",
|
|
525
|
+
"transformOrigin",
|
|
526
|
+
"transitionProperty",
|
|
527
|
+
"transitionDuration",
|
|
528
|
+
"transitionTimingFunction",
|
|
529
|
+
"borderRadius",
|
|
530
|
+
"overflow",
|
|
531
|
+
] as const;
|
|
532
|
+
let wrapper: HTMLElement | null = null;
|
|
533
|
+
let before: Record<(typeof KEYS)[number], string> | null = null;
|
|
534
|
+
let bodyBefore: string | null = null;
|
|
535
|
+
let restoring: ReturnType<typeof setTimeout> | undefined;
|
|
536
|
+
const duration = options.duration ?? 500;
|
|
537
|
+
|
|
538
|
+
const direction = (): DrawerDirection => access(options.direction) ?? "bottom";
|
|
539
|
+
const width = (): number =>
|
|
540
|
+
options.viewport?.() ?? (typeof window === "undefined" ? 0 : window.innerWidth);
|
|
541
|
+
|
|
542
|
+
const paint = (progress: number, animated: boolean): void => {
|
|
543
|
+
if (wrapper === null) return;
|
|
544
|
+
const style = drawerBackgroundStyle(direction(), width(), progress);
|
|
545
|
+
wrapper.style.transitionProperty = animated ? "transform, border-radius" : "none";
|
|
546
|
+
wrapper.style.transitionDuration = `${String(duration)}ms`;
|
|
547
|
+
wrapper.style.transitionTimingFunction = SETTLE_EASE;
|
|
548
|
+
wrapper.style.transform = style.transform;
|
|
549
|
+
wrapper.style.borderRadius = style.borderRadius;
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
return {
|
|
553
|
+
open() {
|
|
554
|
+
if (typeof document === "undefined") return;
|
|
555
|
+
if (restoring !== undefined) {
|
|
556
|
+
// Reopened on its way back: the page is still the one that was found.
|
|
557
|
+
clearTimeout(restoring);
|
|
558
|
+
restoring = undefined;
|
|
559
|
+
} else {
|
|
560
|
+
if (wrapper !== null) return;
|
|
561
|
+
wrapper = document.querySelector<HTMLElement>(WRAPPER);
|
|
562
|
+
if (wrapper === null) return;
|
|
563
|
+
const found = wrapper;
|
|
564
|
+
before = Object.fromEntries(KEYS.map((key) => [key, found.style[key]])) as Record<
|
|
565
|
+
(typeof KEYS)[number],
|
|
566
|
+
string
|
|
567
|
+
>;
|
|
568
|
+
if (access(options.setBackgroundColor) !== false) {
|
|
569
|
+
bodyBefore = document.body.style.background;
|
|
570
|
+
document.body.style.background = "black";
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
if (wrapper === null) return;
|
|
574
|
+
wrapper.style.transformOrigin = axisOf(direction()).vertical ? "top" : "left";
|
|
575
|
+
wrapper.style.overflow = "hidden";
|
|
576
|
+
paint(0, true);
|
|
577
|
+
},
|
|
578
|
+
drag(progress) {
|
|
579
|
+
paint(progress, false);
|
|
580
|
+
},
|
|
581
|
+
settle() {
|
|
582
|
+
paint(0, true);
|
|
583
|
+
},
|
|
584
|
+
close() {
|
|
585
|
+
if (wrapper === null || before === null || restoring !== undefined) return;
|
|
586
|
+
const found = wrapper;
|
|
587
|
+
const was = before;
|
|
588
|
+
found.style.transitionProperty = "transform, border-radius";
|
|
589
|
+
found.style.transform = was.transform;
|
|
590
|
+
found.style.borderRadius = was.borderRadius;
|
|
591
|
+
restoring = setTimeout(() => {
|
|
592
|
+
restoring = undefined;
|
|
593
|
+
for (const key of KEYS) found.style[key] = was[key];
|
|
594
|
+
if (bodyBefore !== null) document.body.style.background = bodyBefore;
|
|
595
|
+
wrapper = null;
|
|
596
|
+
before = null;
|
|
597
|
+
bodyBefore = null;
|
|
598
|
+
}, duration);
|
|
599
|
+
},
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
export interface DrawerKeyboardOptions {
|
|
604
|
+
/** Shorten the drawer rather than lift it. */
|
|
605
|
+
fixed?: MaybeAccessor<boolean | undefined>;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
const TEXT_INPUTS = new Set([
|
|
609
|
+
"text",
|
|
610
|
+
"search",
|
|
611
|
+
"email",
|
|
612
|
+
"url",
|
|
613
|
+
"tel",
|
|
614
|
+
"password",
|
|
615
|
+
"number",
|
|
616
|
+
"date",
|
|
617
|
+
"datetime-local",
|
|
618
|
+
"month",
|
|
619
|
+
"time",
|
|
620
|
+
"week",
|
|
621
|
+
]);
|
|
622
|
+
|
|
623
|
+
function takesText(element: Element | null): boolean {
|
|
624
|
+
if (element === null) return false;
|
|
625
|
+
if (element instanceof HTMLTextAreaElement) return true;
|
|
626
|
+
if (element instanceof HTMLInputElement) return TEXT_INPUTS.has(element.type);
|
|
627
|
+
return element instanceof HTMLElement && element.isContentEditable;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/**
|
|
631
|
+
* Keep a bottom drawer clear of an on-screen keyboard.
|
|
632
|
+
*
|
|
633
|
+
* The keyboard is what the visual viewport loses when a field inside the
|
|
634
|
+
* drawer has focus. The drawer is lifted by that much, and shortened to the
|
|
635
|
+
* viewport if it is taller; `fixed` keeps its bottom edge where it is and only
|
|
636
|
+
* shortens it. Gone again, the drawer's own size comes back. Returns the
|
|
637
|
+
* cleanup.
|
|
638
|
+
*/
|
|
639
|
+
export function drawerKeyboard(
|
|
640
|
+
element: HTMLElement,
|
|
641
|
+
options: DrawerKeyboardOptions = {},
|
|
642
|
+
): () => void {
|
|
643
|
+
const view = element.ownerDocument.defaultView;
|
|
644
|
+
const viewport = view?.visualViewport;
|
|
645
|
+
if (view === null || viewport === null || viewport === undefined) return () => {};
|
|
646
|
+
|
|
647
|
+
let resting: number | null = null;
|
|
648
|
+
const restore = (): void => {
|
|
649
|
+
element.style.height = "";
|
|
650
|
+
element.style.bottom = "";
|
|
651
|
+
};
|
|
652
|
+
|
|
653
|
+
const onResize = (): void => {
|
|
654
|
+
const keyboard = Math.max(0, view.innerHeight - viewport.height);
|
|
655
|
+
const focused = element.ownerDocument.activeElement;
|
|
656
|
+
if (keyboard === 0 || !takesText(focused) || !element.contains(focused)) {
|
|
657
|
+
restore();
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
resting ??= element.getBoundingClientRect().height;
|
|
661
|
+
if (access(options.fixed) === true) {
|
|
662
|
+
element.style.height = `${String(Math.max(0, resting - keyboard))}px`;
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
element.style.bottom = `${String(keyboard)}px`;
|
|
666
|
+
if (resting > viewport.height) element.style.height = `${String(viewport.height)}px`;
|
|
667
|
+
};
|
|
668
|
+
|
|
669
|
+
viewport.addEventListener("resize", onResize);
|
|
670
|
+
return () => {
|
|
671
|
+
viewport.removeEventListener("resize", onResize);
|
|
672
|
+
restore();
|
|
673
|
+
};
|
|
674
|
+
}
|