diff --git a/dist/phlex.js b/dist/phlex.js index 1e325fb..5f10a42 100644 --- a/dist/phlex.js +++ b/dist/phlex.js @@ -7,25 +7,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.getAttribute("phlex-action"); - const targetId = link.getAttribute("phlex-target"); - const fragment = link.getAttribute("phlex-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.getAttribute("phlex-action"); + const targetId = link.getAttribute("phlex-target"); + const fragment = link.getAttribute("phlex-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 f5c6c39..d1f4b54 100644 --- a/src/phlex.ts +++ b/src/phlex.ts @@ -17,31 +17,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.getAttribute("phlex-action"); - const targetId = link.getAttribute("phlex-target"); - const fragment = link.getAttribute("phlex-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.getAttribute("phlex-action"); + const targetId = link.getAttribute("phlex-target"); + const fragment = link.getAttribute("phlex-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; + }); + } } } });