Skip to content

Card ​

A bordered surface holding one unit of content: a header with a title and a description, the content, a footer. Seven parts, all presentational — each is a <div> with a data-slot and a class list, no state and no context — so they nest as plain values, with no () =>.

CardAction is the part worth knowing. Placed in a CardHeader, it sits in the header's second column, opposite the title and aligned to its top, with no flex wrapper and no spacer: the header switches to two columns only when it holds an action.

sh
ream nebula:add card

Molecule · molecules/Card.ts · no dependencies

Usage ​

ts
import { html } from '@c9up/aurora'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@c9up/nebula'

Card({
  children: html`
    ${CardHeader({ children: html`
      ${CardTitle({ children: 'Storage' })}
      ${CardDescription({ children: '12 GB of 50 GB used' })}
    ` })}
    ${CardContent({ children: 'Your files are synced on 3 devices.' })}
  `,
})

Examples ​

ts
import { Button, CardAction, CardFooter } from '@c9up/nebula'

Card({
  class: 'w-full max-w-sm',
  children: html`
    ${CardHeader({ children: html`
      ${CardTitle({ children: 'Deploy your project' })}
      ${CardDescription({ children: 'Ship the main branch to production.' })}
      ${CardAction({ children: Button({ variant: 'link', children: 'History' }) })}
    ` })}
    ${CardContent({ children: deployForm })}
    ${CardFooter({ class: 'justify-end gap-2', children: html`
      ${Button({ variant: 'outline', children: 'Cancel' })}
      ${Button({ children: 'Deploy', onClick: () => deploy() })}
    ` })}
  `,
})

The action spans both rows of the header, so a long description wraps under the title without running beneath the button.

A header given border-b pads itself below its rule, and a footer given border-t pads itself above it — the card's own spacing does not have to be undone:

ts
Card({
  children: html`
    ${CardHeader({ class: 'border-b', children: CardTitle({ children: 'Team members' }) })}
    ${CardContent({ children: memberList })}
    ${CardFooter({ class: 'border-t', children: Button({ variant: 'outline', children: 'Invite' }) })}
  `,
})

Following state ​

Every part's children is a Slot, so text that changes is an accessor:

ts
CardDescription({ children: () => `${used()} GB of ${quota()} GB used` })

API ​

Every part takes the same props, exported as CardProps.

Card ​

The surface: border, rounded corners, a light shadow, and a 24 px gap between its parts.

PropTypeDefaultDescription
childrenSlot—The header, content and footer.
classReactive<string>—Merged last, so a conflicting utility wins.
idstring—The element's id.

CardHeader ​

A grid of title and description, with a second column when it holds a CardAction.

PropTypeDefaultDescription
childrenSlot—CardTitle, CardDescription, and an optional CardAction.
classReactive<string>—Merged last. border-b adds the padding under the rule.
idstring—The element's id.

CardTitle ​

PropTypeDefaultDescription
childrenSlot—The title, in semibold.
classReactive<string>—Merged last.
idstring—The element's id.

CardDescription ​

PropTypeDefaultDescription
childrenSlot—A line under the title, small and muted.
classReactive<string>—Merged last.
idstring—The element's id.

CardAction ​

PropTypeDefaultDescription
childrenSlot—A button, a link or a menu trigger, placed opposite the title.
classReactive<string>—Merged last.
idstring—The element's id.

CardContent ​

PropTypeDefaultDescription
childrenSlot—The body, with the card's side padding.
classReactive<string>—Merged last.
idstring—The element's id.

CardFooter ​

PropTypeDefaultDescription
childrenSlot—A row of actions, centred vertically.
classReactive<string>—Merged last. border-t adds the padding over the rule.
idstring—The element's id.

Accessibility ​

A card is a set of <div>s with no role, and it handles no key. CardTitle is not a heading: where the page's outline needs one, put the heading inside it — CardTitle({ children: html`<h2>Storage</h2>` }) — at the level the page calls for. An id on the title and aria-labelledby on a surrounding <section> make the card a named region when that helps.