Menu

Components

Card

A box that groups related content, as a plain card or as a frame with a raised panel inside.

Introduction

Card groups a piece of content under a title: a settings section, a plan, a summary. It is a plain box with parts you use as needed: a header, the content, and a footer.

  • isInset is a boolean. false is a card as usual. true is a soft frame with the header on it, and the content as a raised panel inside, the look of a settings section that holds an empty state, a table or a list.
  • The header lays out a title, a description and an action in the corner. The description can hold a link.
  • The parts are optional and independent. It has no behavior, so it works in Server Components.

A card is not interactive by itself. Put a button or a link inside it for what the user can do.

Installation

npx shadcn@latest add https://ui.alwinfau.com/r/card.json

Manual installation

Without the CLI: copy these files into your project and keep the paths, or adjust the imports.

1. Install the dependencies

# bun
bun add tailwind-variants tailwind-merge clsx

# pnpm
pnpm add tailwind-variants tailwind-merge clsx

# npm
npm install tailwind-variants tailwind-merge clsx

2. Add the styles

src/types/card-styles.tsShow code
import { tv } from "tailwind-variants";

// Styles for Card. The parts do not read `isInset` from a context: the card
// puts `data-inset` on itself, and the parts answer to it through the
// `group/card` name. That keeps every part usable in a Server Component.
export const cardStyles = tv({
  slots: {
    root: "group/card flex flex-col text-card-fg",
    header: [
      "grid grid-cols-[1fr_auto] items-start gap-x-4 gap-y-1 px-6",
      "group-data-inset/card:px-4 group-data-inset/card:pt-3 group-data-inset/card:pb-2",
    ],
    // Pinned to column 1: without this, an auto-placed title and
    // description (no `CardAction` around to push the second one down)
    // land side by side in row 1 instead of stacking.
    title: "col-start-1 m-0 text-lg leading-snug font-semibold",
    description: "col-start-1 m-0 text-sm text-muted-fg",
    // In the corner of the header, next to the title and the description.
    action: "col-start-2 row-span-2 row-start-1 self-start justify-self-end",
    content: [
      "px-6 text-sm",
      // The inset card's content is the raised panel inside the frame.
      "group-data-inset/card:rounded-xl group-data-inset/card:border group-data-inset/card:bg-card group-data-inset/card:p-6 group-data-inset/card:shadow-xs",
    ],
    footer: [
      "flex items-center gap-2 px-6",
      "group-data-inset/card:px-4 group-data-inset/card:py-2",
    ],
  },
  variants: {
    // Plain card, or the frame with a raised panel inside it.
    isInset: {
      true: { root: "gap-1 rounded-2xl border bg-muted/50 p-1.5" },
      false: { root: "gap-6 rounded-xl border bg-card py-6 shadow-sm" },
    },
  },
  defaultVariants: { isInset: false },
});

3. Add the Card file

src/components/ui/card/card.tsxShow code
import type { ElementType, ReactNode } from "react";
import { cn } from "@/lib/cn";
import { cardStyles } from "@/types/card-styles";

// The parts take their `isInset` look from the card around them, through a CSS
// group, so none of this needs to be a Client Component.
const styles = cardStyles();

export interface CardProps {
  /** A `CardHeader`, a `CardContent` and a `CardFooter`, in any combination. */
  children: ReactNode;
  /**
   * `false` is a plain card. `true` is a frame with the header on it and the
   * content as a raised panel inside: the look of a settings section that
   * holds a list, a table or an empty state.
   */
  isInset?: boolean;
  className?: string;
}

/**
 * A box that groups related content: a header with a title, the content, and
 * a footer with actions. It has no behavior of its own, and works in Server
 * Components.
 *
 * Set `isInset` for the framed look, where the header sits on the frame and
 * `CardContent` is the panel inside it.
 *
 * @example
 * <Card>
 *   <CardHeader>
 *     <CardTitle>Secrets</CardTitle>
 *     <CardDescription>Shared across every environment.</CardDescription>
 *   </CardHeader>
 *   <CardContent>...</CardContent>
 * </Card>
 */
export function Card({ children, isInset = false, className }: CardProps) {
  return (
    <div
      data-inset={isInset || undefined}
      className={cn(cardStyles({ isInset }).root(), className)}
    >
      {children}
    </div>
  );
}

interface PartProps {
  children?: ReactNode;
  className?: string;
}

/** The top of the card: a `CardTitle`, a `CardDescription` and maybe a `CardAction`. */
export function CardHeader({ children, className }: PartProps) {
  return <div className={cn(styles.header(), className)}>{children}</div>;
}

export interface CardTitleProps extends PartProps {
  /** The element to render. Pick the heading level that fits the page. Defaults to `h3`. */
  as?: "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "div";
}

/** The title of the card. */
export function CardTitle({
  as: Tag = "h3",
  children,
  className,
}: CardTitleProps) {
  const Component: ElementType = Tag;

  return (
    <Component className={cn(styles.title(), className)}>{children}</Component>
  );
}

/** A line under the title that says what the card is for. It can hold a link. */
export function CardDescription({ children, className }: PartProps) {
  return <p className={cn(styles.description(), className)}>{children}</p>;
}

/** A control in the corner of the header, such as a button or a menu. */
export function CardAction({ children, className }: PartProps) {
  return <div className={cn(styles.action(), className)}>{children}</div>;
}

/** The body of the card. In an inset card it is the raised panel. */
export function CardContent({ children, className }: PartProps) {
  return <div className={cn(styles.content(), className)}>{children}</div>;
}

/** The bottom of the card: buttons or a note, in a row. */
export function CardFooter({ children, className }: PartProps) {
  return <div className={cn(styles.footer(), className)}>{children}</div>;
}

4. Use it

import {
  Card,
  CardAction,
  CardContent,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle,
} from "@/components/ui/card/card";

Anatomy

A box with a header, a body and a footer, all optional.

<Card isInset>
  <CardHeader>
    <CardTitle>Secrets</CardTitle>
    <CardDescription>Shared across every environment.</CardDescription>
    <CardAction><Button size="sm">New</Button></CardAction>
  </CardHeader>
  <CardContent>{/* the raised panel, when inset */}</CardContent>
  <CardFooter>Last updated today</CardFooter>
</Card>
PartRendered asNotes
CarddivThe box. isInset decides its look: a plain card, or a frame with a raised panel inside.
CardHeaderdivA grid with the title, the description and an optional action.
CardTitleh3 (or as)The title. Choose the heading level with as.
CardDescriptionpA line under the title. It can hold a link.
CardActiondivIn the corner of the header: a button, a menu, a badge.
CardContentdivThe body. In an inset card it becomes the raised panel with its own border.
CardFooterdivThe bottom: buttons or a note, in a row.

Examples

Usage

Secrets

Manage shared secrets and use them across multiple environments.

You don't have any secrets in this organization.

Inset

With isInset the card is a soft frame. The header sits on the frame, and CardContent is a raised panel with its own border, here holding an empty state.

Secrets

Manage shared secrets and use them across multiple environments. Learn more

No secrets

You don't have any secrets in this organization.

Inset or not

The same content in both. isInset is a boolean, and false, the default, is a plain card.

isInset=false

A plain card.

Some content in the card.

isInset=true

A frame with a raised panel.

Some content in the card.

Header action

CardAction puts a control in the corner of the header, next to the title and the description.

Team members

People with access to this project.

3 members, 1 pending invite.

Inset with a list

The raised panel suits a list or a table. Remove its padding with className and let the rows fill it.

Environments

Where your project is deployed.

Productionmain · deployed 2 hours ago
Previewevery pull request
Developmentlocal machines

Parts on their own

Every part is optional. A card can be just content, or just a title.

Just some content.

Only a title

Heading level

The title is an h3 by default. Use as to fit the outline of your page, or a plain div when it is not a heading.

A card with an h2 title

Feature card

No behavior of its own means no restriction, either: this is the same Card, just with its own colors, a grain texture and the icon set beside the title instead of above it.

Composable

Every part is its own piece. Use only the ones a card needs, in any order.

Themeable

Every color is a token, already tuned for both light and dark.

Accessible

Plain HTML underneath, so headings, landmarks and focus order stay correct.

Component API

Card is made of the parts below.

Card

PropTypeDefault
childrenReactNode- (required)
isInsetbooleanfalse
classNamestring-

The other parts

PartProps
CardHeader, CardDescription, CardAction, CardContent, CardFooterchildren, className
CardTitlechildren, className, as ("h1"…"h6" | "div", default "h3")