Skip to content

Commit cfce458

Browse files
authored
fix: use vueuse useEventListener for correct cleanups (#136)
1 parent 2d2f75a commit cfce458

4 files changed

Lines changed: 13 additions & 23 deletions

File tree

app/components/AppFooter.vue

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ function onResize() {
4848
updateFooterPadding()
4949
}
5050
51+
useEventListener('scroll', onScroll, { passive: true })
52+
useEventListener('resize', onResize, { passive: true })
53+
5154
onMounted(() => {
5255
// Feature detect CSS scroll-state container queries (Chrome 133+)
5356
// @see https://developer.mozilla.org/en-US/docs/Web/CSS/@container#scroll-state_container_descriptors
@@ -60,14 +63,6 @@ onMounted(() => {
6063
// Only apply dynamic classes after mount to avoid hydration mismatch
6164
isMounted.value = true
6265
})
63-
64-
window.addEventListener('scroll', onScroll, { passive: true })
65-
window.addEventListener('resize', onResize, { passive: true })
66-
})
67-
68-
onUnmounted(() => {
69-
window.removeEventListener('scroll', onScroll)
70-
window.removeEventListener('resize', onResize)
7166
})
7267
</script>
7368

app/components/ScrollToTop.vue

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,29 @@ const excludedRoutes = new Set(['index', 'code'])
66
77
const isActive = computed(() => !excludedRoutes.has(route.name as string))
88
9-
const isMounted = ref(false)
9+
const isMounted = useMounted()
1010
const isVisible = ref(false)
1111
const scrollThreshold = 300
1212
const supportsScrollStateQueries = ref(false)
1313
1414
function onScroll() {
15+
if (!supportsScrollStateQueries.value) {
16+
return
17+
}
1518
isVisible.value = window.scrollY > scrollThreshold
1619
}
1720
1821
function scrollToTop() {
1922
window.scrollTo({ top: 0, behavior: 'smooth' })
2023
}
2124
25+
useEventListener('scroll', onScroll, { passive: true })
26+
2227
onMounted(() => {
2328
// Feature detect CSS scroll-state container queries (Chrome 133+)
2429
supportsScrollStateQueries.value = CSS.supports('container-type', 'scroll-state')
2530
26-
if (!supportsScrollStateQueries.value) {
27-
window.addEventListener('scroll', onScroll, { passive: true })
28-
onScroll()
29-
}
30-
31-
isMounted.value = true
32-
})
33-
34-
onUnmounted(() => {
35-
window.removeEventListener('scroll', onScroll)
31+
onScroll()
3632
})
3733
</script>
3834

app/pages/code/[...path].vue

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,12 @@ const { data: fileContent, status: fileStatus } = useFetch<PackageFileContentRes
139139
// Track hash manually since we update it via history API to avoid scroll
140140
const currentHash = ref('')
141141
142-
// Initialize from route and listen for popstate (back/forward)
143142
onMounted(() => {
144143
currentHash.value = window.location.hash
145-
window.addEventListener('popstate', () => {
146-
currentHash.value = window.location.hash
147-
})
148144
})
149145
146+
useEventListener('popstate', () => (currentHash.value = window.location.hash))
147+
150148
// Also sync when route changes (e.g., navigating to a different file)
151149
watch(
152150
() => route.hash,

nuxt.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default defineNuxtConfig({
1515
'nuxt-og-image',
1616
'@nuxt/test-utils',
1717
'@vite-pwa/nuxt',
18+
'@vueuse/nuxt',
1819
],
1920

2021
devtools: { enabled: true },

0 commit comments

Comments
 (0)