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

fix: not restoring position when unmounted #55

Merged
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/hot-hats-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vaul-vue": minor
---

fix: not restoring position when unmounted
8 changes: 6 additions & 2 deletions packages/vaul-vue/src/controls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed, ref, watch, watchEffect } from 'vue'
import { computed, onUnmounted, ref, watch, watchEffect } from 'vue'
import type { ComponentPublicInstance, Ref } from 'vue'
import { isClient } from '@vueuse/core'
import { dampenValue, getTranslateY, reset, set } from './helpers'
Expand Down Expand Up @@ -410,7 +410,6 @@ export function useDrawer(props: UseDrawerProps & DialogEmitHandlers): DrawerRoo
})

scaleBackground(false)
restorePositionSetting()

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

onUnmounted(() => {
scaleBackground(false)
restorePositionSetting()
})

function onRelease(event: PointerEvent) {
if (!isDragging.value || !drawerRef.value)
return
Expand Down
15 changes: 12 additions & 3 deletions packages/vaul-vue/src/usePositionFixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,24 @@ export function usePositionFixed(options: PositionFixedOptions) {

setTimeout(() => {
requestAnimationFrame(() => {
// Attempt to check if the bottom bar appeared due to the position change
const bottomBarHeight = innerHeight - window.innerHeight
if (bottomBarHeight && scrollPos.value >= innerHeight)
if (bottomBarHeight && scrollPos.value >= innerHeight) {
// Move the content further up so that the bottom bar doesn't hide it
document.body.style.top = `-${scrollPos.value + bottomBarHeight}px`
}
})
}, 300)
}
}

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

// Restore styles
Object.assign(document.body.style, previousBodyPosition)

requestAnimationFrame(() => {
Expand Down Expand Up @@ -84,10 +89,14 @@ export function usePositionFixed(options: PositionFixedOptions) {
watch([isOpen, hasBeenOpened, activeUrl], () => {
if (nested.value || !hasBeenOpened.value)
return

// This is needed to force Safari toolbar to show **before** the drawer starts animating to prevent a gnarly shift from happening
if (isOpen.value) {
setPositionFixed()
// avoid for standalone mode (PWA)
const isStandalone = window.matchMedia('(display-mode: standalone)').matches
!isStandalone && setPositionFixed()

if (!modal) {
if (!modal.value) {
setTimeout(() => {
restorePositionSetting()
}, 500)
Expand Down
Loading