adoptStyleSheets method
Applies CSS strings as adopted stylesheets to this component's shadow root.
This uses the modern adoptedStyleSheets
API which is more efficient than creating <style> elements because:
- Stylesheets can be shared across multiple shadow roots
- CSS is only parsed once and cached
- The browser can optimize stylesheet application
Note: This only works in the browser. On the server (during SSR),
this is a no-op since styles should be rendered as <style> elements.
Example
@override
void onMount() {
adoptStyleSheets([
':host { display: block; padding: 16px; }',
'button { background: blue; color: white; }',
]);
}
Implementation
void adoptStyleSheets(List<String> cssTexts) {
if (_shadowRoot == null) return;
adopted_styles.setAdoptedStyleSheets(_shadowRoot!, cssTexts);
}