Skip to main content
Decoderen
Browse documentation

Card

Group content about one subject on Material 3's medium shape.

Variants

Discovery

Two weeks to map the work.

Discovery

Two weeks to map the work.

Discovery

Two weeks to map the work.

Show code
Example.astroAstro
---
import Card from '@dhekode/ui/primitives/Card.tsx';
---

<Card variant="outlined">
  <p>Discovery</p>
  <p>Two weeks to map the work.</p>
</Card>
<Card variant="filled">
  <p>Discovery</p>
  <p>Two weeks to map the work.</p>
</Card>
<Card variant="elevated">
  <p>Discovery</p>
  <p>Two weeks to map the work.</p>
</Card>

Linked

The whole card is one link, with the state layer on hover, focus and press.

Show code
Example.astroAstro
---
import Card from '@dhekode/ui/primitives/Card.tsx';
---

<Card href="/decoderen/button/">
  <p>Button</p>
  <p>Actions with a clear priority.</p>
</Card>
API reference
variant

Type: 'outlined' | 'filled' | 'elevated'

Default: 'outlined'

Hairline outline, surface tint, or raised surface with the navy elevation shadow.

href

Type: string

Default: unset

Makes the whole card one link with a state layer. Keep other interactive elements out of it.

as

Type: 'div' | 'article' | 'li'

Default: 'div'

Element for a static card.

className

Type: string | undefined

Default: unset

Adds classes to the React component root, after its own utilities. React components take className, not class.

Slots: children: the card content.

Use a card when its content stands alone. A list of cards needs a list parent and as="li". A linked card is one target, so its heading and text form the link name.

Original component source
packages/ui/src/primitives/Card.tsxAstro
import type { ReactNode } from 'react';
import { classNames } from '@dhekode/utils/class-names';
import { ownStates, stateLayer } from '@dhekode/utils/state-layer';

type Variant = 'outlined' | 'filled' | 'elevated';

export interface CardProps {
  /** Material 3's three cards. */
  variant?: Variant | undefined;
  /** Makes the whole card one link, with a state layer. Keep other links and buttons out of it. */
  href?: string | undefined;
  /** The element for a static card; a linked card is always an anchor. */
  as?: 'div' | 'article' | 'li' | undefined;
  className?: string | undefined;
  children: ReactNode;
}

/** Component tokens: each card's container and outline, on the surface roles. */
const variants: Record<Variant, string> = {
  outlined: 'border-surface-border bg-surface-bg',
  filled: 'border-transparent bg-surface-tint',
  elevated: 'border-transparent bg-surface-raised shadow-(--dk-elevation-1)',
};

const linked = `cursor-pointer no-underline before:rounded-card ${stateLayer} ${ownStates}`;

/** A container for one subject on Material 3's medium shape. */
export default function Card({
  variant = 'outlined',
  href,
  as: Element = 'div',
  className,
  children,
}: CardProps) {
  const classes = classNames(
    'dk-card relative isolate grid content-start gap-sm rounded-card border-(length:--dk-border-width-hairline) border-solid p-md text-surface-text wrap-anywhere',
    variants[variant],
    href !== undefined && linked,
    className,
  );
  if (href !== undefined) {
    return (
      <a className={classes} href={href} data-variant={variant}>
        {children}
      </a>
    );
  }
  return (
    <Element className={classes} data-variant={variant}>
      {children}
    </Element>
  );
}

Best practices

  • Give each card one subject and keep cards in a group parallel.
  • Make the whole card a link only when it leads to a single place.