Skip to main content
BlocksTablesExpandable shipment table with nested legs

shadcn/ui Tables block for Base UIExpandable shipment table with nested legs

A shadcn/ui table where each parent row expands to reveal a nested sub-table of child records. Built with the shadcn/ui Table, Collapsible and Button components, it keeps a fixed column grid so parent and child rows stay aligned. Ideal for hierarchical data such as shipments and their legs, projects and tasks, or orders and line items.

Free@shadcnpro/base/table-01

Preview

Installation

npx shadcn@latest add @shadcnpro/base/table-01

First time? Add the @shadcnpro registry to components.json as shown in Installation. Free blocks install without a license key.

Let your AI editor install it

The shadcn/ui Pro MCP server lets Cursor, Claude Code and VS Code find and install blocks like Expandable shipment table with nested legs from one prompt. It is included in every Pro plan.

Add the shadcn/ui components

Expandable shipment table with nested legs uses Button, Collapsible and Table, from the Base UI components set up in your components.json.

npx shadcn@latest add button collapsible table

Install the npm packages

npm install lucide-react

Copy the code

Paste it into components/blocks/table-01.tsx.

components/blocks/table-01.tsx
'use client';

import { ChevronDownIcon, ChevronRightIcon } from 'lucide-react';
import { useState } from 'react';
import { Button } from '@/components/ui/button';
import { Collapsible, CollapsibleContent } from '@/components/ui/collapsible';
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from '@/components/ui/table';
import { cn } from '@/lib/utils';

interface ShipmentRow {
  id: string;
  name: string;
  service: string;
  cost: number;
  dispatch: string;
  legs?: ShipmentRow[];
}

const shipments: ShipmentRow[] = [
  {
    id: 'HL-4102',
    name: 'Nordic Retail Consolidation',
    service: 'Ocean LCL',
    cost: 38_400,
    dispatch: '2025-02-03',
    legs: [
      {
        id: 'HL-4102-A',
        name: 'Oslo depot pickup',
        service: 'Ground',
        cost: 4_200,
        dispatch: '2025-02-03',
      },
      {
        id: 'HL-4102-B',
        name: 'Rotterdam transshipment',
        service: 'Ocean LCL',
        cost: 27_900,
        dispatch: '2025-02-08',
      },
      {
        id: 'HL-4102-C',
        name: 'Final mile to Bergen',
        service: 'Ground',
        cost: 6_300,
        dispatch: '2025-02-19',
      },
    ],
  },
  {
    id: 'HL-4117',
    name: 'Medical Devices Express',
    service: 'Air Priority',
    cost: 52_750,
    dispatch: '2025-02-05',
    legs: [
      {
        id: 'HL-4117-A',
        name: 'Basel cold-chain collection',
        service: 'Ground',
        cost: 5_150,
        dispatch: '2025-02-05',
      },
      {
        id: 'HL-4117-B',
        name: 'ZRH to ORD airfreight',
        service: 'Air Priority',
        cost: 41_600,
        dispatch: '2025-02-06',
      },
      {
        id: 'HL-4117-C',
        name: 'Chicago customs clearance',
        service: 'Brokerage',
        cost: 6_000,
        dispatch: '2025-02-07',
      },
    ],
  },
  {
    id: 'HL-4123',
    name: 'Iberian Auto Parts Run',
    service: 'Rail',
    cost: 21_900,
    dispatch: '2025-02-10',
    legs: [
      {
        id: 'HL-4123-A',
        name: 'Valencia to Lyon rail block',
        service: 'Rail',
        cost: 15_400,
        dispatch: '2025-02-10',
      },
      {
        id: 'HL-4123-B',
        name: 'Lyon cross-dock and delivery',
        service: 'Ground',
        cost: 6_500,
        dispatch: '2025-02-13',
      },
    ],
  },
  {
    id: 'HL-4131',
    name: 'Dublin Warehouse Restock',
    service: 'Ground',
    cost: 9_650,
    dispatch: '2025-02-12',
  },
];

const GRID_COLS = 'grid grid-cols-[40px_110px_220px_110px_110px_110px]';

interface AccordionRowProps {
  row: ShipmentRow;
  defaultOpen?: boolean;
}

function AccordionRow({ row, defaultOpen = false }: AccordionRowProps) {
  const [isOpen, setIsOpen] = useState(defaultOpen);
  const hasLegs = Boolean(row.legs && row.legs.length > 0);

  return (
    <Collapsible
      onOpenChange={setIsOpen}
      open={isOpen}
      render={
        <TableBody className="[&_tr:last-child]:border-b last:[&_tr:last-child]:border-0" />
      }
    >
      <TableRow
        className={cn(
          GRID_COLS,
          'bg-muted/50 hover:bg-muted/50',
          isOpen && 'border-b-0'
        )}
      >
        <TableCell className="p-0">
          <Button
            aria-label={isOpen ? 'Collapse shipment' : 'Expand shipment'}
            className={cn(
              'h-full w-full rounded-none p-3 text-muted-foreground transition-colors',
              hasLegs && 'hover:bg-transparent hover:text-foreground',
              !hasLegs && 'cursor-default opacity-30'
            )}
            disabled={!hasLegs}
            onClick={() => setIsOpen(!isOpen)}
            size="icon"
            variant="ghost"
          >
            {hasLegs ? (
              isOpen ? (
                <ChevronDownIcon className="size-4 transition-transform duration-200" />
              ) : (
                <ChevronRightIcon className="size-4 transition-transform duration-200" />
              )
            ) : (
              <div className="size-4" />
            )}
          </Button>
        </TableCell>
        <TableCell className="p-3 font-medium font-mono text-muted-foreground text-sm">
          {row.id}
        </TableCell>
        <TableCell className="truncate p-3 font-medium text-sm">
          {row.name}
        </TableCell>
        <TableCell className="p-3 text-muted-foreground text-sm">
          {row.service}
        </TableCell>
        <TableCell className="p-3 text-right font-mono font-semibold text-sm tabular-nums">
          ${row.cost.toLocaleString('en-US')}
        </TableCell>
        <TableCell className="p-3 text-muted-foreground text-sm">
          {row.dispatch}
        </TableCell>
      </TableRow>

      {hasLegs && (
        <TableRow className={cn(GRID_COLS, 'border-b-0 hover:bg-transparent')}>
          <TableCell className="col-span-6 p-0" colSpan={6}>
            <CollapsibleContent>
              <div className="w-full border-border border-b bg-muted/20">
                <Table>
                  <TableHeader>
                    <TableRow
                      className={cn(GRID_COLS, 'border-b-0 bg-muted/30')}
                    >
                      <TableHead className="flex h-7 items-center border-border border-y px-3 py-1.5" />
                      <TableHead className="flex h-7 items-center border-border border-y px-3 py-1.5 text-xs">
                        Leg
                      </TableHead>
                      <TableHead className="flex h-7 items-center border-border border-y px-3 py-1.5 text-xs">
                        Description
                      </TableHead>
                      <TableHead className="flex h-7 items-center border-border border-y px-3 py-1.5 text-xs">
                        Service
                      </TableHead>
                      <TableHead className="flex h-7 items-center justify-end border-border border-y px-3 py-1.5 text-right text-xs">
                        Cost
                      </TableHead>
                      <TableHead className="flex h-7 items-center border-border border-y px-3 py-1.5 text-xs">
                        Dispatch
                      </TableHead>
                    </TableRow>
                  </TableHeader>
                  <TableBody>
                    {row.legs?.map((leg) => (
                      <TableRow className={GRID_COLS} key={leg.id}>
                        <TableCell className="px-3 py-2" />
                        <TableCell className="px-3 py-2 font-mono text-muted-foreground text-xs tabular-nums">
                          {leg.id}
                        </TableCell>
                        <TableCell className="truncate px-3 py-2 font-medium text-xs">
                          {leg.name}
                        </TableCell>
                        <TableCell className="px-3 py-2 text-muted-foreground text-xs">
                          {leg.service}
                        </TableCell>
                        <TableCell className="px-3 py-2 text-right font-mono font-semibold text-xs tabular-nums">
                          ${leg.cost.toLocaleString('en-US')}
                        </TableCell>
                        <TableCell className="px-3 py-2 text-muted-foreground text-xs">
                          {leg.dispatch}
                        </TableCell>
                      </TableRow>
                    ))}
                  </TableBody>
                </Table>
              </div>
            </CollapsibleContent>
          </TableCell>
        </TableRow>
      )}
    </Collapsible>
  );
}

export default function Table01() {
  return (
    <div className="w-full max-w-5xl px-4">
      <div className="mx-auto max-w-fit overflow-hidden rounded-lg border border-border bg-card shadow-sm">
        <div className="overflow-x-auto">
          <Table>
            <TableHeader>
              <TableRow className={cn(GRID_COLS, 'bg-muted/50')}>
                <TableHead className="p-3" />
                <TableHead className="p-3 font-semibold text-foreground text-sm">
                  Ref
                </TableHead>
                <TableHead className="p-3 font-semibold text-foreground text-sm">
                  Shipment
                </TableHead>
                <TableHead className="p-3 font-semibold text-foreground text-sm">
                  Service
                </TableHead>
                <TableHead className="p-3 text-right font-semibold text-foreground text-sm">
                  Freight cost
                </TableHead>
                <TableHead className="p-3 font-semibold text-foreground text-sm">
                  Dispatch
                </TableHead>
              </TableRow>
            </TableHeader>
            {shipments.map((row, index) => (
              <AccordionRow defaultOpen={index === 0} key={row.id} row={row} />
            ))}
          </Table>
        </div>
      </div>
    </div>
  );
}

Use it

Import it where you need it and update the import path to match your project.

Usage
import Table01 from '@/components/blocks/table-01';

export default function Page() {
  return <Table01 />;
}

Overview

Expandable shipment table with nested legs is a free tables block for shadcn/ui and Base UI. It is built from the Button, Collapsible and Table components with lucide-react, and installs as a single file you own and can edit.

It only uses semantic tokens such as bg-background, text-muted-foreground and border-border, so it follows your theme, radius and fonts and switches with dark mode. It is responsive from desktop to mobile; the width controls above the preview show each size. The same layout is in the shadcn/ui Pro Figma kit, so design and code start from one source.

Category
Tables
Component library
Base UI (@base-ui/react)
Also available for
Radix UI, React Aria
Built with
Button, Collapsible and Table
npm packages
lucide-react
Files
components/blocks/table-01.tsx
Access
Free, installs without a license key

Frequently asked questions

Pricing, installation and Base UI details for Expandable shipment table with nested legs.