Skip to content

Commit 4df119c

Browse files
authoredSep 10, 2024··
fix: adjust snapPoints on window resize (#64)
* adjust snapPoints on window resize * add changeset
1 parent bdb5860 commit 4df119c

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed
 

‎.changeset/sweet-crabs-cough.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"vaul-vue": patch
3+
---
4+
5+
adjust snapPoints on window resize

‎packages/vaul-vue/src/useSnapPoints.ts

+31-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type ComponentPublicInstance, type Ref, computed, nextTick, watch } from 'vue'
1+
import { type ComponentPublicInstance, type Ref, computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
22
import { isVertical, set } from './helpers'
33
import { TRANSITIONS, VELOCITY_THRESHOLD } from './constants'
44
import type { DrawerDirection } from './types'
@@ -22,6 +22,30 @@ export function useSnapPoints({
2222
onSnapPointChange,
2323
direction,
2424
}: useSnapPointsProps) {
25+
const windowDimensions = ref(typeof window !== 'undefined'
26+
? {
27+
innerWidth: window.innerWidth,
28+
innerHeight: window.innerHeight,
29+
}
30+
: undefined)
31+
32+
function onResize() {
33+
windowDimensions.value = {
34+
innerWidth: window.innerWidth,
35+
innerHeight: window.innerHeight,
36+
}
37+
}
38+
39+
onMounted(() => {
40+
if (typeof window !== 'undefined')
41+
window.addEventListener('resize', onResize)
42+
})
43+
44+
onBeforeUnmount(() => {
45+
if (typeof window !== 'undefined')
46+
window.removeEventListener('resize', onResize)
47+
})
48+
2549
const isLastSnapPoint = computed(
2650
() =>
2751
(snapPoints.value
@@ -46,25 +70,24 @@ export function useSnapPoints({
4670
const snapPointsOffset = computed(
4771
() =>
4872
snapPoints.value?.map((snapPoint) => {
49-
const hasWindow = typeof window !== 'undefined'
5073
const isPx = typeof snapPoint === 'string'
5174
let snapPointAsNumber = 0
5275

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

5679
if (isVertical(direction.value)) {
57-
const height = isPx ? snapPointAsNumber : hasWindow ? snapPoint * window.innerHeight : 0
80+
const height = isPx ? snapPointAsNumber : windowDimensions.value ? snapPoint * windowDimensions.value.innerHeight : 0
5881

59-
if (hasWindow)
60-
return direction.value === 'bottom' ? window.innerHeight - height : -window.innerHeight + height
82+
if (windowDimensions.value)
83+
return direction.value === 'bottom' ? windowDimensions.value.innerHeight - height : -windowDimensions.value.innerHeight + height
6184

6285
return height
6386
}
64-
const width = isPx ? snapPointAsNumber : hasWindow ? snapPoint * window.innerWidth : 0
87+
const width = isPx ? snapPointAsNumber : windowDimensions.value ? snapPoint * windowDimensions.value.innerWidth : 0
6588

66-
if (hasWindow)
67-
return direction.value === 'right' ? window.innerWidth - width : -window.innerWidth + width
89+
if (windowDimensions.value)
90+
return direction.value === 'right' ? windowDimensions.value.innerWidth - width : -windowDimensions.value.innerWidth + width
6891

6992
return width
7093
}) ?? [],

0 commit comments

Comments
 (0)
Please sign in to comment.