-
-
Notifications
You must be signed in to change notification settings - Fork 15
Work with anchor and target inside same shadow root #353
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
Conversation
✅ Deploy Preview for anchor-polyfill ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for anchor-position-wpt canceled.
|
Thanks for this contribution! I'd definitely love to improve how the polyfill works with the shadow DOM. I don't necessarily think a PR needs to solve the entire problem to be helpful or merged, but am thinking as I type about how to make sure this is headed in the correct direction. From the spec-
Would this be possible with this approach? The other portion of the spec related to shadow DOM is about using
Hmm, this is interesting. I see two uses for a
I think both use cases would be possible by allowing an array, something like |
It should be possible with some adjustments. I double-checked that it's possible for a child component to navigate up through a parent shadow root and into light DOM. We can use the presence of a We'll need an element locator that does this type of navigation up and into light DOM, querying along the way. Once it reaches light DOM it could skip straight to
Have to admit I'm not that familiar with
That makes sense, let's do that instead then. |
Just the one for now to make it work like before
Lets users skip querying for link and style tags inside a shadow root.
Do you know if this is implemented anywhere yet? The spec update was pretty recent, so maybe not. I'm trying to set up an example and control that it works in Chrome (checked Stable and Canary and the Chromium issue tracker). I might very well be doing something wrong, would love to not "fly blind" 😅 |
I'm not aware of this being implemented anywhere, I didn't see any relevant WPTs for this. I'd be content with opening a separate issue to support that, and knowing that this change doesn't make that change harder. |
The last test run failed with a diff above a threshold. Set up a devcontainer to see if it was an OS specific thing, but the tests pass there too. Maybe OK with a retry?
Sounds good. I think the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking great! I have some nits and questions. The tests did seem to pass on re-run.
@jgerigmeyer Do you have any objection to adding the .devcontainer
?
One more area that would be useful to update would be the README.md. Could you document the root(s)
option under Configuration, and also update the line about shadow-dom (L153) to reflect your changes?
Thanks so much for this contribution!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this approach -- and thanks for the contribution!
I'd probably prefer to remove the .devcontainer/
files, unless we add more documentation around them.
I'll remove it then, it's quick to set up should anyone want it for themselves 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks and works great! Thanks for your contribution.
Some ponderings for future exploration (not really a review, but things I thought of while reviewing)-
- Could the custom properties be set on the new root instead of
body
? - If so, what happens when multiple roots are passed?
- Could this be a recommended pattern for non-shadow root situations? By specifying a navbar as the root, for instance, there might be some performance enhancements over parsing unrelated DOM.
Description
Thanks for making it possible to use anchor positioning while supporting older browsers ❤️
I ran into one of the limitations of the current polyfill while writing a web component where both the anchor and target are inside the same shadow root. The reliance on
document.querySelectorAll
to find the elements to measure makes it so the current version can't work in shadow roots.Open for other approaches here, but the gist of it is to pass in a shadow root if there is one to run
querySelectorAll
on that instead ofdocument
. I went for an approach binding the call to the component instance, but it could also be an extraroot
option to the function call that defaults todocument
or something like that.Related Issue(s)
Steps to test/reproduce
Then visit http://localhost:3000/shadow-root.html
Show me
2025-10-01.at.11.49.58.mp4