-
Notifications
You must be signed in to change notification settings - Fork 254
feat(card): scaffold shared CardBase, template #6491
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
5t3ph
wants to merge
5
commits into
main
Choose a base branch
from
seckles/cards-template
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.
+1,543
−4
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8b9f61d
feat(cards): init card base, template, and migration-plan
5t3ph fbd3971
feat(cards): conformance and consistency
5t3ph 3c3f699
feat(cards): add click/keydown decision flowchart
5t3ph 38467a4
feat(cards): add missing properties to the API table
5t3ph b9c8308
feat(cards): explain gallery and collection variants
5t3ph 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 |
|---|---|---|
| @@ -0,0 +1,279 @@ | ||
| /** | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| import { PropertyValues } from 'lit'; | ||
| import { property } from 'lit/decorators.js'; | ||
|
|
||
| import { SpectrumElement } from '@spectrum-web-components/core/element/index.js'; | ||
| import { SizedMixin } from '@spectrum-web-components/core/mixins/index.js'; | ||
|
|
||
| import { | ||
| CARD_DENSITIES, | ||
| CARD_VALID_SIZES, | ||
| CARD_VARIANTS, | ||
| type CardDensity, | ||
| type CardVariant, | ||
| } from './Card.types.js'; | ||
|
|
||
| /** | ||
| * Abstract base class for all card-family components. Owns the semantics | ||
| * shared across every card type: sizing, visual variant, and density. | ||
| * | ||
| * `swc-card`, `swc-user-card`, and `swc-product-card` each extend this base | ||
| * directly and render the shared anatomy via `renderCardTemplate()` in | ||
| * `swc/components/card/card-template.ts`. That template also carries a | ||
| * collection placeholder and an avatar/thumbnail glyph, each supplied | ||
| * per-component via `renderCollection`/`renderGlyph`. | ||
| * | ||
| * @attribute {ElementSize} size - The size of the card. | ||
| * | ||
| * @slot preview - Primary preview content | ||
| * @slot title - Card title | ||
| * @slot actions - Optional action controls | ||
| * @slot description - Supporting description text | ||
| * @slot footer - Optional footer content | ||
| * @slot - Additional body content | ||
| */ | ||
| export abstract class CardBase extends SizedMixin(SpectrumElement, { | ||
| validSizes: CARD_VALID_SIZES, | ||
| }) { | ||
| // ───────────────────────── | ||
| // API TO OVERRIDE | ||
| // ───────────────────────── | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| static readonly VARIANTS: readonly CardVariant[] = CARD_VARIANTS; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| static readonly DENSITIES: readonly CardDensity[] = CARD_DENSITIES; | ||
|
|
||
| // ────────────────── | ||
| // SHARED API | ||
| // ────────────────── | ||
|
|
||
| /** | ||
| * The visual variant of the card. | ||
| * | ||
| * @default primary | ||
| */ | ||
| @property({ type: String, reflect: true }) | ||
| public variant: CardVariant = 'primary'; | ||
|
|
||
| /** | ||
| * The density of the card, controlling internal spacing and gaps. | ||
| * | ||
| * @default regular | ||
| */ | ||
| @property({ type: String, reflect: true }) | ||
| public density: CardDensity = 'regular'; | ||
|
|
||
| /** | ||
| * Indicates the consumer has wrapped their `title` slot content in a real | ||
| * link. Extends that link's hit area to cover the card surface while | ||
| * leaving navigation entirely consumer-owned — Card accepts no `href`. | ||
| * | ||
| * @todo Naming, and the `::slotted(a)::after` vs. click-proxy mechanism, | ||
| * are still open. See the card family plan, A11y-3 / Q2. | ||
| */ | ||
| @property({ type: Boolean, reflect: true, attribute: 'title-as-link' }) | ||
| public titleAsLink = false; | ||
|
|
||
| /** | ||
| * Makes the card focusable and captures surface clicks (excluding nested | ||
| * interactive targets) to dispatch a `swc-card-click` event, independent | ||
| * of `titleAsLink`. Lays groundwork for a future `CardView` selection | ||
| * model without Card owning selected-state UI itself. | ||
| * | ||
| * @todo Event name and `tabindex` management details are still open. See | ||
| * the card family plan, A11y-3 / Q3. | ||
| * @todo No `role` is set when `selectable` is true — deferred rather than | ||
| * defaulting to `role="button"`, since the eventual `CardView` selection | ||
| * model may call for a different role (e.g. `option`/`gridcell`) that | ||
| * `role="button"` would be wrong to have committed to today. See the card | ||
| * family plan, A11y-3 / Q8. | ||
| */ | ||
| @property({ type: Boolean, reflect: true }) | ||
| public selectable = false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Am I understanding correctly that this property will eventually become private/internal? Or is our plan to support selectable cards without CardView? |
||
|
|
||
| // ────────────────────── | ||
| // IMPLEMENTATION | ||
| // ────────────────────── | ||
|
|
||
| public override connectedCallback(): void { | ||
| super.connectedCallback(); | ||
| this.addEventListener('click', this.handleSurfaceClick); | ||
| } | ||
|
|
||
| public override disconnectedCallback(): void { | ||
| this.removeEventListener('click', this.handleSurfaceClick); | ||
| this.removeEventListener('keydown', this.handleSelectableKeydown); | ||
| super.disconnectedCallback(); | ||
| } | ||
|
|
||
| protected override updated(changedProperties: PropertyValues): void { | ||
| super.updated(changedProperties); | ||
|
|
||
| if (changedProperties.has('selectable')) { | ||
| if (this.selectable) { | ||
| this.setAttribute('tabindex', '0'); | ||
| this.addEventListener('keydown', this.handleSelectableKeydown); | ||
| } else { | ||
| this.removeAttribute('tabindex'); | ||
| this.removeEventListener('keydown', this.handleSelectableKeydown); | ||
| } | ||
| } | ||
|
|
||
| if (window.__swc?.DEBUG) { | ||
| const { VARIANTS, DENSITIES } = this.constructor as typeof CardBase; | ||
|
|
||
| if ( | ||
| changedProperties.has('variant') && | ||
| !VARIANTS.includes(this.variant) | ||
| ) { | ||
| window.__swc.warn( | ||
| this, | ||
| `<${this.localName}> received an invalid "variant" value of "${this.variant}". Valid values are ${VARIANTS.join(', ')}.`, | ||
| 'https://opensource.adobe.com/spectrum-web-components/components/card/', | ||
| { issues: [`variant="${this.variant}"`] } | ||
| ); | ||
| } | ||
|
|
||
| if ( | ||
| changedProperties.has('density') && | ||
| !DENSITIES.includes(this.density) | ||
| ) { | ||
| window.__swc.warn( | ||
| this, | ||
| `<${this.localName}> received an invalid "density" value of "${this.density}". Valid values are ${DENSITIES.join(', ')}.`, | ||
| 'https://opensource.adobe.com/spectrum-web-components/components/card/', | ||
| { issues: [`density="${this.density}"`] } | ||
| ); | ||
| } | ||
|
|
||
| if ( | ||
| changedProperties.has('titleAsLink') && | ||
| this.titleAsLink && | ||
| !this.getTitleLinkElement() | ||
| ) { | ||
| window.__swc.warn( | ||
| this, | ||
| `<${this.localName}> has "title-as-link" set but no link element was found in the "title" slot.`, | ||
| 'https://opensource.adobe.com/spectrum-web-components/components/card/', | ||
| { issues: ['title-as-link'] } | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @internal | ||
| * The card's own `title` slot element, or `null` before the concrete | ||
| * class's `renderCardTemplate()` call has rendered. | ||
| */ | ||
| protected getTitleSlotElement(): HTMLSlotElement | null { | ||
| return this.renderRoot?.querySelector('slot[name="title"]') ?? null; | ||
| } | ||
|
|
||
| /** | ||
| * @internal | ||
| * The `title` slot's link, supporting both forms consumers reasonably | ||
| * use: the assigned element itself is the anchor (`<a slot="title" | ||
| * href="...">`), or the anchor is nested inside a wrapper (`<span | ||
| * slot="title"><a href="...">`). Requires `href` — a link with no | ||
| * destination can't usefully be clicked-through. | ||
| */ | ||
| protected getTitleLinkElement(): HTMLAnchorElement | null { | ||
| const assigned = this.getTitleSlotElement()?.assignedElements({ | ||
| flatten: true, | ||
| }); | ||
| for (const element of assigned ?? []) { | ||
| if (element instanceof HTMLAnchorElement && element.href) { | ||
| return element; | ||
| } | ||
| const nested = element.querySelector<HTMLAnchorElement>('a[href]'); | ||
| if (nested) { | ||
| return nested; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * @internal | ||
| * Whether `node` provides its own interaction/focus semantics. Checking | ||
| * the `tabIndex` IDL property (rather than the `tabindex` attribute or a | ||
| * tag-name list) correctly covers natively-interactive elements | ||
| * (`button`, `input`, `select`, `textarea`, `a[href]`) with no explicit | ||
| * attribute, excludes `tabindex="-1"` (focusable for scripting, not a | ||
| * click target) and disabled controls, and works for a custom element's | ||
| * internal shadow-DOM control too — `composedPath()` already traverses | ||
| * into other elements' shadow roots for composed events like `click`, so | ||
| * an internal `<button>` inside e.g. `<swc-button>` is inspected directly | ||
| * regardless of which shadow tree it belongs to. | ||
| */ | ||
| private static isFocusable(node: EventTarget): boolean { | ||
| return node instanceof HTMLElement && node.tabIndex >= 0; | ||
| } | ||
|
|
||
| /** | ||
| * Proxies a qualifying surface click to `titleAsLink`'s link and/or | ||
| * dispatches `swc-card-click` for `selectable`, after filtering out | ||
| * clicks that landed on a nested interactive target (see `isFocusable()` | ||
| * and the `actions`-slot exclusion above). | ||
| */ | ||
| protected readonly handleSurfaceClick = (event: Event): void => { | ||
| if (!this.titleAsLink && !this.selectable) { | ||
| return; | ||
| } | ||
| const path = event.composedPath(); | ||
| const precedingPath = path.slice(0, path.indexOf(this)); | ||
| const actionsSlot = this.renderRoot?.querySelector('slot[name="actions"]'); | ||
| const hitInteractiveTarget = | ||
| // The `actions` slot is unconditionally excluded, defense-in-depth, | ||
| // since it's contractually for interactive content regardless of | ||
| // whether a given control correctly reflects focusability. | ||
| (actionsSlot && precedingPath.includes(actionsSlot)) || | ||
| precedingPath.some(CardBase.isFocusable); | ||
| if (hitInteractiveTarget) { | ||
| return; | ||
| } | ||
| if (this.titleAsLink) { | ||
| this.getTitleLinkElement()?.click(); | ||
| } | ||
| if (this.selectable) { | ||
| this.dispatchEvent( | ||
| new Event('swc-card-click', { bubbles: true, composed: true }) | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * `tabindex` is only ever added alongside this listener (see `updated()`) | ||
| * and removed alongside its removal — a focusable card is never left | ||
| * without keyboard activation. Enter and Space are treated identically | ||
| * and both proxy to `titleAsLink`'s link when set, even though a bare | ||
| * link conventionally responds to Enter only: the card's own activation | ||
| * contract (Enter+Space, more button-like) is authoritative once | ||
| * `selectable` makes the card itself the focused target, regardless of | ||
| * what that activation happens to proxy to. | ||
| */ | ||
| protected readonly handleSelectableKeydown = (event: KeyboardEvent): void => { | ||
| if (event.code === 'Enter' || event.code === 'Space') { | ||
| event.preventDefault(); | ||
| this.handleSurfaceClick(event); | ||
| } | ||
| }; | ||
| } | ||
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,42 @@ | ||
| /** | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| import type { ElementSize } from '@spectrum-web-components/core/mixins/index.js'; | ||
|
|
||
| // ────────────────── | ||
| // SHARED | ||
| // ────────────────── | ||
|
|
||
| export const CARD_VALID_SIZES = [ | ||
| 'xs', | ||
| 's', | ||
| 'm', | ||
| 'l', | ||
| 'xl', | ||
| ] as const satisfies readonly ElementSize[]; | ||
|
|
||
| export const CARD_VARIANTS = [ | ||
| 'primary', | ||
| 'secondary', | ||
| 'tertiary', | ||
| 'quiet', | ||
| ] as const; | ||
|
|
||
| export const CARD_DENSITIES = ['compact', 'regular', 'spacious'] as const; | ||
|
|
||
| // ────────────────── | ||
| // TYPES | ||
| // ────────────────── | ||
|
|
||
| export type CardSize = (typeof CARD_VALID_SIZES)[number]; | ||
| export type CardVariant = (typeof CARD_VARIANTS)[number]; | ||
| export type CardDensity = (typeof CARD_DENSITIES)[number]; |
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,13 @@ | ||
| /** | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
| export * from './Card.base.js'; | ||
| export * from './Card.types.js'; |
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
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.
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.
We've been keeping to the "component has one
rolerule" this far along, so I like deferring this to see how we want to do this.