Skip to content

Commit

Permalink
fix dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
clementroche committed Feb 22, 2024
1 parent d387036 commit 96cbc44
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ yarn-error.log*
.vercel
.eslintcache

/packages/no-code
2 changes: 1 addition & 1 deletion packages/lenis/dist/lenis.cjs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/lenis/dist/lenis.cjs.js.map

Large diffs are not rendered by default.

47 changes: 38 additions & 9 deletions packages/lenis/dist/lenis.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,21 @@
}

class Dimensions {
constructor({ wrapper, content, autoResize = true } = {}) {
constructor({
wrapper,
content,
autoResize = true,
debounce: debounceValue = 250,
} = {}) {
this.wrapper = wrapper;
this.content = content;

if (autoResize) {
const resize = debounce(this.resize, 250);
const resize = debounce(this.resize, debounceValue);

if (this.wrapper !== window) {
if (this.wrapper === window) {
window.addEventListener('resize', resize, false);
} else {
this.wrapperResizeObserver = new ResizeObserver(resize);
this.wrapperResizeObserver.observe(this.wrapper);
}
Expand All @@ -118,6 +125,7 @@
destroy() {
this.wrapperResizeObserver?.disconnect();
this.contentResizeObserver?.disconnect();
window.removeEventListener('resize', resize, false);
}

resize = () => {
Expand All @@ -136,8 +144,13 @@
}

onContentResize = () => {
this.scrollHeight = this.content.scrollHeight;
this.scrollWidth = this.content.scrollWidth;
if (this.wrapper === window) {
this.scrollHeight = this.content.scrollHeight;
this.scrollWidth = this.content.scrollWidth;
} else {
this.scrollHeight = this.wrapper.scrollHeight;
this.scrollWidth = this.wrapper.scrollWidth;
}
}

get limit() {
Expand Down Expand Up @@ -301,7 +314,7 @@
}

class Lenis {
constructor({ wrapper = window, content = document.documentElement, wheelEventsTarget = wrapper, eventsTarget = wheelEventsTarget, smoothWheel = true, syncTouch = false, syncTouchLerp = 0.075, touchInertiaMultiplier = 35, duration, easing = (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)), lerp = !duration && 0.1, infinite = false, orientation = 'vertical', gestureOrientation = 'vertical', touchMultiplier = 1, wheelMultiplier = 1, normalizeWheel = false, autoResize = true, } = {}) {
constructor({ wrapper = window, content = document.documentElement, wheelEventsTarget = wrapper, eventsTarget = wheelEventsTarget, smoothWheel = true, syncTouch = false, syncTouchLerp = 0.075, touchInertiaMultiplier = 35, duration, easing = (t) => Math.min(1, 1.001 - Math.pow(2, -10 * t)), lerp = !duration && 0.1, infinite = false, orientation = 'vertical', gestureOrientation = 'vertical', touchMultiplier = 1, wheelMultiplier = 1, normalizeWheel = false, autoResize = true, __experimental__naiveDimensions = false, } = {}) {
this.__isSmooth = false;
this.__isScrolling = false;
this.__isStopped = false;
Expand All @@ -325,11 +338,12 @@
let composedPath = event.composedPath();
composedPath = composedPath.slice(0, composedPath.indexOf(this.rootElement));
if (!!composedPath.find((node) => {
var _a, _b, _c, _d;
var _a, _b, _c, _d, _e;
return ((_a = node.hasAttribute) === null || _a === void 0 ? void 0 : _a.call(node, 'data-lenis-prevent')) ||
(isTouch && ((_b = node.hasAttribute) === null || _b === void 0 ? void 0 : _b.call(node, 'data-lenis-prevent-touch'))) ||
(isWheel && ((_c = node.hasAttribute) === null || _c === void 0 ? void 0 : _c.call(node, 'data-lenis-prevent-wheel'))) ||
((_d = node.classList) === null || _d === void 0 ? void 0 : _d.contains('lenis'));
(((_d = node.classList) === null || _d === void 0 ? void 0 : _d.contains('lenis')) &&
!((_e = node.classList) === null || _e === void 0 ? void 0 : _e.contains('lenis-stopped')));
}))
return;
if (this.isStopped || this.isLocked) {
Expand Down Expand Up @@ -402,6 +416,7 @@
wheelMultiplier,
normalizeWheel,
autoResize,
__experimental__naiveDimensions,
};
this.animate = new Animate();
this.emitter = new Emitter();
Expand Down Expand Up @@ -464,10 +479,14 @@
this.animate.stop();
}
start() {
if (!this.isStopped)
return;
this.isStopped = false;
this.reset();
}
stop() {
if (this.isStopped)
return;
this.isStopped = true;
this.animate.stop();
this.reset();
Expand Down Expand Up @@ -566,7 +585,17 @@
: this.options.wrapper;
}
get limit() {
return this.dimensions.limit[this.isHorizontal ? 'x' : 'y'];
if (this.options.__experimental__naiveDimensions) {
if (this.isHorizontal) {
return this.rootElement.scrollWidth - this.rootElement.clientWidth;
}
else {
return this.rootElement.scrollHeight - this.rootElement.clientHeight;
}
}
else {
return this.dimensions.limit[this.isHorizontal ? 'x' : 'y'];
}
}
get isHorizontal() {
return this.options.orientation === 'horizontal';
Expand Down
2 changes: 1 addition & 1 deletion packages/lenis/dist/lenis.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/lenis/dist/lenis.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/lenis/dist/lenis.mjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/lenis/dist/lenis.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/lenis/dist/lenis.umd.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion packages/lenis/dist/types/dimensions.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export class Dimensions {
constructor({ wrapper, content, autoResize }?: {
constructor({ wrapper, content, autoResize, debounce: debounceValue, }?: {
wrapper: any;
content: any;
autoResize?: boolean | undefined;
debounce?: number | undefined;
});
wrapper: any;
content: any;
Expand Down
2 changes: 1 addition & 1 deletion packages/lenis/dist/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class Lenis {
__isScrolling: boolean;
__isStopped: boolean;
__isLocked: boolean;
constructor({ wrapper, content, wheelEventsTarget, eventsTarget, smoothWheel, syncTouch, syncTouchLerp, touchInertiaMultiplier, duration, easing, lerp, infinite, orientation, gestureOrientation, touchMultiplier, wheelMultiplier, normalizeWheel, autoResize, }?: LenisOptions);
constructor({ wrapper, content, wheelEventsTarget, eventsTarget, smoothWheel, syncTouch, syncTouchLerp, touchInertiaMultiplier, duration, easing, lerp, infinite, orientation, gestureOrientation, touchMultiplier, wheelMultiplier, normalizeWheel, autoResize, __experimental__naiveDimensions, }?: LenisOptions);
destroy(): void;
on(event: string, callback: Function): any;
off(event: string, callback: Function): any;
Expand Down
1 change: 1 addition & 0 deletions packages/lenis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@studio-freight/lenis",
"version": "1.0.37",
"description": "Lenis is a smooth scroll library to normalize and smooth the scrolling experience across devices",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/darkroomengineering/lenis.git"
Expand Down
23 changes: 18 additions & 5 deletions packages/lenis/src/dimensions.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { debounce } from './debounce'

export class Dimensions {
constructor({ wrapper, content, autoResize = true } = {}) {
constructor({
wrapper,
content,
autoResize = true,
debounce: debounceValue = 250,
} = {}) {
this.wrapper = wrapper
this.content = content

if (autoResize) {
const resize = debounce(this.resize, 250)
const resize = debounce(this.resize, debounceValue)

if (this.wrapper !== window) {
if (this.wrapper === window) {
window.addEventListener('resize', resize, false)
} else {
this.wrapperResizeObserver = new ResizeObserver(resize)
this.wrapperResizeObserver.observe(this.wrapper)
}
Expand All @@ -23,6 +30,7 @@ export class Dimensions {
destroy() {
this.wrapperResizeObserver?.disconnect()
this.contentResizeObserver?.disconnect()
window.removeEventListener('resize', resize, false)
}

resize = () => {
Expand All @@ -41,8 +49,13 @@ export class Dimensions {
}

onContentResize = () => {
this.scrollHeight = this.content.scrollHeight
this.scrollWidth = this.content.scrollWidth
if (this.wrapper === window) {
this.scrollHeight = this.content.scrollHeight
this.scrollWidth = this.content.scrollWidth
} else {
this.scrollHeight = this.wrapper.scrollHeight
this.scrollWidth = this.wrapper.scrollWidth
}
}

get limit() {
Expand Down
17 changes: 15 additions & 2 deletions packages/lenis/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default class Lenis {
wheelMultiplier = 1,
normalizeWheel = false, // deprecated
autoResize = true,
__experimental__naiveDimensions = false,
}: LenisOptions = {}) {
window.lenisVersion = version

Expand Down Expand Up @@ -90,6 +91,7 @@ export default class Lenis {
wheelMultiplier,
normalizeWheel,
autoResize,
__experimental__naiveDimensions,
}

this.animate = new Animate()
Expand Down Expand Up @@ -192,7 +194,8 @@ export default class Lenis {
node.hasAttribute?.('data-lenis-prevent') ||
(isTouch && node.hasAttribute?.('data-lenis-prevent-touch')) ||
(isWheel && node.hasAttribute?.('data-lenis-prevent-wheel')) ||
node.classList?.contains('lenis') // nested lenis instance
(node.classList?.contains('lenis') &&
!node.classList?.contains('lenis-stopped')) // nested lenis instance
)
)
return
Expand Down Expand Up @@ -273,12 +276,14 @@ export default class Lenis {
}

start() {
if (!this.isStopped) return
this.isStopped = false

this.reset()
}

stop() {
if (this.isStopped) return
this.isStopped = true
this.animate.stop()

Expand Down Expand Up @@ -423,7 +428,15 @@ export default class Lenis {
}

get limit() {
return this.dimensions.limit[this.isHorizontal ? 'x' : 'y']
if (this.options.__experimental__naiveDimensions) {
if (this.isHorizontal) {
return this.rootElement.scrollWidth - this.rootElement.clientWidth
} else {
return this.rootElement.scrollHeight - this.rootElement.clientHeight
}
} else {
return this.dimensions.limit[this.isHorizontal ? 'x' : 'y']
}
}

get isHorizontal() {
Expand Down

1 comment on commit 96cbc44

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"⚡️ Lighthouse report for the changes in this commit:

🟢 Performance: 93
🟢 Accessibility: 96
🟢 Best practices: 100
🟠 SEO: 67
🔴 PWA: 33

Lighthouse ran on https://lenis-bol87mmwu-studio-freight.vercel.app/"

Please sign in to comment.