1
1
import * as presence from '@zag-js/presence'
2
2
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'
4
4
import type { Optional } from '../types'
5
5
6
6
export interface UsePresenceProps extends Optional < presence . Context , 'present' > {
@@ -18,20 +18,19 @@ export interface UsePresenceProps extends Optional<presence.Context, 'present'>
18
18
19
19
export type UsePresenceReturn = ReturnType < typeof usePresence >
20
20
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
+ } ) )
23
30
const wasEverPresent = ref ( false )
24
31
const nodeRef = ref < VNodeRef | null > ( null )
25
32
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 } )
35
34
const api = computed ( ( ) => presence . connect ( state . value , send , normalizeProps ) )
36
35
37
36
watch (
@@ -51,7 +50,7 @@ export const usePresence = (props: UsePresenceProps, emit: CallableFunction) =>
51
50
}
52
51
} )
53
52
54
- return computed ( ( ) => ( {
53
+ return toRef ( ( ) => ( {
55
54
isPresent : api . value . isPresent ,
56
55
isUnmounted :
57
56
( ! api . value . isPresent && ! wasEverPresent . value && context . value . lazyMount ) ||
0 commit comments