Skip to content
This repository was archived by the owner on Jan 7, 2026. It is now read-only.

Commit c17a7eb

Browse files
committed
fixed watcher
1 parent 0ee4523 commit c17a7eb

File tree

5 files changed

+54
-57
lines changed

5 files changed

+54
-57
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### v1.2.9
2+
- Fixed issue where watcher was not triggering
3+
- Made it cleaner for multiple instances. Removed need for `.value`
4+
15
### v1.2.8
26
- Updated the composable structure to fix the class issue caused by useState and private attributes
37
- updated Lenis to the latest version

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nuxt-lenis",
3-
"version": "1.2.8",
3+
"version": "1.2.9",
44
"license": "MIT",
55
"type": "module",
66
"repository": {

playground/pages/index.vue

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@
2626

2727
<script setup>
2828
import { ref, reactive, watch, onMounted, computed } from "vue";
29-
import { useLenis } from "#imports";
29+
import { useLenis } from "#imports";
3030
// import { useLenis } from "#app";
31-
const { scrollState, lenis: lenisN, lenisInstance } = useLenis(false);
31+
const { scrollState, lenis: lenisN, lenisInstance } = useLenis();
3232
const lenisRef = ref(null);
3333
var lenisLocalVS = null;
3434
const vsOptions = reactive({});
3535
3636
watch(
37-
()=>scrollState.value.wrapper,
38-
({scroll,...props}) => {
37+
()=>scrollState,
38+
({ scroll, ...props }) => {
3939
console.log("WATCHER", props);
4040
},
4141
{ deep: true }
@@ -49,19 +49,22 @@ const scrollEmitter = (val) => {
4949
// console.log("scrollEmitter", val);
5050
};
5151
const stop = (val) => {
52-
lenisN.wrapper.stop();
52+
// lenisN.wrapper.stop();
53+
lenisN.value.stop();
5354
// lenisLocalVS.stop();
5455
};
5556
const start = (val) => {
5657
// console.log("lenisVs", lenisVs.value);
57-
lenisN.wrapper.start();
58+
// lenisN.wrapper.start();
59+
lenisN.value.start();
60+
console.log(lenisN);
61+
5862
// lenisLocalVS.start();
5963
};
6064
61-
const handleClick = () => {
62-
console.log("scrollState------", lenisN.wrapper);
63-
64-
}
65+
const handleClick = () => {
66+
console.log("scrollState------", lenisN);
67+
};
6568
6669
const initiated = ({ lenis }) => (lenisLocalVS = lenis);
6770
const changeOptions = () => {
@@ -72,8 +75,8 @@ const changeOptions = () => {
7275
onMounted(() => {
7376
// console.log("Lenis Component ref :", lenisRef.value);
7477
// console.log("lenisTest------", lenisInstance("wrapper").isStopped);
75-
// console.log("lenisN------", lenisN.wrapper);
76-
console.log("scrollState------", scrollState.value.wrapper.scroll);
78+
console.log("lenisN------", lenisN.value);
79+
console.log("scrollState------", scrollState.value);
7780
});
7881
</script>
7982

src/runtime/components/lenis.vue

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,17 @@ watch(lenisOptions, (newVal) => {
6767
const initLenis = () => {
6868
if (process.client) {
6969
lenisVS = new Lenis(lenisOptions.value);
70+
71+
setLenis(lenisVS, instanceId.value);
72+
setScrollState(lenisVS, instanceId.value);
73+
7074
7175
lenisVS.on("scroll", (scrollData) => {
72-
7376
setScrollState(scrollData, instanceId.value);
7477
emit("scroll", scrollData);
7578
});
76-
setLenis(lenisVS, instanceId.value);
77-
setScrollState(lenisVS, instanceId.value);
79+
80+
7881
emit("initiated", { lenis: lenisVS });
7982
lenisRaf = requestAnimationFrame(raf);
8083
} else {

src/runtime/composables/useLenis.ts

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,46 @@
11
import { useState } from "#app";
2-
import { computed, reactive } from "#imports";
3-
import { ref, readonly } from "vue";
2+
import { computed } from "#imports";
3+
4+
var lenisVS: Array<Object | Function> = [];
45

5-
const lenisVS: Object = {};
6-
// const scrollState: Object = {};
7-
const ids: Array<Number> = [];
86
export function useLenis(single: Boolean = true): Object {
97
const scrollState = useState("scrollState", () => []);
8+
// const lenisVS = useState("lenisVS", () => []);
9+
const ids = useState("ids", () => []);
1010

1111
const setScrollState = (newScrollState: Object, id: String | Number) => {
12-
// NOTE: This is a workaround because of how useState can't handle classes
13-
scrollState.value[id] = Object.assign(
14-
{},
15-
{
16-
className: newScrollState?.className,
17-
isHorizontal: newScrollState?.isHorizontal,
18-
isLocked: newScrollState?.isLocked,
19-
isScrolling: newScrollState?.isScrolling,
20-
isSmooth: newScrollState?.isSmooth,
21-
isStopped: newScrollState?.isStopped,
22-
limit: newScrollState?.limit,
23-
progress: newScrollState?.progress,
24-
rootElement: newScrollState?.rootElement,
25-
scroll: newScrollState?.scroll,
26-
direction: newScrollState?.direction,
27-
time: newScrollState?.time,
28-
animatedScroll: newScrollState?.animatedScroll,
29-
velocity: newScrollState?.velocity,
30-
lastVelocity: newScrollState?.lastVelocity,
31-
targetScroll: newScrollState?.targetScroll,
32-
}
33-
);
12+
scrollState.value[id] = {
13+
className: newScrollState?.className,
14+
isHorizontal: newScrollState?.isHorizontal,
15+
isLocked: newScrollState?.isLocked,
16+
isScrolling: newScrollState?.isScrolling,
17+
isSmooth: newScrollState?.isSmooth,
18+
isStopped: newScrollState?.isStopped,
19+
limit: newScrollState?.limit,
20+
progress: newScrollState?.progress,
21+
rootElement: newScrollState?.rootElement,
22+
scroll: newScrollState?.scroll,
23+
direction: newScrollState?.direction,
24+
time: newScrollState?.time,
25+
animatedScroll: newScrollState?.animatedScroll,
26+
velocity: newScrollState?.velocity,
27+
lastVelocity: newScrollState?.lastVelocity,
28+
targetScroll: newScrollState?.targetScroll,
29+
};
3430
};
3531
const setLenis = (virtualScroll: Object, id: String | Number) => {
3632
lenisVS[id] = virtualScroll;
37-
ids.push(id);
33+
ids.value.push(id);
3834
};
39-
4035
const getScrollState = computed(() => {
41-
return single ? scrollState.value[ids[0]] : scrollState.value;
36+
return single ? scrollState.value[ids.value[0]] : scrollState.value;
4237
});
43-
4438
const getLenis = computed(() => {
45-
return single ? lenisVS[ids[0]] : lenisVS;
39+
return single ? lenisVS[ids.value[0]] : lenisVS;
4640
});
47-
48-
const getLenisInstance = (id) => {
49-
if (!id) return "no id provided";
50-
return lenisVS[id];
51-
};
52-
5341
return {
54-
scrollState: getScrollState,
55-
lenis: getLenis.value,
56-
lenisInstance: getLenisInstance,
42+
scrollState: single ? getScrollState : getScrollState.value,
43+
lenis: single ? getLenis : getLenis.value,
5744
setScrollState,
5845
setLenis,
5946
};

0 commit comments

Comments
 (0)