You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/merge-styles/README.md
+72Lines changed: 72 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -504,3 +504,75 @@ window.FabricConfig = {
504
504
},
505
505
};
506
506
```
507
+
508
+
## Shadow DOM
509
+
510
+
`merge-styles` has experimental support for [shadow DOM](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_shadow_DOM). This feature is opt-in and incrementally adoptable. To enable the feature you need to include two [React Providers](https://react.dev/reference/react/createContext#provider):
511
+
512
+
1. `MergeStylesRootProvider`: acts as the "global" context for your application. You should have one of these per page.
513
+
2. `MergeStylesShadowRootProvider`: a context for each shadow root in your application. You should have one of these per shadow root.
514
+
515
+
`merge-styles` does not provide an option for creating shadow roots in React as how you get a shadow root doesn't matter, just that you have a reference to one. [`react-shadow`](https://www.npmjs.com/package/react-shadow) is one library that can create shadow roots in React and will be used in examples.
<PrimaryButton>I'm in the shadow DOM!</PrimaryButton>
540
+
</ShadowRoot>
541
+
<PrimaryButton>I'm in the light DOM!</PrimaryButton>
542
+
```
543
+
544
+
### Scoping styles for more efficient CSS
545
+
546
+
You do not _need_ to update your `merge-styles` styles to support shadow DOM but you can make styles more efficient with some updates.
547
+
548
+
Shadow DOM support is achieved in `merge-styles` by using [constructable stylesheets](https://web.dev/articles/constructable-stylesheets) and is scoped by "stylesheet keys". `merge-styles` creates one stylesheet per key and in Fluent this means each component has its own stylesheet. Each `MergeStylesShadowRootProvider` will only adopt styles for components it contains plus the global sheet (we cannot be certain whether we need this sheet or not so we always adopt it). This means a `MergeStylesShadowRootProvider` that contains a button will only adopt button styles (plus the global styles) but not checkbox styles, making styling within the shadow root more efficient.
549
+
550
+
If you use `customizable` or `styled` the existing "scope" value provided to these functions is used a unique key. If no key is provided `merge-styles` falls back to a "global" key. This global key is a catch-all and allows us to support code that was written before shadow DOM support was added or code that is called outside of React context.
551
+
552
+
All `@fluentui/react` styles are scoped to via `customizable` and `styled` (and some updates to specific component styles where needed). If your components use these functions and you set the "scope" property your components will automatically be scoped.
553
+
If you're using `mergeStyles()` (and other `merge-styles` APIs) directly, your styles will be placed in the global scope and still be available in shadow roots, just not as optimally as possible.
0 commit comments