Skip to content

Commit

Permalink
Merge pull request #2 from phlex-ruby/handle-anchor-wrappers
Browse files Browse the repository at this point in the history
Handle clicks that occur inside an <a> tag
  • Loading branch information
joeldrapper authored Mar 6, 2024
2 parents 10a79d2 + 9007f53 commit eeb4b40
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 @@ -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;
});
}
}
}
});
Expand Down

0 comments on commit eeb4b40

Please sign in to comment.