hongluan-ui
Version:
Hongluan Component Library for Vue 3
317 lines (314 loc) • 11.2 kB
JavaScript
import { defineComponent, computed, getCurrentInstance, inject, ref, reactive, watch, provide, onMounted, onBeforeUnmount, h, Fragment, withDirectives, vShow } from 'vue';
import { useTimeoutFn } from '@vueuse/core';
import { HlCollapseTransition } from '../../collapse-transition/index.mjs';
import { HlTooltip } from '../../tooltip/index.mjs';
import '../../../utils/index.mjs';
import '../../system-icon/index.mjs';
import { HlIcon } from '../../icon/index.mjs';
import '../../../hooks/index.mjs';
import useMenu from './use-menu.mjs';
import { buildProps, definePropType } from '../../../utils/vue/props/runtime.mjs';
import { useDeprecated } from '../../../hooks/use-deprecated/index.mjs';
import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
import { throwError } from '../../../utils/error.mjs';
import SystemArrowRight from '../../system-icon/src/arrow-right.mjs';
const subMenuProps = buildProps({
index: {
type: String,
required: true
},
showTimeout: Number,
hideTimeout: Number,
fallbackPlacements: {
type: definePropType(Array)
},
popperClass: String,
disabled: Boolean,
teleported: {
type: Boolean,
default: void 0
},
popperAppendToBody: {
type: Boolean,
default: void 0
},
popperOffset: Number
});
const COMPONENT_NAME = "SubMenu";
var SubMenu = defineComponent({
name: COMPONENT_NAME,
props: subMenuProps,
setup(props, { slots, expose }) {
var _a;
useDeprecated({
from: "popper-append-to-body",
replacement: "teleported",
scope: COMPONENT_NAME,
version: "2.5.0",
ref: "https://element-plus.org/en-US/component/menu.html#submenu-attributes"
}, computed(() => props.popperAppendToBody !== void 0));
const instance = getCurrentInstance();
const { namespace } = useNamespace();
const { indexPath, parentMenu } = useMenu(instance, computed(() => props.index));
const rootMenu = inject("rootMenu");
if (!rootMenu)
throwError(COMPONENT_NAME, "can not inject root menu");
const subMenu = inject(`subMenu:${(_a = parentMenu.value) == null ? void 0 : _a.uid}`);
if (!subMenu)
throwError(COMPONENT_NAME, "can not inject sub menu");
const items = ref({});
const subMenus = ref({});
let timeout;
const mouseInChild = ref(false);
const verticalTitleRef = ref();
const vPopper = ref(null);
const currentPlacement = computed(() => mode.value === "horizontal" && isFirstLevel.value ? "bottom-start" : "right-start");
const isFirstLevel = computed(() => {
let isFirstLevel2 = true;
let parent = instance.parent;
while (parent && parent.type.name !== "Menu") {
if (["SubMenu", "MenuItemGroup"].includes(parent.type.name)) {
isFirstLevel2 = false;
break;
} else {
parent = parent.parent;
}
}
return isFirstLevel2;
});
const appendToBody = computed(() => {
var _a2;
const value = (_a2 = props.teleported) != null ? _a2 : props.popperAppendToBody;
return value === void 0 ? isFirstLevel.value : value;
});
const menuTransitionName = computed(() => rootMenu.finalCollapse ? "zoom-in-left" : "zoom-in-top");
const fallbackPlacements = computed(() => mode.value === "horizontal" && isFirstLevel.value ? [
"bottom-start",
"bottom-end",
"top-start",
"top-end",
"right-start",
"left-start"
] : [
"right-start",
"right",
"right-end",
"left-start",
"bottom-start",
"bottom-end",
"top-start",
"top-end"
]);
const opened = computed(() => rootMenu.openedMenus.includes(props.index));
const active = computed(() => {
let isActive = false;
Object.values(items.value).forEach((item2) => {
if (item2.active) {
isActive = true;
}
});
Object.values(subMenus.value).forEach((subItem) => {
if (subItem.active) {
isActive = true;
}
});
return isActive;
});
const backgroundColor = computed(() => rootMenu.props.backgroundColor || "");
const activeTextColor = computed(() => rootMenu.props.activeTextColor || "");
const textColor = computed(() => rootMenu.props.textColor || "");
const mode = computed(() => rootMenu.props.mode);
const item = reactive({
index: props.index,
indexPath,
active
});
const titleStyle = computed(() => {
if (mode.value !== "horizontal") {
return {
color: textColor.value
};
}
return {
borderBottomColor: active.value ? rootMenu.props.activeTextColor ? activeTextColor.value : "" : "transparent",
color: active.value ? activeTextColor.value : textColor.value
};
});
const subMenuPopperOffset = computed(() => {
var _a2;
return (_a2 = props.popperOffset) != null ? _a2 : rootMenu.props.popperOffset;
});
const subMenuPopperClass = computed(() => {
var _a2;
return (_a2 = props.popperClass) != null ? _a2 : rootMenu.props.popperClass;
});
const subMenuShowTimeout = computed(() => {
var _a2;
return (_a2 = props.showTimeout) != null ? _a2 : rootMenu.props.showTimeout;
});
const subMenuHideTimeout = computed(() => {
var _a2;
return (_a2 = props.hideTimeout) != null ? _a2 : rootMenu.props.hideTimeout;
});
const doDestroy = () => {
var _a2, _b, _c;
return (_c = (_b = (_a2 = vPopper.value) == null ? void 0 : _a2.popperRef) == null ? void 0 : _b.popperInstanceRef) == null ? void 0 : _c.destroy();
};
const handleCollapseToggle = (value) => {
if (!value) {
doDestroy();
}
};
const handleClick = () => {
if (rootMenu.props.menuTrigger === "hover" && rootMenu.props.mode === "horizontal" || rootMenu.finalCollapse && rootMenu.props.mode === "vertical" || props.disabled)
return;
rootMenu.handleSubMenuClick({
index: props.index,
indexPath: indexPath.value,
active: active.value
});
};
const handleMouseenter = (event, showTimeout = subMenuShowTimeout.value) => {
var _a2;
if (event.type === "focus") {
return;
}
if (rootMenu.props.menuTrigger === "click" && rootMenu.props.mode === "horizontal" || !rootMenu.finalCollapse && rootMenu.props.mode === "vertical" || props.disabled) {
subMenu.mouseInChild.value = true;
return;
}
subMenu.mouseInChild.value = true;
timeout == null ? void 0 : timeout();
({ stop: timeout } = useTimeoutFn(() => {
rootMenu.openMenu(props.index, indexPath.value);
}, showTimeout));
if (appendToBody.value) {
(_a2 = parentMenu.value.vnode.el) == null ? void 0 : _a2.dispatchEvent(new MouseEvent("mouseenter"));
}
};
const handleMouseleave = (deepDispatch = false) => {
var _a2;
if (rootMenu.props.menuTrigger === "click" && rootMenu.props.mode === "horizontal" || !rootMenu.finalCollapse && rootMenu.props.mode === "vertical") {
subMenu.mouseInChild.value = false;
return;
}
timeout == null ? void 0 : timeout();
subMenu.mouseInChild.value = false;
({ stop: timeout } = useTimeoutFn(() => !mouseInChild.value && rootMenu.closeMenu(props.index, indexPath.value), subMenuHideTimeout.value));
if (appendToBody.value && deepDispatch) {
(_a2 = subMenu.handleMouseleave) == null ? void 0 : _a2.call(subMenu, true);
}
};
watch(() => rootMenu.finalCollapse, (value) => handleCollapseToggle(Boolean(value)));
{
const addSubMenu = (item2) => {
subMenus.value[item2.index] = item2;
};
const removeSubMenu = (item2) => {
delete subMenus.value[item2.index];
};
provide(`subMenu:${instance.uid}`, {
addSubMenu,
removeSubMenu,
handleMouseleave,
mouseInChild
});
}
expose({
opened
});
onMounted(() => {
rootMenu.addSubMenu(item);
subMenu.addSubMenu(item);
});
onBeforeUnmount(() => {
subMenu.removeSubMenu(item);
rootMenu.removeSubMenu(item);
});
return () => {
var _a2, _b, _c;
const titleTag = [
(_a2 = slots.title) == null ? void 0 : _a2.call(slots),
slots.icon ? (_b = slots.icon) == null ? void 0 : _b.call(slots, { opened: opened.value, collapse: rootMenu.finalCollapse }) : h(HlIcon, {
class: ["menu-arrow"]
}, { default: () => h(SystemArrowRight) })
];
const child = rootMenu.isMenuPopup ? h(HlTooltip, {
ref: vPopper,
manualMode: true,
visible: opened.value,
offset: subMenuPopperOffset.value,
showArrow: false,
persistent: true,
popperClass: `${(_c = subMenuPopperClass.value) != null ? _c : ""} ${namespace.value}-menu-popper`,
placement: currentPlacement.value,
teleported: appendToBody.value,
fallbackPlacements: fallbackPlacements.value,
transition: menuTransitionName.value,
gpuAcceleration: false
}, {
content: () => {
var _a3;
return h("div", {
class: [`menu-${mode.value}`, subMenuPopperClass.value],
onMouseenter: (evt) => handleMouseenter(evt, 100),
onMouseleave: () => handleMouseleave(true),
onFocus: (evt) => handleMouseenter(evt, 100)
}, [
h("ul", {
class: [
namespace.value + "-menu menu-dropdown",
`popup-${currentPlacement.value}`
]
}, [(_a3 = slots.default) == null ? void 0 : _a3.call(slots)])
]);
},
default: () => h("div", {
class: "item-content",
style: [
titleStyle.value,
{ backgroundColor: backgroundColor.value }
],
onClick: handleClick
}, titleTag)
}) : h(Fragment, {}, [
h("div", {
class: "item-content",
style: [
titleStyle.value,
{ backgroundColor: backgroundColor.value }
],
ref: verticalTitleRef,
onClick: handleClick
}, titleTag),
h(HlCollapseTransition, {}, {
default: () => {
var _a3;
return withDirectives(h("ul", {
role: "menu",
class: namespace.value + "-menu menu-inline"
}, [(_a3 = slots.default) == null ? void 0 : _a3.call(slots)]), [[vShow, opened.value]]);
}
})
]);
return h("li", {
class: [
`${namespace.value}-menu-item ${namespace.value}-sub-menu`,
{
"is-active": active.value,
"is-opened": opened.value,
"is-disabled": props.disabled
}
],
role: "menuitem",
ariaHaspopup: true,
ariaExpanded: opened.value,
onMouseenter: handleMouseenter,
onMouseleave: () => handleMouseleave(),
onFocus: handleMouseenter
}, [child]);
};
}
});
export { SubMenu as default, subMenuProps };
//# sourceMappingURL=sub-menu.mjs.map