-
Notifications
You must be signed in to change notification settings - Fork 254
chore(SlotAttributePropagationController): add tests + docs #6490
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
cdransf
wants to merge
3
commits into
main
Choose a base branch
from
cdransf/slot-attribute-propagation-controller-tests-docs
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
125 changes: 125 additions & 0 deletions
125
...ages/core/controllers/slot-attribute-propagation/slot-attribute-propagation.mdx
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 |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { Canvas, Meta } from '@storybook/addon-docs/blocks'; | ||
| import { DocsFooter, DocsHeader } from '../../../swc/.storybook/blocks'; | ||
|
|
||
| import * as Stories from './stories/slot-attribute-propagation.stories'; | ||
|
|
||
| <Meta of={Stories} /> | ||
|
|
||
| <DocsHeader packageName="slot-attribute-propagation" /> | ||
|
|
||
| ## Usage | ||
|
|
||
| `SlotAttributePropagationController` keeps a slot's assigned elements in sync with a host attribute, so consuming components don't each re-implement the same slot-observation logic. It's used by `ButtonGroup` (propagating `size` to the default slot) and `IllustratedMessage` (propagating `size` to the named `actions` slot). | ||
|
|
||
| ### Basic usage | ||
|
|
||
| 1. Construct the controller in the host's constructor, passing `attribute`, `getValue`, and optionally `slotName` and `selector`. | ||
| 2. The controller propagates automatically in `hostUpdated()` whenever the value returned by `getValue` changes, including on first render. | ||
| 3. Wire the host's `slotchange` event on the targeted slot to call `propagate()`, so elements assigned after the first render (for example, content added dynamically) still receive the attribute. `slotchange` does not itself trigger a Lit update, so this call is required. | ||
|
|
||
| ```typescript | ||
| import { LitElement, html } from 'lit'; | ||
| import { property } from 'lit/decorators.js'; | ||
| import { SlotAttributePropagationController } from '@spectrum-web-components/core/controllers/slot-attribute-propagation.js'; | ||
|
|
||
| class MyBase extends LitElement { | ||
| @property({ type: String, reflect: true }) | ||
| public size: MySize = 'm'; | ||
|
|
||
| private readonly sizePropagation = new SlotAttributePropagationController( | ||
| this, | ||
| { | ||
| attribute: 'size', | ||
| getValue: () => this.size, | ||
| slotName: 'actions', | ||
| } | ||
| ); | ||
|
|
||
| protected handleActionsSlotChange(): void { | ||
| this.sizePropagation.propagate(); | ||
| } | ||
|
|
||
| protected override render() { | ||
| return html` | ||
| <slot name="actions" @slotchange=${this.handleActionsSlotChange}></slot> | ||
| `; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| `getValue` can also return `null` for attributes that are only sometimes present on the host; see "Optional attributes" under Behaviors below. | ||
|
|
||
| ## Options | ||
|
|
||
| ### Default slot | ||
|
|
||
| Omit `slotName` to target the default (unnamed) slot. Every element assigned to it receives the attribute. | ||
|
|
||
| <Canvas of={Stories.DefaultSlot} /> | ||
|
|
||
| ### Named slot | ||
|
|
||
| Set `slotName` to target a specific named slot. Elements assigned to other slots on the same host, including the default slot, are left untouched. | ||
|
|
||
| <Canvas of={Stories.NamedSlot} /> | ||
|
|
||
| ### Selector filtering | ||
|
|
||
| Set `selector` to a CSS selector to propagate the attribute only to assigned elements that match it. Elements in the same slot that don't match are left untouched. | ||
|
|
||
| <Canvas of={Stories.Selector} /> | ||
|
|
||
| ## Behaviors | ||
|
|
||
| ### Propagation timing | ||
|
|
||
| The controller propagates the current value to all matching assigned elements on first render and again in `hostUpdated()` any time the value returned by `getValue` differs from the value tracked at the last propagation. If the value hasn't changed, `hostUpdated()` is a no-op: the controller does not touch assigned elements on every unrelated host update. | ||
|
|
||
| ### Optional attributes | ||
|
|
||
| `getValue` can return `null` for attributes that are only sometimes present on the host. When it does, the controller removes the attribute from assigned elements via `removeAttribute` instead of setting it to an empty string. This is the mechanism to use for attributes that are genuinely optional, such as `invalid`, rather than always-present attributes like `size`. | ||
|
|
||
| <Canvas of={Stories.OptionalAttributeRemoval} /> | ||
|
|
||
| ### Propagates to dynamically added elements | ||
|
|
||
| Elements assigned to the slot after the first render (for example, appended to the host's light DOM at runtime) don't trigger a Lit update on their own, so they aren't covered by the automatic `hostUpdated()` propagation. Call `propagate()` from the host's `slotchange` handler to cover them. | ||
|
|
||
| <Canvas of={Stories.DynamicSlotting} /> | ||
|
|
||
| ### Disconnect and reconnect | ||
|
|
||
| `hostDisconnected()` clears the internally tracked value. If the host reconnects and updates again with the _same_ value it had before disconnecting, the controller still re-propagates, because the internal guard has no prior value to compare against. This keeps propagation correct after a host is moved in the DOM (which disconnects and reconnects it). | ||
|
|
||
| ## Accessibility | ||
|
|
||
| ### Features | ||
|
|
||
| `SlotAttributePropagationController` sets a plain DOM attribute on assigned elements via `setAttribute`, or removes it via `removeAttribute` when `getValue` returns `null`. It does not manage ARIA relationships, roles, or attributes on its own: it will propagate whatever attribute name it's configured with, including an ARIA attribute, but the consuming component is responsible for deciding whether that's an appropriate way to convey that particular relationship. | ||
|
|
||
| ### Best practices | ||
|
|
||
| - Use this controller for presentation-oriented attributes that slotted children are expected to read declaratively, such as `size` or a visual `variant`. | ||
| - Prefer explicit ARIA wiring (for example `aria-describedby` set directly on the relevant elements) over propagating ARIA attributes through this controller, since assigned elements may have their own ARIA semantics that a blanket propagation could override unexpectedly. | ||
| - Always wire the targeted slot's `slotchange` event to `propagate()` so elements added after first render aren't missed. | ||
|
|
||
| <Canvas of={Stories.Accessibility} /> | ||
|
|
||
| ## API | ||
|
|
||
| ### Methods | ||
|
|
||
| | Member | Description | | ||
| | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | ||
| | `propagate()` | Propagates the current value (from `getValue()`) to all matching assigned elements immediately, without waiting for `hostUpdated()`. Also updates the internally tracked value, so a subsequent `hostUpdated()` with the same value is a no-op. Call from the host's `slotchange` handler. | | ||
|
|
||
| ### Constructor options (`SlotAttributePropagationControllerOptions`) | ||
|
|
||
| | Option | Type | Description | | ||
| | ----------- | ---------------------- | ------------------------------------------------------------------------------------------------------------- | | ||
| | `attribute` | `string` | The attribute name to propagate to assigned elements. | | ||
| | `getValue` | `() => string \| null` | Returns the current value to propagate. Return `null` to remove the attribute from assigned elements. | | ||
| | `slotName` | `string` | Named slot to target. Omit for the default (unnamed) slot. | | ||
| | `selector` | `string` | CSS selector used to filter assigned elements before propagating. Omit to propagate to all assigned elements. | | ||
|
|
||
| <DocsFooter /> | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was reviewing the existing API contract and then i checked the controller implementation and noticed one thing, let me know what you think about both
getValuesupportingstring | null, whereasnullremoves the attribute? Since this is a general "agnostic controller", we may need to propagate optional attributes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took a look at this in b109cca. Let me know what you think ✨