diff --git a/dist/phlex.js b/dist/phlex.js index daae04e..2859388 100644 --- a/dist/phlex.js +++ b/dist/phlex.js @@ -42,25 +42,28 @@ export function init() { // } // }); document.addEventListener("click", (event) => { - const link = event.target; - if (link instanceof HTMLAnchorElement) { - if (event.metaKey || event.ctrlKey || event.shiftKey) return; - if (link.ariaDisabled) { - event.preventDefault(); - return; - } - const href = link.href; - const action = link.dataset.action; - const targetId = link.dataset.target; - const fragment = link.dataset.fragment; - if (action) { - event.preventDefault(); - link.ariaDisabled = "true"; - createEvent(href, targetId, fragment).then((event) => { - const actionMethod = Actions.get(action); - if (actionMethod) actionMethod(event); - link.ariaDisabled = null; - }); + const targetElement = event.target; + if (targetElement instanceof Element) { + const link = targetElement.closest("a"); + if (link instanceof HTMLAnchorElement) { + if (event.metaKey || event.ctrlKey || event.shiftKey) return; + if (link.ariaDisabled) { + event.preventDefault(); + return; + } + const href = link.href; + const action = link.dataset.action; + const targetId = link.dataset.target; + const fragment = link.dataset.fragment; + if (action) { + event.preventDefault(); + link.ariaDisabled = "true"; + createEvent(href, targetId, fragment).then((event) => { + const actionMethod = Actions.get(action); + if (actionMethod) actionMethod(event); + link.ariaDisabled = null; + }); + } } } }); diff --git a/src/phlex.ts b/src/phlex.ts index 7eae400..8b2a71e 100644 --- a/src/phlex.ts +++ b/src/phlex.ts @@ -59,31 +59,34 @@ export function init(): void { // }); document.addEventListener("click", (event) => { - const link = event.target; - - if (link instanceof HTMLAnchorElement) { - if (event.metaKey || event.ctrlKey || event.shiftKey) return; - - if (link.ariaDisabled) { - event.preventDefault(); - return; - } - - const href = link.href; - const action = link.dataset.action; - const targetId = link.dataset.target; - const fragment = link.dataset.fragment; - - if (action) { - event.preventDefault(); - link.ariaDisabled = "true"; - - createEvent(href, targetId, fragment).then((event) => { - const actionMethod = Actions.get(action); - if (actionMethod) actionMethod(event); - - link.ariaDisabled = null; - }); + const targetElement = event.target; + if (targetElement instanceof Element) { + const link = targetElement.closest("a"); + + if (link instanceof HTMLAnchorElement) { + if (event.metaKey || event.ctrlKey || event.shiftKey) return; + + if (link.ariaDisabled) { + event.preventDefault(); + return; + } + + const href = link.href; + const action = link.dataset.action; + const targetId = link.dataset.target; + const fragment = link.dataset.fragment; + + if (action) { + event.preventDefault(); + link.ariaDisabled = "true"; + + createEvent(href, targetId, fragment).then((event) => { + const actionMethod = Actions.get(action); + if (actionMethod) actionMethod(event); + + link.ariaDisabled = null; + }); + } } } });