Skip to content

Commit d6ff8e1

Browse files
RajdeepcRajdeep ChandraRajdeep ChandraRajdeep ChandraRajdeep Chandra
authored
docs: updated documentation of reactive-controllers (#5802)
* docs: updated documentation of reactive-controllers * docs: updated main readme to align with doc standard * docs: updated color controller readme to align with doc standard * docs: updated dependency controller readme to align with doc standard * docs: updated element resolution controller readme to align with doc standard * docs: updated match media controller readme to align with doc standard * docs: updated pending state controller readme to align with doc standard * docs: updated pending roving tab index controller readme to align with doc standard * docs: updated the element resolution example with form field logic * docs: updated element resolution modal overlay example to show the usage pattern * docs: added new language resolution controller readme * docs: removed commented code in main readme * docs: update docs for the disabled items in robing tab index controller * docs: removed screen reader section in roving tab index * chore: fix up review comments on verbs and script tags * chore: added system context controller readme * chore: main readme formatting fix --------- Co-authored-by: Rajdeep Chandra <[email protected]> Co-authored-by: Rajdeep Chandra <[email protected]> Co-authored-by: Rajdeep Chandra <[email protected]> Co-authored-by: Rajdeep Chandra <[email protected]> Co-authored-by: Rajdeep Chandra <[email protected]>
1 parent 43c8eea commit d6ff8e1

File tree

9 files changed

+2575
-166
lines changed

9 files changed

+2575
-166
lines changed
Lines changed: 267 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,270 @@
1-
## Description
1+
## Overview
22

3-
[Reactive controllers](https://lit.dev/docs/composition/controllers/) are a tool for code reuse and composition within [Lit](https://lit.dev), a core dependency of Spectrum Web Components. Reactive controllers can be reused across components to reduce both code complexity and size, and to deliver a consistent user experience. These reactive controllers are used by the Spectrum Web Components library and are published to NPM for you to leverage in your projects as well.
3+
[Reactive controllers](https://lit.dev/docs/composition/controllers/) are a powerful tool for code reuse and composition within [Lit](https://lit.dev), a core dependency of Spectrum Web Components. They enable you to extract common behaviors into reusable packages that can be shared across multiple components, reducing code complexity and size while delivering a consistent user experience.
44

5-
### Reactive controllers
5+
### Usage
66

7-
- [ColorController](../color-controller)
8-
- [ElementResolutionController](../element-resolution)
9-
- FocusGroupController
10-
- LanguageResolutionController
11-
- [MatchMediaController](../match-media)
12-
- [RovingTabindexController](../roving-tab-index)
13-
- [PendingStateController](../pending-state)
14-
- SystemContextResolutionController
7+
```bash
8+
yarn add @spectrum-web-components/reactive-controllers
9+
```
10+
11+
Reactive controllers are instantiated in your component and automatically hook into the component's lifecycle:
12+
13+
```typescript
14+
import { LitElement, html } from 'lit';
15+
import { MatchMediaController } from '@spectrum-web-components/reactive-controllers/src/MatchMedia.js';
16+
17+
class MyComponent extends LitElement {
18+
// Create controller instance
19+
darkMode = new MatchMediaController(this, '(prefers-color-scheme: dark)');
20+
21+
render() {
22+
// Use controller state in render
23+
return html`
24+
<div class=${this.darkMode.matches ? 'dark' : 'light'}>Content</div>
25+
`;
26+
}
27+
}
28+
```
29+
30+
### Controller lifecycle
31+
32+
Reactive controllers implement the `ReactiveController` interface with the following optional lifecycle methods:
33+
34+
- **`hostConnected()`**: Called when the host element is connected to the DOM
35+
- **`hostDisconnected()`**: Called when the host element is disconnected from the DOM
36+
- **`hostUpdate()`**: Called before the host's `update()` method
37+
- **`hostUpdated()`**: Called after the host's `update()` method
38+
39+
Controllers can also call `host.requestUpdate()` to trigger an update cycle on the host element.
40+
41+
### Creating your own controllers
42+
43+
You can create custom reactive controllers by implementing the `ReactiveController` interface:
44+
45+
```typescript
46+
import { ReactiveController, ReactiveElement } from 'lit';
47+
48+
export class MyController implements ReactiveController {
49+
private host: ReactiveElement;
50+
51+
constructor(host: ReactiveElement) {
52+
this.host = host;
53+
// Register this controller with the host
54+
this.host.addController(this);
55+
}
56+
57+
hostConnected() {
58+
// Called when host is connected to DOM
59+
}
60+
61+
hostDisconnected() {
62+
// Called when host is disconnected from DOM
63+
}
64+
}
65+
```
66+
67+
### Available controllers
68+
69+
#### ColorController
70+
71+
Manages and validates color values in various color spaces (RGB, HSL, HSV, Hex). Provides conversion between formats and state management for color-related interactions.
72+
73+
**Use cases:**
74+
75+
- Color pickers and selectors
76+
- Color input validation
77+
- Color format conversion
78+
- Theme customization UIs
79+
80+
**Key features:**
81+
82+
- Multiple color format support (hex, RGB, HSL, HSV)
83+
- Color validation
84+
- Format preservation
85+
- Undo/redo support
86+
87+
[Learn more →](../color-controller)
88+
89+
---
90+
91+
#### DependencyManagerController
92+
93+
Manages the availability of custom element dependencies, enabling lazy loading patterns and progressive enhancement strategies.
94+
95+
**Use cases:**
96+
97+
- Code splitting and lazy loading
98+
- Progressive enhancement
99+
- Route-based component loading
100+
- Conditional feature loading
101+
102+
**Key features:**
103+
104+
- Tracks custom element registration
105+
- Reactive loading state
106+
- Multiple dependency management
107+
- Works with dynamic imports
108+
109+
[Learn more →](../dependency-manager)
110+
111+
---
112+
113+
#### ElementResolutionController
114+
115+
Maintains an active reference to another element in the DOM tree, automatically tracking changes and updating when the DOM mutates.
116+
117+
**Use cases:**
118+
119+
- Accessible label associations
120+
- Focus trap management
121+
- Form validation connections
122+
- Dynamic element relationships
123+
124+
**Key features:**
125+
126+
- Automatic DOM observation
127+
- ID selector optimization
128+
- Shadow DOM support
129+
- Reactive updates
130+
131+
[Learn more →](../element-resolution)
132+
133+
---
134+
135+
#### FocusGroupController
136+
137+
Base controller for managing keyboard focus within groups of elements. Extended by `RovingTabindexController` with tabindex management capabilities.
138+
139+
**Use cases:**
140+
141+
- Custom composite widgets
142+
- Keyboard navigation patterns
143+
- Focus management
144+
145+
**Key features:**
146+
147+
- Arrow key navigation
148+
- Configurable direction modes
149+
- Focus entry points
150+
- Element enter actions
151+
152+
**Note:** This controller is typically not used directly. Use [RovingTabindexController](../roving-tab-index) instead for most use cases.
153+
154+
---
155+
156+
#### LanguageResolutionController
157+
158+
Resolves and tracks the language/locale context of the host element, responding to changes in the `lang` attribute up the DOM tree.
159+
160+
**Use cases:**
161+
162+
- Internationalization (i18n)
163+
- Localized content
164+
- RTL/LTR text direction
165+
- Locale-specific formatting
166+
167+
**Key features:**
168+
169+
- Automatic language detection
170+
- Locale change tracking
171+
- Supports Shadow DOM
172+
- Bubbles up DOM tree
173+
174+
[Learn more →](../language-resolution)
175+
176+
---
177+
178+
#### MatchMediaController
179+
180+
Binds CSS media queries to reactive elements, automatically updating when queries match or unmatch.
181+
182+
**Use cases:**
183+
184+
- Responsive design
185+
- Dark mode detection
186+
- Mobile/desktop layouts
187+
- Print styles
188+
- Accessibility preferences (prefers-reduced-motion, etc.)
189+
190+
**Key features:**
191+
192+
- Multiple media query support
193+
- Reactive updates
194+
- Predefined queries (DARK_MODE, IS_MOBILE)
195+
- Event-driven
196+
197+
[Learn more →](../match-media)
198+
199+
---
200+
201+
#### PendingStateController
202+
203+
Manages pending/loading states for interactive elements, providing visual feedback and accessible state communication.
204+
205+
**Use cases:**
206+
207+
- Async button actions
208+
- Form submission states
209+
- Loading indicators
210+
- Progress feedback
211+
212+
**Key features:**
213+
214+
- Automatic ARIA label management
215+
- Progress circle rendering
216+
- Label caching and restoration
217+
- Disabled state awareness
218+
219+
**Note:** Currently used primarily by Button component. May be deprecated in future versions.
220+
221+
[Learn more →](../pending-state)
222+
223+
---
224+
225+
#### RovingTabindexController
226+
227+
Implements the W3C ARIA roving tabindex pattern for keyboard navigation in composite widgets, managing `tabindex` attributes and arrow key navigation.
228+
229+
**Use cases:**
230+
231+
- Toolbars
232+
- Tab lists
233+
- Menus
234+
- Radio groups
235+
- Listboxes
236+
- Grids
237+
238+
**Key features:**
239+
240+
- Arrow key navigation (with Home/End support)
241+
- Automatic tabindex management
242+
- Flexible direction modes (horizontal, vertical, both, grid)
243+
- Skips disabled elements
244+
- WCAG compliant
245+
246+
[Learn more →](../roving-tab-index)
247+
248+
---
249+
250+
#### SystemContextResolutionController
251+
252+
Resolves and tracks system-level context like color scheme and scale preferences from Spectrum theme providers.
253+
254+
**Use cases:**
255+
256+
- Theme integration
257+
- Design system variant detection (Spectrum Classic, Express, Spectrum 2)
258+
- System-specific asset loading
259+
- Adaptive UI rendering
260+
261+
**Key features:**
262+
263+
- Automatic theme context resolution
264+
- Reactive system variant updates
265+
- Event-based communication with `<sp-theme>`
266+
- Automatic cleanup on disconnect
267+
268+
**Note:** Private Beta API - subject to changes.
269+
270+
[Learn more →](../system-context-resolution)

0 commit comments

Comments
 (0)