@ark-ui/vue
Version:
A collection of unstyled, accessible UI components for Vue, utilizing state machines for seamless interaction.
58 lines (55 loc) • 2.01 kB
JavaScript
import { defineComponent, ref, watchEffect, onWatcherCleanup, onBeforeUnmount, createBlock, openBlock, unref, withCtx, renderSlot } from 'vue';
import { trapFocus } from '@zag-js/focus-trap';
import { ark } from '../factory.js';
import { useForwardExpose } from '../../utils/use-forward-expose.js';
import { cleanProps } from '../../utils/clean-props.js';
const _sfc_main = /* @__PURE__ */ defineComponent({
__name: "focus-trap",
props: {
disabled: { type: Boolean, default: void 0 },
initialFocus: { type: [String, Boolean, Function], default: void 0 },
fallbackFocus: { type: [String, Function], default: void 0 },
returnFocusOnDeactivate: { type: Boolean, default: void 0 },
setReturnFocus: { type: [String, Boolean, Function], default: void 0 },
asChild: { type: Boolean }
},
emits: ["activate", "deactivate"],
setup(__props, { emit: __emit }) {
const props = __props;
const emits = __emit;
const nodeRef = ref(null);
let cleanup;
watchEffect(() => {
if (props.disabled) return;
if (!nodeRef.value) return;
const node = nodeRef.value.$el ? nodeRef.value.$el : nodeRef.value;
if (!node) return;
const autoFocusNode = node.querySelector("[autofocus], [data-autofocus]");
const trapProps = cleanProps(props);
trapProps.initialFocus ||= autoFocusNode ?? void 0;
cleanup = trapFocus(node, {
...trapProps,
onActivate: () => emits("activate"),
onDeactivate: () => emits("deactivate")
});
onWatcherCleanup(() => cleanup?.());
});
onBeforeUnmount(() => {
cleanup?.();
});
useForwardExpose();
return (_ctx, _cache) => {
return openBlock(), createBlock(unref(ark).div, {
ref_key: "nodeRef",
ref: nodeRef,
"as-child": _ctx.asChild
}, {
default: withCtx(() => [
renderSlot(_ctx.$slots, "default")
]),
_: 3
}, 8, ["as-child"]);
};
}
});
export { _sfc_main as default };