@ark-ui/vue
Version:
A collection of unstyled, accessible UI components for Vue, utilizing state machines for seamless interaction.
42 lines (41 loc) • 1.59 kB
JavaScript
const require_runtime = require("../../_virtual/_rolldown/runtime.cjs");
let vue = require("vue");
let _zag_js_vue = require("@zag-js/vue");
let _zag_js_presence = require("@zag-js/presence");
_zag_js_presence = require_runtime.__toESM(_zag_js_presence, 1);
//#region src/components/presence/use-presence.ts
var usePresence = (props, emit) => {
const wasEverPresent = (0, vue.ref)(false);
const nodeRef = (0, vue.ref)(null);
const machineProps = (0, vue.computed)(() => {
return {
present: (0, vue.toValue)(props).present,
onExitComplete: () => emit?.("exitComplete")
};
});
const service = (0, _zag_js_vue.useMachine)(_zag_js_presence.machine, machineProps);
const api = (0, vue.computed)(() => _zag_js_presence.connect(service, _zag_js_vue.normalizeProps));
(0, vue.watch)(() => api.value.present, () => {
if (api.value.present) wasEverPresent.value = true;
});
(0, vue.watch)(nodeRef, () => {
if (nodeRef.value) {
const node = nodeRef.value.$el ? nodeRef.value.$el : nodeRef.value;
if (node) api.value.setNode(node);
}
});
return (0, vue.computed)(() => {
const localProps = (0, vue.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
exports.usePresence = usePresence;