import {
Table,
TableBody,Table はネイティブ HTML テーブル要素(<table>、<thead>、<tbody>、<th>、<td>、<caption>)をスタイルします。下層にプリミティブライブラリはなく、Radix と Base のバリアントは同じファイルを同梱します。ネオブルータリストのレシピ:テーブル全体を囲む太い枠線と硬い影、見出しフォントのヘッダーセル、2px の行区切りです。
次のような場面で使います:
- 請求書と注文 — 明細行と、合計用の
TableFooter行。 - 管理一覧 — ユーザー、API キー、サブスクリプションと、行アクションのドロップダウン。
- 料金と比較グリッド — 列ヘッダーが意味を担う機能マトリクス。
インストール#
pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/table.jsonnpx shadcn@latest add https://neobrutalism.com/r/base/table.jsonyarn dlx shadcn@latest add https://neobrutalism.com/r/base/table.jsonbunx --bun shadcn@latest add https://neobrutalism.com/r/base/table.json
以下のコードをコピーしてプロジェクトに貼り付けてください。
"use client"
import * as React from "react"
import { cn } from "@/lib/utils"
function Table({ className, ...props }: React.ComponentProps<"table">) {
return (
<div
data-slot="table-container"
className="relative w-full overflow-x-auto rounded border-2 shadow-md"
>
<table
data-slot="table"
className={cn("w-full caption-bottom text-sm", className)}
{...props}
/>
</div>
)
}
function TableHeader({ className, ...props }: React.ComponentProps<"thead">) {
return (
<thead
data-slot="table-header"
className={cn("[&_tr]:border-b-2", className)}
{...props}
/>
)
}
function TableBody({ className, ...props }: React.ComponentProps<"tbody">) {
return (
<tbody
data-slot="table-body"
className={cn("[&_tr:last-child]:border-0", className)}
{...props}
/>
)
}
function TableFooter({ className, ...props }: React.ComponentProps<"tfoot">) {
return (
<tfoot
data-slot="table-footer"
className={cn(
"border-t-2 bg-muted/50 font-medium [&>tr]:last:border-b-0",
className
)}
{...props}
/>
)
}
function TableRow({ className, ...props }: React.ComponentProps<"tr">) {
return (
<tr
data-slot="table-row"
className={cn(
"border-b-2 transition-colors hover:bg-accent has-aria-expanded:bg-accent data-[state=selected]:bg-accent",
className
)}
{...props}
/>
)
}
function TableHead({ className, ...props }: React.ComponentProps<"th">) {
return (
<th
data-slot="table-head"
className={cn(
"h-10 bg-muted px-2 text-left align-middle font-head font-medium whitespace-nowrap text-foreground [&:has([role=checkbox])]:pr-0",
className
)}
{...props}
/>
)
}
function TableCell({ className, ...props }: React.ComponentProps<"td">) {
return (
<td
data-slot="table-cell"
className={cn(
"p-2 align-middle whitespace-nowrap [&:has([role=checkbox])]:pr-0",
className
)}
{...props}
/>
)
}
function TableCaption({
className,
...props
}: React.ComponentProps<"caption">) {
return (
<caption
data-slot="table-caption"
className={cn("mt-4 text-sm text-muted-foreground", className)}
{...props}
/>
)
}
export {
Table,
TableHeader,
TableBody,
TableFooter,
TableHead,
TableRow,
TableCell,
TableCaption,
}
インポートパスをプロジェクトの構成に合わせて更新してください。
使い方#
import {
Table,
TableBody,
TableCaption,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table"<Table>
<TableCaption>A list of your recent invoices.</TableCaption>
<TableHeader>
<TableRow>
<TableHead className="w-[100px]">Invoice</TableHead>
<TableHead>Status</TableHead>
<TableHead>Method</TableHead>
<TableHead className="text-right">Amount</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow>
<TableCell className="font-medium">INV001</TableCell>
<TableCell>Paid</TableCell>
<TableCell>Credit Card</TableCell>
<TableCell className="text-right">$250.00</TableCell>
</TableRow>
</TableBody>
</Table>コンポジション#
以下のコンポジションを使って Table を構築します。
Table
├── TableCaption
├── TableHeader
│ └── TableRow
│ ├── TableHead
│ ├── TableHead
│ ├── TableHead
│ └── TableHead
├── TableBody
│ ├── TableRow
│ │ ├── TableCell
│ │ ├── TableCell
│ │ ├── TableCell
│ │ └── TableCell
│ └── TableRow
│ ├── TableCell
│ ├── TableCell
│ ├── TableCell
│ └── TableCell
└── TableFooter使用例#
フッター#
<TableFooter /> コンポーネントを使って、テーブルにフッターを追加します。
import {
Table,
TableBody,アクション#
<DropdownMenu /> コンポーネントを使って、各行にアクションを表示するテーブルです。
import { MoreHorizontalIcon } from "lucide-react"
import { Button } from "@/components/ui/button"Data Table#
<Table /> コンポーネントを使って、より複雑なデータテーブルを構築できます。@tanstack/react-table と組み合わせれば、並べ替え・フィルタリング・ページネーションを備えたテーブルを作成できます。
詳しくは Data Table のドキュメントをご覧ください。
Tasks のデモでも、データテーブルの例を確認できます。
RTL#
Neobrutalism で RTL を有効にする方法は、RTL 設定ガイドをご覧ください。
"use client"
import * as React from "react"アクセシビリティ#
ネイティブのテーブル要素を描画するため、スクリーンリーダーは ARIA 配線なしでテーブル読み上げ、行/列ナビ、ヘッダー関連付けを得ます。各テーブルに TableCaption で名前を付け、TableHead は本物のヘッダーセルに限定してください。スクロールコンテナはフォーカス可能ではないため、キーボードのみのユーザーは溢れたテーブルをスクロールできません。幅の広いテーブルが想定される場合は、コンポーネントのコピー側でコンテナ div に tabIndex={0}、role="region"、aria-label を追加してください。