Skip to content

Commit c0cd15d

Browse files
committed
fix: fix onExitComplete & types
1 parent 333a401 commit c0cd15d

File tree

3 files changed

+18
-46
lines changed

3 files changed

+18
-46
lines changed
Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
1-
import type { Context } from '@zag-js/presence'
2-
import type { PropType } from 'vue'
3-
import { declareEmits } from '../utils'
4-
import type { UsePresenceProps } from './use-presence'
5-
61
export const props = {
72
present: {
8-
type: Boolean as PropType<Context['present']>,
3+
type: Boolean,
94
default: undefined,
105
},
11-
lazyMount: {
12-
type: Boolean as PropType<UsePresenceProps['lazyMount']>,
13-
default: false,
14-
},
15-
unmountOnExit: {
16-
type: Boolean as PropType<UsePresenceProps['unmountOnExit']>,
17-
default: false,
18-
},
6+
lazyMount: Boolean,
7+
unmountOnExit: Boolean,
198
}
20-
21-
export const emits = declareEmits(['exit-complete'])
Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { defineComponent, type PropType } from 'vue'
1+
import { defineComponent } from 'vue'
22
import { ark, type HTMLArkProps } from '../factory'
33
import type { Assign } from '../types'
4-
import { emits } from './presence.props'
4+
import { props } from './presence.props'
55
import { usePresence, type UsePresenceProps } from './use-presence'
66

77
export interface PresenceProps extends Assign<HTMLArkProps<'div'>, UsePresenceProps> {}
@@ -19,20 +19,6 @@ export const Presence = defineComponent<PresenceProps>(
1919
},
2020
{
2121
name: 'Presence',
22-
props: {
23-
present: {
24-
type: Boolean as PropType<UsePresenceProps['present']>,
25-
default: undefined,
26-
},
27-
lazyMount: {
28-
type: Boolean as PropType<UsePresenceProps['lazyMount']>,
29-
default: undefined,
30-
},
31-
unmountOnExit: {
32-
type: Boolean as PropType<UsePresenceProps['unmountOnExit']>,
33-
default: undefined,
34-
},
35-
},
36-
emits,
22+
props,
3723
},
3824
)

packages/frameworks/vue/src/presence/use-presence.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as presence from '@zag-js/presence'
22
import { normalizeProps, useMachine } from '@zag-js/vue'
3-
import { computed, ref, watch, type VNodeRef } from 'vue'
3+
import { computed, ref, toRef, toValue, watch, type MaybeRef, type VNodeRef } from 'vue'
44
import type { Optional } from '../types'
55

66
export interface UsePresenceProps extends Optional<presence.Context, 'present'> {
@@ -18,20 +18,19 @@ export interface UsePresenceProps extends Optional<presence.Context, 'present'>
1818

1919
export type UsePresenceReturn = ReturnType<typeof usePresence>
2020

21-
export const usePresence = (props: UsePresenceProps, emit: CallableFunction) => {
22-
const context = ref(props)
21+
export const usePresence = (props: MaybeRef<UsePresenceProps>, emit: CallableFunction) => {
22+
const context = computed(() => ({
23+
...toValue(props),
24+
// FIXME: we have to declare here, because zag did not set `onExitComplete` during initialization.
25+
// FIX PR: https://github.com/chakra-ui/zag/pull/1124, maybe we can move `onExitComplete` to init later.
26+
onExitComplete: () => {
27+
emit('exit-complete')
28+
},
29+
}))
2330
const wasEverPresent = ref(false)
2431
const nodeRef = ref<VNodeRef | null>(null)
2532

26-
const [state, send] = useMachine(
27-
presence.machine({
28-
...context.value,
29-
onExitComplete: () => {
30-
emit('exit-complete')
31-
},
32-
}),
33-
{ context },
34-
)
33+
const [state, send] = useMachine(presence.machine(context.value), { context })
3534
const api = computed(() => presence.connect(state.value, send, normalizeProps))
3635

3736
watch(
@@ -51,7 +50,7 @@ export const usePresence = (props: UsePresenceProps, emit: CallableFunction) =>
5150
}
5251
})
5352

54-
return computed(() => ({
53+
return toRef(() => ({
5554
isPresent: api.value.isPresent,
5655
isUnmounted:
5756
(!api.value.isPresent && !wasEverPresent.value && context.value.lazyMount) ||

0 commit comments

Comments
 (0)