lenis 0.0.2 → 1.0.43-dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/lenis.js ADDED
@@ -0,0 +1,795 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
+ typeof define === 'function' && define.amd ? define(factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Lenis = factory());
5
+ })(this, (function () { 'use strict';
6
+
7
+ /******************************************************************************
8
+ Copyright (c) Microsoft Corporation.
9
+
10
+ Permission to use, copy, modify, and/or distribute this software for any
11
+ purpose with or without fee is hereby granted.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
+ PERFORMANCE OF THIS SOFTWARE.
20
+ ***************************************************************************** */
21
+ /* global Reflect, Promise, SuppressedError, Symbol */
22
+
23
+
24
+ var __assign = function() {
25
+ __assign = Object.assign || function __assign(t) {
26
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
27
+ s = arguments[i];
28
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
29
+ }
30
+ return t;
31
+ };
32
+ return __assign.apply(this, arguments);
33
+ };
34
+
35
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
36
+ var e = new Error(message);
37
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
38
+ };
39
+
40
+ var version = "1.0.43-dev.0";
41
+
42
+ // Clamp a value between a minimum and maximum value
43
+ function clamp(min, input, max) {
44
+ return Math.max(min, Math.min(input, max))
45
+ }
46
+
47
+ // Linearly interpolate between two values using an amount (0 <= t <= 1)
48
+ function lerp(x, y, t) {
49
+ return (1 - t) * x + t * y
50
+ }
51
+
52
+ // http://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/
53
+ function damp(x, y, lambda, dt) {
54
+ return lerp(x, y, 1 - Math.exp(-lambda * dt))
55
+ }
56
+
57
+ // Calculate the modulo of the dividend and divisor while keeping the result within the same sign as the divisor
58
+ // https://anguscroll.com/just/just-modulo
59
+ function modulo(n, d) {
60
+ return ((n % d) + d) % d
61
+ }
62
+
63
+ // Animate class to handle value animations with lerping or easing
64
+ class Animate {
65
+ // Advance the animation by the given delta time
66
+ advance(deltaTime) {
67
+ if (!this.isRunning) return
68
+
69
+ let completed = false;
70
+
71
+ if (this.lerp) {
72
+ this.value = damp(this.value, this.to, this.lerp * 60, deltaTime);
73
+ if (Math.round(this.value) === this.to) {
74
+ this.value = this.to;
75
+ completed = true;
76
+ }
77
+ } else {
78
+ this.currentTime += deltaTime;
79
+ const linearProgress = clamp(0, this.currentTime / this.duration, 1);
80
+
81
+ completed = linearProgress >= 1;
82
+ const easedProgress = completed ? 1 : this.easing(linearProgress);
83
+ this.value = this.from + (this.to - this.from) * easedProgress;
84
+ }
85
+
86
+ // Call the onUpdate callback with the current value and completed status
87
+ this.onUpdate?.(this.value, completed);
88
+
89
+ if (completed) {
90
+ this.stop();
91
+ }
92
+ }
93
+
94
+ // Stop the animation
95
+ stop() {
96
+ this.isRunning = false;
97
+ }
98
+
99
+ // Set up the animation from a starting value to an ending value
100
+ // with optional parameters for lerping, duration, easing, and onUpdate callback
101
+ fromTo(
102
+ from,
103
+ to,
104
+ { lerp = 0.1, duration = 1, easing = (t) => t, onStart, onUpdate }
105
+ ) {
106
+ this.from = this.value = from;
107
+ this.to = to;
108
+ this.lerp = lerp;
109
+ this.duration = duration;
110
+ this.easing = easing;
111
+ this.currentTime = 0;
112
+ this.isRunning = true;
113
+
114
+ onStart?.();
115
+ this.onUpdate = onUpdate;
116
+ }
117
+ }
118
+
119
+ function debounce(callback, delay) {
120
+ let timer;
121
+ return function () {
122
+ let args = arguments;
123
+ let context = this;
124
+ clearTimeout(timer);
125
+ timer = setTimeout(function () {
126
+ callback.apply(context, args);
127
+ }, delay);
128
+ }
129
+ }
130
+
131
+ class Dimensions {
132
+ constructor({
133
+ wrapper,
134
+ content,
135
+ autoResize = true,
136
+ debounce: debounceValue = 250,
137
+ } = {}) {
138
+ this.wrapper = wrapper;
139
+ this.content = content;
140
+
141
+ if (autoResize) {
142
+ this.debouncedResize = debounce(this.resize, debounceValue);
143
+
144
+ if (this.wrapper === window) {
145
+ window.addEventListener('resize', this.debouncedResize, false);
146
+ } else {
147
+ this.wrapperResizeObserver = new ResizeObserver(this.debouncedResize);
148
+ this.wrapperResizeObserver.observe(this.wrapper);
149
+ }
150
+
151
+ this.contentResizeObserver = new ResizeObserver(this.debouncedResize);
152
+ this.contentResizeObserver.observe(this.content);
153
+ }
154
+
155
+ this.resize();
156
+ }
157
+
158
+ destroy() {
159
+ this.wrapperResizeObserver?.disconnect();
160
+ this.contentResizeObserver?.disconnect();
161
+ window.removeEventListener('resize', this.debouncedResize, false);
162
+ }
163
+
164
+ resize = () => {
165
+ this.onWrapperResize();
166
+ this.onContentResize();
167
+ }
168
+
169
+ onWrapperResize = () => {
170
+ if (this.wrapper === window) {
171
+ this.width = window.innerWidth;
172
+ this.height = window.innerHeight;
173
+ } else {
174
+ this.width = this.wrapper.clientWidth;
175
+ this.height = this.wrapper.clientHeight;
176
+ }
177
+ }
178
+
179
+ onContentResize = () => {
180
+ if (this.wrapper === window) {
181
+ this.scrollHeight = this.content.scrollHeight;
182
+ this.scrollWidth = this.content.scrollWidth;
183
+ } else {
184
+ this.scrollHeight = this.wrapper.scrollHeight;
185
+ this.scrollWidth = this.wrapper.scrollWidth;
186
+ }
187
+ }
188
+
189
+ get limit() {
190
+ return {
191
+ x: this.scrollWidth - this.width,
192
+ y: this.scrollHeight - this.height,
193
+ }
194
+ }
195
+ }
196
+
197
+ class Emitter {
198
+ constructor() {
199
+ this.events = {};
200
+ }
201
+
202
+ emit(event, ...args) {
203
+ let callbacks = this.events[event] || [];
204
+ for (let i = 0, length = callbacks.length; i < length; i++) {
205
+ callbacks[i](...args);
206
+ }
207
+ }
208
+
209
+ on(event, cb) {
210
+ // Add the callback to the event's callback list, or create a new list with the callback
211
+ this.events[event]?.push(cb) || (this.events[event] = [cb]);
212
+
213
+ // Return an unsubscribe function
214
+ return () => {
215
+ this.events[event] = this.events[event]?.filter((i) => cb !== i);
216
+ }
217
+ }
218
+
219
+ off(event, callback) {
220
+ this.events[event] = this.events[event]?.filter((i) => callback !== i);
221
+ }
222
+
223
+ destroy() {
224
+ this.events = {};
225
+ }
226
+ }
227
+
228
+ const LINE_HEIGHT = 100 / 6;
229
+
230
+ class VirtualScroll {
231
+ constructor(element, { wheelMultiplier = 1, touchMultiplier = 1 }) {
232
+ this.element = element;
233
+ this.wheelMultiplier = wheelMultiplier;
234
+ this.touchMultiplier = touchMultiplier;
235
+
236
+ this.touchStart = {
237
+ x: null,
238
+ y: null,
239
+ };
240
+
241
+ this.emitter = new Emitter();
242
+ window.addEventListener('resize', this.onWindowResize, false);
243
+ this.onWindowResize();
244
+
245
+ this.element.addEventListener('wheel', this.onWheel, { passive: false });
246
+ this.element.addEventListener('touchstart', this.onTouchStart, {
247
+ passive: false,
248
+ });
249
+ this.element.addEventListener('touchmove', this.onTouchMove, {
250
+ passive: false,
251
+ });
252
+ this.element.addEventListener('touchend', this.onTouchEnd, {
253
+ passive: false,
254
+ });
255
+ }
256
+
257
+ // Add an event listener for the given event and callback
258
+ on(event, callback) {
259
+ return this.emitter.on(event, callback)
260
+ }
261
+
262
+ // Remove all event listeners and clean up
263
+ destroy() {
264
+ this.emitter.destroy();
265
+
266
+ window.removeEventListener('resize', this.onWindowResize, false);
267
+
268
+ this.element.removeEventListener('wheel', this.onWheel, {
269
+ passive: false,
270
+ });
271
+ this.element.removeEventListener('touchstart', this.onTouchStart, {
272
+ passive: false,
273
+ });
274
+ this.element.removeEventListener('touchmove', this.onTouchMove, {
275
+ passive: false,
276
+ });
277
+ this.element.removeEventListener('touchend', this.onTouchEnd, {
278
+ passive: false,
279
+ });
280
+ }
281
+
282
+ // Event handler for 'touchstart' event
283
+ onTouchStart = (event) => {
284
+ const { clientX, clientY } = event.targetTouches
285
+ ? event.targetTouches[0]
286
+ : event;
287
+
288
+ this.touchStart.x = clientX;
289
+ this.touchStart.y = clientY;
290
+
291
+ this.lastDelta = {
292
+ x: 0,
293
+ y: 0,
294
+ };
295
+
296
+ this.emitter.emit('scroll', {
297
+ deltaX: 0,
298
+ deltaY: 0,
299
+ event,
300
+ });
301
+ }
302
+
303
+ // Event handler for 'touchmove' event
304
+ onTouchMove = (event) => {
305
+ const { clientX, clientY } = event.targetTouches
306
+ ? event.targetTouches[0]
307
+ : event;
308
+
309
+ const deltaX = -(clientX - this.touchStart.x) * this.touchMultiplier;
310
+ const deltaY = -(clientY - this.touchStart.y) * this.touchMultiplier;
311
+
312
+ this.touchStart.x = clientX;
313
+ this.touchStart.y = clientY;
314
+
315
+ this.lastDelta = {
316
+ x: deltaX,
317
+ y: deltaY,
318
+ };
319
+
320
+ this.emitter.emit('scroll', {
321
+ deltaX,
322
+ deltaY,
323
+ event,
324
+ });
325
+ }
326
+
327
+ onTouchEnd = (event) => {
328
+ this.emitter.emit('scroll', {
329
+ deltaX: this.lastDelta.x,
330
+ deltaY: this.lastDelta.y,
331
+ event,
332
+ });
333
+ }
334
+
335
+ // Event handler for 'wheel' event
336
+ onWheel = (event) => {
337
+ let { deltaX, deltaY, deltaMode } = event;
338
+
339
+ const multiplierX =
340
+ deltaMode === 1 ? LINE_HEIGHT : deltaMode === 2 ? this.windowWidth : 1;
341
+ const multiplierY =
342
+ deltaMode === 1 ? LINE_HEIGHT : deltaMode === 2 ? this.windowHeight : 1;
343
+
344
+ deltaX *= multiplierX;
345
+ deltaY *= multiplierY;
346
+
347
+ deltaX *= this.wheelMultiplier;
348
+ deltaY *= this.wheelMultiplier;
349
+
350
+ this.emitter.emit('scroll', { deltaX, deltaY, event });
351
+ }
352
+
353
+ onWindowResize = () => {
354
+ this.windowWidth = window.innerWidth;
355
+ this.windowHeight = window.innerHeight;
356
+ }
357
+ }
358
+
359
+ var Lenis = /** @class */ (function () {
360
+ function Lenis(_a) {
361
+ var _b = _a === void 0 ? {} : _a, _c = _b.wrapper, wrapper = _c === void 0 ? window : _c, _d = _b.content, content = _d === void 0 ? document.documentElement : _d, _e = _b.wheelEventsTarget, wheelEventsTarget = _e === void 0 ? wrapper : _e, // deprecated
362
+ _f = _b.eventsTarget, // deprecated
363
+ eventsTarget = _f === void 0 ? wheelEventsTarget : _f, _g = _b.smoothWheel, smoothWheel = _g === void 0 ? true : _g, _h = _b.syncTouch, syncTouch = _h === void 0 ? false : _h, _j = _b.syncTouchLerp, syncTouchLerp = _j === void 0 ? 0.075 : _j, _k = _b.touchInertiaMultiplier, touchInertiaMultiplier = _k === void 0 ? 35 : _k, duration = _b.duration, // in seconds
364
+ _l = _b.easing, // in seconds
365
+ easing = _l === void 0 ? function (t) { return Math.min(1, 1.001 - Math.pow(2, -10 * t)); } : _l, _m = _b.lerp, lerp = _m === void 0 ? !duration && 0.1 : _m, _o = _b.infinite, infinite = _o === void 0 ? false : _o, _p = _b.orientation, orientation = _p === void 0 ? 'vertical' : _p, // vertical, horizontal
366
+ _q = _b.gestureOrientation, // vertical, horizontal
367
+ gestureOrientation = _q === void 0 ? 'vertical' : _q, // vertical, horizontal, both
368
+ _r = _b.touchMultiplier, // vertical, horizontal, both
369
+ touchMultiplier = _r === void 0 ? 1 : _r, _s = _b.wheelMultiplier, wheelMultiplier = _s === void 0 ? 1 : _s, _t = _b.autoResize, autoResize = _t === void 0 ? true : _t, _u = _b.__experimental__naiveDimensions, __experimental__naiveDimensions = _u === void 0 ? false : _u;
370
+ var _this = this;
371
+ this.__isSmooth = false; // true if scroll should be animated
372
+ this.__isScrolling = false; // true when scroll is animating
373
+ this.__isStopped = false; // true if user should not be able to scroll - enable/disable programmatically
374
+ this.__isLocked = false; // same as isStopped but enabled/disabled when scroll reaches target
375
+ this.onVirtualScroll = function (_a) {
376
+ var deltaX = _a.deltaX, deltaY = _a.deltaY, event = _a.event;
377
+ // keep zoom feature
378
+ if (event.ctrlKey)
379
+ return;
380
+ var isTouch = event.type.includes('touch');
381
+ var isWheel = event.type.includes('wheel');
382
+ var isTapToStop = _this.options.syncTouch &&
383
+ isTouch &&
384
+ event.type === 'touchstart' &&
385
+ !_this.isStopped &&
386
+ !_this.isLocked;
387
+ if (isTapToStop) {
388
+ _this.reset();
389
+ return;
390
+ }
391
+ var isClick = deltaX === 0 && deltaY === 0; // click event
392
+ // const isPullToRefresh =
393
+ // this.options.gestureOrientation === 'vertical' &&
394
+ // this.scroll === 0 &&
395
+ // !this.options.infinite &&
396
+ // deltaY <= 5 // touch pull to refresh, not reliable yet
397
+ var isUnknownGesture = (_this.options.gestureOrientation === 'vertical' && deltaY === 0) ||
398
+ (_this.options.gestureOrientation === 'horizontal' && deltaX === 0);
399
+ if (isClick || isUnknownGesture) {
400
+ // console.log('prevent')
401
+ return;
402
+ }
403
+ // catch if scrolling on nested scroll elements
404
+ var composedPath = event.composedPath();
405
+ composedPath = composedPath.slice(0, composedPath.indexOf(_this.rootElement)); // remove parents elements
406
+ if (!!composedPath.find(function (node) {
407
+ var _a, _b, _c, _d, _e;
408
+ return ((_a = node.hasAttribute) === null || _a === void 0 ? void 0 : _a.call(node, 'data-lenis-prevent')) ||
409
+ (isTouch && ((_b = node.hasAttribute) === null || _b === void 0 ? void 0 : _b.call(node, 'data-lenis-prevent-touch'))) ||
410
+ (isWheel && ((_c = node.hasAttribute) === null || _c === void 0 ? void 0 : _c.call(node, 'data-lenis-prevent-wheel'))) ||
411
+ (((_d = node.classList) === null || _d === void 0 ? void 0 : _d.contains('lenis')) &&
412
+ !((_e = node.classList) === null || _e === void 0 ? void 0 : _e.contains('lenis-stopped')));
413
+ } // nested lenis instance
414
+ ))
415
+ return;
416
+ if (_this.isStopped || _this.isLocked) {
417
+ event.preventDefault(); // this will stop forwarding the event to the parent, this is problematic
418
+ return;
419
+ }
420
+ _this.isSmooth =
421
+ (_this.options.syncTouch && isTouch) ||
422
+ (_this.options.smoothWheel && isWheel);
423
+ if (!_this.isSmooth) {
424
+ _this.isScrolling = false;
425
+ _this.animate.stop();
426
+ return;
427
+ }
428
+ event.preventDefault();
429
+ var delta = deltaY;
430
+ if (_this.options.gestureOrientation === 'both') {
431
+ delta = Math.abs(deltaY) > Math.abs(deltaX) ? deltaY : deltaX;
432
+ }
433
+ else if (_this.options.gestureOrientation === 'horizontal') {
434
+ delta = deltaX;
435
+ }
436
+ var syncTouch = isTouch && _this.options.syncTouch;
437
+ var isTouchEnd = isTouch && event.type === 'touchend';
438
+ var hasTouchInertia = isTouchEnd && Math.abs(delta) > 5;
439
+ if (hasTouchInertia) {
440
+ delta = _this.velocity * _this.options.touchInertiaMultiplier;
441
+ }
442
+ _this.scrollTo(_this.targetScroll + delta, __assign({ programmatic: false }, (syncTouch
443
+ ? {
444
+ lerp: hasTouchInertia ? _this.options.syncTouchLerp : 1,
445
+ }
446
+ : {
447
+ lerp: _this.options.lerp,
448
+ duration: _this.options.duration,
449
+ easing: _this.options.easing,
450
+ })));
451
+ };
452
+ this.onNativeScroll = function () {
453
+ if (_this.__preventNextScrollEvent)
454
+ return;
455
+ if (!_this.isScrolling) {
456
+ var lastScroll = _this.animatedScroll;
457
+ _this.animatedScroll = _this.targetScroll = _this.actualScroll;
458
+ _this.velocity = 0;
459
+ _this.direction = Math.sign(_this.animatedScroll - lastScroll);
460
+ _this.emit();
461
+ }
462
+ };
463
+ window.lenisVersion = version;
464
+ // if wrapper is html or body, fallback to window
465
+ if (wrapper === document.documentElement || wrapper === document.body) {
466
+ wrapper = window;
467
+ }
468
+ this.options = {
469
+ wrapper: wrapper,
470
+ content: content,
471
+ wheelEventsTarget: wheelEventsTarget,
472
+ eventsTarget: eventsTarget,
473
+ smoothWheel: smoothWheel,
474
+ syncTouch: syncTouch,
475
+ syncTouchLerp: syncTouchLerp,
476
+ touchInertiaMultiplier: touchInertiaMultiplier,
477
+ duration: duration,
478
+ easing: easing,
479
+ lerp: lerp,
480
+ infinite: infinite,
481
+ gestureOrientation: gestureOrientation,
482
+ orientation: orientation,
483
+ touchMultiplier: touchMultiplier,
484
+ wheelMultiplier: wheelMultiplier,
485
+ autoResize: autoResize,
486
+ __experimental__naiveDimensions: __experimental__naiveDimensions,
487
+ };
488
+ this.animate = new Animate();
489
+ this.emitter = new Emitter();
490
+ this.dimensions = new Dimensions({ wrapper: wrapper, content: content, autoResize: autoResize });
491
+ this.toggleClassName('lenis', true);
492
+ this.velocity = 0;
493
+ this.isLocked = false;
494
+ this.isStopped = false;
495
+ this.isSmooth = syncTouch || smoothWheel;
496
+ this.isScrolling = false;
497
+ this.targetScroll = this.animatedScroll = this.actualScroll;
498
+ this.options.wrapper.addEventListener('scroll', this.onNativeScroll, false);
499
+ this.virtualScroll = new VirtualScroll(eventsTarget, {
500
+ touchMultiplier: touchMultiplier,
501
+ wheelMultiplier: wheelMultiplier,
502
+ });
503
+ this.virtualScroll.on('scroll', this.onVirtualScroll);
504
+ }
505
+ Lenis.prototype.destroy = function () {
506
+ this.emitter.destroy();
507
+ this.options.wrapper.removeEventListener('scroll', this.onNativeScroll, false);
508
+ this.virtualScroll.destroy();
509
+ this.dimensions.destroy();
510
+ this.toggleClassName('lenis', false);
511
+ this.toggleClassName('lenis-smooth', false);
512
+ this.toggleClassName('lenis-scrolling', false);
513
+ this.toggleClassName('lenis-stopped', false);
514
+ this.toggleClassName('lenis-locked', false);
515
+ };
516
+ Lenis.prototype.on = function (event, callback) {
517
+ return this.emitter.on(event, callback);
518
+ };
519
+ Lenis.prototype.off = function (event, callback) {
520
+ return this.emitter.off(event, callback);
521
+ };
522
+ Lenis.prototype.setScroll = function (scroll) {
523
+ // apply scroll value immediately
524
+ if (this.isHorizontal) {
525
+ this.rootElement.scrollLeft = scroll;
526
+ }
527
+ else {
528
+ this.rootElement.scrollTop = scroll;
529
+ }
530
+ };
531
+ Lenis.prototype.resize = function () {
532
+ this.dimensions.resize();
533
+ };
534
+ Lenis.prototype.emit = function () {
535
+ this.emitter.emit('scroll', this);
536
+ };
537
+ Lenis.prototype.reset = function () {
538
+ this.isLocked = false;
539
+ this.isScrolling = false;
540
+ this.animatedScroll = this.targetScroll = this.actualScroll;
541
+ this.velocity = 0;
542
+ this.animate.stop();
543
+ };
544
+ Lenis.prototype.start = function () {
545
+ if (!this.isStopped)
546
+ return;
547
+ this.isStopped = false;
548
+ this.reset();
549
+ };
550
+ Lenis.prototype.stop = function () {
551
+ if (this.isStopped)
552
+ return;
553
+ this.isStopped = true;
554
+ this.animate.stop();
555
+ this.reset();
556
+ };
557
+ Lenis.prototype.raf = function (time) {
558
+ var deltaTime = time - (this.time || time);
559
+ this.time = time;
560
+ this.animate.advance(deltaTime * 0.001);
561
+ };
562
+ Lenis.prototype.scrollTo = function (target, _a) {
563
+ var _this = this;
564
+ var _b = _a === void 0 ? {} : _a, _c = _b.offset, offset = _c === void 0 ? 0 : _c, _d = _b.immediate, immediate = _d === void 0 ? false : _d, _e = _b.lock, lock = _e === void 0 ? false : _e, _f = _b.duration, duration = _f === void 0 ? this.options.duration : _f, _g = _b.easing, easing = _g === void 0 ? this.options.easing : _g, _h = _b.lerp, lerp = _h === void 0 ? !duration && this.options.lerp : _h, onComplete = _b.onComplete, _j = _b.force, force = _j === void 0 ? false : _j, // scroll even if stopped
565
+ _k = _b.programmatic, // scroll even if stopped
566
+ programmatic = _k === void 0 ? true : _k;
567
+ if ((this.isStopped || this.isLocked) && !force)
568
+ return;
569
+ // keywords
570
+ if (['top', 'left', 'start'].includes(target)) {
571
+ target = 0;
572
+ }
573
+ else if (['bottom', 'right', 'end'].includes(target)) {
574
+ target = this.limit;
575
+ }
576
+ else {
577
+ var node = void 0;
578
+ if (typeof target === 'string') {
579
+ // CSS selector
580
+ node = document.querySelector(target);
581
+ }
582
+ else if (target === null || target === void 0 ? void 0 : target.nodeType) {
583
+ // Node element
584
+ node = target;
585
+ }
586
+ if (node) {
587
+ if (this.options.wrapper !== window) {
588
+ // nested scroll offset correction
589
+ var wrapperRect = this.options.wrapper.getBoundingClientRect();
590
+ offset -= this.isHorizontal ? wrapperRect.left : wrapperRect.top;
591
+ }
592
+ var rect = node.getBoundingClientRect();
593
+ target =
594
+ (this.isHorizontal ? rect.left : rect.top) + this.animatedScroll;
595
+ }
596
+ }
597
+ if (typeof target !== 'number')
598
+ return;
599
+ target += offset;
600
+ target = Math.round(target);
601
+ if (this.options.infinite) {
602
+ if (programmatic) {
603
+ this.targetScroll = this.animatedScroll = this.scroll;
604
+ }
605
+ }
606
+ else {
607
+ target = clamp(0, target, this.limit);
608
+ }
609
+ if (immediate) {
610
+ this.animatedScroll = this.targetScroll = target;
611
+ this.setScroll(this.scroll);
612
+ this.reset();
613
+ onComplete === null || onComplete === void 0 ? void 0 : onComplete(this);
614
+ return;
615
+ }
616
+ if (!programmatic) {
617
+ if (target === this.targetScroll)
618
+ return;
619
+ this.targetScroll = target;
620
+ }
621
+ this.animate.fromTo(this.animatedScroll, target, {
622
+ duration: duration,
623
+ easing: easing,
624
+ lerp: lerp,
625
+ onStart: function () {
626
+ // started
627
+ if (lock)
628
+ _this.isLocked = true;
629
+ _this.isScrolling = true;
630
+ },
631
+ onUpdate: function (value, completed) {
632
+ _this.isScrolling = true;
633
+ // updated
634
+ _this.velocity = value - _this.animatedScroll;
635
+ _this.direction = Math.sign(_this.velocity);
636
+ _this.animatedScroll = value;
637
+ _this.setScroll(_this.scroll);
638
+ if (programmatic) {
639
+ // wheel during programmatic should stop it
640
+ _this.targetScroll = value;
641
+ }
642
+ if (!completed)
643
+ _this.emit();
644
+ if (completed) {
645
+ _this.reset();
646
+ _this.emit();
647
+ onComplete === null || onComplete === void 0 ? void 0 : onComplete(_this);
648
+ // avoid emitting event twice
649
+ _this.__preventNextScrollEvent = true;
650
+ requestAnimationFrame(function () {
651
+ delete _this.__preventNextScrollEvent;
652
+ });
653
+ }
654
+ },
655
+ });
656
+ };
657
+ Object.defineProperty(Lenis.prototype, "rootElement", {
658
+ get: function () {
659
+ return this.options.wrapper === window
660
+ ? document.documentElement
661
+ : this.options.wrapper;
662
+ },
663
+ enumerable: false,
664
+ configurable: true
665
+ });
666
+ Object.defineProperty(Lenis.prototype, "limit", {
667
+ get: function () {
668
+ if (this.options.__experimental__naiveDimensions) {
669
+ if (this.isHorizontal) {
670
+ return this.rootElement.scrollWidth - this.rootElement.clientWidth;
671
+ }
672
+ else {
673
+ return this.rootElement.scrollHeight - this.rootElement.clientHeight;
674
+ }
675
+ }
676
+ else {
677
+ return this.dimensions.limit[this.isHorizontal ? 'x' : 'y'];
678
+ }
679
+ },
680
+ enumerable: false,
681
+ configurable: true
682
+ });
683
+ Object.defineProperty(Lenis.prototype, "isHorizontal", {
684
+ get: function () {
685
+ return this.options.orientation === 'horizontal';
686
+ },
687
+ enumerable: false,
688
+ configurable: true
689
+ });
690
+ Object.defineProperty(Lenis.prototype, "actualScroll", {
691
+ get: function () {
692
+ // value browser takes into account
693
+ return this.isHorizontal
694
+ ? this.rootElement.scrollLeft
695
+ : this.rootElement.scrollTop;
696
+ },
697
+ enumerable: false,
698
+ configurable: true
699
+ });
700
+ Object.defineProperty(Lenis.prototype, "scroll", {
701
+ get: function () {
702
+ return this.options.infinite
703
+ ? modulo(this.animatedScroll, this.limit)
704
+ : this.animatedScroll;
705
+ },
706
+ enumerable: false,
707
+ configurable: true
708
+ });
709
+ Object.defineProperty(Lenis.prototype, "progress", {
710
+ get: function () {
711
+ // avoid progress to be NaN
712
+ return this.limit === 0 ? 1 : this.scroll / this.limit;
713
+ },
714
+ enumerable: false,
715
+ configurable: true
716
+ });
717
+ Object.defineProperty(Lenis.prototype, "isSmooth", {
718
+ get: function () {
719
+ return this.__isSmooth;
720
+ },
721
+ set: function (value) {
722
+ if (this.__isSmooth !== value) {
723
+ this.__isSmooth = value;
724
+ this.toggleClassName('lenis-smooth', value);
725
+ }
726
+ },
727
+ enumerable: false,
728
+ configurable: true
729
+ });
730
+ Object.defineProperty(Lenis.prototype, "isScrolling", {
731
+ get: function () {
732
+ return this.__isScrolling;
733
+ },
734
+ set: function (value) {
735
+ if (this.__isScrolling !== value) {
736
+ this.__isScrolling = value;
737
+ this.toggleClassName('lenis-scrolling', value);
738
+ }
739
+ },
740
+ enumerable: false,
741
+ configurable: true
742
+ });
743
+ Object.defineProperty(Lenis.prototype, "isStopped", {
744
+ get: function () {
745
+ return this.__isStopped;
746
+ },
747
+ set: function (value) {
748
+ if (this.__isStopped !== value) {
749
+ this.__isStopped = value;
750
+ this.toggleClassName('lenis-stopped', value);
751
+ }
752
+ },
753
+ enumerable: false,
754
+ configurable: true
755
+ });
756
+ Object.defineProperty(Lenis.prototype, "isLocked", {
757
+ get: function () {
758
+ return this.__isLocked;
759
+ },
760
+ set: function (value) {
761
+ if (this.__isLocked !== value) {
762
+ this.__isLocked = value;
763
+ this.toggleClassName('lenis-locked', value);
764
+ }
765
+ },
766
+ enumerable: false,
767
+ configurable: true
768
+ });
769
+ Object.defineProperty(Lenis.prototype, "className", {
770
+ get: function () {
771
+ var className = 'lenis';
772
+ if (this.isStopped)
773
+ className += ' lenis-stopped';
774
+ if (this.isLocked)
775
+ className += ' lenis-locked';
776
+ if (this.isScrolling)
777
+ className += ' lenis-scrolling';
778
+ if (this.isSmooth)
779
+ className += ' lenis-smooth';
780
+ return className;
781
+ },
782
+ enumerable: false,
783
+ configurable: true
784
+ });
785
+ Lenis.prototype.toggleClassName = function (name, value) {
786
+ this.rootElement.classList.toggle(name, value);
787
+ this.emitter.emit('className change', this);
788
+ };
789
+ return Lenis;
790
+ }());
791
+
792
+ return Lenis;
793
+
794
+ }));
795
+ //# sourceMappingURL=lenis.js.map