Card

Slant / Primitives

A surface with the theme's border and radius, a padded body, and an optional header row and footer.

Demo

Usage

@tuttn/slant
import { Badge, Button, Card, CommandBar, Text } from '@tuttn/slant'

// A border, the theme's radius, and an 18px padded body.
<Card><Text>Saved 4 minutes ago on TASK-24.</Text></Card>

// A title alone sits over the body, in the same padded box.
<Card title="Checkpoint"><Text>Saved 4 minutes ago on TASK-24.</Text></Card>

// A header or a footer splits the card into regions, with hairlines between them.
<Card title="Checkpoint" header={<Badge tone="success">Saved</Badge>} elevation={1}
  footer={<><Button size="sm">Discard</Button><Button size="sm" variant="primary">Restore</Button></>}>
  <Text>Saved 4 minutes ago on TASK-24.</Text>
</Card>

// padding={false} lets content with its own padding run to the edges.
<Card padding={false}><CommandBar commands={commands} onRun={run} /></Card>

// A named region: a real heading as the title, and the section labelled by it.
<Card as="section" aria-labelledby="usage-title"
  title={<Text as="h2" variant="label" id="usage-title">Usage</Text>}>…</Card>

Props

PropTypeDefaultDescription
titleReactNode—A small mono label at the start of the header row.
headerReactNode—The rest of the header row, after the title: actions, a badge, a menu.
footerReactNode—A row under the body, laid out at the end: actions, a summary.
paddingbooleantruePads the body. Turn it off to let the content run to the edges; the header and footer keep theirs.
elevation0 | 1 | 2 | 30Lift off the page: none, or the theme's small, medium or large shadow.
as'div' | 'section' | 'article' | 'li''div'The element to render.
…restHTMLAttributes<HTMLElement>—className, style, id, aria-* and handlers go on the root element, and so does the ref. HTML's title attribute isn't one of them: title is the header slot.

Accessibility

  • Card adds no role and no name. When it stands for a region, render it as="section" and point aria-labelledby at its title; use as="article" for self-contained content, and as="li" inside a list.
  • The title is styled text, not a heading. Pass a heading when it belongs in the outline: title={<Text as="h3" variant="label">Checkpoint</Text>} keeps the same look.
  • Card isn't interactive. To make one clickable, put a link or a button inside it, on the title say, rather than an onClick on the card, so it has a role, a name and a tab stop.
  • The edge is a 1px border in every theme and elevation only adds a shadow, so the card stays outlined where shadows are dropped, as in forced-colours mode.

In the header row the title takes the free space, which puts the header content at the end, and the row wraps on narrow screens. The footer lines its children up at the end. With neither a header nor a footer there are no hairlines: a title alone sits over the body in one padded box. Slots that are undefined, null, false or an empty string render nothing, so footer={saving && …} works. A card is a block that fills the width it's given; in a Row, give it style={{ flex: 1 }} to share the row.