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