Skip to content

Commit aba4836

Browse files
committed
fix(Submenu): 添加保持父级 popup 显示的功能,优化子菜单交互体验
1 parent 6134ceb commit aba4836

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

packages/components/menu/submenu.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ export default defineComponent({
116116
if (loopInPopup(related)) return;
117117
handleMouseLeavePopup(e);
118118
},
119+
keepParentPopupVisible: () => {
120+
// 保持当前 popup 显示,同时递归通知父级
121+
isCursorInPopup.value = true;
122+
popupVisible.value = true;
123+
if (submenu?.keepParentPopupVisible) {
124+
submenu.keepParentPopupVisible();
125+
}
126+
},
119127
});
120128

121129
const passSubPopupRefToParent = (val: HTMLElement) => {
@@ -136,13 +144,8 @@ export default defineComponent({
136144
// 如果父 popup 可见,还需要检查鼠标是否真的在父 popup 内
137145
// 避免父 popup 延迟关闭时,鼠标已经移出但子菜单仍被触发
138146
if (e && subPopupRef.value) {
139-
const parentPopupRect = subPopupRef.value.getBoundingClientRect();
140-
const { clientX, clientY } = e;
141-
const isInParentPopup =
142-
clientX >= parentPopupRect.left &&
143-
clientX <= parentPopupRect.right &&
144-
clientY >= parentPopupRect.top &&
145-
clientY <= parentPopupRect.bottom;
147+
const elementAtCursor = document.elementFromPoint(e.clientX, e.clientY);
148+
const isInParentPopup = elementAtCursor && subPopupRef.value.contains(elementAtCursor);
146149

147150
if (!isInParentPopup) {
148151
return;
@@ -199,6 +202,10 @@ export default defineComponent({
199202
};
200203
const handleEnterPopup = () => {
201204
isCursorInPopup.value = true;
205+
// 当鼠标进入子 popup 时,通知所有父级 popup 保持显示状态,防止父级延迟关闭
206+
if (submenu?.keepParentPopupVisible) {
207+
submenu.keepParentPopupVisible();
208+
}
202209
};
203210

204211
const handleSubmenuItemClick = () => {

packages/components/menu/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ export interface TdSubMenuInterface {
2929
setSubPopup?: (popupRef: HTMLElement) => void;
3030
closeParentPopup?: (e: MouseEvent) => void;
3131
popupVisible?: Ref<boolean>;
32+
keepParentPopupVisible?: () => void;
3233
}

0 commit comments

Comments
 (0)