fix: CSS back to <style> for Safari/cascade reasons#9908
Merged
maribethb merged 1 commit intoMay 20, 2026
Merged
Conversation
Reverts the storage mechanism introduced in RaspberryPiFoundation#9611 (constructable stylesheets via `adoptedStyleSheets`) while keeping the per-root injection-site tracking that RaspberryPiFoundation#9611 added for shadow-DOM support. Motivations: - Safari 15.4 compatibility. `new CSSStyleSheet()` and `adoptedStyleSheets` require Safari 16.4+ - Cascade order. `adoptedStyleSheets` apply after `<style>`/`<link>` elements in the document, so Blockly's defaults silently overrode host stylesheets. Prepending a `<style>` to the head (or to the shadow root) restores the pre-RaspberryPiFoundation#9611 behavior where any author stylesheet declared later wins on specificity ties. Trade-offs: - Per-shadow-root CSS text is duplicated rather than shared via a single adopted sheet object. Negligible for typical use. - `Css.register()` calls made after the first `inject()` no longer reach already-injected roots (same as RaspberryPiFoundation#9611's behavior); subsequent `inject()` calls into other roots still pick them up. Web-component consumers can legitimately register late, so this is preferred to reinstating the pre-RaspberryPiFoundation#9611 throw. Fixes RaspberryPiFoundation#9876
gonfunko
approved these changes
May 20, 2026
Contributor
gonfunko
left a comment
There was a problem hiding this comment.
Thanks for putting this together, looks reasonable to me but I'll let Maribeth have a look before merging.
gonfunko
approved these changes
May 20, 2026
Contributor
gonfunko
left a comment
There was a problem hiding this comment.
Thanks for putting this together, looks reasonable to me but I'll let Maribeth have a look before merging.
maribethb
approved these changes
May 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reverts the storage mechanism introduced in #9611 (constructable stylesheets via
adoptedStyleSheets) while keeping the per-root injection-site tracking that #9611 added for shadow-DOM support.Motivations:
new CSSStyleSheet()andadoptedStyleSheetsrequire Safari 16.4+adoptedStyleSheetsapply after<style>/<link>elements in the document, so Blockly's defaults silently overrode host stylesheets. Prepending a<style>to the head (or to the shadow root) restores the pre-feat!: Allow using Blockly in web components/shadow DOM #9611 behavior where any author stylesheet declared later wins on specificity ties.Trade-offs:
Css.register()calls made after the firstinject()no longer reach already-injected roots (same as feat!: Allow using Blockly in web components/shadow DOM #9611's behavior); subsequentinject()calls into other roots still pick them up. Web-component consumers can legitimately register late, so this is preferred to reinstating the pre-feat!: Allow using Blockly in web components/shadow DOM #9611 throw.@gonfunko this is one possible take with the downsides noted above. Another option is to use both mechanisms (tag and adoptedStylesheets) either based on feature detection or based on the root. Then to fix the cascade issue consistently use "
@layerblockly" for Blockly's CSS. That will require auditing of the !important use in Blockly as they would become impossible to override. If relying on feature detection note it is not sufficient to check for CSSStyleSheet being defined as that significantly predates constructable stylesheets.Please feel free to make changes to this PR and/or replace it with one of your own.
The basics
I didn't format as it made a large unrelated diffwhoops, I had outdated prettier, all good now.The details
Resolves
Fixes #9876
Proposed Changes
Reason for Changes
Test Coverage
I checked the web component playground added in the previous PR.
Documentation
Additional Information