lenis 1.1.8 → 1.1.10-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.
@@ -1,269 +1,342 @@
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
- function debounce(callback, delay) {
8
- let timer;
9
- return function () {
10
- let args = arguments;
11
- let context = this;
12
- clearTimeout(timer);
13
- timer = setTimeout(function () {
14
- callback.apply(context, args);
15
- }, delay);
16
- }
17
- }
7
+ function debounce(callback, delay) {
8
+ let timer;
9
+ return function (...args) {
10
+ let context = this;
11
+ clearTimeout(timer);
12
+ timer = setTimeout(() => {
13
+ timer = undefined;
14
+ callback.apply(context, args);
15
+ }, delay);
16
+ };
17
+ }
18
18
 
19
- function removeParentSticky(element) {
20
- const position = getComputedStyle(element).position;
21
- const isSticky = position === 'sticky';
22
- if (isSticky) {
23
- element.style.setProperty('position', 'static');
24
- element.dataset.sticky = 'true';
25
- }
26
- if (element.offsetParent) {
27
- removeParentSticky(element.offsetParent);
28
- }
29
- }
30
- function addParentSticky(element) {
31
- var _a;
32
- if (((_a = element === null || element === void 0 ? void 0 : element.dataset) === null || _a === void 0 ? void 0 : _a.sticky) === 'true') {
33
- element.style.removeProperty('position');
34
- delete element.dataset.sticky;
35
- }
36
- if (element.offsetParent) {
37
- addParentSticky(element.offsetParent);
38
- }
39
- }
40
- function offsetTop(element, accumulator = 0) {
41
- const top = accumulator + element.offsetTop;
42
- if (element.offsetParent) {
43
- return offsetTop(element.offsetParent, top);
44
- }
45
- return top;
46
- }
47
- function offsetLeft(element, accumulator = 0) {
48
- const left = accumulator + element.offsetLeft;
49
- if (element.offsetParent) {
50
- return offsetLeft(element.offsetParent, left);
51
- }
52
- return left;
53
- }
54
- function scrollTop(element, accumulator = 0) {
55
- const top = accumulator + element.scrollTop;
56
- if (element.offsetParent) {
57
- return scrollTop(element.offsetParent, top);
58
- }
59
- return top + window.scrollY;
60
- }
61
- function scrollLeft(element, accumulator = 0) {
62
- const left = accumulator + element.scrollLeft;
63
- if (element.offsetParent) {
64
- return scrollLeft(element.offsetParent, left);
65
- }
66
- return left + window.scrollX;
67
- }
68
- class SnapElement {
69
- constructor(element, { align = ['start'], ignoreSticky = true, ignoreTransform = false, } = {}) {
70
- this.rect = {};
71
- this.onWrapperResize = () => {
72
- let top, left;
73
- if (this.options.ignoreSticky)
74
- removeParentSticky(this.element);
75
- if (this.options.ignoreTransform) {
76
- top = offsetTop(this.element);
77
- left = offsetLeft(this.element);
78
- }
79
- else {
80
- const rect = this.element.getBoundingClientRect();
81
- top = rect.top + scrollTop(this.element);
82
- left = rect.left + scrollLeft(this.element);
83
- }
84
- if (this.options.ignoreSticky)
85
- addParentSticky(this.element);
86
- this.setRect({ top, left });
87
- };
88
- this.onResize = ([entry]) => {
89
- const width = entry.borderBoxSize[0].inlineSize;
90
- const height = entry.borderBoxSize[0].blockSize;
91
- this.setRect({ width, height });
92
- };
93
- this.element = element;
94
- this.options = { align, ignoreSticky, ignoreTransform };
95
- this.align = [align].flat();
96
- this.wrapperResizeObserver = new ResizeObserver(this.onWrapperResize);
97
- this.wrapperResizeObserver.observe(document.body);
98
- this.onWrapperResize();
99
- this.resizeObserver = new ResizeObserver(this.onResize);
100
- this.resizeObserver.observe(this.element);
101
- this.setRect({
102
- width: this.element.offsetWidth,
103
- height: this.element.offsetHeight,
104
- });
105
- }
106
- destroy() {
107
- this.wrapperResizeObserver.disconnect();
108
- this.resizeObserver.disconnect();
109
- }
110
- setRect({ top, left, width, height, element, } = {}) {
111
- top = top !== null && top !== void 0 ? top : this.rect.top;
112
- left = left !== null && left !== void 0 ? left : this.rect.left;
113
- width = width !== null && width !== void 0 ? width : this.rect.width;
114
- height = height !== null && height !== void 0 ? height : this.rect.height;
115
- element = element !== null && element !== void 0 ? element : this.rect.element;
116
- if (top === this.rect.top &&
117
- left === this.rect.left &&
118
- width === this.rect.width &&
119
- height === this.rect.height &&
120
- element === this.rect.element)
121
- return;
122
- this.rect.top = top;
123
- this.rect.y = top;
124
- this.rect.width = width;
125
- this.rect.height = height;
126
- this.rect.left = left;
127
- this.rect.x = left;
128
- this.rect.bottom = top + height;
129
- this.rect.right = left + width;
130
- }
131
- }
19
+ function removeParentSticky(element) {
20
+ const position = getComputedStyle(element).position;
21
+ const isSticky = position === 'sticky';
22
+ if (isSticky) {
23
+ element.style.setProperty('position', 'static');
24
+ element.dataset.sticky = 'true';
25
+ }
26
+ if (element.offsetParent) {
27
+ removeParentSticky(element.offsetParent);
28
+ }
29
+ }
30
+ function addParentSticky(element) {
31
+ var _a;
32
+ if (((_a = element === null || element === void 0 ? void 0 : element.dataset) === null || _a === void 0 ? void 0 : _a.sticky) === 'true') {
33
+ element.style.removeProperty('position');
34
+ delete element.dataset.sticky;
35
+ }
36
+ if (element.offsetParent) {
37
+ addParentSticky(element.offsetParent);
38
+ }
39
+ }
40
+ function offsetTop(element, accumulator = 0) {
41
+ const top = accumulator + element.offsetTop;
42
+ if (element.offsetParent) {
43
+ return offsetTop(element.offsetParent, top);
44
+ }
45
+ return top;
46
+ }
47
+ function offsetLeft(element, accumulator = 0) {
48
+ const left = accumulator + element.offsetLeft;
49
+ if (element.offsetParent) {
50
+ return offsetLeft(element.offsetParent, left);
51
+ }
52
+ return left;
53
+ }
54
+ function scrollTop(element, accumulator = 0) {
55
+ const top = accumulator + element.scrollTop;
56
+ if (element.offsetParent) {
57
+ return scrollTop(element.offsetParent, top);
58
+ }
59
+ return top + window.scrollY;
60
+ }
61
+ function scrollLeft(element, accumulator = 0) {
62
+ const left = accumulator + element.scrollLeft;
63
+ if (element.offsetParent) {
64
+ return scrollLeft(element.offsetParent, left);
65
+ }
66
+ return left + window.scrollX;
67
+ }
68
+ class SnapElement {
69
+ constructor(element, { align = ['start'], ignoreSticky = true, ignoreTransform = false, } = {}) {
70
+ // @ts-ignore
71
+ this.rect = {};
72
+ this.onWrapperResize = () => {
73
+ let top, left;
74
+ if (this.options.ignoreSticky)
75
+ removeParentSticky(this.element);
76
+ if (this.options.ignoreTransform) {
77
+ top = offsetTop(this.element);
78
+ left = offsetLeft(this.element);
79
+ }
80
+ else {
81
+ const rect = this.element.getBoundingClientRect();
82
+ top = rect.top + scrollTop(this.element);
83
+ left = rect.left + scrollLeft(this.element);
84
+ }
85
+ if (this.options.ignoreSticky)
86
+ addParentSticky(this.element);
87
+ this.setRect({ top, left });
88
+ };
89
+ this.onResize = ([entry]) => {
90
+ const width = entry.borderBoxSize[0].inlineSize;
91
+ const height = entry.borderBoxSize[0].blockSize;
92
+ this.setRect({ width, height });
93
+ };
94
+ this.element = element;
95
+ this.options = { align, ignoreSticky, ignoreTransform };
96
+ // this.ignoreSticky = ignoreSticky
97
+ // this.ignoreTransform = ignoreTransform
98
+ this.align = [align].flat();
99
+ // TODO: assing rect immediately
100
+ this.wrapperResizeObserver = new ResizeObserver(this.onWrapperResize);
101
+ this.wrapperResizeObserver.observe(document.body);
102
+ this.onWrapperResize();
103
+ this.resizeObserver = new ResizeObserver(this.onResize);
104
+ this.resizeObserver.observe(this.element);
105
+ this.setRect({
106
+ width: this.element.offsetWidth,
107
+ height: this.element.offsetHeight,
108
+ });
109
+ }
110
+ destroy() {
111
+ this.wrapperResizeObserver.disconnect();
112
+ this.resizeObserver.disconnect();
113
+ }
114
+ setRect({ top, left, width, height, element, } = {}) {
115
+ top = top !== null && top !== void 0 ? top : this.rect.top;
116
+ left = left !== null && left !== void 0 ? left : this.rect.left;
117
+ width = width !== null && width !== void 0 ? width : this.rect.width;
118
+ height = height !== null && height !== void 0 ? height : this.rect.height;
119
+ element = element !== null && element !== void 0 ? element : this.rect.element;
120
+ if (top === this.rect.top &&
121
+ left === this.rect.left &&
122
+ width === this.rect.width &&
123
+ height === this.rect.height &&
124
+ element === this.rect.element)
125
+ return;
126
+ this.rect.top = top;
127
+ this.rect.y = top;
128
+ this.rect.width = width;
129
+ this.rect.height = height;
130
+ this.rect.left = left;
131
+ this.rect.x = left;
132
+ this.rect.bottom = top + height;
133
+ this.rect.right = left + width;
134
+ }
135
+ }
132
136
 
133
- let index = 0;
134
- function uid() {
135
- return index++;
136
- }
137
+ let index = 0;
138
+ function uid() {
139
+ return index++;
140
+ }
137
141
 
138
- class Snap {
139
- constructor(lenis, { type = 'mandatory', lerp, easing, duration, velocityThreshold = 1, debounce: debounceDelay = 0, onSnapStart, onSnapComplete, } = {}) {
140
- this.isStopped = false;
141
- this.onWindowResize = () => {
142
- this.viewport.width = window.innerWidth;
143
- this.viewport.height = window.innerHeight;
144
- };
145
- this.onScroll = ({ lastVelocity, velocity, userData, }) => {
146
- if (this.isStopped)
147
- return;
148
- const isDecelerating = Math.abs(lastVelocity) > Math.abs(velocity);
149
- const isTurningBack = Math.sign(lastVelocity) !== Math.sign(velocity) && velocity !== 0;
150
- if (Math.abs(velocity) < this.options.velocityThreshold &&
151
- isDecelerating &&
152
- !isTurningBack &&
153
- (userData === null || userData === void 0 ? void 0 : userData.initiator) !== 'snap') {
154
- this.onSnapDebounced();
155
- }
156
- };
157
- this.onSnap = () => {
158
- let { scroll, isHorizontal } = this.lenis;
159
- scroll = Math.ceil(this.lenis.scroll);
160
- let snaps = [...this.snaps.values()];
161
- this.elements.forEach(({ rect, align }) => {
162
- let value;
163
- align.forEach((align) => {
164
- if (align === 'start') {
165
- value = rect.top;
166
- }
167
- else if (align === 'center') {
168
- value = isHorizontal
169
- ? rect.left + rect.width / 2 - this.viewport.width / 2
170
- : rect.top + rect.height / 2 - this.viewport.height / 2;
171
- }
172
- else if (align === 'end') {
173
- value = isHorizontal
174
- ? rect.left + rect.width - this.viewport.width
175
- : rect.top + rect.height - this.viewport.height;
176
- }
177
- if (typeof value === 'number') {
178
- snaps.push({ value: Math.ceil(value), userData: {} });
179
- }
180
- });
181
- });
182
- snaps = snaps.sort((a, b) => Math.abs(a.value) - Math.abs(b.value));
183
- let prevSnap = snaps.findLast(({ value }) => value <= scroll);
184
- if (prevSnap === undefined)
185
- prevSnap = snaps[0];
186
- const distanceToPrevSnap = Math.abs(scroll - prevSnap.value);
187
- let nextSnap = snaps.find(({ value }) => value >= scroll);
188
- if (nextSnap === undefined)
189
- nextSnap = snaps[snaps.length - 1];
190
- const distanceToNextSnap = Math.abs(scroll - nextSnap.value);
191
- const snap = distanceToPrevSnap < distanceToNextSnap ? prevSnap : nextSnap;
192
- const distance = Math.abs(scroll - snap.value);
193
- if (this.options.type === 'mandatory' ||
194
- (this.options.type === 'proximity' &&
195
- distance <=
196
- (isHorizontal
197
- ? this.lenis.dimensions.width
198
- : this.lenis.dimensions.height))) {
199
- this.lenis.scrollTo(snap.value, {
200
- lerp: this.options.lerp,
201
- easing: this.options.easing,
202
- duration: this.options.duration,
203
- userData: { initiator: 'snap' },
204
- onStart: () => {
205
- var _a, _b;
206
- (_b = (_a = this.options).onSnapStart) === null || _b === void 0 ? void 0 : _b.call(_a, snap);
207
- },
208
- onComplete: () => {
209
- var _a, _b;
210
- (_b = (_a = this.options).onSnapComplete) === null || _b === void 0 ? void 0 : _b.call(_a, snap);
211
- },
212
- });
213
- }
214
- };
215
- this.lenis = lenis;
216
- this.options = {
217
- type,
218
- lerp,
219
- easing,
220
- duration,
221
- velocityThreshold,
222
- debounce: debounceDelay,
223
- onSnapStart,
224
- onSnapComplete,
225
- };
226
- this.elements = new Map();
227
- this.snaps = new Map();
228
- this.viewport = {
229
- width: window.innerWidth,
230
- height: window.innerHeight,
231
- };
232
- this.onWindowResize();
233
- window.addEventListener('resize', this.onWindowResize, false);
234
- this.onSnapDebounced = debounce(this.onSnap, this.options.debounce);
235
- this.lenis.on('scroll', this.onScroll);
236
- }
237
- destroy() {
238
- this.lenis.off('scroll', this.onScroll);
239
- window.removeEventListener('resize', this.onWindowResize, false);
240
- this.elements.forEach((element) => element.destroy());
241
- }
242
- start() {
243
- this.isStopped = false;
244
- }
245
- stop() {
246
- this.isStopped = true;
247
- }
248
- add(value, userData = {}) {
249
- const id = uid();
250
- this.snaps.set(id, { value, userData });
251
- return () => this.remove(id);
252
- }
253
- remove(id) {
254
- this.snaps.delete(id);
255
- }
256
- addElement(element, options = {}) {
257
- const id = uid();
258
- this.elements.set(id, new SnapElement(element, options));
259
- return () => this.removeElement(id);
260
- }
261
- removeElement(id) {
262
- this.elements.delete(id);
263
- }
264
- }
142
+ /**
143
+ * Snap class to handle the snap functionality
144
+ *
145
+ * @example
146
+ * const snap = new Snap(lenis, {
147
+ * type: 'mandatory', // 'mandatory', 'proximity'
148
+ * lerp: 0.1,
149
+ * duration: 1,
150
+ * easing: (t) => t,
151
+ * onSnapStart: (snap) => {
152
+ * console.log('onSnapStart', snap)
153
+ * },
154
+ * onSnapComplete: (snap) => {
155
+ * console.log('onSnapComplete', snap)
156
+ * },
157
+ * })
158
+ *
159
+ * snap.add(500) // snap at 500px
160
+ *
161
+ * const removeSnap = snap.add(500)
162
+ *
163
+ * if (someCondition) {
164
+ * removeSnap()
165
+ * }
166
+ */
167
+ class Snap {
168
+ constructor(lenis, { type = 'mandatory', lerp, easing, duration, velocityThreshold = 1, debounce: debounceDelay = 0, onSnapStart, onSnapComplete, } = {}) {
169
+ this.lenis = lenis;
170
+ this.elements = new Map();
171
+ this.snaps = new Map();
172
+ this.viewport = {
173
+ width: window.innerWidth,
174
+ height: window.innerHeight,
175
+ };
176
+ this.isStopped = false;
177
+ this.onWindowResize = () => {
178
+ this.viewport.width = window.innerWidth;
179
+ this.viewport.height = window.innerHeight;
180
+ };
181
+ this.onScroll = ({
182
+ // scroll,
183
+ // limit,
184
+ lastVelocity, velocity,
185
+ // isScrolling,
186
+ userData, }) => {
187
+ if (this.isStopped)
188
+ return;
189
+ // return
190
+ const isDecelerating = Math.abs(lastVelocity) > Math.abs(velocity);
191
+ const isTurningBack = Math.sign(lastVelocity) !== Math.sign(velocity) && velocity !== 0;
192
+ if (Math.abs(velocity) < this.options.velocityThreshold &&
193
+ // !isTouching &&
194
+ isDecelerating &&
195
+ !isTurningBack &&
196
+ (userData === null || userData === void 0 ? void 0 : userData.initiator) !== 'snap') {
197
+ this.onSnapDebounced();
198
+ }
199
+ };
200
+ this.onSnap = () => {
201
+ let { scroll, isHorizontal } = this.lenis;
202
+ scroll = Math.ceil(this.lenis.scroll);
203
+ let snaps = [...this.snaps.values()];
204
+ this.elements.forEach(({ rect, align }) => {
205
+ let value;
206
+ align.forEach((align) => {
207
+ if (align === 'start') {
208
+ value = rect.top;
209
+ }
210
+ else if (align === 'center') {
211
+ value = isHorizontal
212
+ ? rect.left + rect.width / 2 - this.viewport.width / 2
213
+ : rect.top + rect.height / 2 - this.viewport.height / 2;
214
+ }
215
+ else if (align === 'end') {
216
+ value = isHorizontal
217
+ ? rect.left + rect.width - this.viewport.width
218
+ : rect.top + rect.height - this.viewport.height;
219
+ }
220
+ if (typeof value === 'number') {
221
+ snaps.push({ value: Math.ceil(value), userData: {} });
222
+ }
223
+ });
224
+ });
225
+ snaps = snaps.sort((a, b) => Math.abs(a.value) - Math.abs(b.value));
226
+ let prevSnap = snaps.findLast(({ value }) => value <= scroll);
227
+ if (prevSnap === undefined)
228
+ prevSnap = snaps[0];
229
+ const distanceToPrevSnap = Math.abs(scroll - prevSnap.value);
230
+ let nextSnap = snaps.find(({ value }) => value >= scroll);
231
+ if (nextSnap === undefined)
232
+ nextSnap = snaps[snaps.length - 1];
233
+ const distanceToNextSnap = Math.abs(scroll - nextSnap.value);
234
+ const snap = distanceToPrevSnap < distanceToNextSnap ? prevSnap : nextSnap;
235
+ const distance = Math.abs(scroll - snap.value);
236
+ if (this.options.type === 'mandatory' ||
237
+ (this.options.type === 'proximity' &&
238
+ distance <=
239
+ (isHorizontal
240
+ ? this.lenis.dimensions.width
241
+ : this.lenis.dimensions.height))) {
242
+ // this.__isScrolling = true
243
+ // this.onSnapStart?.(snap)
244
+ // console.log('scroll to')
245
+ this.lenis.scrollTo(snap.value, {
246
+ lerp: this.options.lerp,
247
+ easing: this.options.easing,
248
+ duration: this.options.duration,
249
+ userData: { initiator: 'snap' },
250
+ onStart: () => {
251
+ var _a, _b;
252
+ (_b = (_a = this.options).onSnapStart) === null || _b === void 0 ? void 0 : _b.call(_a, snap);
253
+ },
254
+ onComplete: () => {
255
+ var _a, _b;
256
+ (_b = (_a = this.options).onSnapComplete) === null || _b === void 0 ? void 0 : _b.call(_a, snap);
257
+ },
258
+ });
259
+ }
260
+ // console.timeEnd('scroll')
261
+ };
262
+ this.options = {
263
+ type,
264
+ lerp,
265
+ easing,
266
+ duration,
267
+ velocityThreshold,
268
+ debounce: debounceDelay,
269
+ onSnapStart,
270
+ onSnapComplete,
271
+ };
272
+ this.onWindowResize();
273
+ window.addEventListener('resize', this.onWindowResize, false);
274
+ this.onSnapDebounced = debounce(this.onSnap, this.options.debounce);
275
+ this.lenis.on('scroll', this.onScroll);
276
+ }
277
+ /**
278
+ * Destroy the snap instance
279
+ */
280
+ destroy() {
281
+ this.lenis.off('scroll', this.onScroll);
282
+ window.removeEventListener('resize', this.onWindowResize, false);
283
+ this.elements.forEach((element) => element.destroy());
284
+ }
285
+ /**
286
+ * Start the snap after it has been stopped
287
+ */
288
+ start() {
289
+ this.isStopped = false;
290
+ }
291
+ /**
292
+ * Stop the snap
293
+ */
294
+ stop() {
295
+ this.isStopped = true;
296
+ }
297
+ /**
298
+ * Add a snap to the snap instance
299
+ *
300
+ * @param value The value to snap to
301
+ * @param userData User data that will be forwarded through the snap event
302
+ * @returns Unsubscribe function
303
+ */
304
+ add(value, userData = {}) {
305
+ const id = uid();
306
+ this.snaps.set(id, { value, userData });
307
+ return () => this.remove(id);
308
+ }
309
+ /**
310
+ * Remove a snap from the snap instance
311
+ *
312
+ * @param id The snap id of the snap to remove
313
+ */
314
+ remove(id) {
315
+ this.snaps.delete(id);
316
+ }
317
+ /**
318
+ * Add an element to the snap instance
319
+ *
320
+ * @param element The element to add
321
+ * @param options The options for the element
322
+ * @returns Unsubscribe function
323
+ */
324
+ addElement(element, options = {}) {
325
+ const id = uid();
326
+ this.elements.set(id, new SnapElement(element, options));
327
+ return () => this.removeElement(id);
328
+ }
329
+ /**
330
+ * Remove an element from the snap instance
331
+ *
332
+ * @param id The snap id of the snap element to remove
333
+ */
334
+ removeElement(id) {
335
+ this.elements.delete(id);
336
+ }
337
+ }
265
338
 
266
- return Snap;
339
+ return Snap;
267
340
 
268
341
  }));
269
342
  //# sourceMappingURL=lenis-snap.js.map