-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline-observer.js
More file actions
743 lines (650 loc) · 21.9 KB
/
Copy pathline-observer.js
File metadata and controls
743 lines (650 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/**
* LineObserver
*
* A performant scroll interaction observer that triggers handlers
* when elements cross a configurable "trigger line" in the viewport.
*
* Features:
* - Single shared RAF loop (only runs when instances are active)
* - Batched DOM read/write operations
* - Configurable trigger line per instance (px, %, vh, vw)
* - Configurable scroll direction behavior
* - Bidirectional scroll support with consistent offset values
* - Default handler exposes --scroll-offset CSS custom property
* - Support for custom scroll handlers
*
* @example
* const observer = new LineObserver({ triggerLine: '50vh' });
*
* observer.register(element, {
* activeClass: 'is-active',
* onActivate: (el) => console.log('Activated!'),
* onDeactivate: (el) => console.log('Deactivated!'),
* onScroll: (offset, el, instance) => {
* const progress = offset / instance.elementHeight;
* // Do something with progress...
* }
* });
*
* @license MIT
*/
class LineObserver {
/**
* Create a new LineObserver instance
*
* @param {Object} options - Global default options
* @param {string|number} [options.triggerLine] - Position of trigger line (px, %, vh, vw)
* @param {string} [options.activeClass] - Class added when element is active
* @param {string} [options.activateFrom] - Which side the element crosses the trigger line from to activate:
* 'below' (element rises from below the line, scrolling down),
* 'above' (element drops from above the line, scrolling up),
* or 'both' (default)
* @param {number} [options.nearMargin] - IO band half-width in px (default: 100). Larger values
* detect elements earlier; smaller values are more efficient.
* @param {boolean} [options.cssCustomProperty] - Set --scroll-offset on active elements (default: true)
* @param {Function} [options.onActivate] - Callback when element becomes active
* @param {Function} [options.onDeactivate] - Callback when element becomes inactive
* @param {Function} [options.onScroll] - Callback on each scroll frame while active
*/
constructor(options = {}) {
this.defaults = {
triggerLine: '50vh',
activeClass: 'is-active',
activateFrom: 'both',
nearMargin: 100,
cssCustomProperty: true,
onActivate: null,
onDeactivate: null,
onScroll: null,
};
this.destroyed = false;
this.defaults = { ...this.defaults, ...options };
// Instance tracking
this.instances = new Map();
this.activeInstances = new Set();
this.nearInstances = new Set();
// RAF state
this.isRunning = false;
this.rafId = null;
this.lastScrollY = window.scrollY;
this.currentDirection = 'down';
// Bind methods
this.tick = this.tick.bind(this);
this.handleResize = this.handleResize.bind(this);
// Resize handling (debounced)
this.resizeTimeout = null;
window.addEventListener('resize', this.handleResize, { passive: true });
// Cache viewport dimensions
this.viewportHeight = window.innerHeight;
this.viewportWidth = window.innerWidth;
}
/**
* Parse trigger line value to pixels
*
* @param {string|number} value - Trigger line value (px, %, vh, vw)
* @return {number} Pixel value
*/
parseTriggerLine(value) {
if (typeof value === 'number') {
return value;
}
const match = String(value).match(/^([\d.]+)(px|%|vh|vw)$/);
if (!match) {
console.warn(
`LineObserver: Invalid trigger line format "${value}", defaulting to 50vh`
);
return this.viewportHeight * 0.5;
}
const num = parseFloat(match[1]);
const unit = match[2];
const unitHandlers = {
px: num,
'%': (num / 100) * this.viewportHeight,
vh: (num / 100) * this.viewportHeight,
vw: (num / 100) * this.viewportWidth,
};
return unitHandlers[unit];
}
/**
* Determine element state based on position relative to trigger line
*
* @param {DOMRect} rect - Element's bounding client rect
* @param {number} triggerLinePx - Trigger line position in pixels
* @return {string} 'inactive', 'active', or 'passed'
* @private
*/
_determineState(rect, triggerLinePx) {
const topAboveLine = rect.top <= triggerLinePx;
const bottomAboveLine = rect.bottom <= triggerLinePx;
if (!topAboveLine) {
return 'inactive';
} else if (!bottomAboveLine) {
return 'active';
}
return 'passed';
}
/**
* Create IntersectionObserver for an instance
*
* @param {Object} instance - The observer instance containing element and trigger configuration.
* @return {IntersectionObserver} The created IntersectionObserver.
* @private
*/
createObserver(instance) {
// Create a wide band around the trigger line as a nearness detector.
// IO only adds/removes elements from `nearInstances` — the RAF loop
// handles precise state transitions using fresh getBoundingClientRect.
//
// The `nearMargin` option (default: 100px) controls the band width.
// Larger values detect elements earlier but keep the RAF loop running
// longer; smaller values are more efficient but may miss fast scrolls.
// The active-instances pass in tick() acts as a safety net for elements
// that skip the band entirely during very fast scrolling.
//
// Note: if nearMargin exceeds the trigger line's distance from a viewport
// edge, the rootMargin becomes positive on that side, extending the band
// beyond the viewport. This is harmless — tick() still does precise checks.
//
// Example: 50vh trigger on 1000px viewport, nearMargin = 100
// topMargin = -500 + 100 = -400 (shrink viewport 400px from top)
// bottomMargin = -(1000 - 500 - 100) = -400 (shrink 400px from bottom)
// rootMargin = "-400px 0px -400px 0px" → 200px visible band centered on trigger
const margin = instance.options.nearMargin;
const topMargin = -Math.floor(instance.triggerLinePx) + margin;
const bottomMargin = -Math.floor(
this.viewportHeight - instance.triggerLinePx - margin
);
const observer = new IntersectionObserver(
(entries) => this.handleIntersection(entries, instance),
{
rootMargin: `${topMargin}px 0px ${bottomMargin}px 0px`,
threshold: [0],
}
);
observer.observe(instance.element);
return observer;
}
/**
* Handle intersection changes — nearness detection only.
*
* IO fires when an element enters or leaves the wide band around the
* trigger line. This method only manages the `nearInstances` set and
* starts the RAF loop. Actual state transitions happen in `tick()`.
*
* @param {IntersectionObserverEntry[]} entries - Array of intersection observer entries.
* @param {Object} instance - The observer instance associated with the intersection.
* @private
*/
handleIntersection(entries, instance) {
if (this.destroyed || !this.instances.has(instance.element)) {
return;
}
const entry = entries[entries.length - 1];
if (entry.isIntersecting) {
this.nearInstances.add(instance);
this.startLoop();
} else {
this.nearInstances.delete(instance);
}
}
/**
* Transition instance to new state
*
* @param {Object} instance - The observer instance to transition.
* @param {string} newState - The new state ('inactive', 'active', or 'passed').
* @param {number} scrollY - The current scroll Y position.
* @private
*/
transitionState(instance, newState, scrollY) {
if (newState === instance.state) {
return;
}
const oldState = instance.state;
instance.state = newState;
const { element, options } = instance;
if (newState === 'active') {
// Entering active state
instance.elementHeight = element.offsetHeight;
// Calculate activationScrollY so that offset (scrollY - activationScrollY)
// starts at 0 and grows as the user scrolls through the element.
//
// When scrolling down (top crossed the line): activationScrollY = current scrollY.
// When scrolling up (bottom crossed the line): we subtract elementHeight so the
// offset reflects distance from the element's top, not its bottom.
//
// In checkInitialState, a different formula is used because we know the element's
// exact position relative to the trigger line: scrollY - (triggerLinePx - rect.top).
if (this.currentDirection === 'down') {
instance.activationScrollY = scrollY;
} else {
instance.activationScrollY = scrollY - instance.elementHeight;
}
element.classList.add(options.activeClass);
this.activeInstances.add(instance);
this.startLoop();
if (options.onActivate) {
options.onActivate(element, instance);
}
} else if (oldState === 'active') {
// Leaving active state
element.classList.remove(options.activeClass);
this.activeInstances.delete(instance);
// Reset scroll offset
if (options.cssCustomProperty) {
element.style.setProperty('--scroll-offset', 0);
}
if (options.onDeactivate) {
options.onDeactivate(element, instance);
}
}
}
/**
* Register an element for observation
*
* @param {Element} element - Element to observe
* @param {Object} [options] - Instance-specific options (overrides defaults)
* @return {LineObserver} This instance for chaining
*/
register(element, options = {}) {
if (this.destroyed) {
console.warn('LineObserver: Cannot register after destroy()');
return this;
}
if (!(element instanceof Element)) {
console.warn('LineObserver: register() requires an Element');
return this;
}
if (this.instances.has(element)) {
console.warn('LineObserver: Element already registered');
return this;
}
const mergedOptions = { ...this.defaults, ...options };
const validActivateFrom = ['both', 'below', 'above'];
if (!validActivateFrom.includes(mergedOptions.activateFrom)) {
console.warn(
`LineObserver: Unsupported activateFrom "${mergedOptions.activateFrom}"`
);
mergedOptions.activateFrom = 'both';
}
const instance = {
element,
options: mergedOptions,
state: 'inactive',
activationScrollY: 0,
elementHeight: 0,
triggerLinePx: this.parseTriggerLine(mergedOptions.triggerLine),
observer: null,
};
// Create and attach observer
instance.observer = this.createObserver(instance);
this.instances.set(element, instance);
// Check initial state (element might already be in view)
this.checkInitialState(instance);
return this;
}
/**
* Check if element is already in active zone on registration.
*
* Called once at registration time. If the element already spans the
* trigger line, it activates immediately regardless of `activateFrom`
* (there is no scroll direction at registration time). Elements fully
* above the trigger line are marked as 'passed'.
*
* @param {Object} instance - The observer instance to check.
* @private
*/
checkInitialState(instance) {
const rect = instance.element.getBoundingClientRect();
const scrollY = window.scrollY;
const initialState = this._determineState(rect, instance.triggerLinePx);
if (initialState === 'active') {
// Override activationScrollY after transitionState sets it, because
// we know the element's exact position. The generic formula in
// transitionState assumes the edge just crossed the line; here
// the element may already be mid-overlap, so we calculate precisely:
// scrollY - (how far the trigger line is into the element).
this.transitionState(instance, 'active', scrollY);
const distanceIntoElement = instance.triggerLinePx - rect.top;
instance.activationScrollY = scrollY - distanceIntoElement;
} else if (initialState === 'passed') {
instance.state = 'passed';
}
}
/**
* Unregister an element
*
* @param {HTMLElement} element - Element to unregister
* @return {LineObserver} This instance for chaining
*/
unregister(element) {
const instance = this.instances.get(element);
if (!instance) {
return this;
}
// Clean up observer
if (instance.observer) {
instance.observer.disconnect();
}
// Remove from tracking sets
this.activeInstances.delete(instance);
this.nearInstances.delete(instance);
// Remove class and custom property
element.classList.remove(instance.options.activeClass);
if (instance.options.cssCustomProperty) {
element.style.removeProperty('--scroll-offset');
}
this.instances.delete(element);
return this;
}
/**
* Check if a state transition should be suppressed by activateFrom filtering.
*
* Truth table (suppress = true):
*
* activateFrom | direction | transition | suppress?
* -------------|-----------|--------------------|---------
* 'both' | any | any | no
* 'below' | 'down' | inactive → active | no (correct direction)
* 'below' | 'up' | inactive → active | YES (wrong direction)
* 'below' | 'up' | active → inactive | YES (suppress snap-back)
* 'below' | 'down' | passed → active | no (correct direction)
* 'below' | 'up' | passed → active | YES (wrong direction)
* 'below' | 'down' | active → passed | no (natural exit)
* 'above' | 'up' | passed → active | no (correct direction)
* 'above' | 'up' | inactive → active | no (correct direction)
* 'above' | 'down' | inactive → active | YES (wrong direction)
* 'above' | 'down' | passed → active | YES (wrong direction)
* 'above' | 'down' | active → passed | YES (suppress snap-back)
* 'above' | 'up' | active → inactive | no (natural exit)
*
* @param {string} activateFrom - The instance's activateFrom option ('both', 'below', 'above').
* @param {string} currentDirection - Current scroll direction ('up' or 'down').
* @param {string} oldState - The instance's current state.
* @param {string} newState - The proposed new state.
* @return {boolean} True if the transition should be suppressed.
* @private
*/
_shouldSuppressTransition(
activateFrom,
currentDirection,
oldState,
newState
) {
if (activateFrom === 'both') {
return false;
}
const wasActive = oldState === 'active';
const willBeActive = newState === 'active';
// 'below' = activate when element crosses trigger from below (scrollY increasing, currentDirection 'down')
// Suppress transitions when scrolling the wrong way (currentDirection 'up')
if (activateFrom === 'below' && currentDirection === 'up') {
if (!wasActive && willBeActive) {
return true;
}
if (wasActive && newState === 'inactive') {
return true;
}
}
// 'above' = activate when element crosses trigger from above (scrollY decreasing, currentDirection 'up')
// Suppress transitions when scrolling the wrong way (currentDirection 'down')
if (activateFrom === 'above' && currentDirection === 'down') {
if (!wasActive && willBeActive) {
return true;
}
if (wasActive && newState === 'passed') {
return true;
}
}
return false;
}
/**
* Main RAF loop
*
* Elements can be deactivated through two paths:
*
* 1. Near-instances pass (below): handles elements the IO band reports as
* near the trigger line. These get fresh getBoundingClientRect checks
* and precise state transitions every frame.
*
* 2. Active-instances pass (further below): handles elements that are active
* but have left the IO band (e.g., fast scrolling moved them far from the
* trigger line). These are caught by checking if the element has scrolled
* fully past the trigger line.
*
* Both paths apply activateFrom filtering via _shouldSuppressTransition().
* The `transitioned` Set prevents double-processing when an element appears
* in both nearInstances and activeInstances.
*
* @private
*/
tick() {
if (
this.destroyed ||
(this.activeInstances.size === 0 && this.nearInstances.size === 0)
) {
this.isRunning = false;
return;
}
const scrollY = window.scrollY;
// Skip all work if scroll position hasn't changed
if (scrollY === this.lastScrollY) {
this.rafId = requestAnimationFrame(this.tick);
return;
}
// Update direction tracking
this.currentDirection = scrollY > this.lastScrollY ? 'down' : 'up';
// === STATE TRANSITIONS FOR NEAR ELEMENTS ===
// IO only tells us an element is near the trigger line. The RAF
// loop does the precise state determination every frame.
const transitions = [];
for (const instance of this.nearInstances) {
if (!instance.element.isConnected) {
transitions.push({ instance, newState: 'inactive' });
continue;
}
const rect = instance.element.getBoundingClientRect();
const newState = this._determineState(rect, instance.triggerLinePx);
if (newState !== instance.state) {
if (
this._shouldSuppressTransition(
instance.options.activateFrom,
this.currentDirection,
instance.state,
newState
)
) {
continue;
}
transitions.push({ instance, newState });
}
}
// Track instances handled here to skip them in the active-instances pass
const transitioned = new Set();
for (const { instance, newState } of transitions) {
this.transitionState(instance, newState, scrollY);
transitioned.add(instance);
}
// === BATCH READ PHASE FOR ACTIVE ELEMENTS ===
const updates = [];
const deactivations = [];
for (const instance of this.activeInstances) {
// Skip instances already handled in the near-instances pass
if (transitioned.has(instance)) {
continue;
}
// Auto-deactivate elements that have been removed from the DOM
if (!instance.element.isConnected) {
deactivations.push({
instance,
newState: 'inactive',
});
continue;
}
const offset = Math.max(0, scrollY - instance.activationScrollY);
const rect = instance.element.getBoundingClientRect();
// Use the same geometric check as the near-instances pass
const newState = this._determineState(rect, instance.triggerLinePx);
if (newState !== 'active') {
if (
!this._shouldSuppressTransition(
instance.options.activateFrom,
this.currentDirection,
'active',
newState
)
) {
deactivations.push({
instance,
newState,
});
continue;
}
}
updates.push({ instance, offset });
}
// === BATCH WRITE PHASE ===
// Process deactivations
for (const { instance, newState } of deactivations) {
this.transitionState(instance, newState, scrollY);
}
// Process offset updates
for (const { instance, offset } of updates) {
// Set CSS custom property
if (instance.options.cssCustomProperty) {
instance.element.style.setProperty('--scroll-offset', offset);
}
// Call custom scroll handler if provided
if (instance.options.onScroll) {
instance.options.onScroll(offset, instance.element, instance);
}
}
// Update tracking
this.lastScrollY = scrollY;
// Continue loop
this.rafId = requestAnimationFrame(this.tick);
}
/**
* Start the RAF loop
*
* @private
*/
startLoop() {
if (!this.isRunning) {
this.isRunning = true;
this.lastScrollY = window.scrollY;
this.rafId = requestAnimationFrame(this.tick);
}
}
/**
* Handle window resize
*
* @private
*/
handleResize() {
clearTimeout(this.resizeTimeout);
this.resizeTimeout = setTimeout(() => {
if (this.destroyed) {
return;
}
this.viewportHeight = window.innerHeight;
this.viewportWidth = window.innerWidth;
// Recalculate trigger line, recreate observers, and re-check states
for (const instance of this.instances.values()) {
if (!instance.element.isConnected) {
continue;
}
if (instance.observer) {
instance.observer.disconnect();
}
instance.triggerLinePx = this.parseTriggerLine(
instance.options.triggerLine
);
instance.observer = this.createObserver(instance);
// Re-evaluate state in case trigger line moved across element
const rect = instance.element.getBoundingClientRect();
const newState = this._determineState(
rect,
instance.triggerLinePx
);
const scrollY = window.scrollY;
if (newState !== instance.state) {
this.transitionState(instance, newState, scrollY);
// Override activationScrollY with precise formula based on
// actual element position, same as checkInitialState.
if (newState === 'active') {
const distanceIntoElement =
instance.triggerLinePx - rect.top;
instance.activationScrollY =
scrollY - distanceIntoElement;
}
}
}
}, 150);
}
/**
* Get number of active instances
*
* @return {number} Count of active instances
*/
getActiveCount() {
return this.activeInstances.size;
}
/**
* Get total number of registered instances
*
* @return {number} Count of all registered instances
*/
getTotalCount() {
return this.instances.size;
}
/**
* Check if RAF loop is running
*
* @return {boolean} True if loop is active
*/
isActive() {
return this.isRunning;
}
/**
* Get current scroll direction
*
* @return {string} 'up' or 'down'
*/
getDirection() {
return this.currentDirection;
}
/**
* Clean up everything
*/
destroy() {
this.destroyed = true;
// Stop RAF
if (this.rafId) {
cancelAnimationFrame(this.rafId);
}
// Clear any pending resize timeout
if (this.resizeTimeout) {
clearTimeout(this.resizeTimeout);
}
// Disconnect all observers
for (const instance of this.instances.values()) {
if (instance.observer) {
instance.observer.disconnect();
}
instance.element.classList.remove(instance.options.activeClass);
if (instance.options.cssCustomProperty) {
instance.element.style.removeProperty('--scroll-offset');
}
}
this.instances.clear();
this.activeInstances.clear();
this.nearInstances.clear();
window.removeEventListener('resize', this.handleResize);
}
}
// Export for different module systems
if (typeof window !== 'undefined') {
window.LineObserver = LineObserver;
}
export { LineObserver };
export default LineObserver;