-
Notifications
You must be signed in to change notification settings - Fork 36
add semantic adaptation guide and demo for usage-aware-component-variations #803
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
castastrophe
wants to merge
3
commits into
main
Choose a base branch
from
usage-aware-component-variations-169
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
160 changes: 159 additions & 1 deletion
160
guides/user-experience/usage-aware-component-variations/demo.html
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,7 @@ | ||
| - The parent containers define semantic context flags using CSS custom properties (e.g., `--surface: featured` vs. the unset default). | ||
| - The component uses `@container style()` queries to adapt its internal visual logic (e.g., button variant, badge visibility) based on these inherited properties. | ||
| - When the surface is `featured`, the component reveals promotional elements (like a badge) and uses a more prominent "filled" button style. | ||
| - When the surface is unset (default), the component hides promotional elements and uses a more subtle "outlined" button style. | ||
| - The component does NOT rely on size queries (`min-width` or `max-width`) for this semantic variation. | ||
| - The implementation uses logical properties (e.g., `inline-size`, `inset-block-start`) for consistent layout. | ||
| - A progressive-enhancement fallback using selectors with `:where()` is provided so browsers without style query support still get the contextual styling. |
108 changes: 107 additions & 1 deletion
108
guides/user-experience/usage-aware-component-variations/guide.md
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,6 +1,112 @@ | ||
| --- | ||
| name: usage-aware-component-variations | ||
| description: Build a component that can adapt its styles conditionally based on where it appears in the DOM (via CSS custom property inheritance) rather than available size. For example, a table of contents components that is expanded in the sidebar but collapsed in a single-column layout. | ||
| description: Build components that adapt visual logic based on semantic context using CSS container style queries. | ||
| web-feature-ids: | ||
| - container-style-queries | ||
| sources: | ||
| - https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@container#container_style_queries | ||
| - https://developer.chrome.com/docs/css-ui/style-queries | ||
| --- | ||
|
|
||
| # Semantic Component Adaptation | ||
|
|
||
| Use style queries (`@container style()`) to trigger component variations based on inherited semantic context flags rather than viewport or container dimensions. This replaces deep descendant selectors (e.g., `.sidebar .card`) and improves component encapsulation. | ||
|
|
||
| In design systems, this lets a component respond to its surrounding context without that context needing to know the component exists. For example, a card placed inside a section marked `--surface: featured` can swap to a filled button and reveal a promotional badge — the section never names `.button` or `.badge`, and the card never inspects its parent. | ||
|
|
||
| ## How is this different from design-token-reactivity? | ||
|
|
||
| [`design-token-reactivity`](../design-token-reactivity/guide.md) covers *token-level* changes: density modes, themes, and other higher-order tokens that uniformly shift values (paddings, colors) across many components. | ||
|
|
||
| This guide covers *behavioral* changes: a component deciding **what to render, how to arrange itself, or which variant to present** in response to its context. Hiding/showing a badge, switching layout direction, or swapping a button variant all fit here. | ||
|
|
||
| ## Mental Model | ||
|
|
||
| - **Size Queries:** Use for layout/density changes (width/height). | ||
| - **Style Queries:** Use for semantic logic — what the component should *do* in this context. | ||
|
|
||
| ## Implementation Directives | ||
|
|
||
| 1. Define a semantic context flag using a CSS custom property on a container. | ||
| 2. **DO NOT** set `container-type` for style queries; they query inherited custom properties on the nearest ancestor and don't require an explicit containment context. | ||
| 3. **MANDATORY**: Use `@container style(--property: value)` to apply conditional styles to descendant elements. Style queries cannot match the element the property is set on — only its descendants. | ||
| 4. **DO** prefer style queries over descendant selectors to avoid specificity inflation and hardcoded DOM dependencies. | ||
|
|
||
| ## Implementation Example | ||
|
|
||
| ```css | ||
| /* 1. Set the context on a layout container */ | ||
| .featured { | ||
| --surface: featured; /* Semantic context flag for children */ | ||
| } | ||
|
|
||
| /* 2. Component reacts to inherited flag */ | ||
| .button { | ||
| /* Default: Outlined informational style */ | ||
| background: transparent; | ||
| border: 1px solid currentColor; | ||
| } | ||
|
|
||
| .badge { | ||
| /* Default: Hidden — only revealed in featured surfaces */ | ||
| display: none; | ||
| } | ||
|
|
||
| @container style(--surface: featured) { | ||
| .button { | ||
| /* Automatic switch to Filled promotional style */ | ||
| background: var(--brand-accent); | ||
| color: white; | ||
| border: none; | ||
| } | ||
|
|
||
| .badge { | ||
| /* Reveal decorative elements only in featured context */ | ||
| display: block; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Fallback Strategies | ||
|
|
||
| {{ BASELINE_STATUS("container-style-queries") }} | ||
|
|
||
| Until container style queries are widely available, layer them on as a progressive enhancement: ship a selector-based default that works everywhere, and let the style query override it when supported. Use `:where()` on the context selector so the style query (same specificity, later in source order) wins automatically. | ||
|
|
||
| ```css | ||
| /* Default styles — work everywhere */ | ||
| .button { | ||
| background: transparent; | ||
| border: 1px solid currentColor; | ||
| } | ||
|
|
||
| .badge { | ||
| display: none; | ||
| } | ||
|
|
||
| /* Selector-based contextual styles — broad browser support */ | ||
| :where(.featured) .button { | ||
| background: var(--brand-accent); | ||
| color: white; | ||
| border: none; | ||
| } | ||
|
|
||
| :where(.featured) .badge { | ||
| display: block; | ||
| } | ||
|
|
||
| /* Progressive enhancement: style queries override when supported */ | ||
| @container style(--surface: featured) { | ||
| .button { | ||
| background: var(--brand-accent); | ||
| color: white; | ||
| border: none; | ||
| } | ||
|
|
||
| .badge { | ||
| display: block; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The selector fallback ties the contextual styling to a class name, which couples the component to its DOM placement. Once style queries are widely supported, the `:where()` rule can be removed. | ||
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.