Skip to content

Commit

Permalink
fix: adjust snapPoints on window resize (#64)
Browse files Browse the repository at this point in the history
* adjust snapPoints on window resize

* add changeset
  • Loading branch information
anbraten authored Sep 10, 2024
1 parent bdb5860 commit 4df119c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-crabs-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vaul-vue": patch
---

adjust snapPoints on window resize
39 changes: 31 additions & 8 deletions packages/vaul-vue/src/useSnapPoints.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ComponentPublicInstance, type Ref, computed, nextTick, watch } from 'vue'
import { type ComponentPublicInstance, type Ref, computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { isVertical, set } from './helpers'
import { TRANSITIONS, VELOCITY_THRESHOLD } from './constants'
import type { DrawerDirection } from './types'
Expand All @@ -22,6 +22,30 @@ export function useSnapPoints({
onSnapPointChange,
direction,
}: useSnapPointsProps) {
const windowDimensions = ref(typeof window !== 'undefined'
? {
innerWidth: window.innerWidth,
innerHeight: window.innerHeight,
}
: undefined)

function onResize() {
windowDimensions.value = {
innerWidth: window.innerWidth,
innerHeight: window.innerHeight,
}
}

onMounted(() => {
if (typeof window !== 'undefined')
window.addEventListener('resize', onResize)
})

onBeforeUnmount(() => {
if (typeof window !== 'undefined')
window.removeEventListener('resize', onResize)
})

const isLastSnapPoint = computed(
() =>
(snapPoints.value
Expand All @@ -46,25 +70,24 @@ export function useSnapPoints({
const snapPointsOffset = computed(
() =>
snapPoints.value?.map((snapPoint) => {
const hasWindow = typeof window !== 'undefined'
const isPx = typeof snapPoint === 'string'
let snapPointAsNumber = 0

if (isPx)
snapPointAsNumber = Number.parseInt(snapPoint, 10)

if (isVertical(direction.value)) {
const height = isPx ? snapPointAsNumber : hasWindow ? snapPoint * window.innerHeight : 0
const height = isPx ? snapPointAsNumber : windowDimensions.value ? snapPoint * windowDimensions.value.innerHeight : 0

if (hasWindow)
return direction.value === 'bottom' ? window.innerHeight - height : -window.innerHeight + height
if (windowDimensions.value)
return direction.value === 'bottom' ? windowDimensions.value.innerHeight - height : -windowDimensions.value.innerHeight + height

return height
}
const width = isPx ? snapPointAsNumber : hasWindow ? snapPoint * window.innerWidth : 0
const width = isPx ? snapPointAsNumber : windowDimensions.value ? snapPoint * windowDimensions.value.innerWidth : 0

if (hasWindow)
return direction.value === 'right' ? window.innerWidth - width : -window.innerWidth + width
if (windowDimensions.value)
return direction.value === 'right' ? windowDimensions.value.innerWidth - width : -windowDimensions.value.innerWidth + width

return width
}) ?? [],
Expand Down

0 comments on commit 4df119c

Please sign in to comment.