# Table

Data table with pixel rules and a font-pixel header.

Source: https://www.ui95.dev/ui/table

## Examples

```tsx
import {
  Table,
  TableBody,
  TableCaption,
  TableCell,
  TableFooter,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table"

import type { ShowcaseDemo } from "../types"

const ROWS = [
  { name: "Alpha", status: "Ready", qty: "12" },
  { name: "Beta", status: "Busy", qty: "7" },
  { name: "Gamma", status: "Idle", qty: "3" },
] as const

export const demo: ShowcaseDemo = {
  span: "wide",
  variants: [
    {
      label: "default",
      render: () => (
        <Table>
          <TableCaption>Units in stock</TableCaption>
          <TableHeader>
            <TableRow>
              <TableHead>Name</TableHead>
              <TableHead>Status</TableHead>
              <TableHead>Qty</TableHead>
            </TableRow>
          </TableHeader>
          <TableBody>
            {ROWS.map((row) => (
              <TableRow key={row.name} data-state={row.name === "Beta" ? "selected" : undefined}>
                <TableCell>{row.name}</TableCell>
                <TableCell>{row.status}</TableCell>
                <TableCell>{row.qty}</TableCell>
              </TableRow>
            ))}
          </TableBody>
          <TableFooter>
            <TableRow>
              <TableCell colSpan={2}>Total</TableCell>
              <TableCell>22</TableCell>
            </TableRow>
          </TableFooter>
        </Table>
      ),
    },
  ],
}
```

## Installation

```bash
npx shadcn@latest add https://www.ui95.dev/r/table.json
```

Run it in a Radix shadcn project that already has the UI95 theme.

Dependencies:

```bash
npm install cn
```

`components/ui/table.tsx`

```tsx
"use client"

import { cn } from "cn"
import * as React from "react"

function Table({ className, ...props }: React.ComponentProps<"table">) {
  return (
    <div
      data-slot="table-container"
      className="relative w-full overflow-x-auto"
    >
      <table
        data-slot="table"
        className={cn("w-full caption-bottom text-lg leading-tight", className)}
        {...props}
      />
    </div>
  )
}

function TableHeader({ className, ...props }: React.ComponentProps<"thead">) {
  return (
    <thead
      data-slot="table-header"
      className={cn("[&_tr]:border-b-(length:--pixel) [&_tr]:border-b-border", className)}
      {...props}
    />
  )
}

function TableBody({ className, ...props }: React.ComponentProps<"tbody">) {
  return (
    <tbody
      data-slot="table-body"
      className={cn("[&_tr:last-child]:border-b-0", className)}
      {...props}
    />
  )
}

function TableFooter({ className, ...props }: React.ComponentProps<"tfoot">) {
  return (
    <tfoot
      data-slot="table-footer"
      className={cn(
        "border-t-(length:--pixel) border-t-border bg-muted 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-(length:--pixel) border-b-border hover:bg-muted has-aria-expanded:bg-muted data-[state=selected]:bg-foreground data-[state=selected]:text-card",
        className
      )}
      {...props}
    />
  )
}

function TableHead({ className, ...props }: React.ComponentProps<"th">) {
  return (
    <th
      data-slot="table-head"
      className={cn(
        "h-10 px-2 text-left align-middle font-pixel text-sm whitespace-nowrap text-foreground uppercase [&: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-lg text-muted-foreground", className)}
      {...props}
    />
  )
}

export {
  Table,
  TableBody,
  TableCaption,
  TableCell,
  TableFooter,
  TableHead,
  TableHeader,
  TableRow,
}
```

## API reference

### Table

No props of its own. It forwards the props of the element or primitive it wraps.

### TableBody

No props of its own. It forwards the props of the element or primitive it wraps.

### TableCaption

No props of its own. It forwards the props of the element or primitive it wraps.

### TableCell

No props of its own. It forwards the props of the element or primitive it wraps.

### TableFooter

No props of its own. It forwards the props of the element or primitive it wraps.

### TableHead

No props of its own. It forwards the props of the element or primitive it wraps.

### TableHeader

No props of its own. It forwards the props of the element or primitive it wraps.

### TableRow

No props of its own. It forwards the props of the element or primitive it wraps.
