Skip to content

Commit 46a8414

Browse files
anbratenzernonia
andauthored
fix: snap points (#65)
* adjust snapPoints on window resize * add changeset * fix snaps * undo unrelated * fix sample * update changeset * fix * Create soft-peas-deliver.md * chore: fix ci --------- Co-authored-by: zernonia <[email protected]> Co-authored-by: zernonia <[email protected]>
1 parent ae25725 commit 46a8414

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

.changeset/soft-peas-deliver.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"vaul-vue": patch
3+
---
4+
5+
Fix snap points

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- run: pnpm build:package
2222
- run: npx playwright install --with-deps
2323
- run: pnpm test || exit 1
24-
- uses: actions/upload-artifact@v3
24+
- uses: actions/upload-artifact@v4
2525
if: always()
2626
with:
2727
name: playwright-report

packages/vaul-vue/src/useSnapPoints.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,14 @@ export function useSnapPoints({
163163
velocity: number
164164
dismissible: boolean
165165
}) {
166-
if (fadeFromIndex === undefined)
166+
if (fadeFromIndex.value === undefined)
167167
return
168168

169169
const currentPosition
170170
= direction.value === 'bottom' || direction.value === 'right'
171171
? (activeSnapPointOffset.value ?? 0) - draggedDistance
172172
: (activeSnapPointOffset.value ?? 0) + draggedDistance
173-
const isOverlaySnapPoint = activeSnapPointIndex.value === (fadeFromIndex.value ?? 0) - 1
173+
const isOverlaySnapPoint = activeSnapPointIndex.value === fadeFromIndex.value - 1
174174
const isFirst = activeSnapPointIndex.value === 0
175175
const hasDraggedUp = draggedDistance > 0
176176

@@ -228,8 +228,8 @@ export function useSnapPoints({
228228
return
229229
const newValue
230230
= direction.value === 'bottom' || direction.value === 'right'
231-
? (activeSnapPointOffset.value ?? 0) - draggedDistance
232-
: (activeSnapPointOffset.value ?? 0) + draggedDistance
231+
? activeSnapPointOffset.value - draggedDistance
232+
: activeSnapPointOffset.value + draggedDistance
233233

234234
// Don't do anything if we exceed the last(biggest) snap point
235235
if ((direction.value === 'bottom' || direction.value === 'right') && newValue < snapPointsOffset.value[snapPointsOffset.value.length - 1])
@@ -245,16 +245,16 @@ export function useSnapPoints({
245245

246246
function getPercentageDragged(absDraggedDistance: number, isDraggingDown: boolean) {
247247
if (
248-
!snapPoints
248+
!snapPoints.value
249249
|| typeof activeSnapPointIndex.value !== 'number'
250250
|| !snapPointsOffset.value
251-
|| fadeFromIndex === undefined
251+
|| fadeFromIndex.value === undefined
252252
)
253253
return null
254254

255255
// If this is true we are dragging to a snap point that is supposed to have an overlay
256-
const isOverlaySnapPoint = activeSnapPointIndex.value === (fadeFromIndex.value ?? 0) - 1
257-
const isOverlaySnapPointOrHigher = activeSnapPointIndex.value >= (fadeFromIndex.value ?? 0)
256+
const isOverlaySnapPoint = activeSnapPointIndex.value === fadeFromIndex.value - 1
257+
const isOverlaySnapPointOrHigher = activeSnapPointIndex.value >= fadeFromIndex.value
258258

259259
if (isOverlaySnapPointOrHigher && isDraggingDown)
260260
return 0

playground/src/views/tests/WithSnapPointsView.vue

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import { DrawerContent, DrawerOverlay, DrawerPortal, DrawerRoot, DrawerTrigger } from 'vaul-vue'
33
import { computed, ref } from 'vue'
44
5-
const snapPoints = [0, '148px', '355px', 1]
5+
const snapPoints = ['148px', '355px', 1]
66
7-
const snap = ref<number | string | null>(snapPoints[1])
7+
const snap = ref<number | string | null>(snapPoints[0])
88
99
const activeSnapPointIndex = computed(() => snapPoints.indexOf(snap.value as string))
1010
11-
const open = ref<boolean>(true)
11+
const open = ref<boolean>(false)
1212
</script>
1313

1414
<template>
@@ -19,7 +19,7 @@ const open = ref<boolean>(true)
1919
<div data-testid="active-snap-index">
2020
{{ activeSnapPointIndex }}
2121
</div>
22-
<DrawerRoot v-model:open="open" :snap-points="snapPoints" :active-snap-point="snap">
22+
<DrawerRoot v-model:open="open" v-model:active-snap-point="snap" :snap-points="snapPoints">
2323
<DrawerTrigger as-child>
2424
<button data-testid="trigger" class="text-2xl">
2525
Open Drawer
@@ -97,7 +97,6 @@ const open = ref<boolean>(true)
9797
/>
9898
</svg>
9999
</div>
100-
{' '}
101100
<h1 class="text-2xl mt-2 font-medium">
102101
The Hidden Details
103102
</h1>

0 commit comments

Comments
 (0)