@ark-ui/vue
Version:
A collection of unstyled, accessible UI components for Vue, utilizing state machines for seamless interaction.
40 lines (39 loc) • 1.38 kB
JavaScript
import { computed, ref, toValue, watch } from "vue";
import { normalizeProps as normalizeProps$1, useMachine } from "@zag-js/vue";
import * as presence from "@zag-js/presence";
//#region src/components/presence/use-presence.ts
var usePresence = (props, emit) => {
const wasEverPresent = ref(false);
const nodeRef = ref(null);
const machineProps = computed(() => {
return {
present: toValue(props).present,
onExitComplete: () => emit?.("exitComplete")
};
});
const service = useMachine(presence.machine, machineProps);
const api = computed(() => presence.connect(service, normalizeProps$1));
watch(() => api.value.present, () => {
if (api.value.present) wasEverPresent.value = true;
});
watch(nodeRef, () => {
if (nodeRef.value) {
const node = nodeRef.value.$el ? nodeRef.value.$el : nodeRef.value;
if (node) api.value.setNode(node);
}
});
return computed(() => {
const localProps = toValue(props);
return {
present: api.value.present,
unmounted: !api.value.present && !wasEverPresent.value && localProps.lazyMount || localProps?.unmountOnExit && !api.value?.present && wasEverPresent.value,
presenceProps: {
ref: nodeRef,
hidden: !api.value.present,
"data-state": api.value.skip && localProps.skipAnimationOnMount ? void 0 : localProps?.present ? "open" : "closed"
}
};
});
};
//#endregion
export { usePresence };