Skip to content

Commit

Permalink
fix: closing not trigger animation (#40)
Browse files Browse the repository at this point in the history
* fix: closing not trigger animation

* fix: other cases failing

* chore: update playground

* chore: revert rendered value

* chore: add changeset
  • Loading branch information
zernonia authored May 1, 2024
1 parent e3291c1 commit b3f6ce6
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-apricots-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vaul-vue": patch
---

fix manual closing doesn't trigger animation
10 changes: 9 additions & 1 deletion packages/vaul-vue/src/DrawerContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DialogContent } from 'radix-vue'
import { injectDrawerRootContext } from './context'
const {
open,
isOpen,
isVisible,
snapPointsOffset,
Expand Down Expand Up @@ -34,7 +35,10 @@ function handlePointerDownOutside(event: Event) {
keyboardIsOpen.value = false
event.preventDefault()
if (!dismissible.value)
if (dismissible.value)
emitOpenChange(false)
if (!dismissible.value || open.value !== undefined)
return
closeDrawer()
Expand Down Expand Up @@ -63,6 +67,10 @@ watch(
@pointermove="onDrag"
@pointerup="onRelease"
@pointer-down-outside="handlePointerDownOutside"
@escape-key-down="(event) => {
if (!dismissible)
event.preventDefault()
}"
>
<slot />
</DialogContent>
Expand Down
13 changes: 9 additions & 4 deletions packages/vaul-vue/src/DrawerRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
const props = withDefaults(defineProps<DrawerRootProps>(), {
open: undefined,
defaultOpen: undefined,
fixed: undefined,
dismissible: true,
activeSnapPoint: undefined,
Expand Down Expand Up @@ -43,11 +44,11 @@ const emitHandlers = {
emitRelease: (open: boolean) => emit('release', open),
emitClose: () => emit('close'),
emitOpenChange: (o: boolean) => {
open.value = o
emit('update:open', o)
},
}
const { closeDrawer, hasBeenOpened, modal } = provideDrawerRootContext(
const { closeDrawer, hasBeenOpened, modal, isOpen } = provideDrawerRootContext(
useDrawer({
...emitHandlers,
...toRefs(props),
Expand All @@ -58,19 +59,23 @@ const { closeDrawer, hasBeenOpened, modal } = provideDrawerRootContext(
)
function handleOpenChange(o: boolean) {
if (open.value !== undefined) {
emitHandlers.emitOpenChange(o)
return
}
if (!o) {
closeDrawer()
}
else {
hasBeenOpened.value = true
open.value = o
isOpen.value = o
}
}
</script>

<template>
<DialogRoot
:open="open"
:open="isOpen"
:modal="modal"
@update:open="handleOpenChange"
>
Expand Down
1 change: 1 addition & 0 deletions packages/vaul-vue/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ComponentPublicInstance, Ref } from 'vue'
import { createContext } from 'radix-vue'

export interface DrawerRootContext {
open: Ref<boolean>
isOpen: Ref<boolean>
modal: Ref<boolean>
hasBeenOpened: Ref<boolean>
Expand Down
29 changes: 20 additions & 9 deletions packages/vaul-vue/src/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function useDrawer(props: UseDrawerProps & DialogEmitHandlers): DrawerRoo
emitRelease,
emitClose,
emitOpenChange,
open: isOpen,
open,
dismissible,
nested,
fixed,
Expand All @@ -113,6 +113,7 @@ export function useDrawer(props: UseDrawerProps & DialogEmitHandlers): DrawerRoo
fadeFromIndex,
} = props

const isOpen = ref(open.value ?? false)
const hasBeenOpened = ref(false)
const isVisible = ref(false)
const isDragging = ref(false)
Expand Down Expand Up @@ -372,7 +373,7 @@ export function useDrawer(props: UseDrawerProps & DialogEmitHandlers): DrawerRoo
})

// Don't reset background if swiped upwards
if (shouldScaleBackground.value && currentSwipeAmount && currentSwipeAmount > 0 && isOpen) {
if (shouldScaleBackground.value && currentSwipeAmount && currentSwipeAmount > 0 && isOpen.value) {
set(
wrapper,
{
Expand Down Expand Up @@ -407,10 +408,9 @@ export function useDrawer(props: UseDrawerProps & DialogEmitHandlers): DrawerRoo
scaleBackground(false)
restorePositionSetting()

isVisible.value = false
window.setTimeout(() => {
emitOpenChange(false)
// isOpen.value = false
isVisible.value = false
isOpen.value = false
}, 300)

window.setTimeout(() => {
Expand Down Expand Up @@ -491,13 +491,23 @@ export function useDrawer(props: UseDrawerProps & DialogEmitHandlers): DrawerRoo
resetDrawer()
}

watch(isOpen, (open) => {
if (open) {
watch(isOpen, (o) => {
if (o) {
openTime.value = new Date()
scaleBackground(true)
}
emitOpenChange(open)
})
emitOpenChange(o)
}, { immediate: true })

watch(open, (o) => {
if (o) {
isOpen.value = o
hasBeenOpened.value = true
}
else {
closeDrawer()
}
}, { immediate: true })

function scaleBackground(open: boolean) {
const wrapper = document.querySelector('[vaul-drawer-wrapper]')
Expand Down Expand Up @@ -587,6 +597,7 @@ export function useDrawer(props: UseDrawerProps & DialogEmitHandlers): DrawerRoo
}

return {
open,
isOpen,
modal,
keyboardIsOpen,
Expand Down
4 changes: 2 additions & 2 deletions playground/src/views/tests/ControlledView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
} from 'vaul-vue'
import { ref } from 'vue'
const open = ref < boolean > (false)
const fullyControlled = ref < boolean > (false)
const open = ref<boolean>(false)
const fullyControlled = ref<boolean>(false)
</script>

<template>
Expand Down
2 changes: 1 addition & 1 deletion playground/src/views/tests/NonDismissibleView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from 'vaul-vue'
import { ref } from 'vue'
const open = ref < boolean > (false)
const open = ref<boolean>(false)
</script>

<template>
Expand Down
2 changes: 1 addition & 1 deletion playground/src/views/tests/WithoutScaledBackgroundView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'vaul-vue'
import { ref } from 'vue'
const open = ref < boolean > (false)
const open = ref<boolean>(false)
</script>

<template>
Expand Down

0 comments on commit b3f6ce6

Please sign in to comment.