@zazz-ui/core 0.13.1 → 0.14.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/CHANGELOG.md +93 -0
- package/README.md +5 -9
- package/dist/sri.json +35 -31
- package/dist/zazz.css +1 -1
- package/dist/zazz.js +1 -1
- package/editor/zazz.css-data.json +173 -3
- package/editor/zazz.html-data.json +138 -0
- package/editor/zazz.language-data.json +313 -125
- package/editor/zazz.lint-data.json +1501 -1383
- package/package.json +1 -5
- package/src/base/_defaults.css +1 -1
- package/src/base/_reset.css +16 -2
- package/src/base/_variables.css +43 -31
- package/src/base/carousel-scroll.d.ts +161 -0
- package/src/base/carousel-scroll.d.ts.map +1 -0
- package/src/base/carousel-scroll.js +1035 -0
- package/src/base/carousel-scroll.ts +1227 -0
- package/src/base/carousel.d.ts +34 -0
- package/src/base/carousel.d.ts.map +1 -0
- package/src/base/carousel.js +279 -0
- package/src/base/carousel.ts +292 -0
- package/src/base/dialog-lifecycle.js +1 -1
- package/src/base/dialog-lifecycle.ts +1 -1
- package/src/base/gesture.d.ts +201 -0
- package/src/base/gesture.d.ts.map +1 -0
- package/src/base/gesture.js +317 -0
- package/src/base/gesture.ts +494 -0
- package/src/base/kit-styling.d.ts.map +1 -1
- package/src/base/kit-styling.js +182 -162
- package/src/base/kit-styling.ts +182 -162
- package/src/base/navigation.d.ts +12 -1
- package/src/base/navigation.d.ts.map +1 -1
- package/src/base/navigation.js +65 -1
- package/src/base/navigation.ts +66 -1
- package/src/base/utils.d.ts +1 -1
- package/src/base/utils.js +2 -2
- package/src/base/utils.ts +2 -2
- package/src/globals.d.ts +7 -30
- package/src/head.d.ts.map +1 -1
- package/src/head.js +1 -25
- package/src/head.ts +1 -25
- package/src/index.css +1 -0
- package/src/index.d.ts +6 -5
- package/src/index.d.ts.map +1 -1
- package/src/index.js +6 -5
- package/src/index.ts +6 -5
- package/src/manifest.d.ts +1 -1
- package/src/manifest.d.ts.map +1 -1
- package/src/manifest.js +25 -13
- package/src/manifest.ts +26 -14
- package/src/primitives/button/button.css +1 -1
- package/src/primitives/carousel/carousel.css +103 -20
- package/src/primitives/carousel/carousel.d.ts +4 -32
- package/src/primitives/carousel/carousel.d.ts.map +1 -1
- package/src/primitives/carousel/carousel.js +39 -14
- package/src/primitives/carousel/carousel.ts +21 -25
- package/src/primitives/dialog/dialog.css +14 -8
- package/src/primitives/drawer/drawer-indent.html +27 -0
- package/src/primitives/drawer/drawer-nested.html +72 -0
- package/src/primitives/drawer/drawer-responsive-dialog.html +44 -0
- package/src/primitives/drawer/drawer-snap.html +26 -0
- package/src/primitives/drawer/drawer.css +528 -0
- package/src/primitives/drawer/drawer.d.ts +24 -0
- package/src/primitives/drawer/drawer.d.ts.map +1 -0
- package/src/primitives/drawer/drawer.html +42 -0
- package/src/primitives/drawer/drawer.js +765 -0
- package/src/primitives/drawer/drawer.ts +823 -0
- package/src/primitives/fields/fields.css +4 -2
- package/src/primitives/input/input.css +1 -1
- package/src/primitives/input-group/input-group.css +5 -5
- package/src/primitives/lightbox/lightbox.css +2 -2
- package/src/primitives/lightbox/lightbox.d.ts.map +1 -1
- package/src/primitives/lightbox/lightbox.js +9 -8
- package/src/primitives/lightbox/lightbox.ts +9 -8
- package/src/primitives/password-group/password-group.css +5 -5
- package/src/primitives/radio/radio.css +15 -10
- package/src/primitives/slider/slider.css +2 -2
- package/src/primitives/switch/switch.css +6 -6
- package/src/primitives/tabs/tabs.css +10 -10
- package/src/primitives/toaster/toaster.css +29 -1
- package/src/primitives/toaster/toaster.d.ts.map +1 -1
- package/src/primitives/toaster/toaster.js +117 -9
- package/src/primitives/toaster/toaster.ts +136 -9
- package/src/primitives/tooltip/tooltip.css +93 -10
- package/src/base/embla.d.ts +0 -90
- package/src/base/embla.d.ts.map +0 -1
- package/src/base/embla.js +0 -878
- package/src/base/embla.ts +0 -1066
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Shared drag tracking for gesture-driven primitives (toast
|
|
5
|
+
* swipe-to-dismiss, the drawer).
|
|
6
|
+
* @description A gesture has two phases, and this module owns only the first.
|
|
7
|
+
*
|
|
8
|
+
* - **Track** (pointer down): `trackDrag()` reports the pointer's offset along a
|
|
9
|
+
* locked axis on every move. The consumer writes it straight to the dragged
|
|
10
|
+
* element's `translate`, with transitions off, so the element follows the
|
|
11
|
+
* pointer 1:1. `translate` composes with whatever `transform` the element
|
|
12
|
+
* already has, and writing it on the element itself (never a custom property
|
|
13
|
+
* on an ancestor) keeps the per-frame style work to that one element.
|
|
14
|
+
* - **Release** (pointer up): the consumer decides with `shouldDismiss()`
|
|
15
|
+
* (distance *or* velocity), then lets its own CSS transitions finish the
|
|
16
|
+
* motion, either to the dismissed state or back to rest. `flickDuration()`
|
|
17
|
+
* shortens that transition for a fast flick. Transitions, never keyframes:
|
|
18
|
+
* they retarget from wherever the element is, so motion stays interruptible.
|
|
19
|
+
*
|
|
20
|
+
* A drag starts only once the pointer has moved past a small slop, so a press
|
|
21
|
+
* on a button inside the element is still a click; the click that would follow
|
|
22
|
+
* a real drag is swallowed. Form fields, editable content, and anything inside
|
|
23
|
+
* `[data-ui-no-drag]` never start a drag, and neither does a press that is
|
|
24
|
+
* selecting text: give a surface that should drag from its text
|
|
25
|
+
* `user-select: none`.
|
|
26
|
+
*
|
|
27
|
+
* Elements that always drag set `touch-action` in CSS. Where the answer depends
|
|
28
|
+
* on state (a drawer's scrolling body), `trackDrag()` cancels the first
|
|
29
|
+
* `touchmove` of a gesture it has claimed, which is the one moment a browser
|
|
30
|
+
* lets a script take a touch away from native scrolling.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
// --- Types ---
|
|
34
|
+
|
|
35
|
+
/** The axis a drag is locked to. */
|
|
36
|
+
type DragAxis = "x" | "y";
|
|
37
|
+
|
|
38
|
+
/** One drag, as reported to every callback. */
|
|
39
|
+
interface DragState {
|
|
40
|
+
/** The axis the drag locked to. */
|
|
41
|
+
axis: DragAxis;
|
|
42
|
+
/** Signed px along `axis` since the drag started (0 at start, no slop jump). */
|
|
43
|
+
offset: number;
|
|
44
|
+
/** Signed px/ms along `axis`, measured over the last ~100ms. */
|
|
45
|
+
velocity: number;
|
|
46
|
+
/** Viewport x of the original press. */
|
|
47
|
+
startX: number;
|
|
48
|
+
/** Viewport y of the original press. */
|
|
49
|
+
startY: number;
|
|
50
|
+
/** The element that was pressed. */
|
|
51
|
+
target: Element;
|
|
52
|
+
/** `mouse`, `touch`, or `pen`. */
|
|
53
|
+
pointerType: string;
|
|
54
|
+
/** The pointer event behind this report. */
|
|
55
|
+
event: PointerEvent;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Options for `trackDrag()`. */
|
|
59
|
+
interface DragOptions {
|
|
60
|
+
/** The axis to track; `both` locks to whichever axis the pointer moves on first. */
|
|
61
|
+
axis: DragAxis | "both";
|
|
62
|
+
/** Px the pointer travels before a drag starts. Default: 6 for a mouse, 0 for touch and pen. */
|
|
63
|
+
slop?: number;
|
|
64
|
+
/**
|
|
65
|
+
* Veto, asked once when the axis locks. `offset` is the travel so far, so
|
|
66
|
+
* its sign is the drag's direction. Returning false leaves the gesture to
|
|
67
|
+
* the browser (scrolling, text selection) until the pointer lifts.
|
|
68
|
+
*/
|
|
69
|
+
canStart?: (drag: DragState) => boolean;
|
|
70
|
+
/** The drag started; `offset` is 0. */
|
|
71
|
+
onStart?: (drag: DragState) => void;
|
|
72
|
+
/** The pointer moved. */
|
|
73
|
+
onMove: (drag: DragState) => void;
|
|
74
|
+
/** The pointer lifted. */
|
|
75
|
+
onEnd: (drag: DragState) => void;
|
|
76
|
+
/** The browser took the pointer away, or tracking was aborted mid-drag. Default: `onEnd`. */
|
|
77
|
+
onCancel?: (drag: DragState) => void;
|
|
78
|
+
/** Unbinds everything; a drag in flight is cancelled. */
|
|
79
|
+
signal: AbortSignal;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** A pointer position at a point in time. */
|
|
83
|
+
interface VelocitySample {
|
|
84
|
+
/** Timestamp in ms. */
|
|
85
|
+
time: number;
|
|
86
|
+
/** Viewport x. */
|
|
87
|
+
x: number;
|
|
88
|
+
/** Viewport y. */
|
|
89
|
+
y: number;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Rolling pointer-velocity estimate. */
|
|
93
|
+
interface VelocityTracker {
|
|
94
|
+
/** Records a position. */
|
|
95
|
+
add(time: number, x: number, y: number): void;
|
|
96
|
+
/** Signed px/ms on each axis over the tracked window. */
|
|
97
|
+
velocity(): { x: number; y: number };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// --- Pure helpers ---
|
|
101
|
+
|
|
102
|
+
/** How far back the velocity estimate looks, in ms. */
|
|
103
|
+
const VELOCITY_WINDOW_MS = 100;
|
|
104
|
+
|
|
105
|
+
/** Slop for a mouse: a click with a small wobble is not a drag. */
|
|
106
|
+
const MOUSE_SLOP_PX = 6;
|
|
107
|
+
|
|
108
|
+
/** Shortest release transition a flick can ask for, in ms. */
|
|
109
|
+
const MIN_FLICK_MS = 120;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* How long after a drag the click it produces is swallowed, in ms. The click
|
|
113
|
+
* follows the pointer up directly; the window only has to outlast that, and
|
|
114
|
+
* stays short so a deliberate click right after a swipe still lands.
|
|
115
|
+
*/
|
|
116
|
+
const CLICK_SUPPRESS_MS = 50;
|
|
117
|
+
|
|
118
|
+
/** Targets that never start a drag: they need the pointer for themselves. */
|
|
119
|
+
const NO_DRAG =
|
|
120
|
+
'input, select, textarea, [contenteditable]:not([contenteditable="false"]), [data-ui-no-drag]';
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @description Creates a rolling velocity estimate from the samples of the
|
|
124
|
+
* last 100ms. A whole-gesture average (distance over total time) under-reads
|
|
125
|
+
* a flick that follows a pause and over-reads a pause that follows a flick;
|
|
126
|
+
* the window reads what the pointer was doing when it lifted.
|
|
127
|
+
*
|
|
128
|
+
* @returns The tracker.
|
|
129
|
+
*/
|
|
130
|
+
function createVelocityTracker(): VelocityTracker {
|
|
131
|
+
let samples: VelocitySample[] = [];
|
|
132
|
+
return {
|
|
133
|
+
add(time, x, y) {
|
|
134
|
+
samples.push({ time, x, y });
|
|
135
|
+
samples = samples.filter((sample) => time - sample.time <= VELOCITY_WINDOW_MS);
|
|
136
|
+
},
|
|
137
|
+
velocity() {
|
|
138
|
+
const first = samples[0];
|
|
139
|
+
const last = samples.at(-1);
|
|
140
|
+
if (!first || !last) return { x: 0, y: 0 };
|
|
141
|
+
const elapsed = last.time - first.time;
|
|
142
|
+
if (elapsed <= 0) return { x: 0, y: 0 };
|
|
143
|
+
return { x: (last.x - first.x) / elapsed, y: (last.y - first.y) / elapsed };
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* @description Resistance past a boundary: the further the pointer goes, the
|
|
150
|
+
* less the element follows, approaching `limit` and never passing it.
|
|
151
|
+
*
|
|
152
|
+
* @param overshoot - Px the pointer has travelled past the boundary (sign kept).
|
|
153
|
+
* @param limit - The most the element may move, in px.
|
|
154
|
+
* @param constant - Initial follow ratio (0.55 is the iOS scroll-view feel).
|
|
155
|
+
* @returns The damped distance, same sign as `overshoot`.
|
|
156
|
+
*/
|
|
157
|
+
function rubberBand(overshoot: number, limit: number, constant = 0.55): number {
|
|
158
|
+
if (limit <= 0 || overshoot === 0) return 0;
|
|
159
|
+
const distance = Math.abs(overshoot);
|
|
160
|
+
return (Math.sign(overshoot) * (distance * limit * constant)) / (limit + constant * distance);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @description Decides a release: dismiss on a fast enough flick, or on enough
|
|
165
|
+
* distance unless the pointer was flicking back toward rest. Both inputs are
|
|
166
|
+
* measured toward the dismissal direction (positive = away from rest).
|
|
167
|
+
*
|
|
168
|
+
* @param release - The release measurements and thresholds.
|
|
169
|
+
* @returns Whether to dismiss.
|
|
170
|
+
*/
|
|
171
|
+
function shouldDismiss(release: {
|
|
172
|
+
/** Px travelled toward dismissal. */
|
|
173
|
+
offset: number;
|
|
174
|
+
/** Px/ms toward dismissal at release. */
|
|
175
|
+
velocity: number;
|
|
176
|
+
/** Px of travel that dismisses on its own. */
|
|
177
|
+
distance: number;
|
|
178
|
+
/** Px/ms that dismisses on its own. */
|
|
179
|
+
velocityThreshold: number;
|
|
180
|
+
}): boolean {
|
|
181
|
+
const { offset, velocity, distance, velocityThreshold } = release;
|
|
182
|
+
if (offset <= 0) return false;
|
|
183
|
+
if (velocity > velocityThreshold) return true;
|
|
184
|
+
return offset >= distance && velocity > -velocityThreshold;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* @description Picks where an element with several resting places settles on
|
|
189
|
+
* release. Stops are positions along the dismissal axis in px, the furthest
|
|
190
|
+
* from dismissal last (a drawer's largest snap point is `0`, smaller ones are
|
|
191
|
+
* pushed further down). A very fast flick goes to the far end or dismisses, a
|
|
192
|
+
* flick over a short distance moves one stop, and anything slower settles on
|
|
193
|
+
* the nearest stop, or dismisses once it is far enough past the first.
|
|
194
|
+
*
|
|
195
|
+
* @param release - The release measurements, the stops, and the thresholds.
|
|
196
|
+
* @returns The index of the stop to settle on, or `-1` to dismiss.
|
|
197
|
+
*/
|
|
198
|
+
function pickSnap(release: {
|
|
199
|
+
/** Resting positions in px toward dismissal, descending (index 0 is nearest dismissal). */
|
|
200
|
+
stops: readonly number[];
|
|
201
|
+
/** Where the element is now, on the same axis. */
|
|
202
|
+
position: number;
|
|
203
|
+
/** Px/ms toward dismissal at release. */
|
|
204
|
+
velocity: number;
|
|
205
|
+
/** The stop the drag started from. */
|
|
206
|
+
active: number;
|
|
207
|
+
/** The position at which the element is fully dismissed. */
|
|
208
|
+
size: number;
|
|
209
|
+
/** Share of the first stop's visible extent that dismisses on its own (default 0.25). */
|
|
210
|
+
closeRatio?: number;
|
|
211
|
+
/** Px/ms that moves one stop (default 0.4). */
|
|
212
|
+
flick?: number;
|
|
213
|
+
/** Px/ms that skips to the far end or dismisses (default 2). */
|
|
214
|
+
fling?: number;
|
|
215
|
+
/** A fast flick moves one stop instead of skipping. */
|
|
216
|
+
sequential?: boolean;
|
|
217
|
+
}): number {
|
|
218
|
+
const { stops, position, velocity, active, size, sequential = false } = release;
|
|
219
|
+
const { closeRatio = 0.25, flick = 0.4, fling = 2 } = release;
|
|
220
|
+
const last = stops.length - 1;
|
|
221
|
+
if (last < 0) return -1;
|
|
222
|
+
const speed = Math.abs(velocity);
|
|
223
|
+
const towardDismissal = velocity > 0;
|
|
224
|
+
|
|
225
|
+
if (!sequential && speed > fling) return towardDismissal ? -1 : last;
|
|
226
|
+
|
|
227
|
+
const from = stops[Math.min(Math.max(active, 0), last)] ?? 0;
|
|
228
|
+
if (speed > flick && Math.abs(position - from) < size * 0.4) {
|
|
229
|
+
return towardDismissal ? active - 1 : Math.min(last, active + 1);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const first = stops[0] ?? 0;
|
|
233
|
+
if (position > first) {
|
|
234
|
+
// past the first stop: enough travel dismisses, unless the pointer was heading back
|
|
235
|
+
return position - first >= (size - first) * closeRatio && velocity > -flick ? -1 : 0;
|
|
236
|
+
}
|
|
237
|
+
let nearest = 0;
|
|
238
|
+
for (let index = 1; index <= last; index++) {
|
|
239
|
+
if (Math.abs((stops[index] ?? 0) - position) < Math.abs((stops[nearest] ?? 0) - position)) {
|
|
240
|
+
nearest = index;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return nearest;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* @description The release transition's duration for a flick: the time the
|
|
248
|
+
* remaining travel takes at the release speed, never longer than the
|
|
249
|
+
* element's own duration (so `0` under reduced motion stays `0`) and never
|
|
250
|
+
* shorter than a floor that keeps the motion visible.
|
|
251
|
+
*
|
|
252
|
+
* @param remaining - Px left to travel.
|
|
253
|
+
* @param velocity - Release speed in px/ms (sign ignored).
|
|
254
|
+
* @param max - The element's own transition duration in ms.
|
|
255
|
+
* @returns The duration in ms.
|
|
256
|
+
*/
|
|
257
|
+
function flickDuration(remaining: number, velocity: number, max: number): number {
|
|
258
|
+
const speed = Math.abs(velocity);
|
|
259
|
+
if (speed === 0 || max <= 0) return Math.max(0, max);
|
|
260
|
+
return Math.min(max, Math.max(Math.min(MIN_FLICK_MS, max), Math.abs(remaining) / speed));
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// --- DOM helpers ---
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* @description An element's longest computed transition duration.
|
|
267
|
+
*
|
|
268
|
+
* @param element - The element to read.
|
|
269
|
+
* @returns The duration in ms.
|
|
270
|
+
*/
|
|
271
|
+
function maxTransitionMs(element: Element): number {
|
|
272
|
+
return (
|
|
273
|
+
getComputedStyle(element)
|
|
274
|
+
.transitionDuration.split(",")
|
|
275
|
+
.reduce((max, value) => Math.max(max, Number.parseFloat(value) || 0), 0) * 1000
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* @description An element's current `translate`, mid-transition included, so
|
|
281
|
+
* a drag that catches the element on its way back to rest continues from
|
|
282
|
+
* where it is instead of jumping to the pointer.
|
|
283
|
+
*
|
|
284
|
+
* @param element - The element to read.
|
|
285
|
+
* @returns The translation in px.
|
|
286
|
+
*/
|
|
287
|
+
function currentTranslate(element: Element): { x: number; y: number } {
|
|
288
|
+
const [x = 0, y = 0] = getComputedStyle(element)
|
|
289
|
+
.translate.split(/\s+/)
|
|
290
|
+
.map((part) => Number.parseFloat(part) || 0);
|
|
291
|
+
return { x, y };
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** Swallows the click a finished drag produces, wherever it lands. */
|
|
295
|
+
function suppressNextClick(): void {
|
|
296
|
+
const swallow = (event: Event) => {
|
|
297
|
+
event.preventDefault();
|
|
298
|
+
event.stopPropagation();
|
|
299
|
+
};
|
|
300
|
+
document.addEventListener("click", swallow, { capture: true, once: true });
|
|
301
|
+
window.setTimeout(
|
|
302
|
+
() => document.removeEventListener("click", swallow, { capture: true }),
|
|
303
|
+
CLICK_SUPPRESS_MS,
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// --- Drag tracking ---
|
|
308
|
+
|
|
309
|
+
/** A press being followed: undecided, dragging, or left to the browser. */
|
|
310
|
+
interface DragSession {
|
|
311
|
+
phase: "pending" | "dragging" | "rejected";
|
|
312
|
+
pointerId: number;
|
|
313
|
+
pointerType: string;
|
|
314
|
+
target: Element;
|
|
315
|
+
/** Where the press landed. */
|
|
316
|
+
startX: number;
|
|
317
|
+
startY: number;
|
|
318
|
+
/** Where the drag started (the press plus the slop), the zero of `offset`. */
|
|
319
|
+
originX: number;
|
|
320
|
+
originY: number;
|
|
321
|
+
axis: DragAxis;
|
|
322
|
+
tracker: VelocityTracker;
|
|
323
|
+
/** The page's selected text at the press, to tell a selection this press is making. */
|
|
324
|
+
selection: string;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* @description Tracks drags that start on `element`. One pointer at a time:
|
|
329
|
+
* further touches are ignored until the first lifts.
|
|
330
|
+
*
|
|
331
|
+
* @param element - The element a drag must start on (usually the one that moves).
|
|
332
|
+
* @param options - Axis, callbacks, and the abort signal that unbinds it.
|
|
333
|
+
* @example
|
|
334
|
+
* trackDrag(sheet, {
|
|
335
|
+
* axis: "y",
|
|
336
|
+
* signal,
|
|
337
|
+
* onStart: () => sheet.setAttribute("data-sheet-state", "dragging"),
|
|
338
|
+
* onMove: ({ offset }) => (sheet.style.translate = `0 ${Math.max(0, offset)}px`),
|
|
339
|
+
* onEnd: ({ offset, velocity }) => { … },
|
|
340
|
+
* });
|
|
341
|
+
*/
|
|
342
|
+
function trackDrag(element: HTMLElement, options: DragOptions): void {
|
|
343
|
+
const { signal } = options;
|
|
344
|
+
if (signal.aborted) return;
|
|
345
|
+
|
|
346
|
+
let session: DragSession | null = null;
|
|
347
|
+
|
|
348
|
+
const report = (active: DragSession, event: PointerEvent): DragState => {
|
|
349
|
+
const horizontal = active.axis === "x";
|
|
350
|
+
const velocity = active.tracker.velocity();
|
|
351
|
+
return {
|
|
352
|
+
axis: active.axis,
|
|
353
|
+
offset: horizontal ? event.clientX - active.originX : event.clientY - active.originY,
|
|
354
|
+
velocity: horizontal ? velocity.x : velocity.y,
|
|
355
|
+
startX: active.startX,
|
|
356
|
+
startY: active.startY,
|
|
357
|
+
target: active.target,
|
|
358
|
+
pointerType: active.pointerType,
|
|
359
|
+
event,
|
|
360
|
+
};
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
const release = () => {
|
|
364
|
+
document.removeEventListener("pointermove", onPointerMove);
|
|
365
|
+
document.removeEventListener("pointerup", onPointerUp);
|
|
366
|
+
document.removeEventListener("pointercancel", onPointerCancel);
|
|
367
|
+
session = null;
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
const onPointerDown = (event: PointerEvent) => {
|
|
371
|
+
if (session || !event.isPrimary) return;
|
|
372
|
+
if (event.pointerType === "mouse" && event.button !== 0) return;
|
|
373
|
+
const target = event.target;
|
|
374
|
+
if (!(target instanceof Element) || target.closest(NO_DRAG)) return;
|
|
375
|
+
|
|
376
|
+
const tracker = createVelocityTracker();
|
|
377
|
+
tracker.add(event.timeStamp, event.clientX, event.clientY);
|
|
378
|
+
session = {
|
|
379
|
+
phase: "pending",
|
|
380
|
+
pointerId: event.pointerId,
|
|
381
|
+
pointerType: event.pointerType,
|
|
382
|
+
target,
|
|
383
|
+
startX: event.clientX,
|
|
384
|
+
startY: event.clientY,
|
|
385
|
+
originX: event.clientX,
|
|
386
|
+
originY: event.clientY,
|
|
387
|
+
axis: options.axis === "x" ? "x" : "y",
|
|
388
|
+
tracker,
|
|
389
|
+
selection: document.getSelection()?.toString() ?? "",
|
|
390
|
+
};
|
|
391
|
+
// on the document: before capture, a mouse can leave the element
|
|
392
|
+
document.addEventListener("pointermove", onPointerMove);
|
|
393
|
+
document.addEventListener("pointerup", onPointerUp);
|
|
394
|
+
document.addEventListener("pointercancel", onPointerCancel);
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
const onPointerMove = (event: PointerEvent) => {
|
|
398
|
+
const active = session;
|
|
399
|
+
if (!active || event.pointerId !== active.pointerId) return;
|
|
400
|
+
active.tracker.add(event.timeStamp, event.clientX, event.clientY);
|
|
401
|
+
|
|
402
|
+
if (active.phase === "pending") {
|
|
403
|
+
const dx = event.clientX - active.startX;
|
|
404
|
+
const dy = event.clientY - active.startY;
|
|
405
|
+
const slop = options.slop ?? (active.pointerType === "mouse" ? MOUSE_SLOP_PX : 0);
|
|
406
|
+
if (dx === 0 && dy === 0) return;
|
|
407
|
+
if (Math.hypot(dx, dy) < slop) return;
|
|
408
|
+
|
|
409
|
+
const dominant: DragAxis = Math.abs(dx) > Math.abs(dy) ? "x" : "y";
|
|
410
|
+
active.axis = options.axis === "both" ? dominant : options.axis;
|
|
411
|
+
// a press that is selecting text keeps doing that; text selected before
|
|
412
|
+
// the press, or elsewhere on the page, is no reason to refuse
|
|
413
|
+
const selected = document.getSelection()?.toString() ?? "";
|
|
414
|
+
const selecting = selected.length > 0 && selected !== active.selection;
|
|
415
|
+
// decided on the travel so far, so `offset` carries the direction
|
|
416
|
+
if (
|
|
417
|
+
dominant !== active.axis ||
|
|
418
|
+
selecting ||
|
|
419
|
+
(options.canStart && !options.canStart(report(active, event)))
|
|
420
|
+
) {
|
|
421
|
+
active.phase = "rejected";
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
active.phase = "dragging";
|
|
426
|
+
active.originX = event.clientX;
|
|
427
|
+
active.originY = event.clientY;
|
|
428
|
+
try {
|
|
429
|
+
element.setPointerCapture(active.pointerId);
|
|
430
|
+
} catch {
|
|
431
|
+
// a pointer that is already gone (or a synthetic one) cannot be captured
|
|
432
|
+
}
|
|
433
|
+
options.onStart?.(report(active, event));
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
if (active.phase === "dragging") options.onMove(report(active, event));
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
const onPointerUp = (event: PointerEvent) => {
|
|
441
|
+
const active = session;
|
|
442
|
+
if (!active || event.pointerId !== active.pointerId) return;
|
|
443
|
+
active.tracker.add(event.timeStamp, event.clientX, event.clientY);
|
|
444
|
+
release();
|
|
445
|
+
if (active.phase !== "dragging") return;
|
|
446
|
+
suppressNextClick();
|
|
447
|
+
options.onEnd(report(active, event));
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
const onPointerCancel = (event: PointerEvent) => {
|
|
451
|
+
const active = session;
|
|
452
|
+
if (!active || event.pointerId !== active.pointerId) return;
|
|
453
|
+
release();
|
|
454
|
+
if (active.phase === "dragging") (options.onCancel ?? options.onEnd)(report(active, event));
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
// A claimed touch must not also scroll. Only the first touchmove of a
|
|
458
|
+
// gesture is cancelable, and pointermove is dispatched before it, so the
|
|
459
|
+
// claim above is already made when this runs.
|
|
460
|
+
const onTouchMove = (event: TouchEvent) => {
|
|
461
|
+
if (session?.phase === "dragging" && event.cancelable) event.preventDefault();
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
element.addEventListener("pointerdown", onPointerDown, { signal });
|
|
465
|
+
element.addEventListener("touchmove", onTouchMove, { passive: false, signal });
|
|
466
|
+
signal.addEventListener(
|
|
467
|
+
"abort",
|
|
468
|
+
() => {
|
|
469
|
+
const active = session;
|
|
470
|
+
if (!active) return;
|
|
471
|
+
release();
|
|
472
|
+
if (active.phase !== "dragging") return;
|
|
473
|
+
const last = new PointerEvent("pointercancel", {
|
|
474
|
+
pointerId: active.pointerId,
|
|
475
|
+
clientX: active.originX,
|
|
476
|
+
clientY: active.originY,
|
|
477
|
+
});
|
|
478
|
+
(options.onCancel ?? options.onEnd)(report(active, last));
|
|
479
|
+
},
|
|
480
|
+
{ once: true },
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
export {
|
|
485
|
+
createVelocityTracker,
|
|
486
|
+
currentTranslate,
|
|
487
|
+
flickDuration,
|
|
488
|
+
maxTransitionMs,
|
|
489
|
+
pickSnap,
|
|
490
|
+
rubberBand,
|
|
491
|
+
shouldDismiss,
|
|
492
|
+
trackDrag,
|
|
493
|
+
};
|
|
494
|
+
export type { DragAxis, DragOptions, DragState, VelocityTracker };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kit-styling.d.ts","sourceRoot":"","sources":["kit-styling.ts"],"names":[],"mappings":"AAEA,sGAAsG;AACtG,eAAO,MAAM,aAAa,EAAE,SAAS,MAAM,
|
|
1
|
+
{"version":3,"file":"kit-styling.d.ts","sourceRoot":"","sources":["kit-styling.ts"],"names":[],"mappings":"AAEA,sGAAsG;AACtG,eAAO,MAAM,aAAa,EAAE,SAAS,MAAM,EAyS1C,CAAC;AAEF,0GAA0G;AAC1G,eAAO,MAAM,UAAU,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CA2DlE,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CAsLnE,CAAC"}
|