Skip to content

Commit ef9eca3

Browse files
committed
fix: useElementSize instead
1 parent d5d9a23 commit ef9eca3

1 file changed

Lines changed: 8 additions & 25 deletions

File tree

app/components/PackageDownloadAnalytics.vue

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { ref, computed, shallowRef, watch } from 'vue'
33
import type { VueUiXyDatasetItem } from 'vue-data-ui'
44
import { VueUiXy } from 'vue-data-ui/vue-ui-xy'
5-
import { useDebounceFn } from '@vueuse/core'
5+
import { useDebounceFn, useElementSize } from '@vueuse/core'
66
77
const {
88
weeklyDownloads,
@@ -18,8 +18,10 @@ const {
1818
1919
const { accentColors, selectedAccentColor } = useAccentColor()
2020
const colorMode = useColorMode()
21-
2221
const resolvedMode = ref<'light' | 'dark'>('light')
22+
const rootEl = shallowRef<HTMLElement | null>(null)
23+
24+
const { width } = useElementSize(rootEl)
2325
2426
onMounted(() => {
2527
resolvedMode.value = colorMode.value === 'dark' ? 'dark' : 'light'
@@ -51,32 +53,13 @@ const accent = computed(() => {
5153
})
5254
5355
const mobileBreakpointWidth = 640
54-
const isMobile = ref(false)
55-
56-
let resizeObserver: ResizeObserver | null = null
5756
58-
function updateIsMobileFromWidth(width: number) {
59-
isMobile.value = width < mobileBreakpointWidth
60-
}
61-
62-
onMounted(() => {
63-
if (!import.meta.client) return
64-
65-
updateIsMobileFromWidth(window.innerWidth)
66-
67-
resizeObserver = new ResizeObserver(entries => {
68-
const entry = entries[0]
69-
if (!entry) return
70-
updateIsMobileFromWidth(entry.contentRect.width)
71-
})
72-
73-
resizeObserver.observe(document.documentElement)
57+
const isMobile = computed(() => {
58+
return width.value > 0 && width.value < mobileBreakpointWidth
7459
})
7560
76-
onBeforeUnmount(() => {
77-
if (!resizeObserver) return
78-
resizeObserver.disconnect()
79-
resizeObserver = null
61+
onMounted(() => {
62+
rootEl.value = document.documentElement
8063
})
8164
8265
type ChartTimeGranularity = 'daily' | 'weekly' | 'monthly' | 'yearly'

0 commit comments

Comments
 (0)