Skip to content

Commit dafa5c3

Browse files
committed
feat: switch to lenis for smooth scroll
1 parent 1f62b22 commit dafa5c3

File tree

6 files changed

+27
-55
lines changed

6 files changed

+27
-55
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "timber-starter-theme",
2+
"name": "laurus-africa-theme",
33
"version": "1.0.0",
44
"main": "index.js",
55
"author": "Kwadwo Amoh <kwadwo.amoh@gmail.com>",
@@ -17,7 +17,7 @@
1717
"debounce": "^2.1.0",
1818
"gsap": "^3.12.5",
1919
"laravel-vite-plugin": "^1.0.4",
20-
"locomotive-scroll": "^5.0.0-beta.13",
20+
"lenis": "^1.1.13",
2121
"modujs": "^1.4.2",
2222
"normalize.css": "^8.0.1"
2323
}

resources/scripts/modules.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
// export { default as Scroll } from "./modules/Scroll";
1+
export { default as Scroll } from "./modules/Scroll";
22
export { default as Menu } from "./modules/Menu";
33
export { default as History } from "./modules/History";

resources/scripts/modules/Scroll.js

+18-34
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,36 @@
11
import { module as Module } from "modujs";
2-
import LocomotiveScroll from "locomotive-scroll";
2+
import Lenis from "lenis";
3+
4+
import { $html } from "../utils/dom";
35

46
export default class extends Module {
57
constructor(m) {
68
super(m);
7-
8-
this.onScrollBind = this.onScroll.bind(this);
99
}
1010

1111
init() {
12-
this.locomotiveScroll = new LocomotiveScroll({
13-
modularInstance: this,
14-
scrollCallback: this.onScrollBind,
12+
this.$wrapper = this.el.parentNode;
13+
this.lenis = new Lenis({
14+
wrapper: this.$wrapper,
15+
content: this.el,
16+
smoothWheel: true,
17+
duration: 1,
18+
lerp: 0.3,
19+
prevent: () => $html.classList.contains("has-menu-opened"),
1520
});
16-
}
17-
18-
onScroll({ scroll, limit, velocity, direction, progress }) {
19-
// Store our LS data on window to allow global access
20-
window.locomotiveScrollData = {
21-
scroll,
22-
limit,
23-
velocity,
24-
direction,
25-
progress,
26-
};
27-
}
28-
29-
/**
30-
* Scroll to
31-
*
32-
* @param {object}
33-
*/
34-
scrollTo(params) {
35-
let { target, options } = params;
36-
37-
this.locomotiveScroll?.scrollTo(target, options);
38-
}
21+
window.scrollObjectInstance = this.lenis;
3922

40-
stop() {
41-
this.locomotiveScroll?.stop();
23+
this.raf(0);
4224
}
4325

44-
start() {
45-
this.locomotiveScroll?.start();
26+
raf(time) {
27+
this.lenis.raf(time);
28+
this.rafInstance = requestAnimationFrame((time) => this.raf(time));
4629
}
4730

4831
destroy() {
4932
super.destroy();
50-
this.locomotiveScroll?.destroy();
33+
this.lenis?.destroy();
34+
cancelAnimationFrame(this.rafInstance);
5135
}
5236
}

resources/styles/_document.scss

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
html {
3838
height: 100%;
39+
overflow: hidden;
3940
}
4041

4142
body {

resources/styles/styles.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// Vendors
99
// ==========================================================================
10-
@import "../../node_modules/locomotive-scroll/dist/locomotive-scroll";
10+
@import "../../node_modules/lenis/dist/lenis.css";
1111

1212
@import "overrides/wpcf7";
1313

yarn.lock

+4-17
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,6 @@ braces@~3.0.2:
242242
optionalDependencies:
243243
fsevents "~2.3.2"
244244

245-
clsx@^2.1.0:
246-
version "2.1.1"
247-
resolved "https://registry.yarnpkg.com/clsx/-/clsx-2.1.1.tgz#eed397c9fd8bd882bfb18deab7102049a2f32999"
248-
integrity sha512-eYm0QWBtUrBWZWG0d386OGAw16Z995PiOVo2B7bjWSbHedGl5e0ZWaq65kOGgUSNesEIDkB9ISbTg/JK9dhCZA==
249-
250245
debounce@^2.1.0:
251246
version "2.1.0"
252247
resolved "https://registry.yarnpkg.com/debounce/-/debounce-2.1.0.tgz#eab25eaf254b848fcfccffbde75bdaa44842caa3"
@@ -342,20 +337,12 @@ laravel-vite-plugin@^1.0.4:
342337
picocolors "^1.0.0"
343338
vite-plugin-full-reload "^1.1.0"
344339

345-
lenis@1.0.45:
346-
version "1.0.45"
347-
resolved "https://registry.yarnpkg.com/lenis/-/lenis-1.0.45.tgz#5e34437527f30e10c87ada3c3c756c7c0caf8475"
348-
integrity sha512-wiaYRxr7rK1rY+dhGdMLowV0rkjg+1B2nI50Igyu5USkjDY6lkBNCJi0mVaSL/X3ECyAm/oA0nsihq8ic3p9wg==
340+
lenis@^1.1.13:
341+
version "1.1.13"
342+
resolved "https://registry.yarnpkg.com/lenis/-/lenis-1.1.13.tgz#4234c4ba302c211ddf0f0a21d73de99d5a78528b"
343+
integrity sha512-iny+vWwUHcZGbJz+p2I75H1muLBRZqSG7t04jnhA+CWOQDFL1vt9bsZDwPPtYEmYVVvqAdo5MbXVFzNOlbgUkQ==
349344
dependencies:
350345
"@darkroom.engineering/tempus" "^0.0.46"
351-
clsx "^2.1.0"
352-
353-
locomotive-scroll@^5.0.0-beta.13:
354-
version "5.0.0-beta.13"
355-
resolved "https://registry.yarnpkg.com/locomotive-scroll/-/locomotive-scroll-5.0.0-beta.13.tgz#002c8a6d65caaf0e56e3607ad59f61fcba8c502e"
356-
integrity sha512-zA4E6jbakBVwakb8KzTF6kVhdql1HCZbWCY0BzZ2LNNm9G5uWlZc9+Q5Lcxl7DmygY+5Yt1EfT5p3l3JLG8RPg==
357-
dependencies:
358-
lenis "1.0.45"
359346

360347
modujs@^1.4.2:
361348
version "1.4.2"

0 commit comments

Comments
 (0)