@cloudlet/toast 1.4.0 → 1.5.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/CHANGELOG.md +165 -0
- package/README.md +3 -0
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +19 -15
- package/dist/agent.js.map +1 -1
- package/dist/center-react.d.ts +286 -0
- package/dist/center-react.d.ts.map +1 -0
- package/dist/center-react.js +2 -0
- package/dist/center.d.ts +3 -0
- package/dist/center.d.ts.map +1 -0
- package/dist/center.js +1 -0
- package/dist/core/agent-port.d.ts +2 -0
- package/dist/core/agent-port.d.ts.map +1 -1
- package/dist/core/agent-port.js +3 -0
- package/dist/core/agent-port.js.map +1 -1
- package/dist/react/card.d.ts +4 -0
- package/dist/react/card.d.ts.map +1 -1
- package/dist/react/card.js +3 -8
- package/dist/react/card.js.map +1 -1
- package/dist/react/center-backdrop.d.ts +69 -0
- package/dist/react/center-backdrop.d.ts.map +1 -0
- package/dist/react/center-backdrop.js +81 -0
- package/dist/react/center-backdrop.js.map +1 -0
- package/dist/react/center-clear.d.ts +16 -0
- package/dist/react/center-clear.d.ts.map +1 -0
- package/dist/react/center-clear.js +24 -0
- package/dist/react/center-clear.js.map +1 -0
- package/dist/react/center-gestures.d.ts +47 -0
- package/dist/react/center-gestures.d.ts.map +1 -0
- package/dist/react/center-gestures.js +74 -0
- package/dist/react/center-gestures.js.map +1 -0
- package/dist/react/center-motion.d.ts +174 -0
- package/dist/react/center-motion.d.ts.map +1 -0
- package/dist/react/center-motion.js +344 -0
- package/dist/react/center-motion.js.map +1 -0
- package/dist/react/center.d.ts +15 -0
- package/dist/react/center.d.ts.map +1 -0
- package/dist/react/center.js +1287 -0
- package/dist/react/center.js.map +1 -0
- package/dist/react/focus.d.ts +7 -0
- package/dist/react/focus.d.ts.map +1 -0
- package/dist/react/focus.js +14 -0
- package/dist/react/focus.js.map +1 -0
- package/dist/react/gestures.d.ts +33 -2
- package/dist/react/gestures.d.ts.map +1 -1
- package/dist/react/gestures.js +181 -149
- package/dist/react/gestures.js.map +1 -1
- package/dist/react/host.d.ts.map +1 -1
- package/dist/react/host.js +287 -283
- package/dist/react/host.js.map +1 -1
- package/dist/react/motion.d.ts +18 -11
- package/dist/react/motion.d.ts.map +1 -1
- package/dist/react/motion.js +6 -3
- package/dist/react/motion.js.map +1 -1
- package/dist/react/reduced.d.ts +24 -0
- package/dist/react/reduced.d.ts.map +1 -0
- package/dist/react/reduced.js +35 -0
- package/dist/react/reduced.js.map +1 -0
- package/dist/react/zoom.d.ts +12 -0
- package/dist/react/zoom.d.ts.map +1 -0
- package/dist/react/zoom.js +21 -0
- package/dist/react/zoom.js.map +1 -0
- package/dist/react.d.ts +4 -0
- package/dist/react.d.ts.map +1 -1
- package/dist/styles.css +1 -1
- package/package.json +2 -1
- package/src/agent.ts +11 -6
- package/src/center-react.ts +300 -0
- package/src/center.ts +27 -0
- package/src/core/agent-port.ts +3 -0
- package/src/react/card.tsx +5 -6
- package/src/react/center-backdrop.tsx +151 -0
- package/src/react/center-clear.ts +56 -0
- package/src/react/center-gestures.ts +143 -0
- package/src/react/center-motion.ts +504 -0
- package/src/react/center.tsx +1557 -0
- package/src/react/focus.ts +18 -0
- package/src/react/gestures.ts +127 -37
- package/src/react/host.tsx +17 -5
- package/src/react/motion.ts +16 -9
- package/src/react/reduced.ts +52 -0
- package/src/react/zoom.ts +26 -0
- package/src/react.ts +4 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Focuses `element` only where a person could reach it: connected, enabled, and neither hidden,
|
|
3
|
+
* inert, `aria-hidden`, `display: none`, `visibility: hidden` nor skipped by `content-visibility`.
|
|
4
|
+
* Returns whether focus landed on it.
|
|
5
|
+
*/
|
|
6
|
+
export function focusIfVisible(element: HTMLElement | null | undefined): boolean {
|
|
7
|
+
if (!element?.isConnected || element.closest('[hidden],[inert],[aria-hidden="true"]')
|
|
8
|
+
|| element.matches(':disabled')) return false;
|
|
9
|
+
const doc = element.ownerDocument;
|
|
10
|
+
const view = doc.defaultView;
|
|
11
|
+
for (let ancestor: HTMLElement | null = element; ancestor; ancestor = ancestor.parentElement) {
|
|
12
|
+
const style = view?.getComputedStyle(ancestor);
|
|
13
|
+
if (style?.display === 'none' || style?.visibility === 'hidden' || style?.visibility === 'collapse'
|
|
14
|
+
|| style?.contentVisibility === 'hidden') return false;
|
|
15
|
+
}
|
|
16
|
+
element.focus({ preventScroll: true });
|
|
17
|
+
return doc.activeElement === element;
|
|
18
|
+
}
|
package/src/react/gestures.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MOTION } from './motion.js';
|
|
2
|
+
import { cssZoom } from './zoom.js';
|
|
2
3
|
|
|
3
4
|
const { swipe } = MOTION;
|
|
4
5
|
|
|
@@ -15,7 +16,7 @@ export function rubberBand(offset: number, dimension: number, coefficient: numbe
|
|
|
15
16
|
/**
|
|
16
17
|
* Release decision along one axis. Returns the sign of the travel when the swipe
|
|
17
18
|
* commits that way (distance past `MOTION.swipe.distance` of `dimension`, or a flick of
|
|
18
|
-
* at least `
|
|
19
|
+
* at least `flick` px released at `velocity` px/ms or faster), 0 when it springs back.
|
|
19
20
|
* A direction without a destination never commits; a flick back always cancels.
|
|
20
21
|
*/
|
|
21
22
|
export function decideRelease(offset: number, velocity: number, dimension: number,
|
|
@@ -23,12 +24,49 @@ export function decideRelease(offset: number, velocity: number, dimension: numbe
|
|
|
23
24
|
const sign = offset > 0 ? 1 : offset < 0 ? -1 : 0;
|
|
24
25
|
if (!sign || !(sign > 0 ? allowed.positive : allowed.negative)) return 0;
|
|
25
26
|
const toward = Number.isFinite(velocity) ? velocity * sign : 0;
|
|
26
|
-
if (toward
|
|
27
|
+
if (toward <= -swipe.velocity) return 0;
|
|
27
28
|
const distance = Math.abs(offset);
|
|
28
29
|
if (dimension > 0 && distance > dimension * swipe.distance) return sign;
|
|
29
|
-
return distance >= swipe.
|
|
30
|
+
return distance >= swipe.flick && toward >= swipe.velocity ? sign : 0;
|
|
30
31
|
}
|
|
31
32
|
|
|
33
|
+
/**
|
|
34
|
+
* The release speed of one axis: the travel over the last `MOTION.swipe.window` ms of samples, in
|
|
35
|
+
* px/ms, at most `maxVelocity` either way; a span shorter than one frame counts as one frame. The
|
|
36
|
+
* sample just before the window anchors it. Of a wheel stream (`'wheel'`), a quiet time before a
|
|
37
|
+
* burst counts too: batched deltas after a pause are no flick. A pointer (`'pointer'`) sends a move
|
|
38
|
+
* whenever it moves, so a quiet time is stillness: the anchor stands until a frame before the next
|
|
39
|
+
* sample, at most back to the window's start, and a finger that rests and then flicks is measured
|
|
40
|
+
* over the flick.
|
|
41
|
+
*/
|
|
42
|
+
export function createVelocity(input: 'pointer' | 'wheel' = 'wheel') {
|
|
43
|
+
const samples: { time: number; position: number }[] = [];
|
|
44
|
+
return {
|
|
45
|
+
reset(time: number, position: number) {
|
|
46
|
+
samples.length = 0;
|
|
47
|
+
samples.push({ time, position });
|
|
48
|
+
},
|
|
49
|
+
add(time: number, position: number) {
|
|
50
|
+
samples.push({ time, position });
|
|
51
|
+
// Keep the window and the one sample before it.
|
|
52
|
+
while (samples.length > 2 && samples[1]!.time <= time - swipe.window) samples.shift();
|
|
53
|
+
},
|
|
54
|
+
get(): number {
|
|
55
|
+
const last = samples[samples.length - 1];
|
|
56
|
+
const anchor = samples[0];
|
|
57
|
+
if (!last || !anchor || last === anchor) return 0;
|
|
58
|
+
// A pointer rested at the anchor until a frame before its next move: within the window, that
|
|
59
|
+
// rest is no travel time.
|
|
60
|
+
const from = input === 'pointer'
|
|
61
|
+
? Math.max(anchor.time, Math.min(samples[1]!.time - 16, last.time - swipe.window)) : anchor.time;
|
|
62
|
+
const span = Math.max(last.time - from, 16);
|
|
63
|
+
const velocity = (last.position - anchor.position) / span;
|
|
64
|
+
return Number.isFinite(velocity) ? Math.max(-swipe.maxVelocity, Math.min(swipe.maxVelocity, velocity)) : 0;
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
type Velocity = ReturnType<typeof createVelocity>;
|
|
69
|
+
|
|
32
70
|
export type SwipeAxis = 'x' | 'y';
|
|
33
71
|
|
|
34
72
|
/** A started swipe along one axis. Offsets are raw finger travel in CSS px. */
|
|
@@ -38,13 +76,19 @@ export interface SwipeSession {
|
|
|
38
76
|
readonly end: (offset: number, velocity: number) => void;
|
|
39
77
|
/** Lost capture, a second pointer, orientation change or an external cancel. */
|
|
40
78
|
readonly cancel: () => void;
|
|
79
|
+
/**
|
|
80
|
+
* Whether a release now would commit; a trackpad stream ends as soon as it does. Without it the
|
|
81
|
+
* stream commits past `MOTION.swipe` of the root's width in either direction.
|
|
82
|
+
*/
|
|
83
|
+
readonly decide?: (offset: number, velocity: number) => boolean;
|
|
41
84
|
}
|
|
42
85
|
|
|
43
86
|
/** What may start from one pointerdown or wheel stream; `release` ends the intent window. */
|
|
44
87
|
export interface SwipeCandidate {
|
|
45
88
|
readonly x: boolean;
|
|
46
89
|
readonly y: boolean;
|
|
47
|
-
|
|
90
|
+
/** `direction` is the sign of the travel that decided the axis (+1 right or down). */
|
|
91
|
+
readonly start: (axis: SwipeAxis, direction: 1 | -1) => SwipeSession | null;
|
|
48
92
|
readonly release: () => void;
|
|
49
93
|
}
|
|
50
94
|
|
|
@@ -57,12 +101,21 @@ interface SwipeOptions {
|
|
|
57
101
|
* even where no swipe can start (an end of the slider, reduced motion, outside the slides).
|
|
58
102
|
*/
|
|
59
103
|
readonly claimsWheel?: (target: Element) => boolean;
|
|
104
|
+
/**
|
|
105
|
+
* A wheel stream that started over the root stays the root's until it goes idle, wherever the
|
|
106
|
+
* pointer is now: the rest of a committed swipe is swallowed even when the root has left the hit
|
|
107
|
+
* test (the notification center's panel turns inert as a swipe closes it). Streams still start
|
|
108
|
+
* only over the root, and the window has a wheel listener only while such a stream lasts.
|
|
109
|
+
*/
|
|
110
|
+
readonly holdsStream?: boolean;
|
|
60
111
|
readonly now: () => number;
|
|
61
112
|
}
|
|
62
113
|
|
|
63
114
|
interface PointerSwipe {
|
|
64
115
|
readonly pointer: number;
|
|
65
116
|
readonly candidate: SwipeCandidate;
|
|
117
|
+
/** Viewport px per CSS px of the host (`cssZoom`): pointer travel is divided by it. */
|
|
118
|
+
readonly zoom: number;
|
|
66
119
|
readonly startX: number;
|
|
67
120
|
readonly startY: number;
|
|
68
121
|
session: SwipeSession | null;
|
|
@@ -70,16 +123,18 @@ interface PointerSwipe {
|
|
|
70
123
|
captured: boolean;
|
|
71
124
|
x: number;
|
|
72
125
|
y: number;
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
126
|
+
/** Release speeds over the last samples (CSS px/ms). */
|
|
127
|
+
readonly velocityX: Velocity;
|
|
128
|
+
readonly velocityY: Velocity;
|
|
129
|
+
/** The time of the last sample. */
|
|
130
|
+
sampled: number;
|
|
78
131
|
}
|
|
79
132
|
|
|
80
133
|
interface WheelSwipe {
|
|
81
134
|
/** Started over the open Island (`claimsWheel`). */
|
|
82
135
|
readonly claimed: boolean;
|
|
136
|
+
/** Viewport px per CSS px of the host (`cssZoom`): wheel deltas are divided by it. */
|
|
137
|
+
readonly zoom: number;
|
|
83
138
|
/** Decided after `MOTION.swipe.intent` px of summed travel, like a pointer. */
|
|
84
139
|
axis: SwipeAxis | null;
|
|
85
140
|
travelX: number;
|
|
@@ -91,7 +146,8 @@ interface WheelSwipe {
|
|
|
91
146
|
spentAt: number;
|
|
92
147
|
lastDx: number;
|
|
93
148
|
offset: number;
|
|
94
|
-
|
|
149
|
+
/** Release speed over the last deltas (CSS px/ms), of `offset`. */
|
|
150
|
+
readonly velocity: Velocity;
|
|
95
151
|
time: number;
|
|
96
152
|
timer: number;
|
|
97
153
|
}
|
|
@@ -104,6 +160,10 @@ const interactive = 'button,a,input,select,textarea,summary,label,[role="button"
|
|
|
104
160
|
* browser (native scroll) for the rest of the gesture. Buttons and links never start
|
|
105
161
|
* a swipe, except the collapsed pill, which is itself the swipe surface. Layout,
|
|
106
162
|
* physics and store commands stay with the caller.
|
|
163
|
+
*
|
|
164
|
+
* Travel, velocity and thresholds are CSS px of the host: under a CSS `zoom` on an
|
|
165
|
+
* ancestor, pointer and wheel input (viewport px) is divided by it, so content follows
|
|
166
|
+
* the finger and every distance scales with the zoomed interface.
|
|
107
167
|
*/
|
|
108
168
|
export function createSwipe(root: HTMLElement, options: SwipeOptions) {
|
|
109
169
|
const view = root.ownerDocument.defaultView;
|
|
@@ -128,10 +188,11 @@ export function createSwipe(root: HTMLElement, options: SwipeOptions) {
|
|
|
128
188
|
const stream = wheel;
|
|
129
189
|
if (!stream) return;
|
|
130
190
|
wheel = null;
|
|
191
|
+
if (options.holdsStream) view!.removeEventListener('wheel', held as EventListener, false);
|
|
131
192
|
view!.clearTimeout(stream.timer);
|
|
132
193
|
if (stream.session) delete root.dataset.dragging;
|
|
133
194
|
stream.candidate?.release();
|
|
134
|
-
if (commit) stream.session?.end(stream.offset, stream.velocity);
|
|
195
|
+
if (commit) stream.session?.end(stream.offset, stream.velocity.get());
|
|
135
196
|
else stream.session?.cancel();
|
|
136
197
|
}
|
|
137
198
|
const cancel = () => {
|
|
@@ -153,24 +214,31 @@ export function createSwipe(root: HTMLElement, options: SwipeOptions) {
|
|
|
153
214
|
endWheel(false);
|
|
154
215
|
const candidate = options.begin(target, 'pointer');
|
|
155
216
|
if (!candidate) return;
|
|
217
|
+
const velocityX = createVelocity('pointer');
|
|
218
|
+
const velocityY = createVelocity('pointer');
|
|
219
|
+
velocityX.reset(event.timeStamp, 0);
|
|
220
|
+
velocityY.reset(event.timeStamp, 0);
|
|
156
221
|
current = {
|
|
157
|
-
pointer: event.pointerId, candidate, startX: event.clientX, startY: event.clientY, session: null, axis: null,
|
|
158
|
-
captured: false, x: 0, y: 0,
|
|
222
|
+
pointer: event.pointerId, candidate, zoom: cssZoom(root), startX: event.clientX, startY: event.clientY, session: null, axis: null,
|
|
223
|
+
captured: false, x: 0, y: 0, velocityX, velocityY, sampled: event.timeStamp,
|
|
159
224
|
};
|
|
160
225
|
};
|
|
161
|
-
function sample(gesture: PointerSwipe, event: PointerEvent
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
gesture
|
|
226
|
+
function sample(gesture: PointerSwipe, event: PointerEvent) {
|
|
227
|
+
// A busy main thread hands over the moves of several frames at once: their own times keep a
|
|
228
|
+
// steady drag from reading as a flick. Out-of-order times are left to the event's own.
|
|
229
|
+
for (const moved of event.getCoalescedEvents?.() ?? []) {
|
|
230
|
+
const time = moved.timeStamp;
|
|
231
|
+
if (!Number.isFinite(time) || time <= gesture.sampled || time >= event.timeStamp) continue;
|
|
232
|
+
gesture.sampled = time;
|
|
233
|
+
gesture.velocityX.add(time, (moved.clientX - gesture.startX) / gesture.zoom);
|
|
234
|
+
gesture.velocityY.add(time, (moved.clientY - gesture.startY) / gesture.zoom);
|
|
170
235
|
}
|
|
171
|
-
gesture.x = event.clientX - gesture.startX;
|
|
172
|
-
gesture.y = event.clientY - gesture.startY;
|
|
173
|
-
|
|
236
|
+
gesture.x = (event.clientX - gesture.startX) / gesture.zoom;
|
|
237
|
+
gesture.y = (event.clientY - gesture.startY) / gesture.zoom;
|
|
238
|
+
// A pause before lifting the finger is in the window too: it is no flick.
|
|
239
|
+
gesture.velocityX.add(event.timeStamp, gesture.x);
|
|
240
|
+
gesture.velocityY.add(event.timeStamp, gesture.y);
|
|
241
|
+
gesture.sampled = Math.max(gesture.sampled, event.timeStamp);
|
|
174
242
|
}
|
|
175
243
|
const move = (event: PointerEvent) => {
|
|
176
244
|
const gesture = current;
|
|
@@ -182,7 +250,8 @@ export function createSwipe(root: HTMLElement, options: SwipeOptions) {
|
|
|
182
250
|
if (Math.max(horizontal, vertical) < swipe.intent) return;
|
|
183
251
|
// The first directional intent wins; an axis the target does not own stays native.
|
|
184
252
|
const axis: SwipeAxis = horizontal > vertical ? 'x' : 'y';
|
|
185
|
-
const
|
|
253
|
+
const travel = axis === 'x' ? gesture.x : gesture.y;
|
|
254
|
+
const session = gesture.candidate[axis] ? gesture.candidate.start(axis, travel < 0 ? -1 : 1) : null;
|
|
186
255
|
if (!session || current !== gesture) { cancel(); return; }
|
|
187
256
|
gesture.session = session;
|
|
188
257
|
gesture.axis = axis;
|
|
@@ -202,10 +271,10 @@ export function createSwipe(root: HTMLElement, options: SwipeOptions) {
|
|
|
202
271
|
const up = (event: PointerEvent) => {
|
|
203
272
|
const gesture = current;
|
|
204
273
|
if (!gesture || event.pointerId !== gesture.pointer) return;
|
|
205
|
-
sample(gesture, event
|
|
274
|
+
sample(gesture, event);
|
|
206
275
|
finish();
|
|
207
276
|
const x = gesture.axis === 'x';
|
|
208
|
-
gesture.session?.end(x ? gesture.x : gesture.y, x ? gesture.velocityX : gesture.velocityY);
|
|
277
|
+
gesture.session?.end(x ? gesture.x : gesture.y, x ? gesture.velocityX.get() : gesture.velocityY.get());
|
|
209
278
|
};
|
|
210
279
|
const cancelled = (event: PointerEvent) => {
|
|
211
280
|
if (current?.pointer === event.pointerId) cancel();
|
|
@@ -227,7 +296,10 @@ export function createSwipe(root: HTMLElement, options: SwipeOptions) {
|
|
|
227
296
|
const scroll = (event: WheelEvent) => {
|
|
228
297
|
// Pinch zoom (ctrl+wheel) and a running pointer swipe stay untouched.
|
|
229
298
|
if (disposed || current || event.ctrlKey) return;
|
|
230
|
-
|
|
299
|
+
if (options.holdsStream && !wheel && !(event.target instanceof view.Node && root.contains(event.target))) return;
|
|
300
|
+
// Pixel deltas are viewport px; lines and pages are counted in the host's own CSS px.
|
|
301
|
+
const zoom = wheel?.zoom ?? cssZoom(root);
|
|
302
|
+
const unit = event.deltaMode === 1 ? 16 : event.deltaMode === 2 ? root.clientWidth || 1 : 1 / zoom;
|
|
231
303
|
const dx = event.deltaX * unit;
|
|
232
304
|
const dy = event.deltaY * unit;
|
|
233
305
|
const time = event.timeStamp;
|
|
@@ -249,9 +321,12 @@ export function createSwipe(root: HTMLElement, options: SwipeOptions) {
|
|
|
249
321
|
if (!wheel) {
|
|
250
322
|
const claimed = event.target instanceof view.Element && (options.claimsWheel?.(event.target) ?? false);
|
|
251
323
|
wheel = {
|
|
252
|
-
claimed, axis: null, travelX: 0, travelY: 0, candidate: null, session: null,
|
|
253
|
-
spent: false, spentAt: 0, lastDx: dx, offset: 0, velocity:
|
|
324
|
+
claimed, zoom, axis: null, travelX: 0, travelY: 0, candidate: null, session: null,
|
|
325
|
+
spent: false, spentAt: 0, lastDx: dx, offset: 0, velocity: createVelocity(), time, timer: 0,
|
|
254
326
|
};
|
|
327
|
+
// The stream's events outside the root reach the window only while the stream lasts: the
|
|
328
|
+
// page's own wheel scrolling never waits on a listener that can cancel it.
|
|
329
|
+
if (options.holdsStream) view.addEventListener('wheel', held as EventListener, { capture: false, passive: false });
|
|
255
330
|
}
|
|
256
331
|
const stream = wheel;
|
|
257
332
|
view.clearTimeout(stream.timer);
|
|
@@ -267,13 +342,15 @@ export function createSwipe(root: HTMLElement, options: SwipeOptions) {
|
|
|
267
342
|
stream.axis = horizontal > vertical ? 'x' : 'y';
|
|
268
343
|
const target = event.target;
|
|
269
344
|
const candidate = stream.axis === 'x' && target instanceof view.Element ? options.begin(target, 'wheel') : null;
|
|
270
|
-
|
|
345
|
+
// Fingers moving left scroll right (positive deltaX): the travel is the opposite of the deltas.
|
|
346
|
+
const session = candidate?.x ? candidate.start('x', stream.travelX > 0 ? -1 : 1) : null;
|
|
271
347
|
if (candidate && !session) candidate.release();
|
|
272
348
|
if (session) {
|
|
273
349
|
stream.candidate = candidate;
|
|
274
350
|
stream.session = session;
|
|
275
351
|
// The travel of the intent window counts, as a pointer's does.
|
|
276
352
|
stream.offset = dx - stream.travelX;
|
|
353
|
+
stream.velocity.reset(stream.time, stream.offset);
|
|
277
354
|
}
|
|
278
355
|
}
|
|
279
356
|
}
|
|
@@ -286,24 +363,37 @@ export function createSwipe(root: HTMLElement, options: SwipeOptions) {
|
|
|
286
363
|
}
|
|
287
364
|
event.preventDefault();
|
|
288
365
|
// Fingers moving left scroll right (positive deltaX): the content follows the fingers.
|
|
289
|
-
const elapsed = time - stream.time;
|
|
290
|
-
if (elapsed > 0) stream.velocity = -dx / elapsed;
|
|
291
366
|
stream.offset -= dx;
|
|
292
367
|
stream.time = time;
|
|
368
|
+
stream.velocity.add(time, stream.offset);
|
|
293
369
|
root.dataset.dragging = 'x';
|
|
294
370
|
stream.session.move(stream.offset);
|
|
295
371
|
// A trackpad swipe commits as soon as its release would: macOS momentum keeps
|
|
296
372
|
// sending wheel events for a second or more, which otherwise holds the preview in place.
|
|
297
|
-
|
|
373
|
+
const velocity = stream.velocity.get();
|
|
374
|
+
const commits = stream.session.decide
|
|
375
|
+
? stream.session.decide(stream.offset, velocity)
|
|
376
|
+
: decideRelease(stream.offset, velocity, root.clientWidth || 0, { negative: true, positive: true }) !== 0;
|
|
377
|
+
if (commits) {
|
|
298
378
|
const session = stream.session;
|
|
299
379
|
stream.session = null;
|
|
300
380
|
stream.spent = true;
|
|
301
381
|
stream.spentAt = time;
|
|
302
382
|
delete root.dataset.dragging;
|
|
303
383
|
stream.candidate?.release();
|
|
304
|
-
session.end(stream.offset,
|
|
384
|
+
session.end(stream.offset, velocity);
|
|
305
385
|
}
|
|
306
386
|
};
|
|
387
|
+
/** The last wheel event the root heard: it reaches the window's listener too, and counts once. */
|
|
388
|
+
let heard: Event | null = null;
|
|
389
|
+
const own = (event: WheelEvent) => {
|
|
390
|
+
heard = event;
|
|
391
|
+
scroll(event);
|
|
392
|
+
};
|
|
393
|
+
const held = (event: WheelEvent) => {
|
|
394
|
+
if (event === heard) return;
|
|
395
|
+
scroll(event);
|
|
396
|
+
};
|
|
307
397
|
const listeners: [EventTarget, string, EventListener, boolean][] = [
|
|
308
398
|
[view, 'pointerdown', secondPointer as EventListener, true],
|
|
309
399
|
[root, 'pointerdown', down as EventListener, false],
|
|
@@ -314,7 +404,7 @@ export function createSwipe(root: HTMLElement, options: SwipeOptions) {
|
|
|
314
404
|
[root, 'click', click as EventListener, true],
|
|
315
405
|
[root, 'dragstart', native, false],
|
|
316
406
|
[root, 'selectstart', native, false],
|
|
317
|
-
[root, 'wheel',
|
|
407
|
+
[root, 'wheel', own as EventListener, false],
|
|
318
408
|
[view, 'orientationchange', cancel, false],
|
|
319
409
|
];
|
|
320
410
|
if (view.screen?.orientation) listeners.push([view.screen.orientation, 'change', cancel, false]);
|
package/src/react/host.tsx
CHANGED
|
@@ -14,6 +14,7 @@ import { createRoundedRectPath } from './shape.js';
|
|
|
14
14
|
import { emptyView, planShell, shellView } from './plan.js';
|
|
15
15
|
import type { ShellPlan, ShellView } from './plan.js';
|
|
16
16
|
import { visibleViewport } from './viewport.js';
|
|
17
|
+
import { cssZoom } from './zoom.js';
|
|
17
18
|
|
|
18
19
|
interface HostProps {
|
|
19
20
|
readonly bridge: RendererBridge<ReactNode>;
|
|
@@ -248,8 +249,11 @@ function createHostRuntime(owner: Host) {
|
|
|
248
249
|
const width = doc.documentElement.clientWidth || view.innerWidth;
|
|
249
250
|
const height = doc.documentElement.clientHeight || view.innerHeight;
|
|
250
251
|
const rect = visibleViewport(width, height, view.visualViewport);
|
|
252
|
+
// The viewport is measured in viewport pixels, a `zoom` on <html> included; the host's insets
|
|
253
|
+
// are CSS pixels inside the zoom of its ancestors.
|
|
254
|
+
const zoom = cssZoom(rootRef.current);
|
|
251
255
|
for (const edge of ['top', 'right', 'bottom', 'left', 'width', 'height'] as const) {
|
|
252
|
-
rules.viewport?.setProperty(`--cloudlet-viewport-${edge}`, `${rect[edge]}px`);
|
|
256
|
+
rules.viewport?.setProperty(`--cloudlet-viewport-${edge}`, `${rect[edge] / zoom}px`);
|
|
253
257
|
}
|
|
254
258
|
}
|
|
255
259
|
const bottomEdge = () => rootRef.current?.dataset.edge === 'bottom';
|
|
@@ -345,13 +349,19 @@ function createHostRuntime(owner: Host) {
|
|
|
345
349
|
if (progressActive && progressFill?.isConnected) progressFill.setAttribute('stroke-dasharray', `${Math.round(frame.progress * 10000) / 100} 100`);
|
|
346
350
|
}
|
|
347
351
|
|
|
348
|
-
/**
|
|
352
|
+
/**
|
|
353
|
+
* Layout size of the stack equals the shell's target box and is unaffected by its transforms.
|
|
354
|
+
* The rectangle is in viewport pixels and the shape is written in the stack's CSS pixels: a CSS
|
|
355
|
+
* `zoom` on an ancestor is divided out, or the shell would grow by it.
|
|
356
|
+
*/
|
|
349
357
|
function measure() {
|
|
350
358
|
const stack = stackRef.current;
|
|
351
359
|
const clip = clipRef.current;
|
|
352
360
|
const presented = shellView(owner.props.snapshot).presented;
|
|
353
361
|
if (!stack || !clip || !view || !motion || !presented) { measurable = false; return; }
|
|
354
|
-
const
|
|
362
|
+
const rect = stack.getBoundingClientRect();
|
|
363
|
+
const zoom = cssZoom(stack);
|
|
364
|
+
const bounds = { width: rect.width / zoom, height: rect.height / zoom };
|
|
355
365
|
measurable = bounds.width > 0 && bounds.height > 0;
|
|
356
366
|
syncReadable(presented);
|
|
357
367
|
if (!measurable) return;
|
|
@@ -503,7 +513,9 @@ function createHostRuntime(owner: Host) {
|
|
|
503
513
|
measure();
|
|
504
514
|
const from = pending?.heading;
|
|
505
515
|
const to = from && plan.morph !== 'none' ? heading()?.getBoundingClientRect() : null;
|
|
506
|
-
|
|
516
|
+
// Both rectangles are viewport pixels; the shift is written in the shell's CSS pixels.
|
|
517
|
+
const zoom = from && to ? cssZoom(shellRef.current) : 1;
|
|
518
|
+
if (from && to) motion.shiftHeading((from.left - to.left) / zoom, (from.top - to.top) / zoom, plan.morph === 'open' ? 'open' : 'close');
|
|
507
519
|
const edge = edgeSign();
|
|
508
520
|
const flung = fling && previous.presented?.handle === fling.handle && !next.stack.includes(fling.handle) ? fling : undefined;
|
|
509
521
|
if (fling && !next.stack.includes(fling.handle)) fling = null;
|
|
@@ -630,7 +642,7 @@ function createHostRuntime(owner: Host) {
|
|
|
630
642
|
function collapseSession(): SwipeSession {
|
|
631
643
|
const toward = bottomEdge() ? 1 : -1;
|
|
632
644
|
// Towards the edge the Island stretches softly; away from it the resistance is strong.
|
|
633
|
-
const resist = (offset: number) => rubberBand(offset, offset * toward > 0 ? layout.height : MOTION.swipe.
|
|
645
|
+
const resist = (offset: number) => rubberBand(offset, offset * toward > 0 ? layout.height : MOTION.swipe.resist);
|
|
634
646
|
return {
|
|
635
647
|
move: offset => motion?.drag('dragY', resist(offset)),
|
|
636
648
|
end: (offset, velocity) => {
|
package/src/react/motion.ts
CHANGED
|
@@ -33,16 +33,23 @@ export const MOTION = {
|
|
|
33
33
|
/** Reduced motion: every transition becomes an opacity change of this length; swipes are off. */
|
|
34
34
|
reduced: 80,
|
|
35
35
|
/**
|
|
36
|
-
* Swipes: the axis is decided after `intent` px;
|
|
37
|
-
* `distance` of the swiped dimension, or
|
|
38
|
-
* towards the commit (a flick back cancels).
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
36
|
+
* Swipes: the axis is decided after `intent` px; the content follows the finger 1:1. A release
|
|
37
|
+
* commits past `distance` of the swiped dimension, or as a flick: at least `flick` px of travel
|
|
38
|
+
* with a release speed of `velocity` px/ms or more towards the commit (a flick back cancels).
|
|
39
|
+
* The release speed is the travel over the last `window` ms of input, at most `maxVelocity`
|
|
40
|
+
* px/ms, so events that arrive in a burst (a stall, batched trackpad deltas) are no flick, and a
|
|
41
|
+
* pause before letting go is none either. Edges resist with the rubber-band coefficient over
|
|
42
|
+
* `resist` px where the swipe has nowhere to go; a trackpad swipe ends after `wheelIdle` ms
|
|
43
|
+
* without wheel events and commits as soon as its release would; for `wheelLock` ms after that
|
|
44
|
+
* the same stream cannot commit again (one gesture keeps accelerating), later only a faster or
|
|
45
|
+
* reversed delta starts a new swipe. Released offsets spring home with the `slide` spring and the
|
|
46
|
+
* release velocity. Collapsing towards the screen edge has little room: it also commits after
|
|
47
|
+
* `edge` px.
|
|
44
48
|
*/
|
|
45
|
-
swipe: {
|
|
49
|
+
swipe: {
|
|
50
|
+
intent: 8, distance: 0.4, flick: 60, velocity: 0.6, maxVelocity: 3, window: 100, resist: 24,
|
|
51
|
+
edge: 32, rubberBand: 0.55, wheelIdle: 140, wheelLock: 400, suppressClick: 500,
|
|
52
|
+
},
|
|
46
53
|
} as const;
|
|
47
54
|
|
|
48
55
|
/** The tunable part of MOTION: everything but the swipe thresholds. */
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Watches `attribute` on the ancestors of `root`, by presence: `present()` says whether one of them
|
|
3
|
+
* has it, `onChange` runs whenever it is set, changed or removed on any of them, `dispose` stops
|
|
4
|
+
* listening. The search starts at the parent, so the root's own attribute never counts, and only the
|
|
5
|
+
* ancestor chain is observed, for this one attribute.
|
|
6
|
+
*/
|
|
7
|
+
export function watchAncestorAttribute(root: HTMLElement, attribute: string, onChange: () => void) {
|
|
8
|
+
const view = root.ownerDocument.defaultView;
|
|
9
|
+
const observer = view?.MutationObserver ? new view.MutationObserver(onChange) : null;
|
|
10
|
+
for (let node = root.parentElement; node && observer; node = node.parentElement) {
|
|
11
|
+
observer.observe(node, { attributes: true, attributeFilter: [attribute] });
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
present: () => !!root.parentElement?.closest(`[${attribute}]`),
|
|
15
|
+
dispose() { observer?.disconnect(); },
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Reduced motion as the Toaster's host and the notification center's panel read it, live: the
|
|
21
|
+
* system's `prefers-reduced-motion: reduce`, the app's own `reducedMotion` prop, or
|
|
22
|
+
* `data-reduced-motion` on any ancestor of `root` (by presence, as the registry components read
|
|
23
|
+
* it). The app's setting can only add calm. The search starts at the parent, so the root's own
|
|
24
|
+
* output attribute never feeds back. `onChange` runs on every change after the first reading;
|
|
25
|
+
* `sync` re-reads after a render (the prop); `dispose` stops listening.
|
|
26
|
+
*/
|
|
27
|
+
export function watchReducedMotion(root: HTMLElement, prop: () => boolean, onChange: (reduced: boolean) => void) {
|
|
28
|
+
const view = root.ownerDocument.defaultView;
|
|
29
|
+
const media = view?.matchMedia?.('(prefers-reduced-motion: reduce)');
|
|
30
|
+
const ancestors = watchAncestorAttribute(root, 'data-reduced-motion', () => {
|
|
31
|
+
fromAncestor = ancestors.present();
|
|
32
|
+
sync();
|
|
33
|
+
});
|
|
34
|
+
let fromAncestor = ancestors.present();
|
|
35
|
+
const resolve = () => (media?.matches ?? false) || prop() || fromAncestor;
|
|
36
|
+
let reduced = resolve();
|
|
37
|
+
const sync = () => {
|
|
38
|
+
const next = resolve();
|
|
39
|
+
if (next === reduced) return;
|
|
40
|
+
reduced = next;
|
|
41
|
+
onChange(reduced);
|
|
42
|
+
};
|
|
43
|
+
media?.addEventListener('change', sync);
|
|
44
|
+
return {
|
|
45
|
+
get reduced() { return reduced; },
|
|
46
|
+
sync,
|
|
47
|
+
dispose() {
|
|
48
|
+
media?.removeEventListener('change', sync);
|
|
49
|
+
ancestors.dispose();
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Viewport pixels per CSS pixel of `element`: the product of the CSS `zoom` of the element and its
|
|
3
|
+
* ancestors. `getBoundingClientRect`, pointer coordinates and wheel deltas are viewport pixels,
|
|
4
|
+
* while the lengths the host writes (`width`, `translate`) are CSS pixels inside the same zoom:
|
|
5
|
+
* divide by this before writing one into the other.
|
|
6
|
+
*
|
|
7
|
+
* `currentCSSZoom` answers directly. An engine without it falls back to the ratio of the element's
|
|
8
|
+
* rectangle to its own CSS size, which is right whether that engine's rectangle includes the zoom
|
|
9
|
+
* or not. 1 for an element without a box.
|
|
10
|
+
*/
|
|
11
|
+
export function cssZoom(element: Element | null | undefined): number {
|
|
12
|
+
if (!element) return 1;
|
|
13
|
+
const current = (element as Element & { readonly currentCSSZoom?: unknown }).currentCSSZoom;
|
|
14
|
+
if (typeof current === 'number') return current > 0 && Number.isFinite(current) ? current : 1;
|
|
15
|
+
const view = element.ownerDocument.defaultView;
|
|
16
|
+
const width = view ? borderBoxWidth(view.getComputedStyle(element)) : NaN;
|
|
17
|
+
const ratio = element.getBoundingClientRect().width / width;
|
|
18
|
+
return ratio > 0 && Number.isFinite(ratio) ? ratio : 1;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function borderBoxWidth(style: CSSStyleDeclaration): number {
|
|
22
|
+
const width = parseFloat(style.width);
|
|
23
|
+
if (style.boxSizing === 'border-box') return width;
|
|
24
|
+
return ['paddingLeft', 'paddingRight', 'borderLeftWidth', 'borderRightWidth']
|
|
25
|
+
.reduce((sum, side) => sum + (parseFloat(style[side as 'paddingLeft']) || 0), width);
|
|
26
|
+
}
|
package/src/react.ts
CHANGED
|
@@ -48,6 +48,10 @@ export interface CloudletLabels {
|
|
|
48
48
|
readonly close: string;
|
|
49
49
|
readonly retry: string;
|
|
50
50
|
readonly cancel: string;
|
|
51
|
+
/**
|
|
52
|
+
* Not shown: a pending operation's cancel button leaves once cancellation is requested, and the
|
|
53
|
+
* title says so (`messages.cancellationRequested` of `createCloudlet`). Kept for compatibility.
|
|
54
|
+
*/
|
|
51
55
|
readonly cancellationRequested: string;
|
|
52
56
|
readonly acknowledgeOverflow: string;
|
|
53
57
|
/** The Island's clear button. */
|