UNPKG

@opensig/opendesign

Version:

172 lines (171 loc) 6.2 kB
import { defineComponent, inject, computed, provide, useTemplateRef, createElementBlock, openBlock, unref, normalizeStyle, normalizeClass, createElementVNode, createBlock, createCommentVNode, createVNode, renderSlot, resolveDynamicComponent, withCtx } from "vue"; import { subMenuProps } from "./types.mjs"; import { IconChevronDownBold } from "../_utils/icons.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-sub-menu-arrow" }; const _hoisted_3 = { key: 1, class: "o-sub-menu-title-icon" }; const _hoisted_4 = { key: 2, class: "o-sub-menu-arrow" }; const _hoisted_5 = { class: "o-sub-menu-children-wrap" }; const _sfc_main = /* @__PURE__ */ defineComponent({ __name: "OSubMenu", props: subMenuProps, setup(__props) { const props = __props; const menuInjection = inject(menuInjectKey, null); const subMenuInjection = inject(subMenuInjectKey, null); const { arrowPosition } = menuInjection || {}; const isExpanded = computed(() => { if (isUndefined(props.value)) { return false; } if (menuInjection) { return menuInjection.realExpanded.value.includes(props.value); } return false; }); const isAssociatedSelected = computed(() => { if (menuInjection) { return menuInjection.activeNodes.value.includes(props.value); } return false; }); const isSelected = computed(() => { if (menuInjection) { return menuInjection.realValue.value === props.value; } return false; }); const onSubItemClick = (ev) => { ev.stopPropagation(); if (isUndefined(props.value)) { return; } let set = menuInjection ? /* @__PURE__ */ new Set([...menuInjection.realExpanded.value]) : /* @__PURE__ */ new Set([]); if (isExpanded.value && set.has(props.value)) { set.delete(props.value); } if (!isExpanded.value && !set.has(props.value)) { if (menuInjection == null ? void 0 : menuInjection.accordion.value) { const siblings = (menuInjection == null ? void 0 : menuInjection.menuTree.getSiblings(props.value)) || []; siblings.forEach((val) => { set.delete(val); }); } set.add(props.value); } const expandedVal = Array.from(set); menuInjection == null ? void 0 : menuInjection.updateExpanded(expandedVal); if (props.selectable) { menuInjection == null ? void 0 : menuInjection.updateModelValue(props.value); } }; const currentDepth = subMenuInjection ? subMenuInjection.parentDepth + 1 : 0; provide(subMenuInjectKey, { value: props.value, parentDepth: currentDepth }); menuInjection == null ? void 0 : menuInjection.menuTree.addChild({ value: props.value, parentVal: subMenuInjection == null ? void 0 : subMenuInjection.value }); menuInjection == null ? void 0 : menuInjection.notifyTreeChange(); const subMenuTitleRef = useTemplateRef("subMenuTitleRef"); const itemContentRef = useTemplateRef("itemContentRef"); const isContentOverflow = useElementOverflown(itemContentRef); return (_ctx, _cache) => { return openBlock(), createElementBlock("li", { class: normalizeClass({ "o-sub-menu": true, "o-sub-menu-selected": isSelected.value, "o-sub-menu-associated-selected": isAssociatedSelected.value, "o-sub-menu-expanded": isExpanded.value }), style: normalizeStyle({ "--menu-level": unref(currentDepth) }), "data-level": unref(currentDepth) }, [ createElementVNode( "div", { ref_key: "subMenuTitleRef", ref: subMenuTitleRef, class: "o-sub-menu-title", onClick: onSubItemClick }, [ unref(arrowPosition) === "left" ? (openBlock(), createElementBlock("div", _hoisted_2, [ createVNode(unref(IconChevronDownBold)) ])) : createCommentVNode("v-if", true), _ctx.$slots.icon || props.icon ? (openBlock(), createElementBlock("div", _hoisted_3, [ renderSlot(_ctx.$slots, "icon", {}, () => [ (openBlock(), createBlock(resolveDynamicComponent(props.icon))) ]) ])) : createCommentVNode("v-if", true), createElementVNode( "div", { ref_key: "itemContentRef", ref: itemContentRef, class: "o-sub-menu-title-content" }, [ renderSlot(_ctx.$slots, "title") ], 512 /* NEED_PATCH */ ), !unref(arrowPosition) || unref(arrowPosition) === "right" ? (openBlock(), createElementBlock("div", _hoisted_4, [ createVNode(unref(IconChevronDownBold)) ])) : createCommentVNode("v-if", true) ], 512 /* NEED_PATCH */ ), createElementVNode( "ul", { class: normalizeClass(["o-sub-menu-children", { expanded: isExpanded.value }]) }, [ createElementVNode("div", _hoisted_5, [ renderSlot(_ctx.$slots, "default") ]) ], 2 /* CLASS */ ), unref(isContentOverflow) ? (openBlock(), createBlock(unref(OPopover), { key: 0, offset: 12, target: subMenuTitleRef.value, position: "bottom", "wrap-class": "o-menu-popover" }, { default: withCtx(() => [ renderSlot(_ctx.$slots, "title") ]), _: 3 /* FORWARDED */ }, 8, ["target"])) : createCommentVNode("v-if", true) ], 14, _hoisted_1); }; } }); export { _sfc_main as default };