Skip to content

Commit

Permalink
Make component reactive to window resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
zysim committed May 10, 2024
1 parent 0517c5b commit 9739ef6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 29 deletions.
19 changes: 15 additions & 4 deletions components/blocks/LeaderboardInfo/LeaderboardInfo.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<script setup lang="ts">
import { useThrottleFn } from '@vueuse/core'
import resolveConfig from 'tailwindcss/resolveConfig'
import { ref } from 'vue'
import tailwindConfig from 'root/tailwind.config'
import Desktop from './Desktop/Desktop.vue'
import Mobile from './Mobile/Mobile.vue'
Expand All @@ -17,16 +19,25 @@ const emit = defineEmits<{
(event: 'follow', leaderboardId: number): void
}>()
const isMobile =
window.innerWidth <=
parseInt(resolveConfig(tailwindConfig).theme.screens.sm.replace('px', ''), 10)
const mobileWidth = parseInt(
resolveConfig(tailwindConfig).theme.screens.sm.replace('px', ''),
10,
)
const isMobile = ref(window.innerWidth <= mobileWidth)
function checkIsMobile() {
isMobile.value = window.innerWidth <= mobileWidth
}
window.addEventListener('resize', useThrottleFn(checkIsMobile, 20))
defineProps<LeaderboardInfoProps>()
</script>

<template>
<Mobile
v-if="isMobile"
v-if="isMobile.valueOf()"
data-testid="child"
:leaderboard="leaderboard"
:todo-platforms="todoPlatforms"
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,8 @@
"vitest": "^1.2.2",
"vitest-fetch-mock": "^0.2.2",
"vue": "^3.4.19"
},
"dependencies": {
"@vueuse/core": "^10.9.0"
}
}
74 changes: 49 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9739ef6

Please sign in to comment.