reka-ui
Version:
Vue port for Radix UI Primitives.
90 lines (87 loc) • 2.91 kB
JavaScript
import { useForwardExpose } from "../shared/useForwardExpose.js";
import { ITEM_SELECT, SELECTION_KEYS } from "./utils.js";
import { injectMenuRootContext } from "./MenuRoot.js";
import { injectMenuContentContext } from "./MenuContentImpl.js";
import { MenuItemImpl_default } from "./MenuItemImpl.js";
import { createBlock, defineComponent, mergeProps, nextTick, openBlock, ref, renderSlot, unref, withCtx } from "vue";
//#region src/Menu/MenuItem.vue?vue&type=script&setup=true&lang.ts
var MenuItem_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
__name: "MenuItem",
props: {
disabled: {
type: Boolean,
required: false
},
textValue: {
type: String,
required: false
},
asChild: {
type: Boolean,
required: false
},
as: {
type: null,
required: false
}
},
emits: ["select"],
setup(__props, { emit: __emit }) {
const props = __props;
const emits = __emit;
const { forwardRef, currentElement } = useForwardExpose();
const rootContext = injectMenuRootContext();
const contentContext = injectMenuContentContext();
const isPointerDownRef = ref(false);
async function handleSelect() {
const menuItem = currentElement.value;
if (!props.disabled && menuItem) {
const itemSelectEvent = new CustomEvent(ITEM_SELECT, {
bubbles: true,
cancelable: true
});
emits("select", itemSelectEvent);
await nextTick();
if (itemSelectEvent.defaultPrevented) isPointerDownRef.value = false;
else rootContext.onClose();
}
}
return (_ctx, _cache) => {
return openBlock(), createBlock(MenuItemImpl_default, mergeProps(props, {
ref: unref(forwardRef),
onClick: handleSelect,
onPointerdown: _cache[0] || (_cache[0] = () => {
isPointerDownRef.value = true;
}),
onPointerup: _cache[1] || (_cache[1] = async (event) => {
await nextTick();
if (event.defaultPrevented) return;
if (!isPointerDownRef.value) event.currentTarget?.click();
}),
onKeydown: _cache[2] || (_cache[2] = async (event) => {
const isTypingAhead = unref(contentContext).searchRef.value !== "";
if (_ctx.disabled || isTypingAhead && event.key === " ") return;
if (unref(SELECTION_KEYS).includes(event.key)) {
event.currentTarget.click();
/**
* We prevent default browser behaviour for selection keys as they should trigger
* a selection only:
* - prevents space from scrolling the page.
* - if keydown causes focus to move, prevents keydown from firing on the new target.
*/
event.preventDefault();
}
})
}), {
default: withCtx(() => [renderSlot(_ctx.$slots, "default")]),
_: 3
}, 16);
};
}
});
//#endregion
//#region src/Menu/MenuItem.vue
var MenuItem_default = MenuItem_vue_vue_type_script_setup_true_lang_default;
//#endregion
export { MenuItem_default };
//# sourceMappingURL=MenuItem.js.map