|
| 1 | +## Description |
| 2 | + |
| 3 | +System context (private beta API — subject to change) |
| 4 | + |
| 5 | +The `<sp-theme>` element provides a private beta “system” context. Components can consume it via a controller that resolves the current system (e.g. `spectrum`, `express`, `spectrum-two`) and notifies on change. |
| 6 | + |
| 7 | +### Usage |
| 8 | + |
| 9 | +[](https://www.npmjs.com/package/@spectrum-web-components/reactive-controllers) |
| 10 | +[](https://bundlephobia.com/result?p=@spectrum-web-components/reactive-controllers) |
| 11 | + |
| 12 | +``` |
| 13 | +yarn add @spectrum-web-components/reactive-controllers |
| 14 | +``` |
| 15 | + |
| 16 | +Import the controller and symbol via: |
| 17 | + |
| 18 | +``` |
| 19 | +import { |
| 20 | + SystemResolutionController, |
| 21 | + systemResolverUpdatedSymbol, |
| 22 | +} from '@spectrum-web-components/reactive-controllers/src/SystemContextResolution.js'; |
| 23 | +import type { SystemVariant } from '@spectrum-web-components/theme'; |
| 24 | +``` |
| 25 | + |
| 26 | +## Example |
| 27 | + |
| 28 | +Consume the system context and respond to updates in a LitElement host: |
| 29 | + |
| 30 | +```js |
| 31 | +import { html, LitElement } from 'lit'; |
| 32 | +import { |
| 33 | + SystemResolutionController, |
| 34 | + systemResolverUpdatedSymbol, |
| 35 | +} from '@spectrum-web-components/reactive-controllers/src/SystemContextResolution.js'; |
| 36 | + |
| 37 | +class MyComponent extends LitElement { |
| 38 | + systemResolver = new SystemResolutionController(this); |
| 39 | + |
| 40 | + protected update(changes) { |
| 41 | + super.update(changes); |
| 42 | + if (changes.has(systemResolverUpdatedSymbol)) { |
| 43 | + this.handleSystemChange(); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + handleSystemChange() { |
| 48 | + const currentSystem = this.systemResolver.system; // "spectrum" | "express" | "spectrum-two" |
| 49 | + // react to system updates: adjust styles, state, or rendering |
| 50 | + } |
| 51 | + |
| 52 | + render() { |
| 53 | + return html`Current system: ${this.systemResolver.system}`; |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## Notes |
| 59 | + |
| 60 | +- This is a private beta API and may change. |
| 61 | +- If no `<sp-theme>` is present, `system` remains `"spectrum"` until a provider appears. |
| 62 | +- Pair with explicit `<sp-theme system="…" color="…" scale="…">` to avoid fallback behavior and ensure predictable theming. |
0 commit comments