Refactor event path handling to use getComposedPath utility

- Replaced direct usage of event.composedPath() with a new utility function getComposedPath in Control component.
- Updated includeFromEvent function to utilize getComposedPath for better compatibility.
- Implemented getComposedPath function to ensure consistent event path retrieval across different browsers.
This commit is contained in:
Harvey Zack committed 2025-08-03 11:43:01 +08:00
1 parent 412b8e7fa7
commit 05ce97c6fd
12 files changed
+41 -14

No files matched your search

+2 -1
View File
@@ -20,6 +20,7 @@ import {
inverseClass,
createElement,
includeFromEvent,
getComposedPath,
} from '../utils';
export default class Control extends Component {
@@ -265,7 +266,7 @@ export default class Control extends Component {
}
const event = proxy($list, 'click', async (event) => {
const path = event.composedPath() || [];
const path = getComposedPath(event);
const item = option.selector.find(
(item) => item.$control_item === path.find(($item) => item.$control_item === $item),
);
+15 -1
View File
@@ -76,7 +76,7 @@ export function isInViewport(el, offset = 0) {
}
export function includeFromEvent(event, target) {
return event.composedPath && event.composedPath().indexOf(target) > -1;
return getComposedPath(event).indexOf(target) > -1;
}
export function replaceElement(newChild, oldChild) {
@@ -161,3 +161,17 @@ export function loadImg(url, scale) {
img.src = url;
});
}
export function getComposedPath(event) {
if (event.composedPath) return event.composedPath();
const path = [];
let node = event.target;
while (node) {
path.push(node);
node = node.parentNode;
}
if (path.indexOf(window) === -1 && window !== undefined) {
path.push(window);
}
return path;
}