Skip to content

Commit

Permalink
Handle clicks that occur inside an <a> tag
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
willcosgrove committed Mar 5, 2024
1 parent 211c9dc commit 7fcded7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 44 deletions.
41 changes: 22 additions & 19 deletions dist/phlex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 28 additions & 25 deletions src/phlex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
}
}
});
Expand Down

0 comments on commit 7fcded7

Please sign in to comment.