-
Notifications
You must be signed in to change notification settings - Fork 382
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
[Proposal] Stylesheet Observer #1041
Comments
Would it make sense to observe a const observer = new StyleSheetObserver();
observer.observe(document.styleSheets);
observer.observe(document.adoptedStyleSheets);
observer.observe(shadowRoot.adoptedStyleSheets); A related point that I mentioned in w3c/csswg-drafts#10013, but is also worth repeating here. One of the main use-cases for observing stylesheets is to keep stylesheets synced between shadowRoot.adoptedStyleSheets.push(document.styleSheets); Since For other, more advanced cases, |
This feels like a missing piece to user land APIs for "open-stylable shadow roots"... Does "observing" or a "live list" feel more natural here? If we can setting on the same one feeling more natural, it would serve to see an explainer/proposal brought to life on this so we could share it with the appropriate working groups. Here's a link to the most recent one in our repo, as a start. |
I'm not sure the use cases warrant a sophisticated observer object like document.addEventListener('stylesheetschanged', e => {
console.log(document.styleSheets);
});
document.addEventListener('adoptedstylesheetschanged', e => {
console.log(document.adoptedStyleSheets);
});
document.querySelector('my-element').shadowRoot.addEventListener('stylesheetschanged', e => {
console.log(e.target.styleSheets);
});
document.querySelector('my-element').shadowRootaddEventListener('adoptedstylesheetschanged', e => {
console.log(e.target.adoptedStyleSheets);
}); There are some tricky timing bits here though:
|
The only benefits of a special observer class as proposed that I see are:
But I think even if such needs become pressing in the future, we could add that on (a StyleSheetObserver). The simplicity of the suggested alternative I think not only makes sense from the "baby steps" point of view, but even from a developer point of view, it is a simpler API, so if none of that finessing is needed, providing the developer a shorter path to what they need would always be useful. |
It would be nice for a web component to be able to observe stylesheets as they are adopted or imported by the parent Shadow DOM scope or the root document, and pick and choose which ones to import.
So the basic shape would look like:
The text was updated successfully, but these errors were encountered: