@jsb188/gfx 1.1.2

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 (65) hide show
  1. package/README.md +522 -0
  2. package/elements/Shapes.helpers.ts +204 -0
  3. package/elements/Shapes.hooks.ts +171 -0
  4. package/elements/Shapes.tsx +505 -0
  5. package/elements/Shapes.types.ts +81 -0
  6. package/elements/Stack.helpers.ts +42 -0
  7. package/elements/Stack.tsx +143 -0
  8. package/elements/Text.helpers.ts +45 -0
  9. package/elements/Text.hooks.ts +197 -0
  10. package/elements/Text.tsx +98 -0
  11. package/libs/chain.ts +174 -0
  12. package/libs/dotGrid.helpers.ts +53 -0
  13. package/libs/dotGrid.ts +954 -0
  14. package/libs/effects/dotEffects.ts +136 -0
  15. package/libs/effects/effectQuad.ts +146 -0
  16. package/libs/effects/effectRegion.ts +299 -0
  17. package/libs/effects/imageEffects.ts +599 -0
  18. package/libs/effects/index.ts +8 -0
  19. package/libs/effects/ripple.ts +160 -0
  20. package/libs/glsl.ts +134 -0
  21. package/libs/passes.ts +374 -0
  22. package/libs/pointer.ts +151 -0
  23. package/libs/runtime.ts +329 -0
  24. package/libs/texture.ts +160 -0
  25. package/libs/types.ts +94 -0
  26. package/libs/uniforms.ts +32 -0
  27. package/main.ts +4 -0
  28. package/modules/Carousel.helpers.ts +316 -0
  29. package/modules/Carousel.hooks.ts +520 -0
  30. package/modules/Carousel.tsx +631 -0
  31. package/modules/Dots.tsx +269 -0
  32. package/modules/DotsBoard.helpers.ts +388 -0
  33. package/modules/DotsBoard.hooks.ts +144 -0
  34. package/modules/DotsBoard.strings.ts +649 -0
  35. package/modules/DotsBoard.types.ts +240 -0
  36. package/modules/DotsBoardCanvas.hooks.ts +49 -0
  37. package/modules/DotsBoardCanvas.tsx +190 -0
  38. package/modules/DotsBoardElements.tsx +330 -0
  39. package/modules/Gallery.helpers.ts +32 -0
  40. package/modules/Gallery.hooks.ts +149 -0
  41. package/modules/Gallery.tsx +194 -0
  42. package/modules/GfxCanvas.hooks.ts +12 -0
  43. package/modules/GfxCanvas.tsx +189 -0
  44. package/modules/Hero.tsx +90 -0
  45. package/modules/KineticHeader.helpers.ts +44 -0
  46. package/modules/KineticHeader.hooks.ts +206 -0
  47. package/modules/KineticHeader.tsx +305 -0
  48. package/modules/KineticHeader.types.ts +56 -0
  49. package/modules/ScrollSteps.hooks.ts +138 -0
  50. package/modules/ScrollSteps.tsx +98 -0
  51. package/package.json +52 -0
  52. package/scenes/Carousel.ts +277 -0
  53. package/scenes/DotsBoard.helpers.ts +12 -0
  54. package/scenes/DotsBoard.ts +245 -0
  55. package/scenes/Hero.ts +280 -0
  56. package/tsconfig.json +13 -0
  57. package/utils/align.ts +18 -0
  58. package/utils/color.ts +112 -0
  59. package/utils/dotEffectSpecs.ts +79 -0
  60. package/utils/dotGridSpecs.ts +469 -0
  61. package/utils/easing.ts +18 -0
  62. package/utils/imageEffectSpecs.ts +89 -0
  63. package/utils/motion.ts +88 -0
  64. package/utils/random.ts +22 -0
  65. package/utils/strings.ts +116 -0
@@ -0,0 +1,520 @@
1
+ import { useEffect, useLayoutEffect, useMemo, useRef } from 'react';
2
+ import type { DragEvent, FocusEvent, MouseEvent, PointerEvent as ReactPointerEvent, RefObject } from 'react';
3
+ import { easeOutCubic } from '../utils/easing.ts';
4
+ import { prefersReducedMotion } from '../utils/motion.ts';
5
+ import {
6
+ canScrollSideways,
7
+ getBlocksPressKind,
8
+ getDragVelocity,
9
+ getNearestBlockIndex,
10
+ getRubberBandOffset,
11
+ getSettleIndex,
12
+ getWheelDeltaX,
13
+ measureBlocksTrack,
14
+ } from './Carousel.helpers.ts';
15
+ import type { BlocksAlign, BlocksTrackMeasure, DragSample } from './Carousel.helpers.ts';
16
+
17
+ /**
18
+ * Types
19
+ */
20
+
21
+ export interface BlocksCarouselOptions {
22
+ align: BlocksAlign;
23
+ /** Controlled current block; a change glides the track to it. */
24
+ index?: number;
25
+ defaultIndex: number;
26
+ onIndexChange?: (index: number) => void;
27
+ /** Duration of the glide to a snap position, in ms; 0 jumps. */
28
+ transitionMs: number;
29
+ /** Whether a finished gesture locks the track onto the nearest block; `false` leaves it where the gesture put it. */
30
+ lock: boolean;
31
+ /** Shift of the content in CSS pixels, at the start and throughout: -20 shows it 20px further left. */
32
+ contentOffset: number;
33
+ /** Whether a mouse or pen press on text selects it (as anywhere else on the page) instead of dragging the track. */
34
+ selectableText: boolean;
35
+ }
36
+
37
+ /** The pointer that is down on the carousel, and what it has done since. */
38
+ interface BlocksPress {
39
+ pointerId: number;
40
+ pointerType: string;
41
+ startX: number;
42
+ startOffset: number;
43
+ startIndex: number;
44
+ /** Whether the press stopped a wheel gesture; a release without a drag then settles from where the wheel left the track. */
45
+ stoppedWheel: boolean;
46
+ /** True once the pointer has moved past the drag threshold; before that a release is a click. */
47
+ dragging: boolean;
48
+ samples: DragSample[];
49
+ }
50
+
51
+ /** Everything the gesture handlers and the glide frames share: one mutable record, so none of them ever reads a stale closure. */
52
+ interface BlocksTrackState {
53
+ /** How far the track has moved left, in CSS pixels; what its transform shows right now. */
54
+ offset: number;
55
+ /** The block the track rests on, or is gliding to. */
56
+ index: number;
57
+ /** The last measurement; taken again at the start of each gesture and whenever the container or track resizes. */
58
+ measure: BlocksTrackMeasure | null;
59
+ /** The glide's pending animation frame, or 0 while the track is still. */
60
+ rafId: number;
61
+ /** A press that may become a drag; null while no pointer is down, or while one selects text or works a form field. */
62
+ drag: BlocksPress | null;
63
+ /** The sideways wheel or trackpad gesture in progress: it ends when its events stop coming. */
64
+ wheel: { timer: number; startOffset: number; startIndex: number } | null;
65
+ /** Set when a drag ends, so the click the browser fires next does not reach the block. */
66
+ suppressClick: boolean;
67
+ /** The container's cursor as last written, so hovering writes it only when it changes. */
68
+ cursor: string;
69
+ }
70
+
71
+ /**
72
+ * Constants
73
+ */
74
+
75
+ const BLOCKS_GESTURE = {
76
+ /** Pointer movement, in CSS pixels, before a press becomes a drag; below it a release on a block is a click on it. */
77
+ dragThresholdPx: 6,
78
+ /** How far back the pointer's positions are kept, in ms, for the velocity at release. */
79
+ velocityWindowMs: 100,
80
+ /** A release faster than this, in CSS pixels per second, is a flick: it always leaves the current block. */
81
+ flickVelocity: 300,
82
+ /** How long the release velocity is carried on, in ms, to find the block the track coasts to. */
83
+ projectionMs: 200,
84
+ /** How long the release velocity is carried on, in ms, when the track is not locked to blocks and coasts freely. */
85
+ coastMs: 400,
86
+ /** Wheel events further apart than this end the scroll gesture, and the track snaps. */
87
+ wheelSettleMs: 120,
88
+ /** Wheel travel, in CSS pixels, from which a gesture means to move on even when it rounds back to its block. */
89
+ wheelMinTravelPx: 24,
90
+ };
91
+
92
+ /**
93
+ * Hooks
94
+ */
95
+
96
+ /*
97
+ * The gestures of a blocks carousel, on a clipping container and the track inside it: dragging
98
+ * the track with any pointer, scrolling it sideways with a wheel or trackpad, and gliding it to
99
+ * the nearest block's snap position once either ends. With `selectableText`, a mouse or pen press
100
+ * on text selects it instead (the cursor shows an I-beam there and an open hand elsewhere); a
101
+ * press on a form field is always the field's. The track and the cursor are written to the DOM
102
+ * directly, never through React state, so no gesture costs a render. Returns the handlers to
103
+ * spread on the container. The wheel and selectstart listeners are attached natively: the wheel
104
+ * one non-passive, because the sideways events have to be kept from the page. With `lock` off a
105
+ * finished gesture leaves the track where it is instead (a flick coasts on, and a drag past an
106
+ * end still springs back).
107
+ */
108
+ export function useBlocksCarousel(
109
+ containerRef: RefObject<HTMLDivElement | null>,
110
+ trackRef: RefObject<HTMLDivElement | null>,
111
+ options: BlocksCarouselOptions,
112
+ ) {
113
+ const { align, index: controlledIndex, defaultIndex, contentOffset } = options;
114
+ // The handlers are created once and read the latest props from here.
115
+ const optionsRef = useRef(options);
116
+ optionsRef.current = options;
117
+ const stateRef = useRef<BlocksTrackState | null>(null);
118
+ if (!stateRef.current) {
119
+ stateRef.current = {
120
+ offset: 0,
121
+ index: controlledIndex ?? defaultIndex,
122
+ measure: null,
123
+ rafId: 0,
124
+ drag: null,
125
+ wheel: null,
126
+ suppressClick: false,
127
+ cursor: '',
128
+ };
129
+ }
130
+
131
+ const gestures = useMemo(() => {
132
+ const s = stateRef.current!;
133
+
134
+ /*
135
+ * Move the track; the only place its transform is written.
136
+ */
137
+ const applyOffset = (offset: number) => {
138
+ s.offset = offset;
139
+ const track = trackRef.current;
140
+ if (track) track.style.transform = `translate3d(${-offset}px, 0, 0)`;
141
+ };
142
+
143
+ /*
144
+ * Show `cursor` over the container; elements with a cursor of their own (links, buttons) keep theirs.
145
+ */
146
+ const setCursor = (cursor: string) => {
147
+ const container = containerRef.current;
148
+ if (!container || s.cursor === cursor) return;
149
+ s.cursor = cursor;
150
+ container.style.cursor = cursor;
151
+ };
152
+
153
+ /*
154
+ * What a press where the pointer is would start; text is only selected by pointers that aim
155
+ * (mouse, pen), since on a touch screen a long press is what selects.
156
+ */
157
+ const getPressKind = (e: ReactPointerEvent<HTMLDivElement>) =>
158
+ getBlocksPressKind(
159
+ e.target as Element,
160
+ e.currentTarget,
161
+ e.clientX,
162
+ e.clientY,
163
+ e.pointerType !== 'touch' && optionsRef.current.selectableText,
164
+ );
165
+
166
+ const stopGlide = () => {
167
+ if (s.rafId) cancelAnimationFrame(s.rafId);
168
+ s.rafId = 0;
169
+ };
170
+
171
+ const measure = () => {
172
+ const container = containerRef.current;
173
+ const track = trackRef.current;
174
+ const { align, contentOffset } = optionsRef.current;
175
+ s.measure = container && track ? measureBlocksTrack(container, track, align, contentOffset) : null;
176
+ return s.measure;
177
+ };
178
+
179
+ /*
180
+ * Glide the track from where it is to `to` over the transition, or jump there when asked, when
181
+ * motion is reduced, or when it is already there.
182
+ */
183
+ const glideTo = (to: number, animate: boolean) => {
184
+ stopGlide();
185
+ const from = s.offset;
186
+ const { transitionMs } = optionsRef.current;
187
+ if (!animate || transitionMs <= 0 || prefersReducedMotion() || Math.abs(to - from) < 0.5) {
188
+ applyOffset(to);
189
+ return;
190
+ }
191
+ const start = performance.now();
192
+ const frame = (now: number) => {
193
+ const t = Math.min(1, (now - start) / transitionMs);
194
+ applyOffset(from + (to - from) * easeOutCubic(t));
195
+ s.rafId = t < 1 ? requestAnimationFrame(frame) : 0;
196
+ };
197
+ s.rafId = requestAnimationFrame(frame);
198
+ };
199
+
200
+ /*
201
+ * Glide the track to the snap offset of `index` (clamped to the blocks).
202
+ */
203
+ const goTo = (index: number, animate: boolean) => {
204
+ const m = s.measure ?? measure();
205
+ if (!m?.offsets.length) return;
206
+ s.index = Math.max(0, Math.min(m.offsets.length - 1, index));
207
+ glideTo(m.offsets[s.index], animate);
208
+ };
209
+
210
+ /*
211
+ * A gesture has picked its block: glide there and tell the owner if it is a different one.
212
+ */
213
+ const settleTo = (index: number) => {
214
+ const changed = index !== s.index;
215
+ goTo(index, true);
216
+ if (changed) optionsRef.current.onIndexChange?.(s.index);
217
+ };
218
+
219
+ /*
220
+ * The track is not locked to its blocks: carry the release velocity on for `coastMs` and come
221
+ * to rest there, no further than the track's ends (so a drag past one springs back). The
222
+ * block nearest the resting place becomes the current one, and the owner hears of a change.
223
+ */
224
+ const settleFree = (velocity: number) => {
225
+ const m = s.measure;
226
+ if (!m?.offsets.length) return;
227
+ const to = Math.max(m.minOffset, Math.min(m.maxOffset, s.offset + (velocity * BLOCKS_GESTURE.coastMs) / 1000));
228
+ glideTo(to, true);
229
+ const index = getNearestBlockIndex(m.offsets, to);
230
+ if (index === s.index) return;
231
+ s.index = index;
232
+ optionsRef.current.onIndexChange?.(index);
233
+ };
234
+
235
+ const endWheel = () => {
236
+ if (!s.wheel) return;
237
+ clearTimeout(s.wheel.timer);
238
+ s.wheel = null;
239
+ };
240
+
241
+ /*
242
+ * The wheel events have stopped: snap to the nearest block, or on past the start block when
243
+ * the gesture travelled far enough to mean it (a mouse wheel's notch is one such).
244
+ */
245
+ const settleWheel = () => {
246
+ const w = s.wheel;
247
+ if (!w || !s.measure) return;
248
+ s.wheel = null;
249
+ if (!optionsRef.current.lock) {
250
+ settleFree(0);
251
+ return;
252
+ }
253
+ const travelled = s.offset - w.startOffset;
254
+ const intent = Math.abs(travelled) >= BLOCKS_GESTURE.wheelMinTravelPx ? (Math.sign(travelled) as -1 | 1) : 0;
255
+ settleTo(getSettleIndex(s.measure.offsets, s.offset, 0, w.startIndex, intent));
256
+ };
257
+
258
+ /*
259
+ * Sideways scrolling is the carousel's, and is kept from the page; a vertical wheel scrolls
260
+ * the page as usual. The offset follows the deltas, clamped to the track's ends, until the
261
+ * events stop for `wheelSettleMs`.
262
+ */
263
+ const onWheel = (e: WheelEvent) => {
264
+ const container = containerRef.current;
265
+ if (s.drag || !container) return;
266
+ // Every event of a gesture under way is ours; a new one must be more sideways than vertical, and not for a scroller inside a block.
267
+ if (!s.wheel && (Math.abs(e.deltaX) <= Math.abs(e.deltaY) || canScrollSideways(e.target as Element, container, e.deltaX))) {
268
+ return;
269
+ }
270
+ e.preventDefault();
271
+ const m = s.wheel ? s.measure : measure();
272
+ if (!m) return;
273
+ if (s.wheel) clearTimeout(s.wheel.timer);
274
+ else {
275
+ stopGlide();
276
+ s.wheel = { timer: 0, startOffset: s.offset, startIndex: getNearestBlockIndex(m.offsets, s.offset) };
277
+ }
278
+ applyOffset(Math.max(m.minOffset, Math.min(m.maxOffset, s.offset + getWheelDeltaX(e, m.viewportWidth))));
279
+ s.wheel.timer = window.setTimeout(settleWheel, BLOCKS_GESTURE.wheelSettleMs);
280
+ };
281
+
282
+ /*
283
+ * A press that never became a drag is over: the track resumes the glide the press stopped, or
284
+ * settles from where a wheel gesture the press stopped left it; unlocked, it only comes back
285
+ * within its ends.
286
+ */
287
+ const settlePress = (d: BlocksPress) => {
288
+ if (!optionsRef.current.lock) settleFree(0);
289
+ else if (d.stoppedWheel && s.measure) settleTo(getNearestBlockIndex(s.measure.offsets, s.offset));
290
+ else goTo(s.index, true);
291
+ };
292
+
293
+ /*
294
+ * A press holds the track where it is (a glide or scroll under way stops) and may become a
295
+ * drag. A press on a form field is left to the field, and one on text to the browser's
296
+ * selection, unless the track is moving: then any press catches it.
297
+ */
298
+ const onPointerDown = (e: ReactPointerEvent<HTMLDivElement>) => {
299
+ if (!e.isPrimary || (e.pointerType === 'mouse' && e.button !== 0)) return;
300
+ const kind = getPressKind(e);
301
+ if (kind === 'native' || (kind === 'text' && !s.rafId && !s.wheel)) return;
302
+ const m = measure();
303
+ if (!m) return;
304
+ const stoppedWheel = s.wheel !== null;
305
+ endWheel();
306
+ stopGlide();
307
+ s.drag = {
308
+ pointerId: e.pointerId,
309
+ pointerType: e.pointerType,
310
+ startX: e.clientX,
311
+ startOffset: s.offset,
312
+ startIndex: getNearestBlockIndex(m.offsets, s.offset),
313
+ stoppedWheel,
314
+ dragging: false,
315
+ samples: [{ time: e.timeStamp, offset: s.offset }],
316
+ };
317
+ setCursor('grabbing');
318
+ };
319
+
320
+ /*
321
+ * Past the threshold the press is a drag: the pointer is captured so the track keeps following
322
+ * it outside the container, and past either end it gives with rubber-band resistance.
323
+ */
324
+ const onPointerMove = (e: ReactPointerEvent<HTMLDivElement>) => {
325
+ const d = s.drag;
326
+ if (!d) {
327
+ // Hovering with no button down: the cursor tells what a press here would do.
328
+ if (e.pointerType !== 'touch' && !e.buttons) setCursor({ drag: 'grab', text: 'text', native: 'auto' }[getPressKind(e)]);
329
+ return;
330
+ }
331
+ if (e.pointerId !== d.pointerId || !s.measure) return;
332
+ const dx = e.clientX - d.startX;
333
+ if (!d.dragging) {
334
+ if (Math.abs(dx) < BLOCKS_GESTURE.dragThresholdPx) return;
335
+ d.dragging = true;
336
+ // A selection made in the blocks before (a double click, say) would otherwise ride along with the track.
337
+ const selection = window.getSelection();
338
+ if (selection?.anchorNode && !selection.isCollapsed && e.currentTarget.contains(selection.anchorNode)) {
339
+ selection.removeAllRanges();
340
+ }
341
+ try {
342
+ e.currentTarget.setPointerCapture(e.pointerId);
343
+ } catch {}
344
+ }
345
+ const raw = d.startOffset - dx;
346
+ applyOffset(getRubberBandOffset(raw, s.measure.minOffset, s.measure.maxOffset, s.measure.viewportWidth));
347
+ d.samples.push({ time: e.timeStamp, offset: raw });
348
+ while (d.samples.length > 1 && e.timeStamp - d.samples[0].time > BLOCKS_GESTURE.velocityWindowMs) d.samples.shift();
349
+ };
350
+
351
+ /*
352
+ * Release: a press that never became a drag is a click (the block's own handlers get it, and
353
+ * a glide the press interrupted resumes); a drag coasts with its release velocity to the
354
+ * nearest block, and the click the browser fires next is swallowed. A cancelled pointer (the
355
+ * browser took the touch for a vertical scroll) settles from where it is, with no flick.
356
+ */
357
+ const onPointerUp = (e: ReactPointerEvent<HTMLDivElement>) => {
358
+ const d = s.drag;
359
+ if (!d || e.pointerId !== d.pointerId) return;
360
+ s.drag = null;
361
+ setCursor('grab');
362
+ if (!d.dragging) {
363
+ settlePress(d);
364
+ return;
365
+ }
366
+ s.suppressClick = true;
367
+ // The click follows the release in the same task; if none comes (released off the window) forget it.
368
+ window.setTimeout(() => {
369
+ s.suppressClick = false;
370
+ }, 0);
371
+ if (!s.measure) return;
372
+ const velocity = e.type === 'pointercancel' ? 0 : getDragVelocity(d.samples, e.timeStamp, BLOCKS_GESTURE.velocityWindowMs);
373
+ if (!optionsRef.current.lock) {
374
+ settleFree(velocity);
375
+ return;
376
+ }
377
+ const intent = Math.abs(velocity) >= BLOCKS_GESTURE.flickVelocity ? (Math.sign(velocity) as -1 | 1) : 0;
378
+ const projection = (velocity * BLOCKS_GESTURE.projectionMs) / 1000;
379
+ settleTo(getSettleIndex(s.measure.offsets, s.offset, projection, d.startIndex, intent));
380
+ };
381
+
382
+ /*
383
+ * A pointer that leaves before dragging (so without capture) will not report its release here;
384
+ * the press is over.
385
+ */
386
+ const onPointerLeave = (e: ReactPointerEvent<HTMLDivElement>) => {
387
+ const d = s.drag;
388
+ if (!d || d.dragging || e.pointerId !== d.pointerId) return;
389
+ s.drag = null;
390
+ setCursor('grab');
391
+ settlePress(d);
392
+ };
393
+
394
+ /*
395
+ * No text selection starts under a press that may become a drag (a press on blank space, or
396
+ * anywhere while the track moves). A long press on a touch screen is the exception: it is how
397
+ * text is selected there, so the press gives up its drag to it.
398
+ */
399
+ const onSelectStart = (e: Event) => {
400
+ const d = s.drag;
401
+ if (!d) return;
402
+ if (d.pointerType === 'touch' && !d.dragging) {
403
+ s.drag = null;
404
+ settlePress(d);
405
+ return;
406
+ }
407
+ e.preventDefault();
408
+ };
409
+
410
+ /*
411
+ * An image or link under a press that may become a drag must not start the browser's own drag
412
+ * and drop; selected text dragged from a text press still may.
413
+ */
414
+ const onDragStart = (e: DragEvent<HTMLDivElement>) => {
415
+ if (s.drag) e.preventDefault();
416
+ };
417
+
418
+ const onClickCapture = (e: MouseEvent<HTMLDivElement>) => {
419
+ if (!s.suppressClick) return;
420
+ s.suppressClick = false;
421
+ e.preventDefault();
422
+ e.stopPropagation();
423
+ };
424
+
425
+ /*
426
+ * Keyboard focus landing in a block that is clipped away brings that block to its snap position,
427
+ * so tabbing never focuses something invisible. Focus by pointer (a click on a block's button)
428
+ * moves nothing.
429
+ */
430
+ const onFocus = (e: FocusEvent<HTMLDivElement>) => {
431
+ const track = trackRef.current;
432
+ if (!track || s.drag) return;
433
+ let focusVisible = true;
434
+ try {
435
+ focusVisible = e.target.matches(':focus-visible');
436
+ } catch {}
437
+ const index = Array.from(track.children).findIndex((block) => block.contains(e.target));
438
+ if (!focusVisible || index === -1) return;
439
+ const m = measure();
440
+ if (!m) return;
441
+ const rect = track.children[index].getBoundingClientRect();
442
+ // The block's left edge seen from the viewport's: its layout edge in the track, less the track's travel.
443
+ const left = rect.left - track.getBoundingClientRect().left - s.offset;
444
+ if (left >= 0 && left + rect.width <= m.viewportWidth) return;
445
+ settleTo(index);
446
+ };
447
+
448
+ /*
449
+ * The container, the track, or the content offset changed: keep the current block at its snap
450
+ * position, with no motion, unless a gesture is under way (it reads the new measurement and
451
+ * settles on its own). An unlocked track that has been moved keeps its place, only pulled back
452
+ * within the new ends.
453
+ */
454
+ const refresh = () => {
455
+ const before = s.measure;
456
+ const m = measure();
457
+ if (!m || s.drag || s.wheel) return;
458
+ if (!optionsRef.current.lock && !s.rafId && before && s.offset !== before.minOffset) {
459
+ applyOffset(Math.max(m.minOffset, Math.min(m.maxOffset, s.offset)));
460
+ return;
461
+ }
462
+ goTo(s.index, false);
463
+ };
464
+
465
+ const dispose = () => {
466
+ stopGlide();
467
+ endWheel();
468
+ };
469
+
470
+ return {
471
+ goTo,
472
+ refresh,
473
+ dispose,
474
+ onWheel,
475
+ onSelectStart,
476
+ handlers: {
477
+ onPointerDown,
478
+ onPointerMove,
479
+ onPointerUp,
480
+ onPointerCancel: onPointerUp,
481
+ onPointerLeave,
482
+ onClickCapture,
483
+ onDragStart,
484
+ onFocus,
485
+ },
486
+ };
487
+ }, []);
488
+
489
+ // Place the track on its first block before paint, and again whenever the container or the blocks change size or the content offset changes.
490
+ useLayoutEffect(() => {
491
+ gestures.refresh();
492
+ const container = containerRef.current;
493
+ const track = trackRef.current;
494
+ if (!container || !track || typeof ResizeObserver === 'undefined') return;
495
+ const observer = new ResizeObserver(gestures.refresh);
496
+ observer.observe(container);
497
+ observer.observe(track);
498
+ return () => observer.disconnect();
499
+ }, [gestures, align, contentOffset]);
500
+
501
+ useEffect(() => {
502
+ const container = containerRef.current;
503
+ if (!container) return;
504
+ container.addEventListener('wheel', gestures.onWheel, { passive: false });
505
+ container.addEventListener('selectstart', gestures.onSelectStart);
506
+ return () => {
507
+ container.removeEventListener('wheel', gestures.onWheel);
508
+ container.removeEventListener('selectstart', gestures.onSelectStart);
509
+ gestures.dispose();
510
+ };
511
+ }, [gestures]);
512
+
513
+ // A controlled owner picks the block.
514
+ useEffect(() => {
515
+ if (controlledIndex === undefined || controlledIndex === stateRef.current!.index) return;
516
+ gestures.goTo(controlledIndex, true);
517
+ }, [gestures, controlledIndex]);
518
+
519
+ return gestures.handlers;
520
+ }