@opensig/opendesign
Version:
103 lines (102 loc) • 3.61 kB
JavaScript
import { defineComponent, inject, computed, useTemplateRef, createElementBlock, openBlock, unref, normalizeStyle, normalizeClass, createCommentVNode, createElementVNode, createBlock, renderSlot, resolveDynamicComponent, withCtx } from "vue";
import { menuItemProps } from "./types.mjs";
import { menuInjectKey, subMenuInjectKey } from "./provide.mjs";
import { isUndefined } from "../_utils/is.mjs";
import { useElementOverflown } from "../hooks/use-element-overflown.mjs";
import "../hooks/use-theme.mjs";
import "../_utils/global.mjs";
import "@vueuse/core";
import { OPopover } from "../popover/index.mjs";
const _hoisted_1 = ["data-level"];
const _hoisted_2 = {
key: 0,
class: "o-menu-item-icon"
};
const _sfc_main = /* @__PURE__ */ defineComponent({
__name: "OMenuItem",
props: menuItemProps,
emits: ["click"],
setup(__props, { emit: __emit }) {
const props = __props;
const emits = __emit;
const menuInjection = inject(menuInjectKey, null);
const subMenuInjection = inject(subMenuInjectKey, null);
const isSelected = computed(() => {
if (menuInjection) {
return menuInjection.realValue.value === props.value;
}
return false;
});
const onItemClick = (ev) => {
ev.stopPropagation();
if (props.disabled) {
return;
}
if (isUndefined(props.value)) {
return;
}
emits("click", ev);
menuInjection == null ? void 0 : menuInjection.updateModelValue(props.value);
};
const currentDepth = subMenuInjection ? subMenuInjection.parentDepth + 1 : 0;
menuInjection == null ? void 0 : menuInjection.menuTree.addChild({
value: props.value,
parentVal: subMenuInjection == null ? void 0 : subMenuInjection.value
});
menuInjection == null ? void 0 : menuInjection.notifyTreeChange();
const menuItemRef = useTemplateRef("menuItemRef");
const itemContentRef = useTemplateRef("itemContentRef");
const isContentOverflow = useElementOverflown(itemContentRef);
return (_ctx, _cache) => {
return openBlock(), createElementBlock("li", {
ref_key: "menuItemRef",
ref: menuItemRef,
class: normalizeClass({
"o-menu-item": true,
"o-menu-item-selected": isSelected.value,
"o-menu-item-disabled": _ctx.$props.disabled
}),
style: normalizeStyle({
"--menu-level": unref(currentDepth)
}),
"data-level": unref(currentDepth),
onClick: onItemClick
}, [
props.icon || _ctx.$slots.icon ? (openBlock(), createElementBlock("div", _hoisted_2, [
renderSlot(_ctx.$slots, "icon", {}, () => [
(openBlock(), createBlock(resolveDynamicComponent(props.icon)))
])
])) : createCommentVNode("v-if", true),
createElementVNode(
"div",
{
ref_key: "itemContentRef",
ref: itemContentRef,
class: "o-menu-item-content"
},
[
renderSlot(_ctx.$slots, "default")
],
512
/* NEED_PATCH */
),
unref(isContentOverflow) ? (openBlock(), createBlock(unref(OPopover), {
key: 1,
offset: 12,
target: menuItemRef.value,
position: "bottom",
"wrap-class": "o-menu-popover"
}, {
default: withCtx(() => [
renderSlot(_ctx.$slots, "default")
]),
_: 3
/* FORWARDED */
}, 8, ["target"])) : createCommentVNode("v-if", true)
], 14, _hoisted_1);
};
}
});
export {
_sfc_main as default
};