# Card

A card groups related content on one surface, such as a settings section, a summary or a choice.

A card puts related content on one surface with its own padding and corner, so people read it as one unit. A list of records with the same fields reads better as a [Table](/docs/table). A message about a condition that is true now is a [Notice](/docs/notice).


## Usage

```tsx
import { Button, Card, Flex, Heading, Text } from '@kushagradhawan/kookie-ui';

export function PlanCard({ onUpgrade }: { onUpgrade: () => void }) {
  return (
    <Card>
      <Flex direction="column" gap="3">
        <Heading as="h3" size="3">Free plan</Heading>
        <Text size="2" emphasis="medium">
          3 projects and 1 GB of storage. Upgrade to add your team.
        </Text>
        <Flex justify="end">
          <Button emphasis="loud" onClick={onUpgrade}>
            Upgrade plan
          </Button>
        </Flex>
      </Flex>
    </Card>
  );
}
```

## Best practices

**Use a card only when the content belongs together.** A card tells people that what is inside it is one thing. Wrapping every section of a page in a card adds borders and padding without telling them anything new.

**Don't put a card inside a card.** Group things inside one card with space, a `Flex` with a gap. Put several cards side by side in a `Flex` or `Grid` on the page, not in another card. In development, a card inside a card logs a warning.

**Put the layout inside the card.** A Card has no gap or direction of its own. Place a `Flex` or `Grid` inside it and set the spacing there.

**Make the whole card the link, or nothing.** When a card opens a page, render the link with `asChild` so the full surface is one target. A card that holds a link and two buttons should stay a plain `div`, because nested targets are hard to press.

**Use one variant for cards at the same level.** Cards side by side in a grid read as a set when they share a look. Change the variant only when the card means something different, such as `soft` for an empty state.

**Don't use a card to hold a single sentence.** A short message that has no action is text. Use [Text](/docs/text), or a [Notice](/docs/notice) when it must stand apart.

## Variant

`variant` sets how the card stands apart from the page.

| Variant | Look | Use it for |
| --- | --- | --- |
| `raised` | Solid surface with a shadow | Most cards. This is the default |
| `surface` | Light fill and a border | Cards on a busy or tinted background |
| `outline` | A border and no fill | Dense layouts where shadows would add noise |
| `soft` | A gray fill and no border | Empty states and secondary groups |
| `ghost` | No fill and no border | Links and buttons that should sit flat on the page until hover |

When the card is a link, a `button` or a `label`, every variant shows a hover and a press state.

## Size

`size` sets the padding and the corner. Sizes 1 to 4 pad by 16, 24, 32 and 40 pixels. When you leave it unset, the card follows the size of the nearest [Theme](/start/theming). Use size 1 for compact cards in a grid and size 3 or 4 for a card that is the main content of a page. `size` accepts a responsive object, such as `{ initial: '1', md: '2' }`.

## Examples

### Card that opens a page

Render your router's link with `asChild`. The element stays an anchor, so it opens in a new tab like any link, and the card shows a focus ring for the keyboard.

```tsx
<Card asChild>
  <Link href="/projects/atlas">
    <Flex direction="column" gap="1">
      <Heading as="h3" size="3">Atlas</Heading>
      <Text size="2" emphasis="medium">Updated 2 hours ago</Text>
    </Flex>
  </Link>
</Card>
```

### Choosing one option

A card rendered as a `label` that holds a checked [Checkbox](/docs/checkbox) or [Radio](/docs/radio-group) shows an accent fill and border, and a press anywhere on the card toggles the control. A plain card holding a form of checkboxes doesn't light up.

```tsx
<Card asChild>
  <label>
    <Flex gap="3" align="start">
      <Radio name="plan" value="pro" />
      <Text size="2">Pro, with unlimited projects</Text>
    </Flex>
  </label>
</Card>
```

### Image to the edge

Set a margin of `bleed` on a `Box` to pull it out by the card's padding, so an image reaches the edge. The card clips it to its corner.

```tsx
<Card>
  <Box mx="bleed" mt="bleed" mb="4">
    <Image src="/cover.jpg" alt="Mountain lake at dawn" />
  </Box>
  <Text size="2">Lake Louise</Text>
</Card>
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `'raised' \| 'surface' \| 'outline' \| 'soft' \| 'ghost'` | `'raised'` | How the card stands apart from the page |
| `size` | `'1' \| '2' \| '3' \| '4'`, responsive | Theme size | The padding and corner step |
| `asChild` | `boolean` | `false` | Renders the child element, such as a link or a label, with the card's look |

Card also accepts every attribute of the HTML `div` element.

## Accessibility

- **Structure:** A card is a `div` and adds no role. Start it with a heading when it is a section, so people who use a screen reader can find it by heading.
- **Links:** A card rendered as a link announces all of its text as the link name. Keep the content short, and put the most important words first.
- **Focus:** A plain card is not focusable. A card rendered as a link, a `button` or a `label` shows the focus ring for the keyboard only.
- **High contrast:** In forced colors mode, such as Windows High Contrast, every variant except `ghost` draws a solid border, and a chosen card draws it in the highlight colour.
