reka-ui
Version:
Vue port for Radix UI Primitives.
121 lines (118 loc) • 3.96 kB
JavaScript
import { createContext } from "../shared/createContext.js";
import { getActiveElement } from "../shared/getActiveElement.js";
import { useArrowNavigation } from "../shared/useArrowNavigation.js";
import { useForwardExpose } from "../shared/useForwardExpose.js";
import { useId } from "../shared/useId.js";
import { Primitive } from "../Primitive/Primitive.js";
import { useCollection } from "../Collection/Collection.js";
import { injectNavigationMenuContext } from "./NavigationMenuRoot.js";
import { focusFirst, getTabbableCandidates, makeContentId, removeFromTabOrder } from "./utils.js";
import { createBlock, defineComponent, openBlock, ref, renderSlot, unref, withCtx, withKeys } from "vue";
//#region src/NavigationMenu/NavigationMenuItem.vue?vue&type=script&setup=true&lang.ts
const [injectNavigationMenuItemContext, provideNavigationMenuItemContext] = createContext("NavigationMenuItem");
var NavigationMenuItem_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
__name: "NavigationMenuItem",
props: {
value: {
type: String,
required: false
},
asChild: {
type: Boolean,
required: false
},
as: {
type: null,
required: false,
default: "li"
}
},
setup(__props) {
const props = __props;
useForwardExpose();
const { getItems } = useCollection({ key: "NavigationMenu" });
const context = injectNavigationMenuContext();
const value = useId(props.value);
const triggerRef = ref();
const focusProxyRef = ref();
const contentId = makeContentId(context.baseId, value);
let restoreContentTabOrderRef = () => ({});
const wasEscapeCloseRef = ref(false);
async function handleContentEntry(side = "start") {
const el = document.getElementById(contentId);
if (el) {
restoreContentTabOrderRef();
const candidates = getTabbableCandidates(el);
if (candidates.length) focusFirst(side === "start" ? candidates : candidates.reverse());
}
}
function handleContentExit() {
const el = document.getElementById(contentId);
if (el) {
const candidates = getTabbableCandidates(el);
if (candidates.length) restoreContentTabOrderRef = removeFromTabOrder(candidates);
}
}
provideNavigationMenuItemContext({
value,
contentId,
triggerRef,
focusProxyRef,
wasEscapeCloseRef,
onEntryKeyDown: handleContentEntry,
onFocusProxyEnter: handleContentEntry,
onContentFocusOutside: handleContentExit,
onRootContentClose: handleContentExit
});
function handleClose() {
context.onItemDismiss();
triggerRef.value?.focus();
}
function handleKeydown(ev) {
const currentFocus = getActiveElement();
if (ev.keyCode === 32 || ev.key === "Enter") if (context.modelValue.value === value) {
handleClose();
ev.preventDefault();
return;
} else {
ev.target.click();
ev.preventDefault();
return;
}
const itemsArray = getItems().filter((i) => i.ref.parentElement?.hasAttribute("data-menu-item")).map((i) => i.ref);
if (!itemsArray.includes(currentFocus)) return;
const newSelectedElement = useArrowNavigation(ev, currentFocus, void 0, {
itemsArray,
loop: false
});
if (newSelectedElement) newSelectedElement?.focus();
ev.preventDefault();
ev.stopPropagation();
}
return (_ctx, _cache) => {
return openBlock(), createBlock(unref(Primitive), {
"as-child": _ctx.asChild,
as: _ctx.as,
"data-menu-item": "",
onKeydown: withKeys(handleKeydown, [
"up",
"down",
"left",
"right",
"home",
"end",
"space"
])
}, {
default: withCtx(() => [renderSlot(_ctx.$slots, "default")]),
_: 3
}, 8, ["as-child", "as"]);
};
}
});
//#endregion
//#region src/NavigationMenu/NavigationMenuItem.vue
var NavigationMenuItem_default = NavigationMenuItem_vue_vue_type_script_setup_true_lang_default;
//#endregion
export { NavigationMenuItem_default, injectNavigationMenuItemContext };
//# sourceMappingURL=NavigationMenuItem.js.map