Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust snapPoints on window resize #64

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading