Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
279 changes: 279 additions & 0 deletions 2nd-gen/packages/core/components/card/Card.base.ts
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;
Comment on lines +102 to +109

Copy link
Copy Markdown
Contributor

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 role rule" this far along, so I like deferring this to see how we want to do this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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);
}
};
}
42 changes: 42 additions & 0 deletions 2nd-gen/packages/core/components/card/Card.types.ts
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];
13 changes: 13 additions & 0 deletions 2nd-gen/packages/core/components/card/index.ts
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';
7 changes: 7 additions & 0 deletions 2nd-gen/packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
"types": "./dist/components/button-group/index.d.ts",
"import": "./dist/components/button-group/index.js"
},
"./components/card": {
"types": "./dist/components/card/index.d.ts",
"import": "./dist/components/card/index.js"
},
"./components/divider": {
"types": "./dist/components/divider/index.d.ts",
"import": "./dist/components/divider/index.js"
Expand Down Expand Up @@ -231,6 +235,9 @@
"components/button-group": [
"dist/components/button-group/index.d.ts"
],
"components/card": [
"dist/components/card/index.d.ts"
],
"components/divider": [
"dist/components/divider/index.d.ts"
],
Expand Down
7 changes: 3 additions & 4 deletions 2nd-gen/packages/swc/.storybook/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ const preview = {
'Migration plan',
'Rendering and styling migration analysis',
],
'Card',
['Migration plan'],
'Checkbox',
['Rendering and styling migration analysis'],
'Close button',
Expand Down Expand Up @@ -523,10 +525,7 @@ const preview = {
'Rendering and styling migration analysis',
],
'Toast',
[
'Accessibility migration analysis',
'Rendering and styling migration analysis',
],
['Accessibility migration analysis'],
'Tooltip',
[
'Accessibility migration analysis',
Expand Down
Loading
Loading