コンテンツへスキップ

カード

ダッシュボード、価格プラン、製品一覧用の枠付きボックスです。ヘッダー、コンテンツ、フッターには、ネオブルータリズム風のハードシャドウ効果が施されています。

import { Button } from "@/components/ui/button"
import {
  Card,

Card は、関連コンテンツを自己完結したボックスにまとめます。任意のヘッダー、コンテンツ、フッターのスロットがあります。下層にプリミティブはなく、プレーンなマークアップをネオブルータリストのレシピに沿ってスタイルしています。太い枠線、硬いオフセットの影、見出しフォントのタイトルです。

次のような場面で使います:

  • ダッシュボードのパネル — 統計タイル、チャート、アクティビティフィードをグリッドに並べます。
  • 料金プラン — ヘッダーにプラン名と価格、本文に機能、フッターに購入ボタン。
  • 独立したフォーム — ログイン、サインアップ、設定など、視覚的な境界が必要なボックス。

インストール

pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/card.json
npx shadcn@latest add https://neobrutalism.com/r/base/card.json
yarn dlx shadcn@latest add https://neobrutalism.com/r/base/card.json
bunx --bun shadcn@latest add https://neobrutalism.com/r/base/card.json

以下のコードをコピーして、プロジェクトに貼り付けます。

components/ui/card.tsx
import * as React from "react"

import { cn } from "@/lib/utils"

function Card({
  className,
  size = "default",
  ...props
}: React.ComponentProps<"div"> & { size?: "default" | "sm" }) {
  return (
    <div
      data-slot="card"
      data-size={size}
      className={cn(
        "group/card flex flex-col gap-(--card-spacing) overflow-hidden rounded border-2 bg-card py-(--card-spacing) text-sm text-card-foreground shadow-md [--card-spacing:--spacing(4)] has-data-[slot=card-footer]:pb-0 has-[>img:first-child]:pt-0 data-[size=sm]:[--card-spacing:--spacing(3)] data-[size=sm]:has-data-[slot=card-footer]:pb-0 *:[img:first-child]:rounded-t-xl *:[img:last-child]:rounded-b-xl",
        className
      )}
      {...props}
    />
  )
}

function CardHeader({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="card-header"
      className={cn(
        "group/card-header @container/card-header grid auto-rows-min items-start gap-1 rounded-t-xl px-(--card-spacing) has-data-[slot=card-action]:grid-cols-[1fr_auto] has-data-[slot=card-description]:grid-rows-[auto_auto] [.border-b]:pb-(--card-spacing)",
        className
      )}
      {...props}
    />
  )
}

function CardTitle({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="card-title"
      className={cn(
        "cn-font-heading text-base leading-snug font-medium group-data-[size=sm]/card:text-sm",
        className
      )}
      {...props}
    />
  )
}

function CardDescription({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="card-description"
      className={cn("text-sm text-muted-foreground", className)}
      {...props}
    />
  )
}

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}
    />
  )
}

function CardContent({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="card-content"
      className={cn("px-(--card-spacing)", className)}
      {...props}
    />
  )
}

function CardFooter({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="card-footer"
      className={cn(
        "flex items-center rounded-b-xl border-t-2 bg-muted/50 p-(--card-spacing)",
        className
      )}
      {...props}
    />
  )
}

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

インポートのパスをプロジェクトの構成に合わせて更新します。

使い方

import {
  Card,
  CardAction,
  CardContent,
  CardDescription,
  CardFooter,
  CardHeader,
  CardTitle,
} from "@/components/ui/card"
<Card>
  <CardHeader>
    <CardTitle>Card Title</CardTitle>
    <CardDescription>Card Description</CardDescription>
    <CardAction>Card Action</CardAction>
  </CardHeader>
  <CardContent>
    <p>Card Content</p>
  </CardContent>
  <CardFooter>
    <p>Card Footer</p>
  </CardFooter>
</Card>

コンポジション

Card を構築するには、次のコンポジションを使用します。

Card
├── CardHeader
│   ├── CardTitle
│   ├── CardDescription
│   └── CardAction
├── CardContent
└── CardFooter

使用例

サイズ

size="sm" プロパティを使用すると、カードのサイズを小さく設定できます。小サイズのバリアントでは、より狭い間隔が使用されます。

import { ChevronRightIcon } from "lucide-react"

import { Button } from "@/components/ui/button"

間隔

size プロパティに加えて、--card-spacing CSS 変数を使用して、セクション間の間隔やカード各部のインセットを制御できます。

"use client"

import * as React from "react"

-mx-(--card-spacing) で負のマージンを使用すると、カードのインセットに揃えたまま、コンテンツを端から端まで広げられます。端から端まで広がるコンテンツがフッターの上に配置される場合は、CardContent-mb-(--card-spacing) を適用して、セクション間の余白を削除します。

import { Button } from "@/components/ui/button"
import {
  Card,

画像

カードのヘッダーの前に画像を追加すると、画像付きのカードを作成できます。

import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import {

RTL

Neobrutalism で RTL を有効にする方法は、RTL 設定ガイドをご覧ください。

"use client"

import * as React from "react"

アクセシビリティ

Card は見た目専用です。各パーツは暗黙の role のないプレーンな <div> を描画するため、アクセシビリティツリーには何も足しません。CardTitle は見出し要素ではないため、ドキュメントアウトラインに載せたい場合は中に本物の <h2><h4> を入れてください。クリック可能なカードでは、リンクやボタンをカード内に置き、ヒット領域を表面全体に広げます。カードの <div> 自体に onClick を付けないでください。

APIリファレンス

Card

Card コンポーネントは、カードコンテンツのルートコンテナーです。

PropTypeDefault
size"default" | "sm""default"
classNamestring-

CardHeader

CardHeader コンポーネントは、タイトル、説明、および任意のアクションに使用します。

PropTypeDefault
classNamestring-

CardTitle

CardTitle コンポーネントは、カードのタイトルに使用します。

PropTypeDefault
classNamestring-

CardDescription

CardDescription コンポーネントは、タイトルの下に表示する補助テキストに使用します。

PropTypeDefault
classNamestring-

CardAction

CardAction コンポーネントは、ヘッダーの右上にコンテンツ(例: ボタンやバッジ)を配置します。

PropTypeDefault
classNamestring-

CardContent

CardContent コンポーネントは、カードの本体に使用します。

PropTypeDefault
classNamestring-

CardFooter

CardFooter コンポーネントは、カード下部のアクションや補助的なコンテンツに使用します。

PropTypeDefault
classNamestring-

変更履歴

間隔変数

以前のバージョンの Card コンポーネントからアップグレードする場合は、--card-spacing 変数を使用するために、次の更新を適用する必要があります。

Card ルートの間隔クラスを更新します。

ハードコードされた gap と縦方向のパディングを --card-spacing に置き換え、ルートにデフォルトと小サイズの値を設定します。

  className={cn(
-   "group/card flex flex-col gap-4 overflow-hidden rounded-xl bg-card py-4 text-sm text-card-foreground ring-1 ring-foreground/10 has-data-[slot=card-footer]:pb-0 has-[>img:first-child]:pt-0 data-[size=sm]:gap-3 data-[size=sm]:py-3 data-[size=sm]:has-data-[slot=card-footer]:pb-0 *:[img:first-child]:rounded-t-xl *:[img:last-child]:rounded-b-xl",
+   "group/card flex flex-col gap-(--card-spacing) overflow-hidden rounded-xl bg-card py-(--card-spacing) text-sm text-card-foreground ring-1 ring-foreground/10 [--card-spacing:--spacing(4)] has-data-[slot=card-footer]:pb-0 has-[>img:first-child]:pt-0 data-[size=sm]:[--card-spacing:--spacing(3)] data-[size=sm]:has-data-[slot=card-footer]:pb-0 *:[img:first-child]:rounded-t-xl *:[img:last-child]:rounded-b-xl",
    className
  )}

CardHeader の間隔クラスを更新します。

横方向のパディングとボーダーの間隔を共有変数に置き換えます。

  className={cn(
-   "group/card-header @container/card-header grid auto-rows-min items-start gap-1 rounded-t-xl px-4 group-data-[size=sm]/card:px-3 has-data-[slot=card-action]:grid-cols-[1fr_auto] has-data-[slot=card-description]:grid-rows-[auto_auto] [.border-b]:pb-4 group-data-[size=sm]/card:[.border-b]:pb-3",
+   "group/card-header @container/card-header grid auto-rows-min items-start gap-1 rounded-t-xl px-(--card-spacing) has-data-[slot=card-action]:grid-cols-[1fr_auto] has-data-[slot=card-description]:grid-rows-[auto_auto] [.border-b]:pb-(--card-spacing)",
    className
  )}

CardContent と CardFooter の間隔クラスを更新します。

コンテンツのインセットとフッターのパディングには --card-spacing を使用します。

  function CardContent({ className, ...props }: React.ComponentProps<"div">) {
    return (
      <div
        data-slot="card-content"
-       className={cn("px-4 group-data-[size=sm]/card:px-3", className)}
+       className={cn("px-(--card-spacing)", className)}
        {...props}
      />
    )
  }
  className={cn(
-   "flex items-center rounded-b-xl border-t bg-muted/50 p-4 group-data-[size=sm]/card:p-3",
+   "flex items-center rounded-b-xl border-t bg-muted/50 p-(--card-spacing)",
    className
  )}

これらの変更を適用すると、任意のプロパティクラスで Card--card-spacing を設定して、カードの間隔をカスタマイズできます。

function Example() {
  return <Card className="[--card-spacing:--spacing(6)]">...</Card>
}