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

feat: allow hooking on Dialog animation end #901

Merged
merged 5 commits into from
May 9, 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
2 changes: 1 addition & 1 deletion packages/radix-vue/src/Dialog/DialogContentImpl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type DialogContentImplEmits = DismissableLayerEmits & {
* Event handler called when auto-focusing on close.
* Can be prevented.
*/
'closeAutoFocus': [ event: Event]
'closeAutoFocus': [event: Event]
}

export interface DialogContentImplProps extends DismissableLayerProps {
Expand Down
24 changes: 21 additions & 3 deletions packages/radix-vue/src/Presence/usePresence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export function usePresence(
},
})

const dispatchCustomEvent = (name: 'enter' | 'after-enter' | 'leave' | 'after-leave') => {
const customEvent = new CustomEvent(name, { bubbles: false, cancelable: false })
node.value?.dispatchEvent(customEvent)
}

watch(
present,
async (currentPresent, prevPresent) => {
Expand All @@ -34,6 +39,9 @@ export function usePresence(

if (currentPresent) {
dispatch('MOUNT')
dispatchCustomEvent('enter')
if (currentAnimationName === 'none')
dispatchCustomEvent('after-enter')
}
else if (
currentAnimationName === 'none'
Expand All @@ -42,6 +50,8 @@ export function usePresence(
// If there is no exit animation or the element is hidden, animations won't run
// so we unmount instantly rv
dispatch('UNMOUNT')
dispatchCustomEvent('leave')
dispatchCustomEvent('after-leave')
}
else {
/**
Expand All @@ -51,9 +61,14 @@ export function usePresence(
* fires after `animation-delay` has expired which would be too late.
*/
const isAnimating = prevAnimationName !== currentAnimationName
if (prevPresent && isAnimating)
if (prevPresent && isAnimating) {
dispatch('ANIMATION_OUT')
else dispatch('UNMOUNT')
dispatchCustomEvent('leave')
}
else {
dispatch('UNMOUNT')
dispatchCustomEvent('after-leave')
}
}
}
},
Expand All @@ -70,8 +85,11 @@ export function usePresence(
const isCurrentAnimation = currentAnimationName.includes(
event.animationName,
)
if (event.target === node.value && isCurrentAnimation)
const directionName = state.value === 'mounted' ? 'enter' : 'leave'
if (event.target === node.value && isCurrentAnimation) {
dispatchCustomEvent(`after-${directionName}`)
dispatch('ANIMATION_END')
}
// if no animation, immediately trigger 'ANIMATION_END'
if (event.target === node.value && currentAnimationName === 'none')
dispatch('ANIMATION_END')
Expand Down
Loading