Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standard Element.matches API overridden on router element #463

Open
turjmner8 opened this issue Oct 2, 2023 · 3 comments
Open

Standard Element.matches API overridden on router element #463

turjmner8 opened this issue Oct 2, 2023 · 3 comments

Comments

@turjmner8
Copy link

turjmner8 commented Oct 2, 2023

It looks like the current implementation of preact-router breaks the standard Element API on the router element itself. The router's logical 'matches' are stored as the prop 'matches' on the router element, and any JS that tries to call the standard Element.matches function on that element does not work as expected.

https://www.w3schools.com/jsref/met_element_matches.asp

If possible, it would be better to store the logical 'matches' under a different name to avoid conflicts with the standard Element API.

@rschristian
Copy link
Member

Can you provide a situation in which this is actually an issue?

@turjmner8
Copy link
Author

turjmner8 commented Oct 3, 2023

I work on a component library that has various container components that walk their child DOM (and sometimes parent DOM) to find specific elements via the 'matches' Element API. A customer recently logged a bug against us where this logic was falling over, and we tracked this down to their use of preact-router on their page.

Uncaught (in promise) TypeError: element.matches is not a function

@turjmner8
Copy link
Author

turjmner8 commented Oct 6, 2023

We were able to get our customer to work around this by wrapping the contents of their Router in a simple Component. If someone is using a setup with a regular HTMLElement as a child of their Router, then the cloned HTMLElement has its 'matches' function overridden. If they instead use a Component as the child, whatever HTMLElement that component actually produces does not get affected, and no issue is seen.

Another possible solution for this could be to only copy the 'matches' props down if the child being cloned is a Component and not a plain HTMLElement.

For example, this fiddle example provided in the preact-router does not have any issues:
https://jsfiddle.net/developit/qc73v9va/

But the div used as the 'demo fallback route' in this sample code would fall over if any JS tries to call 'matches' on the div actually rendered by the Router for that route:
https://github.com/preactjs/preact-router#active-matching--links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants