Skip to content

Commit e1cfb98

Browse files
authored
fix: not restoring position when unmounted (#55)
* fix: not restoring position when unmounted * Create hot-hats-obey.md
1 parent 9ecd93b commit e1cfb98

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

.changeset/hot-hats-obey.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"vaul-vue": minor
3+
---
4+
5+
fix: not restoring position when unmounted

packages/vaul-vue/src/controls.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { computed, ref, watch, watchEffect } from 'vue'
1+
import { computed, onUnmounted, ref, watch, watchEffect } from 'vue'
22
import type { ComponentPublicInstance, Ref } from 'vue'
33
import { isClient } from '@vueuse/core'
44
import { dampenValue, getTranslateY, reset, set } from './helpers'
@@ -410,7 +410,6 @@ export function useDrawer(props: UseDrawerProps & DialogEmitHandlers): DrawerRoo
410410
})
411411

412412
scaleBackground(false)
413-
restorePositionSetting()
414413

415414
window.setTimeout(() => {
416415
isVisible.value = false
@@ -434,6 +433,11 @@ export function useDrawer(props: UseDrawerProps & DialogEmitHandlers): DrawerRoo
434433
}
435434
})
436435

436+
onUnmounted(() => {
437+
scaleBackground(false)
438+
restorePositionSetting()
439+
})
440+
437441
function onRelease(event: PointerEvent) {
438442
if (!isDragging.value || !drawerRef.value)
439443
return

packages/vaul-vue/src/usePositionFixed.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,24 @@ export function usePositionFixed(options: PositionFixedOptions) {
4040

4141
setTimeout(() => {
4242
requestAnimationFrame(() => {
43+
// Attempt to check if the bottom bar appeared due to the position change
4344
const bottomBarHeight = innerHeight - window.innerHeight
44-
if (bottomBarHeight && scrollPos.value >= innerHeight)
45+
if (bottomBarHeight && scrollPos.value >= innerHeight) {
46+
// Move the content further up so that the bottom bar doesn't hide it
4547
document.body.style.top = `-${scrollPos.value + bottomBarHeight}px`
48+
}
4649
})
4750
}, 300)
4851
}
4952
}
5053

5154
function restorePositionSetting(): void {
5255
if (previousBodyPosition !== null) {
56+
// Convert the position from "px" to Int
5357
const y = -Number.parseInt(document.body.style.top, 10)
5458
const x = -Number.parseInt(document.body.style.left, 10)
5559

60+
// Restore styles
5661
Object.assign(document.body.style, previousBodyPosition)
5762

5863
requestAnimationFrame(() => {
@@ -84,10 +89,14 @@ export function usePositionFixed(options: PositionFixedOptions) {
8489
watch([isOpen, hasBeenOpened, activeUrl], () => {
8590
if (nested.value || !hasBeenOpened.value)
8691
return
92+
93+
// This is needed to force Safari toolbar to show **before** the drawer starts animating to prevent a gnarly shift from happening
8794
if (isOpen.value) {
88-
setPositionFixed()
95+
// avoid for standalone mode (PWA)
96+
const isStandalone = window.matchMedia('(display-mode: standalone)').matches
97+
!isStandalone && setPositionFixed()
8998

90-
if (!modal) {
99+
if (!modal.value) {
91100
setTimeout(() => {
92101
restorePositionSetting()
93102
}, 500)

0 commit comments

Comments
 (0)