- 
                Notifications
    
You must be signed in to change notification settings  - Fork 235
 
docs: updated documentation of reactive-controllers #5802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            Rajdeepc
  wants to merge
  24
  commits into
  main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
rajdeep/swc-429
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from 21 commits
      Commits
    
    
            Show all changes
          
          
            24 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      4718466
              
                docs: updated documentation of reactive-controllers
              
              
                 d703c09
              
                Merge branch 'main' into rajdeep/swc-429
              
              
                Rajdeepc fb835a8
              
                docs: updated main readme to align with doc standard
              
              
                 442dfd6
              
                Merge branch 'rajdeep/swc-429' of https://github.com/adobe/spectrum-w…
              
              
                 fdc8571
              
                docs: updated color controller readme to align with doc standard
              
              
                 60df8b0
              
                docs: updated dependency controller readme to align with doc standard
              
              
                 aa88fc4
              
                docs: updated element resolution controller readme to align with doc …
              
              
                 cc621ec
              
                docs: updated match media controller readme to align with doc standard
              
              
                 2584cab
              
                docs: updated pending state controller readme to align with doc standard
              
              
                 a0eb897
              
                docs: updated pending roving tab index controller readme to align wit…
              
              
                 3dc61ac
              
                Merge branch 'main' into rajdeep/swc-429
              
              
                Rajdeepc 5ff113f
              
                Merge branch 'main' of https://github.com/adobe/spectrum-web-componen…
              
              
                 b1e1e41
              
                docs: updated the element resolution example with form field logic
              
              
                 f31137d
              
                docs: updated element resolution modal overlay example to show the us…
              
              
                 63ab40f
              
                docs: added new language resolution controller readme
              
              
                 b3b3459
              
                docs: removed commented code in main readme
              
              
                 22a5a1c
              
                docs: update docs for the disabled items in robing tab index controller
              
              
                 7a6c840
              
                docs: removed screen reader section in roving tab index
              
              
                 38c4ad4
              
                Merge branch 'rajdeep/swc-429' of https://github.com/adobe/spectrum-w…
              
              
                 746a320
              
                chore: fix up review comments on verbs and script tags
              
              
                 5bd78cb
              
                chore: added system context controller readme
              
              
                 6b98e9e
              
                chore: main readme formatting fix
              
              
                 1811ade
              
                Merge branch 'main' into rajdeep/swc-429
              
              
                Rajdeepc 9e6283a
              
                Merge branch 'main' into rajdeep/swc-429
              
              
                Rajdeepc File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,14 +1,270 @@ | ||
| ## Description | ||
| ## Overview | ||
| 
     | 
||
| [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. | ||
| [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. | ||
| 
     | 
||
| ### Reactive controllers | ||
| ### Usage | ||
| 
     | 
||
| - [ColorController](../color-controller) | ||
| - [ElementResolutionController](../element-resolution) | ||
| - FocusGroupController | ||
| - LanguageResolutionController | ||
| - [MatchMediaController](../match-media) | ||
| - [RovingTabindexController](../roving-tab-index) | ||
| - [PendingStateController](../pending-state) | ||
| - SystemContextResolutionController | ||
| ```bash | ||
| yarn add @spectrum-web-components/reactive-controllers | ||
| ``` | ||
| 
     | 
||
| Reactive controllers are instantiated in your component and automatically hook into the component's lifecycle: | ||
| 
     | 
||
| ```typescript | ||
| import { LitElement, html } from 'lit'; | ||
| import { MatchMediaController } from '@spectrum-web-components/reactive-controllers/src/MatchMedia.js'; | ||
| 
     | 
||
| class MyComponent extends LitElement { | ||
| // Create controller instance | ||
| darkMode = new MatchMediaController(this, '(prefers-color-scheme: dark)'); | ||
| 
     | 
||
| render() { | ||
| // Use controller state in render | ||
| return html` | ||
| <div class=${this.darkMode.matches ? 'dark' : 'light'}>Content</div> | ||
| `; | ||
| } | ||
| } | ||
| ``` | ||
| 
     | 
||
| ### Controller lifecycle | ||
| 
     | 
||
| Reactive controllers implement the `ReactiveController` interface with the following optional lifecycle methods: | ||
| 
     | 
||
| - **`hostConnected()`**: Called when the host element is connected to the DOM | ||
| - **`hostDisconnected()`**: Called when the host element is disconnected from the DOM | ||
| - **`hostUpdate()`**: Called before the host's `update()` method | ||
| - **`hostUpdated()`**: Called after the host's `update()` method | ||
| 
     | 
||
| Controllers can also call `host.requestUpdate()` to trigger an update cycle on the host element. | ||
| 
     | 
||
| ### Creating your own controllers | ||
| 
     | 
||
| You can create custom reactive controllers by implementing the `ReactiveController` interface: | ||
| 
     | 
||
| ```typescript | ||
| import { ReactiveController, ReactiveElement } from 'lit'; | ||
| 
     | 
||
| export class MyController implements ReactiveController { | ||
| private host: ReactiveElement; | ||
| 
     | 
||
| constructor(host: ReactiveElement) { | ||
| this.host = host; | ||
| // Register this controller with the host | ||
| this.host.addController(this); | ||
| } | ||
| 
     | 
||
| hostConnected() { | ||
| // Called when host is connected to DOM | ||
| } | ||
| 
     | 
||
| hostDisconnected() { | ||
| // Called when host is disconnected from DOM | ||
| } | ||
| } | ||
| ``` | ||
| 
     | 
||
| ### Available controllers | ||
| 
     | 
||
| #### ColorController | ||
| 
     | 
||
| Manages and validates color values in various color spaces (RGB, HSL, HSV, Hex). Provides conversion between formats and state management for color-related interactions. | ||
| 
     | 
||
| **Use cases:** | ||
| 
     | 
||
| - Color pickers and selectors | ||
| - Color input validation | ||
| - Color format conversion | ||
| - Theme customization UIs | ||
| 
     | 
||
| **Key features:** | ||
| 
     | 
||
| - Multiple color format support (hex, RGB, HSL, HSV) | ||
| - Color validation | ||
| - Format preservation | ||
| - Undo/redo support | ||
| 
     | 
||
| [Learn more →](../color-controller) | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| #### DependencyManagerController | ||
| 
     | 
||
| Manages the availability of custom element dependencies, enabling lazy loading patterns and progressive enhancement strategies. | ||
| 
     | 
||
| **Use cases:** | ||
| 
     | 
||
| - Code splitting and lazy loading | ||
| - Progressive enhancement | ||
| - Route-based component loading | ||
| - Conditional feature loading | ||
| 
     | 
||
| **Key features:** | ||
| 
     | 
||
| - Tracks custom element registration | ||
| - Reactive loading state | ||
| - Multiple dependency management | ||
| - Works with dynamic imports | ||
| 
     | 
||
| [Learn more →](../dependency-manager) | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| #### ElementResolutionController | ||
| 
     | 
||
| Maintains an active reference to another element in the DOM tree, automatically tracking changes and updating when the DOM mutates. | ||
| 
     | 
||
| **Use cases:** | ||
| 
     | 
||
| - Accessible label associations | ||
| - Focus trap management | ||
| - Form validation connections | ||
| - Dynamic element relationships | ||
| 
     | 
||
| **Key features:** | ||
| 
     | 
||
| - Automatic DOM observation | ||
| - ID selector optimization | ||
| - Shadow DOM support | ||
| - Reactive updates | ||
| 
     | 
||
| [Learn more →](../element-resolution) | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| #### FocusGroupController | ||
| 
     | 
||
| Base controller for managing keyboard focus within groups of elements. Extended by `RovingTabindexController` with tabindex management capabilities. | ||
| 
     | 
||
| **Use cases:** | ||
| 
     | 
||
| - Custom composite widgets | ||
| - Keyboard navigation patterns | ||
| - Focus management | ||
| 
     | 
||
| **Key features:** | ||
| 
     | 
||
| - Arrow key navigation | ||
| - Configurable direction modes | ||
| - Focus entry points | ||
| - Element enter actions | ||
| 
     | 
||
| **Note:** This controller is typically not used directly. Use [RovingTabindexController](../roving-tab-index) instead for most use cases. | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| #### LanguageResolutionController | ||
| 
     | 
||
| Resolves and tracks the language/locale context of the host element, responding to changes in the `lang` attribute up the DOM tree. | ||
| 
     | 
||
| **Use cases:** | ||
| 
     | 
||
| - Internationalization (i18n) | ||
| - Localized content | ||
| - RTL/LTR text direction | ||
| - Locale-specific formatting | ||
| 
     | 
||
| **Key features:** | ||
| 
     | 
||
| - Automatic language detection | ||
| - Locale change tracking | ||
| - Supports Shadow DOM | ||
| - Bubbles up DOM tree | ||
| 
     | 
||
| [Learn more →](../language-resolution) | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| #### MatchMediaController | ||
| 
     | 
||
| Binds CSS media queries to reactive elements, automatically updating when queries match or unmatch. | ||
| 
     | 
||
| **Use cases:** | ||
| 
     | 
||
| - Responsive design | ||
| - Dark mode detection | ||
| - Mobile/desktop layouts | ||
| - Print styles | ||
| - Accessibility preferences (prefers-reduced-motion, etc.) | ||
| 
     | 
||
| **Key features:** | ||
| 
     | 
||
| - Multiple media query support | ||
| - Reactive updates | ||
| - Predefined queries (DARK_MODE, IS_MOBILE) | ||
| - Event-driven | ||
| 
     | 
||
| [Learn more →](../match-media) | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| #### PendingStateController | ||
| 
     | 
||
| Manages pending/loading states for interactive elements, providing visual feedback and accessible state communication. | ||
| 
     | 
||
| **Use cases:** | ||
| 
     | 
||
| - Async button actions | ||
| - Form submission states | ||
| - Loading indicators | ||
| - Progress feedback | ||
| 
     | 
||
| **Key features:** | ||
| 
     | 
||
| - Automatic ARIA label management | ||
| - Progress circle rendering | ||
| - Label caching and restoration | ||
| - Disabled state awareness | ||
| 
     | 
||
| **Note:** Currently used primarily by Button component. May be deprecated in future versions. | ||
| 
     | 
||
| [Learn more →](../pending-state) | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| #### RovingTabindexController | ||
| 
     | 
||
| Implements the W3C ARIA roving tabindex pattern for keyboard navigation in composite widgets, managing `tabindex` attributes and arrow key navigation. | ||
| 
     | 
||
| **Use cases:** | ||
| 
     | 
||
| - Toolbars | ||
| - Tab lists | ||
| - Menus | ||
| - Radio groups | ||
| - Listboxes | ||
| - Grids | ||
| 
     | 
||
| **Key features:** | ||
| 
     | 
||
| - Arrow key navigation (with Home/End support) | ||
| - Automatic tabindex management | ||
| - Flexible direction modes (horizontal, vertical, both, grid) | ||
| - Skips disabled elements | ||
| - WCAG compliant | ||
| 
     | 
||
| [Learn more →](../roving-tab-index) | ||
| 
     | 
||
| --- | ||
| 
     | 
||
| ### [SystemContextResolutionController](./system-context-resolution.md) | ||
                
      
                  rise-erpelding marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| Resolves and tracks system-level context like color scheme and scale preferences from Spectrum theme providers. | ||
| 
     | 
||
| **Use cases:** | ||
| 
     | 
||
| - Theme integration | ||
| - Design system variant detection (Spectrum Classic, Express, Spectrum 2) | ||
| - System-specific asset loading | ||
| - Adaptive UI rendering | ||
| 
     | 
||
| **Key features:** | ||
| 
     | 
||
| - Automatic theme context resolution | ||
| - Reactive system variant updates | ||
| - Event-based communication with `<sp-theme>` | ||
| - Automatic cleanup on disconnect | ||
| 
     | 
||
| **Note:** Private Beta API - subject to changes. | ||
| 
     | 
||
| [Learn more →](../system-context-resolution) | ||
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.