Card

A raised surface that groups related content.

A card has a header (title, description and an optional action), a body and a footer, all optional. size="sm" tightens its spacing for dense panels.

Preview
basic.tsx
import { Ellipsis } from "lucide-react"

import { Button } from "~/components/ui/button"
import {
  Card,
  CardAction,
  CardContent,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle,
} from "~/components/ui/card"
import { Text } from "~/components/ui/text"

export default function Example() {
  return (
    <Card className="w-full max-w-sm">
      <CardHeader>
        <CardTitle>Weekend edition</CardTitle>
        <CardDescription>24 pages · due Friday 6pm</CardDescription>
        <CardAction>
          <Button variant="ghost" size="icon" aria-label="More">
            <Ellipsis />
          </Button>
        </CardAction>
      </CardHeader>
      <CardContent>
        <Text tone="muted">
          Layout is complete for 22 pages. The front page and A2 still have overflowing copy.
        </Text>
      </CardContent>
      <CardFooter className="justify-end">
        <Button variant="outline" size="sm">
          Open
        </Button>
        <Button size="sm">Export</Button>
      </CardFooter>
    </Card>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesTextprimitive
Used byNothing yet.
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/card.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/card.tsx
import { renderElement, type RenderProp } from "@ui-sdk/react/primitive"
import type * as React from "react"

import { textVariants } from "~/components/ui/text"
import { cn } from "~/lib/utils"

type CardProps = React.ComponentProps<"div"> & {
  /** Padding and gaps. `sm` suits dense panels. */
  size?: "sm" | "md"
  /** The element to render instead of a `<div>`. Give it `<a href="…" />` to make the whole card a link. */
  render?: RenderProp
}

/** A raised surface grouping related content, with an optional header, body and footer. */
function Card({ size = "md", render, className, ...props }: CardProps) {
  return renderElement(
    "div",
    {
      "data-slot": "card",
      "data-size": size,
      className: cn(
        textVariants({ size: "sm" }),
        "group/card flex flex-col gap-xl overflow-hidden rounded-xl bg-card py-xl text-card-foreground shadow-sm ring-1 ring-foreground/5 data-[size=sm]:gap-lg data-[size=sm]:py-lg dark:ring-foreground/10",
        "has-[>img:first-child]:pt-0",
        // As a link: a hover and a focus ring, like the system's other links.
        "[a&]:transition-colors [a&]:outline-none [a&]:hover:bg-muted/50 [a&]:focus-visible:ring-3 [a&]:focus-visible:ring-ring/30",
        className,
      ),
      ...props,
    },
    render,
  )
}

/** The card's title, description and an optional action beside them. */
function CardHeader({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="card-header"
      className={cn(
        "grid auto-rows-min items-start gap-xs px-xl group-data-[size=sm]/card:px-lg has-data-[slot=card-action]:grid-cols-[1fr_auto] [.border-b]:pb-xl group-data-[size=sm]/card:[.border-b]:pb-lg",
        className,
      )}
      {...props}
    />
  )
}

/** Names the card. */
function CardTitle({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="card-title"
      className={cn(
        textVariants({ size: "md", weight: "medium", family: "heading", leading: "snug" }),
        className,
      )}
      {...props}
    />
  )
}

/** A line under the title. */
function CardDescription({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="card-description"
      className={cn(textVariants({ size: "sm", tone: "muted" }), className)}
      {...props}
    />
  )
}

/** A control in the header's top-right corner. */
function CardAction({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="card-action"
      className={cn("col-start-2 row-span-2 row-start-1 self-start justify-self-end", className)}
      {...props}
    />
  )
}

/** The card's body. */
function CardContent({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="card-content"
      className={cn("px-xl group-data-[size=sm]/card:px-lg", className)}
      {...props}
    />
  )
}

/** Actions or notes at the bottom of the card. */
function CardFooter({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="card-footer"
      className={cn(
        "flex items-center gap-sm px-xl group-data-[size=sm]/card:px-lg [.border-t]:pt-xl group-data-[size=sm]/card:[.border-t]:pt-lg",
        className,
      )}
      {...props}
    />
  )
}

export { Card, CardAction, CardContent, CardDescription, CardFooter, CardHeader, CardTitle }
export type { CardProps }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Card

A raised surface grouping related content, with an optional header, body and footer.

renderours

The element to render instead of a <div>. Give it <a href="…" /> to make the whole card a link.

Type RenderProp

sizeours

Padding and gaps. sm suits dense panels.

Type "sm" | "md" · Default md

CardAction

A control in the header's top-right corner.

Takes the props of a native div.

CardContent

The card's body.

Takes the props of a native div.

CardDescription

A line under the title.

Takes the props of a native div.

CardFooter

Actions or notes at the bottom of the card.

Takes the props of a native div.

CardHeader

The card's title, description and an optional action beside them.

Takes the props of a native div.

CardTitle

Names the card.

Takes the props of a native div.