Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

iAPI Router: Prevent navigation on links with on--click directives - #16

Closed
DAreRodz wants to merge 1 commit into
interactivity-apifrom
prevent-navigation-on-links-with-router-directives
Closed

DAreRodz wants to merge 1 commit into
interactivity-apifrom
prevent-navigation-on-links-with-router-directives

Conversation

@DAreRodz

@DAreRodz DAreRodz commented May 30, 2025 •

Copy link
Copy Markdown
Collaborator

What?

The Interactivity Router uses event delegation to capture click and mouseenter events from all links to call actions.navigate and actions.prefetch respectively.

This PR prevents doing so for link elements that already have directives for that purpose, e.g., links on a Pagination block inside a Query Loop with "Reload full page" disabled.

Why?

To prevent unnecessary or duplicated navigation by giving more precedence to block-specific actions.

How?

It checks the event.target elements for data-wp-on--click or data-wp-on--mouseenter directives (also for data-wp-on-async with the same events). If they are found, the router actions are not called. Directives with a unique ID are also considered (e.g., data-wp-on--click--navigation).

This is a heuristic we can iterate on later, as these directives could be unrelated to router actions (e.g., CSS animations or analytics callbacks).

@DAreRodz

Copy link
Copy Markdown
Collaborator Author

Taking a second look at the code, this could be simply fixed by changing when the global event listeners are executed, setting useCapture to false.

Inside isValidEvent(), we are already marking events with defaultPrevented as invalid:

const isValidEvent = ( event: MouseEvent ) =>
event &&
event.button === 0 && // Left clicks only.
! event.metaKey && // Open in new tab (Mac).
! event.ctrlKey && // Open in new tab (Windows).
! event.altKey && // Download.
! event.shiftKey &&
! event.defaultPrevented;

That would make the global event listeners skip those events.

@luisherranz

Copy link
Copy Markdown
Collaborator

Great catch! We can rely on that, which would actually it'd be much better since it will not bail out for things like analytics 🙂

Can we close this PR, since that's already how it works? Or perhaps you want to add an end-to-end test to document that case?

@DAreRodz

Copy link
Copy Markdown
Collaborator Author

Taking a second look at the code, this could be simply fixed by changing when the global event listeners are executed, setting useCapture to false.

I've created this PR in Gutenberg to fix this.

We can continue the discussion there. I'm closing this draft.

@DAreRodz DAreRodz closed this Jun 30, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants