Skip to content

Responsive table

Download .md

A responsive table shows records in a table on wide screens and as an accordion on phones, where a table would need sideways scrolling.

  • Use it for any list page that must work on phones, such as releases or payouts.
  • Keep the Table as it is from md up; below it, each row becomes one Accordion item.
  • Use a list of Item rows instead when a row has only two or three facts.
  1. From md up: the table, inside a card, with the row action at the end of each row.
  2. Below md: one accordion item per row. The trigger holds the main column (the title) and the status badge; the content holds the other columns as label–value pairs and the row action.
  • Same data, same actions. Every column and every action of the table is still there in the accordion.
  • Only one is shown. Hide the other with hidden / md:hidden, so screen readers never hear the records twice.
  • Trigger: the title and the status, nothing else, so the closed list stays easy to scan.
  • No sideways scrolling at 390px.
import { MoreHorizontalIcon } from "lucide-react"
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
const releases = [
{ title: "Senja di Kota", artist: "Rara Senandika", status: "success", label: "Terbit", date: "14 Nov 2026" },
{ title: "Kaset Pertama", artist: "Bima Laras", status: "warning", label: "Menunggu", date: "21 Nov 2026" },
{ title: "Pulang Sore", artist: "Nada Kirana", status: "info", label: "Dijadwalkan", date: "28 Nov 2026" },
{ title: "Hujan Bulan Juni", artist: "Arka Wening", status: "success", label: "Terbit", date: "3 Nov 2026" },
{ title: "Lampu Kota", artist: "Sinta Maharani", status: "destructive", label: "Ditolak", date: "2 Nov 2026" },
] as const
function RowActions({ title }: { title: string }) {
return (
<DropdownMenu>
<DropdownMenuTrigger render={<Button variant="ghost" size="icon-sm" aria-label={`Aksi untuk ${title}`} />}>
<MoreHorizontalIcon />
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem>Lihat</DropdownMenuItem>
<DropdownMenuItem>Ubah</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem variant="destructive">Hapus</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)
}
/** A table from md up; below it every row becomes an accordion item. Only one of them is ever shown. */
export default function ResponsiveTableDemo() {
return (
<div className="min-h-svh bg-background p-4 text-foreground md:p-6">
<div className="hidden rounded-xl border border-border bg-card md:block">
<Table>
<TableHeader>
<TableRow>
<TableHead>Judul</TableHead>
<TableHead>Artis</TableHead>
<TableHead>Status</TableHead>
<TableHead>Tanggal</TableHead>
<TableHead className="w-12">
<span className="sr-only">Aksi</span>
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{releases.map((r) => (
<TableRow key={r.title}>
<TableCell className="font-medium">{r.title}</TableCell>
<TableCell className="text-muted-foreground">{r.artist}</TableCell>
<TableCell>
<Badge variant={r.status}>{r.label}</Badge>
</TableCell>
<TableCell className="text-muted-foreground">{r.date}</TableCell>
<TableCell>
<RowActions title={r.title} />
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
<Accordion className="rounded-xl border border-border bg-card px-4 md:hidden">
{releases.map((r) => (
<AccordionItem key={r.title} value={r.title}>
<AccordionTrigger>
<span className="flex flex-1 items-center justify-between gap-2">
<span>{r.title}</span>
<Badge variant={r.status}>{r.label}</Badge>
</span>
</AccordionTrigger>
<AccordionContent>
<dl className="grid grid-cols-[auto_1fr] gap-x-4 gap-y-1">
<dt className="text-muted-foreground">Artis</dt>
<dd>{r.artist}</dd>
<dt className="text-muted-foreground">Tanggal</dt>
<dd>{r.date}</dd>
</dl>
<div className="mt-2 flex justify-end">
<RowActions title={r.title} />
</div>
</AccordionContent>
</AccordionItem>
))}
</Accordion>
</div>
)
}

Table, Accordion, Badge and Dropdown Menu.

Use a LayoutBuilder: from 768 logical pixels show ShadTable.list; below it, a ShadAccordion with one item per row.