@vectoriox/iox-ui 5.161.0 → 5.162.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.
@@ -1,7 +1,7 @@
1
1
  import { SceneRole } from '@vectoriox/iox-ui/composite-core';
2
2
  import { FramePlayer, normalizeFrameSequence, VideoAttribute } from '@vectoriox/iox-ui/media-core';
3
3
 
4
- import { heldProgress, normalizeTracks, SCENE_PHONE_MAX_WIDTH, SceneAttribute, sceneFrame, SceneFrame, scenePlayed, sceneWordOpacity, sceneWordsLit, SceneTrack } from './scene';
4
+ import { crossesStage, heldProgress, normalizeTracks, SCENE_PHONE_MAX_WIDTH, SceneAttribute, sceneFrame, SceneFrame, scenePlayed, SceneProgress, sceneWordOpacity, sceneWordsLit, SceneTrack } from './scene';
5
5
  import { rangeDistance, rangeProgress, ScrollBox, ScrollRange } from './scroll-range';
6
6
  import { LIGHT_UP_PIECE_CAP, SplitText, splitText } from './split-text';
7
7
 
@@ -11,7 +11,10 @@ import { LIGHT_UP_PIECE_CAP, SplitText, splitText } from './split-text';
11
11
  * canvas's zoom the same rectangle divides by the same zoom, and the answer is the same at any scale.
12
12
  *
13
13
  * - The value is DAMPED: the scroll stops and the page settles a moment later. This is much of the prototype's weight,
14
- * and why this is not a CSS scroll timeline, which cannot do it.
14
+ * and why this is not a CSS scroll timeline, which cannot do it. Smoothed ONCE (award plan "Reddottours", RT-D2): when
15
+ * the page itself scrolls through Lenis its scroll is already smoothed, so the driver FOLLOWS it with no damping of
16
+ * its own (`follow`) — a second damping on top made every picture trail the hand. The canvas (its Lenis scrolls a
17
+ * wrapper the driver pins against) and a page the browser scrolls natively keep the driver's damping.
15
18
  * - The live site pins with `position: sticky`, on the compositor. The canvas cannot (its page never scrolls itself,
16
19
  * its Lenis scrolls a wrapper around a zoomed frame), so `pin` holds the stage there with `translate` instead.
17
20
  * `translate`, not `transform`: a Stage may carry tracks of its own.
@@ -27,6 +30,10 @@ const MAX_FRAME_SECONDS = 0.1;
27
30
  const WAKING_FRAME_SECONDS = 1 / 60;
28
31
  const STAGE_SELECTOR = `.iox-role-${SceneRole.Stage.replace(/\./g, '-')}, [${SceneAttribute.Stage}]`;
29
32
  const TRANSFORM_WILL_CHANGE = 'transform, opacity';
33
+ /** The class Lenis keeps on the element it scrolls (the document's root, on the live site) for as long as it runs. */
34
+ export const LENIS_ROOT_CLASS = 'lenis';
35
+ /** A crossing that moved less than this since the last frame has stopped moving (it is a 0–1 value). */
36
+ const CROSSING_SETTLED = 0.0001;
30
37
 
31
38
  /**
32
39
  * Dispatched (bubbling) by something the driver plays that arrived after the driver last looked — a Swiper booted on a
@@ -63,6 +70,47 @@ export function sceneScrollState(rootTop: number, originTop: number, zoom: numbe
63
70
  return { local, progress: local / travel };
64
71
  }
65
72
 
73
+ /** Page px a root has been scrolled past its hold's start, from 0 up to `distance` (its hold and its leaving). */
74
+ export function sceneScrollAlong(rootTop: number, originTop: number, zoom: number, distance: number): number {
75
+ if (distance <= 0) return 0;
76
+ return Math.min(distance, Math.max(0, -(rootTop - originTop) / (zoom || 1)));
77
+ }
78
+
79
+ /** How far through a crossing: `entered` px of `span` (the stage's size plus the layer's), clamped 0–1. */
80
+ export function crossingOf(entered: number, span: number): number {
81
+ if (!(span > 0)) return 0;
82
+ return Math.round(Math.min(1, Math.max(0, entered / span)) * 10000) / 10000;
83
+ }
84
+
85
+ /**
86
+ * An element's box on screen without its OWN transform: the box of the nearest ancestor that draws one (it carries every
87
+ * move above it, a walking row's included — a host the component path renders with `display: contents` draws none) plus
88
+ * where the layout places the element inside it. Null when its place cannot be read (no such ancestor, not laid out).
89
+ */
90
+ export function unmovedBox(el: HTMLElement): { left: number; top: number; right: number; bottom: number; width: number; height: number } | null {
91
+ let holder = el.parentElement;
92
+ while (holder && holder.getClientRects().length === 0) holder = holder.parentElement;
93
+ if (!holder) return null;
94
+ const outer = holder.getBoundingClientRect();
95
+ const zoom = holder.offsetWidth > 0 && outer.width > 0 ? outer.width / holder.offsetWidth : 1;
96
+ let x: number;
97
+ let y: number;
98
+ if (el.offsetParent === holder) {
99
+ x = el.offsetLeft + holder.clientLeft;
100
+ y = el.offsetTop + holder.clientTop;
101
+ } else if (el.offsetParent && el.offsetParent === holder.offsetParent) {
102
+ x = el.offsetLeft - holder.offsetLeft;
103
+ y = el.offsetTop - holder.offsetTop;
104
+ } else {
105
+ return null;
106
+ }
107
+ const left = outer.left + x * zoom;
108
+ const top = outer.top + y * zoom;
109
+ const width = el.offsetWidth * zoom;
110
+ const height = el.offsetHeight * zoom;
111
+ return { left, top, right: left + width, bottom: top + height, width, height };
112
+ }
113
+
66
114
  export interface ScrollDriverOptions {
67
115
  /** Hold the stage with `translate` (the canvas). The live site's stage is sticky and needs nothing. */
68
116
  pin?: boolean;
@@ -71,6 +119,12 @@ export interface ScrollDriverOptions {
71
119
  /** Follow what changes inside a root: tracks edited, layers added (the canvas). */
72
120
  observe?: boolean;
73
121
  damping?: number;
122
+ /**
123
+ * Follow the scroll exactly, with no damping of its own (RT-D2): true when something already smooths the scroll the
124
+ * driver reads. A function is asked on every frame. Absent: true while Lenis runs on the host's document (its root
125
+ * carries `LENIS_ROOT_CLASS`) and the driver does not `pin` (the canvas keeps its damping).
126
+ */
127
+ follow?: boolean | (() => boolean);
74
128
  /** Called after every frame it writes, outside any framework's zone (the canvas's selection box follows on it). */
75
129
  onFrame?: (root: HTMLElement, state: SceneFrameState) => void;
76
130
  /** The part of the screen the page is seen through, when that is not the window (the canvas's scroller). Links only. */
@@ -156,6 +210,11 @@ interface Layer {
156
210
  /** Its pictures, while a Play track plays them, and what they were built from: a change starts a new player. */
157
211
  player: FramePlayer | null;
158
212
  playerKey: string;
213
+ /** It has a track measured over its own crossing of the stage: measured in every frame's read. */
214
+ crosses: boolean;
215
+ /** Where it stands in its crossing (0–1), inline and block, as last read. */
216
+ crossInline: number;
217
+ crossBlock: number;
159
218
  }
160
219
 
161
220
  interface Scene {
@@ -164,9 +223,13 @@ interface Scene {
164
223
  layers: Layer[];
165
224
  progress: number;
166
225
  target: number;
226
+ /** Page px scrolled past the hold's start, over the hold and the leaving (`travel + exit`): what is damped. */
227
+ position: number;
167
228
  /** Page px and the stage's own width, measured when something resizes and never per frame. */
168
229
  height: number;
169
230
  travel: number;
231
+ /** Page px the root takes to leave after its hold: the stage's height (an Exit track runs over it). */
232
+ exit: number;
170
233
  /** Page px from the top of the screen at which the stage holds: 0 for a Scene, mid-screen for a played slider. */
171
234
  stick: number;
172
235
  phone: boolean;
@@ -211,7 +274,7 @@ export class ScrollDriver {
211
274
  if (holder && holder !== this) return false;
212
275
  this.remove(root);
213
276
  HELD.set(root, this);
214
- const scene: Scene = { root, stage: null, layers: [], progress: 0, target: 0, height: 0, travel: 0, stick: 0, phone: false, inlineSign: 1, fresh: true, mutations: null };
277
+ const scene: Scene = { root, stage: null, layers: [], progress: 0, target: 0, position: 0, height: 0, travel: 0, exit: 0, stick: 0, phone: false, inlineSign: 1, fresh: true, mutations: null };
215
278
  this.scenes.set(root, scene);
216
279
  this.resize?.observe(root);
217
280
  if (this.options.observe) {
@@ -357,7 +420,13 @@ export class ScrollDriver {
357
420
  private readLayer(el: HTMLElement, scene: Scene): Layer {
358
421
  const previous = scene.layers.find(layer => layer.el === el);
359
422
  el.style.setProperty('will-change', TRANSFORM_WILL_CHANGE);
360
- return { el, tracks: parseTracks(el.getAttribute(SceneAttribute.Tracks)), phoneTracks: parseTracks(el.getAttribute(SceneAttribute.PhoneTracks)), last: previous?.last ?? {}, stale: true, words: previous?.words ?? null, wordsRefused: false, player: previous?.player ?? null, playerKey: previous?.playerKey ?? '' };
423
+ const tracks = parseTracks(el.getAttribute(SceneAttribute.Tracks));
424
+ const phoneTracks = parseTracks(el.getAttribute(SceneAttribute.PhoneTracks));
425
+ return {
426
+ el, tracks, phoneTracks, last: previous?.last ?? {}, stale: true, words: previous?.words ?? null, wordsRefused: false,
427
+ player: previous?.player ?? null, playerKey: previous?.playerKey ?? '',
428
+ crosses: crossesStage(tracks) || crossesStage(phoneTracks), crossInline: previous?.crossInline ?? 0, crossBlock: previous?.crossBlock ?? 0,
429
+ };
361
430
  }
362
431
 
363
432
  private remeasureAll(): void {
@@ -368,6 +437,7 @@ export class ScrollDriver {
368
437
  private measure(scene: Scene): void {
369
438
  scene.height = scene.root.offsetHeight;
370
439
  scene.travel = Math.max(0, scene.height - (scene.stage?.offsetHeight ?? scene.height));
440
+ scene.exit = scene.stage?.offsetHeight ?? 0;
371
441
  // Where the stage holds: its sticky top (a played slider holds mid-screen; a Scene's stage at 0). Read when
372
442
  // something resizes, never per frame.
373
443
  scene.stick = scene.stage ? (parseFloat(this.host.getComputedStyle(scene.stage).top) || 0) : 0;
@@ -380,16 +450,26 @@ export class ScrollDriver {
380
450
  this.frame = 0;
381
451
  const seconds = this.lastTime ? Math.min(MAX_FRAME_SECONDS, (now - this.lastTime) / 1000) : WAKING_FRAME_SECONDS;
382
452
  this.lastTime = now;
453
+ const follow = this.following();
383
454
  const reads = [...this.scenes.values()].map(scene => this.read(scene));
455
+ // Each layer that moves over its own crossing is read with the rest, before any write: where its row stood after the
456
+ // last frame. A row that moved since is read again on the next frame, so the crossing lands where the row lands.
457
+ const crossed = reads.map(({ scene }) => this.readCrossings(scene)).some(Boolean);
384
458
  const view = this.links.size ? this.viewBox() : null;
385
459
  const linkReads = [...this.links].map(state => this.readLink(state, view!));
386
- let moving = false;
387
- for (const { scene, local, target } of reads) {
460
+ let moving = crossed;
461
+ for (const { scene, local, along } of reads) {
388
462
  if (this.options.pin && scene.stage) scene.stage.style.setProperty('translate', `0 ${local}px`);
389
- const next = scene.fresh ? target : dampToward(scene.progress, target, seconds, this.options.damping);
463
+ const immediate = scene.fresh || follow;
464
+ const next = immediate ? along : dampToward(scene.position, along, seconds, this.options.damping);
390
465
  // The step onto the scroll is what is left: taken once it is under SETTLED_PX, so it never shows.
391
- const settled = scene.fresh || Math.abs(target - scene.progress) * Math.max(1, scene.travel) < SETTLED_PX;
392
- scene.progress = settled ? target : next;
466
+ const settled = immediate || Math.abs(along - scene.position) < SETTLED_PX;
467
+ const before = scene.position;
468
+ scene.position = settled ? along : next;
469
+ // A crossing was read before this frame moved its row: it is read again on the next frame, after the move.
470
+ if (scene.position !== before && scene.layers.some(layer => layer.crosses)) moving = true;
471
+ const target = scene.travel > 0 ? local / scene.travel : 0;
472
+ scene.progress = scene.travel > 0 ? Math.min(scene.travel, scene.position) / scene.travel : 0;
393
473
  scene.target = target;
394
474
  scene.fresh = false;
395
475
  this.write(scene);
@@ -397,7 +477,7 @@ export class ScrollDriver {
397
477
  moving = moving || !settled;
398
478
  }
399
479
  for (const { state, target } of linkReads) {
400
- const immediate = state.fresh || !!state.link.immediate;
480
+ const immediate = state.fresh || !!state.link.immediate || follow;
401
481
  const next = immediate ? target : dampToward(state.progress, target, seconds, this.options.damping);
402
482
  const settled = immediate || Math.abs(target - state.progress) * Math.max(1, state.geometry.distance) < SETTLED_PX;
403
483
  state.progress = settled ? target : next;
@@ -410,6 +490,38 @@ export class ScrollDriver {
410
490
  else this.lastTime = 0;
411
491
  };
412
492
 
493
+ /** True when the scroll the driver reads is already smoothed: it follows it exactly (RT-D2). */
494
+ private following(): boolean {
495
+ const follow = this.options.follow;
496
+ if (typeof follow === 'function') return follow();
497
+ if (typeof follow === 'boolean') return follow;
498
+ return !this.options.pin && !!this.host.document?.documentElement?.classList.contains(LENIS_ROOT_CLASS);
499
+ }
500
+
501
+ /**
502
+ * Where each crossing layer of a Scene stands in its crossing of the stage. Its box is read WITHOUT its own move (its
503
+ * parent's box, which carries a walking row's move, plus its offset in it), so what it is moved by never feeds back into
504
+ * how far it is moved. True when any of them moved since the last frame.
505
+ */
506
+ private readCrossings(scene: Scene): boolean {
507
+ const crossing = scene.layers.filter(layer => layer.crosses);
508
+ if (!crossing.length) return false;
509
+ const stage = (scene.stage ?? scene.root).getBoundingClientRect();
510
+ let moved = false;
511
+ for (const layer of crossing) {
512
+ const box = unmovedBox(layer.el);
513
+ if (!box) continue;
514
+ const inline = scene.inlineSign === 1
515
+ ? crossingOf(stage.right - box.left, stage.width + box.width)
516
+ : crossingOf(box.right - stage.left, stage.width + box.width);
517
+ const block = crossingOf(stage.bottom - box.top, stage.height + box.height);
518
+ moved = moved || Math.abs(inline - layer.crossInline) > CROSSING_SETTLED || Math.abs(block - layer.crossBlock) > CROSSING_SETTLED;
519
+ layer.crossInline = inline;
520
+ layer.crossBlock = block;
521
+ }
522
+ return moved;
523
+ }
524
+
413
525
  /** The part of the page on screen: the host's own, or the window. */
414
526
  private viewBox(): ScrollBox {
415
527
  return this.options.view?.() ?? { top: 0, height: this.host.innerHeight };
@@ -432,23 +544,25 @@ export class ScrollDriver {
432
544
  return rangeProgress(link.range, { top: view.top + (box.top - top), height: box.height }, view);
433
545
  }
434
546
 
435
- private read(scene: Scene): { scene: Scene; local: number; target: number } {
547
+ private read(scene: Scene): { scene: Scene; local: number; along: number } {
436
548
  const rect = scene.root.getBoundingClientRect();
437
549
  const origin = this.options.originOf?.(scene.root)?.getBoundingClientRect().top ?? 0;
438
550
  const zoom = scene.height ? rect.height / scene.height : 1;
439
- const { local, progress } = sceneScrollState(rect.top, origin + scene.stick * zoom, zoom, scene.travel);
440
- return { scene, local, target: progress };
551
+ const { local } = sceneScrollState(rect.top, origin + scene.stick * zoom, zoom, scene.travel);
552
+ return { scene, local, along: sceneScrollAlong(rect.top, origin + scene.stick * zoom, zoom, scene.travel + scene.exit) };
441
553
  }
442
554
 
443
555
  private write(scene: Scene): void {
444
556
  // A slider the scroll plays (M4): the damped moment, held on each picture.
445
557
  const slider = sliderOf(scene.stage);
446
558
  if (slider) slider.setProgress!(heldProgress(scene.progress, slider.slides?.length ?? 0), 0);
559
+ const exit = scene.exit > 0 ? Math.min(1, Math.max(0, (scene.position - scene.travel) / scene.exit)) : 0;
447
560
  for (const layer of scene.layers) {
448
561
  const tracks = scene.phone && layer.phoneTracks.length ? layer.phoneTracks : layer.tracks;
449
- this.writeWords(layer, sceneWordsLit(tracks, scene.progress));
450
- this.writePlayed(layer, scene, scenePlayed(tracks, scene.progress));
451
- const frame = sceneFrame(tracks, scene.progress, { inlineSign: scene.inlineSign });
562
+ const at: SceneProgress = { hold: scene.progress, exit, crossInline: layer.crossInline, crossBlock: layer.crossBlock };
563
+ this.writeWords(layer, sceneWordsLit(tracks, at));
564
+ this.writePlayed(layer, scene, scenePlayed(tracks, at));
565
+ const frame = sceneFrame(tracks, at, { inlineSign: scene.inlineSign });
452
566
  for (const key of Object.keys(frame) as (keyof SceneFrame)[]) {
453
567
  if (layer.stale || frame[key] !== layer.last[key]) layer.el.style[key] = frame[key] ?? '';
454
568
  }