@cloudlet/toast 0.0.0-stage → 1.0.0-rc.3

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.
Files changed (100) hide show
  1. package/CHANGELOG.md +236 -0
  2. package/LICENSE +21 -0
  3. package/README.md +64 -2
  4. package/THIRD_PARTY_NOTICES.md +149 -0
  5. package/dist/contracts/controller.d.ts +71 -0
  6. package/dist/contracts/controller.d.ts.map +1 -0
  7. package/dist/contracts/events.d.ts +72 -0
  8. package/dist/contracts/events.d.ts.map +1 -0
  9. package/dist/contracts/identity.d.ts +32 -0
  10. package/dist/contracts/identity.d.ts.map +1 -0
  11. package/dist/contracts/internal.d.ts +188 -0
  12. package/dist/contracts/internal.d.ts.map +1 -0
  13. package/dist/contracts/notifications.d.ts +101 -0
  14. package/dist/contracts/notifications.d.ts.map +1 -0
  15. package/dist/contracts/operations.d.ts +76 -0
  16. package/dist/contracts/operations.d.ts.map +1 -0
  17. package/dist/contracts/snapshots.d.ts +48 -0
  18. package/dist/contracts/snapshots.d.ts.map +1 -0
  19. package/dist/core/controller.d.ts +7 -0
  20. package/dist/core/controller.d.ts.map +1 -0
  21. package/dist/core/controller.js +954 -0
  22. package/dist/core/controller.js.map +1 -0
  23. package/dist/core/validation.d.ts +17 -0
  24. package/dist/core/validation.d.ts.map +1 -0
  25. package/dist/core/validation.js +298 -0
  26. package/dist/core/validation.js.map +1 -0
  27. package/dist/create-cloudlet.d.ts +5 -0
  28. package/dist/create-cloudlet.d.ts.map +1 -0
  29. package/dist/create-cloudlet.js +34 -0
  30. package/dist/create-cloudlet.js.map +1 -0
  31. package/dist/index.d.ts +10 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +3 -0
  34. package/dist/react/announcer.d.ts +15 -0
  35. package/dist/react/announcer.d.ts.map +1 -0
  36. package/dist/react/announcer.js +134 -0
  37. package/dist/react/announcer.js.map +1 -0
  38. package/dist/react/bindings.d.ts +8 -0
  39. package/dist/react/bindings.d.ts.map +1 -0
  40. package/dist/react/bindings.js +84 -0
  41. package/dist/react/bindings.js.map +1 -0
  42. package/dist/react/card.d.ts +58 -0
  43. package/dist/react/card.d.ts.map +1 -0
  44. package/dist/react/card.js +300 -0
  45. package/dist/react/card.js.map +1 -0
  46. package/dist/react/generators.d.ts +46 -0
  47. package/dist/react/generators.d.ts.map +1 -0
  48. package/dist/react/generators.js +115 -0
  49. package/dist/react/generators.js.map +1 -0
  50. package/dist/react/gestures.d.ts +50 -0
  51. package/dist/react/gestures.d.ts.map +1 -0
  52. package/dist/react/gestures.js +239 -0
  53. package/dist/react/gestures.js.map +1 -0
  54. package/dist/react/host.d.ts +37 -0
  55. package/dist/react/host.d.ts.map +1 -0
  56. package/dist/react/host.js +816 -0
  57. package/dist/react/host.js.map +1 -0
  58. package/dist/react/motion.d.ts +206 -0
  59. package/dist/react/motion.d.ts.map +1 -0
  60. package/dist/react/motion.js +414 -0
  61. package/dist/react/motion.js.map +1 -0
  62. package/dist/react/plan.d.ts +45 -0
  63. package/dist/react/plan.d.ts.map +1 -0
  64. package/dist/react/plan.js +58 -0
  65. package/dist/react/plan.js.map +1 -0
  66. package/dist/react/shape.d.ts +7 -0
  67. package/dist/react/shape.d.ts.map +1 -0
  68. package/dist/react/shape.js +21 -0
  69. package/dist/react/shape.js.map +1 -0
  70. package/dist/react/viewport.d.ts +15 -0
  71. package/dist/react/viewport.d.ts.map +1 -0
  72. package/dist/react/viewport.js +16 -0
  73. package/dist/react/viewport.js.map +1 -0
  74. package/dist/react.d.ts +176 -0
  75. package/dist/react.d.ts.map +1 -0
  76. package/dist/react.js +2 -0
  77. package/dist/styles.css +1 -0
  78. package/package.json +17 -4
  79. package/src/contracts/controller.ts +61 -0
  80. package/src/contracts/events.ts +47 -0
  81. package/src/contracts/identity.ts +27 -0
  82. package/src/contracts/internal.ts +150 -0
  83. package/src/contracts/notifications.ts +104 -0
  84. package/src/contracts/operations.ts +71 -0
  85. package/src/contracts/snapshots.ts +59 -0
  86. package/src/core/controller.ts +1038 -0
  87. package/src/core/validation.ts +290 -0
  88. package/src/create-cloudlet.ts +43 -0
  89. package/src/index.ts +34 -0
  90. package/src/react/announcer.ts +189 -0
  91. package/src/react/bindings.tsx +100 -0
  92. package/src/react/card.tsx +222 -0
  93. package/src/react/generators.ts +274 -0
  94. package/src/react/gestures.ts +296 -0
  95. package/src/react/host.tsx +947 -0
  96. package/src/react/motion.ts +484 -0
  97. package/src/react/plan.ts +101 -0
  98. package/src/react/shape.ts +33 -0
  99. package/src/react/viewport.ts +10 -0
  100. package/src/react.ts +190 -0
@@ -0,0 +1,484 @@
1
+ import type { CloudletMotionConstants, CloudletMotionOverrides } from '../react.js';
2
+ import { spring, tween as tweenGenerator } from './generators.js';
3
+ import type { Generator } from './generators.js';
4
+
5
+ /**
6
+ * Values of DESIGN §7 accepted by the owner on the laboratory (task 17, D47), the one place
7
+ * to tune them. Durations are milliseconds at `timeScale: 1`; offsets are CSS px.
8
+ */
9
+ export const MOTION = {
10
+ /** First pill of an empty stack: scale and translateY on the spring, opacity on a `fade` ms tween (it grows, not fades). */
11
+ enter: { duration: 200, bounce: 0.25, scale: 0.25, offset: 1, fade: 160 },
12
+ /** Exit of the last record or of the dismissed top pill: the reverse of entering (same scale and offset). */
13
+ exit: { duration: 300, bounce: 0 },
14
+ /** Shape springs: opening, and collapsing or adjusting to new content. */
15
+ shapeOpen: { stiffness: 900, damping: 60, mass: 2 },
16
+ shapeClose: { stiffness: 900, damping: 45, mass: 1 },
17
+ /** Tails retract in the first 150 ms of opening and return in the last 150 ms of collapsing. */
18
+ tails: 150,
19
+ /** Close button, body and indicator: delayed fade + 4 px on open, 70 ms fade on collapse. */
20
+ chromeDelay: 100, chromeIn: 200, chromeOut: 70, chromeOffset: 4,
21
+ /** Title and icon blur swap: outgoing 0→6 px and 1→0, incoming 6→0 px and 0→1. */
22
+ headerOut: 200, headerIn: 260, blur: 6,
23
+ /** Body crossfade: outgoing opacity, incoming opacity + translateY 4→0; a stream of updates (progress) crossfades once, then changes in place. */
24
+ bodyOut: 120, bodyIn: 200, bodyOffset: 4, bodyQuiet: 600,
25
+ /** Horizontal slide of the body between notifications (the heading blur-swaps in place); swipes release with it. */
26
+ slide: { stiffness: 1200, damping: 60, mass: 1 },
27
+ /** Badge presence (scale .6↔1, opacity), badge number and indicator text blur swap. */
28
+ badge: { duration: 200, bounce: 0, scale: 0.6 }, countSwap: 160,
29
+ /** Numeric progress value tween. */
30
+ progress: 1000,
31
+ /** Hover spreads the tails by 2 px per step. */
32
+ hover: 200, hoverLift: 2,
33
+ /** Reduced motion: every transition becomes an opacity change of this length; swipes are off. */
34
+ reduced: 80,
35
+ /**
36
+ * Swipes (DESIGN §2–§3): the axis is decided after `intent` px; a release commits past
37
+ * `distance` of the swiped dimension, or after `minDistance` px with `velocity` px/ms
38
+ * towards the commit (a flick back cancels). Edges resist with the rubber-band
39
+ * coefficient; a trackpad swipe ends after `wheelIdle` ms without wheel events and commits
40
+ * as soon as its release would; for `wheelLock` ms after that the same stream cannot commit
41
+ * again (one gesture keeps accelerating), later only a faster or reversed delta starts a new swipe.
42
+ * Released offsets spring home with the `slide` spring and the release velocity.
43
+ * Collapsing towards the screen edge has little room: it also commits after `edge` px.
44
+ */
45
+ swipe: { intent: 8, distance: 0.3, minDistance: 24, velocity: 0.5, edge: 32, rubberBand: 0.55, wheelIdle: 140, wheelLock: 400, suppressClick: 500 },
46
+ } as const;
47
+
48
+ /** The tunable part of MOTION: everything but the swipe thresholds. */
49
+ const tunable: CloudletMotionConstants = MOTION;
50
+ const positive = new Set(['stiffness', 'mass']);
51
+
52
+ /**
53
+ * Resolves `ToasterMotion.overrides` (a debugging channel, DESIGN §7) onto the starting
54
+ * values. A field that is not a finite number in range (springs: stiffness and mass > 0,
55
+ * everything else ≥ 0) keeps its default; unknown keys are ignored.
56
+ */
57
+ export function resolveMotion(overrides: CloudletMotionOverrides | undefined): CloudletMotionConstants {
58
+ if (!overrides || typeof overrides !== 'object') return tunable;
59
+ const valid = (key: string, value: unknown): value is number =>
60
+ typeof value === 'number' && Number.isFinite(value) && (positive.has(key) ? value > 0 : value >= 0);
61
+ const result: Record<string, unknown> = {};
62
+ for (const [key, base] of Object.entries(tunable) as [string, unknown][]) {
63
+ if (key === 'swipe') continue;
64
+ const override = (overrides as Record<string, unknown>)[key];
65
+ if (typeof base === 'number') result[key] = valid(key, override) ? override : base;
66
+ else {
67
+ const fields = { ...(base as Record<string, number>) };
68
+ if (override && typeof override === 'object') {
69
+ for (const field of Object.keys(fields)) {
70
+ const value = (override as Record<string, unknown>)[field];
71
+ if (valid(field, value)) fields[field] = value;
72
+ }
73
+ }
74
+ result[key] = fields;
75
+ }
76
+ }
77
+ return result as unknown as CloudletMotionConstants;
78
+ }
79
+
80
+ const channelKeys = [
81
+ 'opacity', 'scale', 'y', 'width', 'height', 'radius',
82
+ 'tail1', 'tail1Opacity', 'tail2', 'tail2Opacity', 'lift', 'badge',
83
+ 'chrome', 'chromeOut', 'headerIn', 'headerOut', 'bodyIn', 'bodyOut',
84
+ 'slide', 'slideIn', 'slideOut', 'badgeIn', 'badgeOut', 'pageIn', 'pageOut',
85
+ 'progress', 'exitOpacity', 'exitScale', 'exitY', 'exitX', 'headX', 'headY', 'dragX', 'dragY',
86
+ ] as const;
87
+ export type Channel = typeof channelKeys[number];
88
+ /** Spatial channels jump under reduced motion; every other channel fades for MOTION.reduced. */
89
+ const spatial = new Set<Channel>(['scale', 'y', 'width', 'height', 'radius', 'tail1', 'tail2', 'lift', 'slide', 'progress',
90
+ 'exitScale', 'exitY', 'exitX', 'headX', 'headY', 'dragX', 'dragY']);
91
+ const initial: Partial<Record<Channel, number>> = { scale: 1, headerIn: 1, bodyIn: 1, slideIn: 1, badgeIn: 1, pageIn: 1, exitScale: 1 };
92
+
93
+ export type ShellFrame = Readonly<Record<Channel, number>> & {
94
+ readonly reduced: boolean;
95
+ /** +1 when the incoming slide arrives from the inline end, −1 from the start. */
96
+ readonly slideDirection: number;
97
+ readonly slideWidth: number;
98
+ };
99
+ export type SwapLayer = 'header' | 'body' | 'badge' | 'page';
100
+ export type ShapeMode = 'open' | 'close' | 'instant';
101
+ export interface ShellGeometry { readonly width: number; readonly height: number; readonly radius: number }
102
+ /** Channels a finger moves directly: the pill or Island (`dragX`, `dragY`) and the Island content (`slide`). */
103
+ export type DragChannel = 'dragX' | 'dragY' | 'slide';
104
+ /** A swipe-dismissed pill keeps flying sideways from where the finger released it. */
105
+ export interface Fling { readonly direction: 1 | -1; readonly distance: number; readonly velocity: number }
106
+
107
+ interface ShellMotionOptions {
108
+ readonly now: () => number;
109
+ readonly requestFrame: (callback: (time: number) => void) => number;
110
+ readonly cancelFrame: (frame: number) => void;
111
+ /** The sole DOM writer: scoped CSSOM rules and SVG attributes, never a style attribute. */
112
+ readonly write: (frame: ShellFrame) => void;
113
+ readonly reducedMotion?: boolean;
114
+ readonly timeScale?: number | undefined;
115
+ /** Resolved constants (`resolveMotion`); MOTION when omitted. */
116
+ readonly constants?: CloudletMotionConstants | undefined;
117
+ }
118
+
119
+ type Spec =
120
+ | { readonly stiffness: number; readonly damping: number; readonly mass: number }
121
+ | { readonly duration: number; readonly bounce: number }
122
+ | { readonly tween: number };
123
+ interface ChannelState {
124
+ value: number;
125
+ target: number;
126
+ /** Real-time units per second, independent of timeScale. */
127
+ velocity: number;
128
+ animation: { generator: Generator; start: number; delay: number } | null;
129
+ }
130
+
131
+ const swaps = (m: CloudletMotionConstants): Record<SwapLayer | 'chrome', readonly [Channel, Channel, number, number]> => ({
132
+ header: ['headerIn', 'headerOut', m.headerIn, m.headerOut],
133
+ body: ['bodyIn', 'bodyOut', m.bodyIn, m.bodyOut],
134
+ badge: ['badgeIn', 'badgeOut', m.countSwap, m.countSwap],
135
+ page: ['pageIn', 'pageOut', m.countSwap, m.countSwap],
136
+ chrome: ['chrome', 'chromeOut', m.chromeIn, m.chromeOut],
137
+ });
138
+ const validScale = (value: number | undefined) => typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : 1;
139
+
140
+ /**
141
+ * One RAF sampler per host over the generators of generators.ts (the numeric part of
142
+ * Motion 14.3.0, frame-exact by the reference test): next() takes milliseconds,
143
+ * velocity() returns the analytical units/s derivative accepted by the next
144
+ * physical spring, so every retarget starts from the current value and speed.
145
+ * Time-defined springs (duration/bounce) intentionally ignore velocity there.
146
+ * `timeScale` stretches generator time for every channel at once. Nothing is
147
+ * scheduled while idle; completion callbacks run only for the latest request
148
+ * of their group and never synchronously.
149
+ */
150
+ export function createShellMotion(options: ShellMotionOptions) {
151
+ const values = Object.fromEntries(channelKeys.map(key => {
152
+ const value = initial[key] ?? 0;
153
+ return [key, { value, target: value, velocity: 0, animation: null } satisfies ChannelState];
154
+ })) as Record<Channel, ChannelState>;
155
+ const groups = new Map<string, { keys: readonly Channel[]; done: () => void }>();
156
+ let frame: number | null = null;
157
+ let disposed = false;
158
+ let measured = false;
159
+ let reduced = options.reducedMotion ?? false;
160
+ let timeScale = validScale(options.timeScale);
161
+ // Read at the start of every transition: new values apply live, running ones finish as started.
162
+ let m = options.constants ?? tunable;
163
+ let slideDirection = 1;
164
+ let slideWidth = 0;
165
+
166
+ const active = (keys: readonly Channel[]) => keys.some(key => values[key].animation !== null);
167
+ const output = (): ShellFrame => {
168
+ const result = { reduced, slideDirection, slideWidth } as Record<string, number | boolean>;
169
+ for (const key of channelKeys) result[key] = values[key].value;
170
+ return result as unknown as ShellFrame;
171
+ };
172
+ const write = () => { if (measured && !disposed) options.write(output()); };
173
+ function sample(time: number) {
174
+ for (const key of channelKeys) {
175
+ const state = values[key];
176
+ const animation = state.animation;
177
+ if (!animation) continue;
178
+ const elapsed = Math.max(0, (time - animation.start) * timeScale - animation.delay);
179
+ const next = animation.generator.next(elapsed);
180
+ state.value = next.value;
181
+ state.velocity = next.done ? 0 : (animation.generator.velocity?.(elapsed) ?? 0) * timeScale;
182
+ if (next.done) { state.animation = null; state.value = state.target; }
183
+ }
184
+ }
185
+ function schedule() {
186
+ if (!disposed && frame === null) frame = options.requestFrame(tick);
187
+ }
188
+ function complete() {
189
+ for (const [name, group] of [...groups]) {
190
+ if (disposed) return;
191
+ if (groups.get(name) !== group || active(group.keys)) continue;
192
+ // Clear before invoking: completion can synchronously retarget or dispose.
193
+ groups.delete(name);
194
+ group.done();
195
+ }
196
+ }
197
+ function tick(time: number) {
198
+ frame = null;
199
+ if (disposed) return;
200
+ sample(time);
201
+ write();
202
+ complete();
203
+ if (active(channelKeys)) schedule();
204
+ }
205
+ function jump(key: Channel, value: number) {
206
+ const state = values[key];
207
+ state.value = state.target = value;
208
+ state.velocity = 0;
209
+ state.animation = null;
210
+ }
211
+ function to(key: Channel, target: number, spec: Spec, time: number, delay = 0) {
212
+ const state = values[key];
213
+ if (reduced) {
214
+ if (spatial.has(key)) return jump(key, target);
215
+ spec = { tween: m.reduced };
216
+ delay = 0;
217
+ }
218
+ state.target = target;
219
+ if (state.value === target && !state.animation) return;
220
+ const generator = 'tween' in spec
221
+ ? tweenGenerator(state.value, target, spec.tween)
222
+ : spring({ from: state.value, to: target, velocity: state.velocity / timeScale, ...spec });
223
+ state.animation = { generator, start: time, delay };
224
+ }
225
+ function after(name: string, keys: readonly Channel[], done?: () => void) {
226
+ if (done) groups.set(name, { keys, done }); else groups.delete(name);
227
+ }
228
+ /** Shared entry for every public request: sample now, mutate, write the first frame, schedule. */
229
+ function request(change: (time: number) => void) {
230
+ if (disposed) return;
231
+ const time = options.now();
232
+ sample(time);
233
+ change(time);
234
+ write();
235
+ // Completions are always delivered from a frame, never synchronously.
236
+ if (groups.size || active(channelKeys)) schedule();
237
+ }
238
+ const enterSpring = () => ({ duration: m.enter.duration, bounce: m.enter.bounce });
239
+ const exitSpring = () => ({ duration: m.exit.duration, bounce: m.exit.bounce });
240
+ const badgeSpring = () => ({ duration: m.badge.duration, bounce: m.badge.bounce });
241
+ const tween = (duration: number) => ({ tween: duration });
242
+
243
+ /** Visual settle time of a running channel in real milliseconds (bounded, includes a pending delay). */
244
+ function remaining(key: Channel, time: number): number {
245
+ const animation = values[key].animation;
246
+ if (!animation) return 0;
247
+ const target = values[key].target;
248
+ const elapsed = (time - animation.start) * timeScale - animation.delay;
249
+ let t = Math.max(0, elapsed);
250
+ for (const end = t + 2000; t < end; t += 8) {
251
+ const next = animation.generator.next(t);
252
+ if (next.done || Math.abs(next.value - target) < 0.5) break;
253
+ }
254
+ // Generators keep mutable iterator state: resample the current time.
255
+ animation.generator.next(Math.max(0, elapsed));
256
+ return (t - elapsed) / timeScale;
257
+ }
258
+ function swap(layer: SwapLayer | 'chrome', time: number, done?: () => void, incomingOnly = false) {
259
+ const [incoming, outgoing, inMs, outMs] = swaps(m)[layer];
260
+ // The live layer was just cloned: the snapshot continues from its current look.
261
+ jump(outgoing, incomingOnly ? 0 : values[incoming].value);
262
+ jump(incoming, 0);
263
+ to(outgoing, 0, tween(outMs), time);
264
+ to(incoming, 1, tween(inMs), time, layer === 'chrome' ? m.chromeDelay : 0);
265
+ after(layer, [incoming, outgoing], done);
266
+ }
267
+ function tails(depth: number, time: number, spec: Spec, delay: number, from?: (index: number) => { slot?: number; opacity?: number }) {
268
+ for (const index of [1, 2] as const) {
269
+ const slot = `tail${index}` as const;
270
+ const opacity = `tail${index}Opacity` as const;
271
+ const start = from?.(index);
272
+ if (start?.slot !== undefined) jump(slot, start.slot);
273
+ if (start?.opacity !== undefined) jump(opacity, start.opacity);
274
+ if (index <= depth) {
275
+ to(slot, index, spec, time, delay);
276
+ to(opacity, 1, 'tween' in spec ? spec : tween(m.tails), time, delay);
277
+ } else to(opacity, 0, tween(m.tails), time);
278
+ }
279
+ }
280
+ const tailKeys: readonly Channel[] = ['tail1', 'tail1Opacity', 'tail2', 'tail2Opacity', 'badge'];
281
+
282
+ return {
283
+ /** Target geometry measured from HTML; the first measurement and `instant` never animate. */
284
+ setShape(target: ShellGeometry, mode: ShapeMode) {
285
+ request(time => {
286
+ const spec = mode === 'open' ? m.shapeOpen : m.shapeClose;
287
+ for (const key of ['width', 'height', 'radius'] as const) {
288
+ if (!measured || mode === 'instant') jump(key, target[key]);
289
+ else if (values[key].target !== target[key]) to(key, target[key], spec, time);
290
+ }
291
+ measured = true;
292
+ });
293
+ },
294
+ /**
295
+ * The title and icon are the same nodes in both states, so a morph moves them
296
+ * (FLIP): the layout jump is cancelled and springs back with the shape. Text never scales.
297
+ */
298
+ shiftHeading(dx: number, dy: number, mode: ShapeMode) {
299
+ request(time => {
300
+ const spec = mode === 'open' ? m.shapeOpen : m.shapeClose;
301
+ for (const [key, delta] of [['headX', dx], ['headY', dy]] as const) {
302
+ if (!Number.isFinite(delta) || Math.abs(delta) < 0.5) continue;
303
+ values[key].value += delta;
304
+ to(key, 0, spec, time);
305
+ }
306
+ });
307
+ },
308
+ /** The first pill: opacity 0→1 (tween), scale and translateY from the edge on the enter spring. */
309
+ enter(edge: -1 | 1, done?: () => void) {
310
+ request(time => {
311
+ jump('opacity', 0); jump('scale', m.enter.scale); jump('y', edge * m.enter.offset);
312
+ // The shell becomes opaque within `fade`; only scale and y follow the spring (D47).
313
+ to('opacity', 1, tween(m.enter.fade), time);
314
+ for (const key of ['scale', 'y'] as const) to(key, key === 'y' ? 0 : 1, enterSpring(), time);
315
+ after('presence', ['opacity', 'scale', 'y'], done);
316
+ });
317
+ },
318
+ /** The first tail moves up into the pill's place after the top pill was dismissed. */
319
+ rise(offset: number, scale: number, done?: () => void) {
320
+ request(time => {
321
+ jump('y', offset); jump('scale', scale);
322
+ to('y', 0, enterSpring(), time); to('scale', 1, enterSpring(), time); to('opacity', 1, enterSpring(), time);
323
+ after('presence', ['opacity', 'scale', 'y'], done);
324
+ });
325
+ },
326
+ /**
327
+ * The exit layer leaves from the shell's current presence; the shell itself is free again.
328
+ * With a fling (swipe dismiss) the snapshot continues sideways with the release velocity
329
+ * (px/s) instead of falling back to the edge, and the live shell returns to the centre.
330
+ */
331
+ exit(edge: -1 | 1, done?: () => void, fling?: Fling) {
332
+ request(time => {
333
+ jump('exitOpacity', values.opacity.value); jump('exitScale', values.scale.value);
334
+ jump('exitY', values.y.value + values.dragY.value); jump('exitX', fling ? values.dragX.value : 0);
335
+ to('exitOpacity', 0, exitSpring(), time);
336
+ if (fling) {
337
+ values.exitX.velocity = fling.velocity;
338
+ to('exitX', fling.direction * fling.distance, m.slide, time);
339
+ } else {
340
+ to('exitScale', m.enter.scale, exitSpring(), time);
341
+ to('exitY', edge * m.enter.offset, exitSpring(), time);
342
+ }
343
+ jump('dragX', 0); jump('dragY', 0);
344
+ after('exit', ['exitOpacity', 'exitScale', 'exitY', 'exitX'], done);
345
+ });
346
+ },
347
+ /** Hides the live shell at once (its look moved to the exit layer). */
348
+ hide() { request(() => jump('opacity', 0)); },
349
+ /**
350
+ * Collapsed stack depth (0–2 tails). `arrival`: tails shift back from the pill;
351
+ * `rise`: they move up one step; `settle`: spring to their slots in place.
352
+ */
353
+ setTails(depth: number, change: 'arrival' | 'rise' | 'settle', previousDepth: number, done?: () => void) {
354
+ request(time => {
355
+ if (change === 'settle') tails(depth, time, enterSpring(), 0);
356
+ else tails(depth, time, enterSpring(), 0, index => {
357
+ if (index > depth) return { opacity: 0 };
358
+ if (change === 'arrival') return { slot: index - 1, opacity: 1 };
359
+ return { slot: index + 1, opacity: index + 1 <= previousDepth ? 1 : 0 };
360
+ });
361
+ after('tails', tailKeys, done);
362
+ });
363
+ },
364
+ setBadge(visible: boolean) { request(time => to('badge', visible ? 1 : 0, badgeSpring(), time)); },
365
+ /** True while the layer's incoming side is still fading in: a further update joins it instead of restarting. */
366
+ swapping(layer: SwapLayer): boolean { return !!values[swaps(m)[layer][0]].animation; },
367
+ /** The sampler's clock (real ms), so the host's update timing follows the same source. */
368
+ now(): number { return options.now(); },
369
+ /** Opening: tails and badge leave in 150 ms; close, body and indicator appear after 100 ms. */
370
+ open(done?: () => void) {
371
+ request(time => {
372
+ for (const index of [1, 2] as const) { to(`tail${index}`, 0, tween(m.tails), time); to(`tail${index}Opacity`, 0, tween(m.tails), time); }
373
+ to('badge', 0, tween(m.tails), time);
374
+ jump('chromeOut', 0);
375
+ jump('chrome', 0);
376
+ to('chrome', 1, tween(m.chromeIn), time, m.chromeDelay);
377
+ after('tails', tailKeys, done);
378
+ });
379
+ },
380
+ /**
381
+ * Collapsing: the chrome snapshot fades in 70 ms while the shape springs back;
382
+ * tails and badge return during the last 150 ms of the shape spring.
383
+ */
384
+ collapse(depth: number, badge: boolean, done?: { chrome?: () => void; tails?: () => void }) {
385
+ request(time => {
386
+ jump('chrome', 0);
387
+ jump('chromeOut', 1);
388
+ to('chromeOut', 0, tween(m.chromeOut), time);
389
+ after('chrome', ['chromeOut'], done?.chrome);
390
+ const delay = Math.max(0, Math.max(remaining('width', time), remaining('height', time)) - m.tails) * timeScale;
391
+ tails(depth, time, tween(m.tails), delay, () => ({ slot: 0, opacity: 0 }));
392
+ to('badge', badge ? 1 : 0, tween(m.tails), time, delay);
393
+ after('tails', tailKeys, done?.tails);
394
+ });
395
+ },
396
+ /** One outgoing snapshot per layer: a new swap restarts from the live layer's current look. */
397
+ swap(layer: SwapLayer, done?: () => void, incomingOnly = false) {
398
+ request(time => swap(layer, time, done, incomingOnly));
399
+ },
400
+ /** Slide inside a fixed shell. The outgoing snapshot follows the incoming layer one width behind. */
401
+ slide(direction: 1 | -1, width: number, done?: () => void) {
402
+ request(time => {
403
+ if (reduced) {
404
+ jump('slide', 0);
405
+ jump('slideOut', 1); jump('slideIn', 0);
406
+ to('slideOut', 0, tween(m.reduced), time); to('slideIn', 1, tween(m.reduced), time);
407
+ } else {
408
+ jump('slideOut', 1); jump('slideIn', 1);
409
+ // Keep the strip continuous and its velocity when a slide interrupts a slide.
410
+ const state = values.slide;
411
+ state.value += direction * width;
412
+ to('slide', 0, m.slide, time);
413
+ }
414
+ slideDirection = direction;
415
+ slideWidth = width;
416
+ after('slide', ['slide', 'slideIn', 'slideOut'], done);
417
+ });
418
+ },
419
+ /** The finger owns the channel: no spring, no velocity until `release`. */
420
+ drag(channel: DragChannel, value: number) {
421
+ request(() => { if (Number.isFinite(value)) jump(channel, value); });
422
+ },
423
+ /**
424
+ * The finger lets go: the channel springs home (0) with the release velocity in px/s.
425
+ * A committed navigation instead keeps the offset and velocity and calls `slide`.
426
+ */
427
+ release(channel: DragChannel, velocity: number, done?: () => void) {
428
+ request(time => {
429
+ values[channel].velocity = Number.isFinite(velocity) ? velocity : 0;
430
+ to(channel, 0, m.slide, time);
431
+ after(`release:${channel}`, [channel], done);
432
+ });
433
+ },
434
+ /** Sets the speed a following `slide` starts with (a swipe released towards a neighbour). */
435
+ fling(channel: DragChannel, velocity: number) {
436
+ if (!disposed && !values[channel].animation) values[channel].velocity = Number.isFinite(velocity) ? velocity : 0;
437
+ },
438
+ setProgress(value: number | null, immediate: boolean) {
439
+ request(time => { if (value === null || immediate) jump('progress', value ?? 0); else to('progress', value, tween(m.progress), time); });
440
+ },
441
+ setHover(active: boolean) { request(time => to('lift', active ? 1 : 0, tween(m.hover), time)); },
442
+ setReducedMotion(next: boolean) {
443
+ if (disposed || reduced === next) return;
444
+ request(time => {
445
+ reduced = next;
446
+ if (!reduced) return;
447
+ for (const key of channelKeys) {
448
+ const state = values[key];
449
+ if (!state.animation) continue;
450
+ if (spatial.has(key)) jump(key, state.target);
451
+ else { state.animation = null; state.velocity = 0; to(key, state.target, tween(m.reduced), time); }
452
+ }
453
+ });
454
+ },
455
+ setTimeScale(next: number | undefined) {
456
+ const scale = validScale(next);
457
+ if (disposed || scale === timeScale) return;
458
+ const time = options.now();
459
+ sample(time);
460
+ // Rebase running animations so their generator time stays continuous.
461
+ for (const key of channelKeys) {
462
+ const animation = values[key].animation;
463
+ if (animation) animation.start = time - (time - animation.start) * timeScale / scale;
464
+ }
465
+ timeScale = scale;
466
+ },
467
+ /** New constants apply to transitions started from now on; running ones keep their curves. */
468
+ setConstants(next: CloudletMotionConstants) { m = next; },
469
+ /** The constants in effect (the renderer reads offsets and blur from them). */
470
+ get constants() { return m; },
471
+ /** True while a frame is scheduled; an idle host never keeps a RAF loop. */
472
+ get running() { return frame !== null; },
473
+ dispose() {
474
+ if (disposed) return;
475
+ disposed = true;
476
+ if (frame !== null) options.cancelFrame(frame);
477
+ frame = null;
478
+ groups.clear();
479
+ for (const key of channelKeys) values[key].animation = null;
480
+ },
481
+ };
482
+ }
483
+
484
+ export type ShellMotion = ReturnType<typeof createShellMotion>;
@@ -0,0 +1,101 @@
1
+ import type { ReactNode } from 'react';
2
+ import type { NotificationHandle } from '../contracts/identity.js';
3
+ import type { RendererSnapshot } from '../contracts/internal.js';
4
+ import type { NotificationSnapshot } from '../contracts/snapshots.js';
5
+
6
+ type Presented = NotificationSnapshot<ReactNode>;
7
+
8
+ /** What the shell shows for one renderer snapshot. */
9
+ export interface ShellView {
10
+ readonly presented: Presented | null;
11
+ readonly open: boolean;
12
+ readonly stack: readonly NotificationHandle[];
13
+ readonly index: number;
14
+ readonly total: number;
15
+ /** Tails behind the collapsed pill (0–2); none in the Island. */
16
+ readonly depth: number;
17
+ /** Missed notifications (overflow), part of the badge and the pill name (DESIGN §10). */
18
+ readonly missed: number;
19
+ /** Badge number: records in the stack plus missed ones; shown on the pill at ≥ 2 or with missed ones. */
20
+ readonly count: number;
21
+ readonly badge: boolean;
22
+ }
23
+
24
+ export function shellView(snapshot: RendererSnapshot<ReactNode>): ShellView {
25
+ const presented = snapshot.notifications.find(item => item.location === 'current') ?? null;
26
+ const open = snapshot.island.open && presented !== null;
27
+ const total = snapshot.stack.length;
28
+ const missed = snapshot.overflow.pending ? snapshot.overflow.count : 0;
29
+ return { presented, open, stack: snapshot.stack, total, index: presented ? snapshot.stack.indexOf(presented.handle) : -1,
30
+ depth: open || !presented ? 0 : Math.min(2, Math.max(0, total - 1)),
31
+ missed, count: total + missed, badge: !open && !!presented && (total > 1 || missed > 0) };
32
+ }
33
+
34
+ /** The collapsed view before the first commit. */
35
+ export const emptyView: ShellView = { presented: null, open: false, stack: [], index: -1, total: 0, depth: 0, missed: 0, count: 0, badge: false };
36
+
37
+ /** Visual transition between two committed views; pure, so it is unit-tested without DOM. */
38
+ export interface ShellPlan {
39
+ /**
40
+ * enter: from the edge; exit: the shell leaves (last record); arrival: a new top
41
+ * pill enters while the old one becomes the first tail; rise: the top pill leaves
42
+ * and the first tail moves up; replace: the old pill leaves and a new one enters.
43
+ */
44
+ readonly presence: 'none' | 'enter' | 'exit' | 'arrival' | 'rise' | 'replace';
45
+ readonly morph: 'none' | 'open' | 'close';
46
+ /** +1: the incoming slide comes from the inline end (towards older records). */
47
+ readonly slide: 0 | 1 | -1;
48
+ readonly header: boolean;
49
+ readonly body: boolean;
50
+ readonly badge: boolean;
51
+ readonly page: boolean;
52
+ /** Same record, new message: hide it from the scheduler until the swap is readable. */
53
+ readonly begin: boolean;
54
+ readonly tails: 'none' | 'arrival' | 'rise' | 'settle';
55
+ }
56
+
57
+ const headerKey = (item: Presented) => `${item.kind}\u0000${item.content.title}`;
58
+ function bodyKey(item: Presented) {
59
+ const { content, operation, action } = item;
60
+ const description = content.description;
61
+ return [description?.kind, description?.kind === 'text' ? description.text : '', content.progress?.kind,
62
+ content.action?.kind, content.action?.label, action.status === 'failed' ? action.message : '',
63
+ operation?.status, operation?.canRetry, operation?.canCancel].join('\u0000');
64
+ }
65
+
66
+ export function planShell(previous: ShellView, next: ShellView): ShellPlan {
67
+ const plan = { presence: 'none', morph: 'none', slide: 0, header: false, body: false, badge: false, page: false,
68
+ begin: false, tails: 'none' } as { -readonly [Key in keyof ShellPlan]: ShellPlan[Key] };
69
+ const before = previous.presented;
70
+ const after = next.presented;
71
+ if (!after) {
72
+ if (before) plan.presence = 'exit';
73
+ return plan;
74
+ }
75
+ if (!before) {
76
+ plan.presence = 'enter';
77
+ return plan;
78
+ }
79
+ if (previous.open !== next.open) plan.morph = next.open ? 'open' : 'close';
80
+ if (before.handle !== after.handle) {
81
+ if (previous.open && next.open) {
82
+ // Only the body slides (task 17, owner): the title and icon blur-swap in place.
83
+ plan.slide = next.index >= previous.index ? 1 : -1;
84
+ plan.header = headerKey(before) !== headerKey(after) || before.content.icon !== after.content.icon;
85
+ }
86
+ else if (plan.morph !== 'none') plan.header = true;
87
+ else if (next.stack.includes(before.handle)) { plan.presence = 'arrival'; plan.tails = 'arrival'; plan.header = true; }
88
+ else if (previous.stack.includes(after.handle)) { plan.presence = 'rise'; plan.tails = 'rise'; }
89
+ else plan.presence = 'replace';
90
+ } else {
91
+ plan.header = headerKey(before) !== headerKey(after) || before.content.icon !== after.content.icon;
92
+ plan.body = previous.open && next.open && (bodyKey(before) !== bodyKey(after)
93
+ || (before.content.description?.kind === 'rich' && after.content.description?.kind === 'rich'
94
+ && before.content.description.content !== after.content.description.content));
95
+ plan.begin = before.kind !== after.kind || before.operation?.status !== after.operation?.status;
96
+ }
97
+ if (plan.tails === 'none' && plan.morph === 'none' && !next.open && previous.depth !== next.depth) plan.tails = 'settle';
98
+ plan.badge = previous.badge && next.badge && previous.count !== next.count;
99
+ plan.page = previous.open && next.open && next.total > 5 && (previous.index !== next.index || previous.total !== next.total);
100
+ return plan;
101
+ }
@@ -0,0 +1,33 @@
1
+ const finite = (value: number) => Number.isFinite(value) ? Math.max(0, value) : 0;
2
+ const number = (value: number) => String(Math.round(value * 1000) / 1000);
3
+ // Cubic Bézier handle length for a quarter circle.
4
+ const curve = 0.5522847498;
5
+
6
+ /**
7
+ * One whole rounded rectangle, the only silhouette of the pill and the Island.
8
+ * The same string feeds the SVG background `d` and the HTML `clip-path: path()`.
9
+ * Invalid or negative sizes collapse to zero; the radius never exceeds half a side.
10
+ */
11
+ export function createRoundedRectPath(width: number, height: number, radius: number, x = 0, y = 0): string {
12
+ const w = finite(width);
13
+ const h = finite(height);
14
+ const r = Math.min(finite(radius), w / 2, h / 2);
15
+ const left = Number.isFinite(x) ? x : 0;
16
+ const top = Number.isFinite(y) ? y : 0;
17
+ const right = left + w;
18
+ const bottom = top + h;
19
+ const k = r * curve;
20
+ const p = (px: number, py: number) => `${number(px)} ${number(py)}`;
21
+ return [
22
+ `M ${p(left + r, top)}`,
23
+ `L ${p(right - r, top)}`,
24
+ `C ${p(right - r + k, top)} ${p(right, top + r - k)} ${p(right, top + r)}`,
25
+ `L ${p(right, bottom - r)}`,
26
+ `C ${p(right, bottom - r + k)} ${p(right - r + k, bottom)} ${p(right - r, bottom)}`,
27
+ `L ${p(left + r, bottom)}`,
28
+ `C ${p(left + r - k, bottom)} ${p(left, bottom - r + k)} ${p(left, bottom - r)}`,
29
+ `L ${p(left, top + r)}`,
30
+ `C ${p(left, top + r - k)} ${p(left + r - k, top)} ${p(left + r, top)}`,
31
+ 'Z',
32
+ ].join(' ');
33
+ }
@@ -0,0 +1,10 @@
1
+ /** Layout-viewport CSS pixels. Deliberately never read VisualViewport.scale. */
2
+ export function visibleViewport(width: number, height: number, visual?: {
3
+ readonly offsetLeft: number; readonly offsetTop: number; readonly width: number; readonly height: number;
4
+ } | null) {
5
+ const left = Math.min(width, Math.max(0, visual?.offsetLeft ?? 0));
6
+ const top = Math.min(height, Math.max(0, visual?.offsetTop ?? 0));
7
+ const right = Math.max(left, Math.min(width, visual ? visual.offsetLeft + visual.width : width));
8
+ const bottom = Math.max(top, Math.min(height, visual ? visual.offsetTop + visual.height : height));
9
+ return { left, top, right: width - right, bottom: height - bottom, width: right - left, height: bottom - top };
10
+ }