Skip to content

Commit 377310f

Browse files
committed
fix(CSidebar): IntersectionObserver is not defined
1 parent e4e0acb commit 377310f

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/components/sidebar/CSidebar.ts

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent, h, onMounted, ref } from 'vue'
1+
import { defineComponent, h, onBeforeUnmount, onMounted, ref } from 'vue'
22

33
const CSidebar = defineComponent({
44
name: 'CSidebar',
@@ -81,21 +81,26 @@ const CSidebar = defineComponent({
8181
threshold: 1.0,
8282
}
8383

84-
const callback = (entries: IntersectionObserverEntry[]) => {
85-
entries.forEach((entry) => {
86-
if (entry.isIntersecting !== props.visible || typeof visible.value === 'undefined') {
87-
visible.value = entry.isIntersecting
88-
emit('visible-change', visible.value)
89-
}
90-
})
91-
}
92-
93-
const observer = new IntersectionObserver(callback, options)
84+
let observer: IntersectionObserver
9485

9586
onMounted(() => {
87+
const callback = (entries: IntersectionObserverEntry[]) => {
88+
entries.forEach((entry) => {
89+
if (entry.isIntersecting !== props.visible || typeof visible.value === 'undefined') {
90+
visible.value = entry.isIntersecting
91+
emit('visible-change', visible.value)
92+
}
93+
})
94+
}
95+
96+
observer = new IntersectionObserver(callback, options)
9697
observer.observe(sidebarRef.value)
9798
})
9899

100+
onBeforeUnmount(() => {
101+
observer.disconnect()
102+
})
103+
99104
return () =>
100105
h(
101106
'div',

0 commit comments

Comments
 (0)