# Column Visibility Menu

> Columns dropdown for data tables with a checkbox item per column. It returns the updated list of visible column ids so you can show or hide table columns.

- Kind: Component (Tables)
- Docs: https://ui.minidev.pro/docs/column-visibility-menu
- Source (registry JSON with the full file): https://ui.minidev.pro/r/column-visibility-menu.json
- License: MIT. One file, yours to edit after install.

## When to use

Put it in a TableToolbar above a DataTable, then filter your DataTable `columns` by the visible ids. Ids always come back in column order. For a full saved-view system use SavedViews.

## Installation

### shadcn CLI (copies the source into your project)

```bash
npx shadcn@latest add @minidev/column-visibility-menu
```

Writes `components/ui/column-visibility-menu.tsx` plus the MiniDev files it is built from (button, dropdown-menu) and installs its npm dependencies. Older CLI versions: `npx shadcn@latest add https://ui.minidev.pro/r/column-visibility-menu.json`.

### npm package

```bash
npm i minidev-ui-kit lucide-react
```

Add the tokens to your global stylesheet once:

```css
@import "tailwindcss";
@import "minidev-ui-kit/styles.css";
@source "../node_modules/minidev-ui-kit";
```

Next.js also needs `transpilePackages: ["minidev-ui-kit"]` in next.config.

## Import

After the shadcn install:

```tsx
import { ColumnVisibilityMenu } from "@/components/ui/column-visibility-menu"
```

From the npm package:

```tsx
import { ColumnVisibilityMenu } from "minidev-ui-kit/ui/column-visibility-menu"
```

## Usage

```tsx
import { ColumnVisibilityMenu } from "@/components/ui/column-visibility-menu"

export function Example() {
  return (
    <ColumnVisibilityMenu />
  )
}
```

## Props

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `columns` | `Column[]` | `SAMPLE_COLUMNS` | no | Every toggleable column, in table order. |
| `visible` | `string[]` | - | no | Controlled list of visible column ids. Leave undefined for an uncontrolled menu. |
| `defaultVisible` | `string[]` | - | no | Initial visible ids for the uncontrolled menu. Defaults to every column. |
| `onChange` | `(ids: string[]) => void` | - | no | Called with the visible ids, in column order, after each toggle. |
| `label` | `string` | `"Columns"` | no | Trigger button text. Defaults to "Columns". |
| `className` | `string` | - | no | Classes for the trigger button. |

## Examples

### Controlling DataTable columns

Keep the visible ids in state and filter the DataTable columns with them; Method starts hidden.

```tsx
type Payout = { id: string; account: string; method: string; arrives: string; amount: number }

const payouts: Payout[] = [
  { id: "po_31", account: "Northwind Labs", method: "SEPA", arrives: "Nov 17", amount: 12400 },
  { id: "po_30", account: "Halcyon Health", method: "ACH", arrives: "Nov 18", amount: 3890 },
  { id: "po_29", account: "Fathom Analytics", method: "Wire", arrives: "Nov 20", amount: 21750 },
]

const payoutColumns: Column<Payout>[] = [
  { id: "account", header: "Account", cell: (p) => p.account },
  { id: "method", header: "Method", cell: (p) => p.method },
  { id: "arrives", header: "Arrives", cell: (p) => p.arrives },
  { id: "amount", header: "Amount", align: "right", cell: (p) => "$" + p.amount.toLocaleString("en-US") },
]

function PayoutsWithColumns() {
  const [visible, setVisible] = React.useState(["account", "arrives", "amount"])
  return (
    <div className="flex w-full max-w-xl flex-col gap-3">
      <div className="flex justify-end">
        <ColumnVisibilityMenu
          columns={payoutColumns.map((c) => ({ id: c.id, label: String(c.header) }))}
          visible={visible}
          onChange={setVisible}
        />
      </div>
      <DataTable columns={payoutColumns.filter((c) => visible.includes(c.id))} data={payouts} getRowId={(p) => p.id} caption="Payouts" />
    </div>
  )
}
```

### Uncontrolled with a custom label

defaultVisible sets the first state and the menu tracks changes itself.

```tsx
<ColumnVisibilityMenu
  label="Display"
  columns={[
    { id: "name", label: "Customer" },
    { id: "plan", label: "Plan" },
    { id: "mrr", label: "MRR" },
    { id: "seats", label: "Seats" },
    { id: "renewal", label: "Renewal date" },
  ]}
  defaultVisible={["name", "plan", "mrr"]}
/>
```

## Keyboard interactions

| Key | Action |
| --- | --- |
| `Enter` or `Space` or `ArrowDown` | On the Columns button, opens the menu. |
| `ArrowDown` or `ArrowUp` | Moves between the columns, wrapping at the ends. |
| `Home` or `End` | Moves to the first or last column. |
| `Enter` or `Space` | Shows or hides the focused column. The menu stays open. |
| `Escape` | Closes the menu and returns focus to the Columns button. |

## Accessibility

- Each column is a Base UI checkbox item with `role="menuitemcheckbox"` and `aria-checked`.
- The "Toggle columns" label is a Base UI group label, so the group of checkboxes is named by it.
- The columns icon on the trigger is `aria-hidden`, so the button's name is the `label` text, "Columns" by default.

## FAQ

### How do I connect it to DataTable?

Keep the visible ids in state with `visible={visible} onChange={setVisible}`, then pass `columns={columns.filter((c) => visible.includes(c.id))}` to `DataTable`. The ids always come back in the order of `columns`, whatever order they were clicked in, so the table never reorders.

### How do I remember the chosen columns between visits?

The menu stores nothing. Save the array from `onChange`, for example in `localStorage` or the user's settings, and pass it back as `visible`. `defaultVisible` is only read on the first render, so it cannot load a value that arrives later.

### Can I stop people hiding a required column such as Name?

There is no per-item disabled option. Leave that column out of the menu's `columns` and always keep it in your filter: `columns.filter((c) => c.id === "name" || visible.includes(c.id))`.

### Does the menu close after each toggle?

No. Base UI checkbox items keep the menu open (`closeOnClick` is false), so people can toggle several columns in one go. Escape or a click outside closes it.

## Rules for generating code

- Use semantic tokens only (bg, surface, sunken, border, fg, fg-muted, fg-subtle, ink, accent, success, warning, danger). Never raw hex or palette steps like blue-500.
- Shadows only from the set: shadow-xs, shadow-sm, shadow-raised, shadow-md, shadow-lg, shadow-overlay.
- Numbers use tabular-nums. Dark mode is the `dark` class on <html>; components never branch on theme.

## Dependencies

- npm: `lucide-react`
- Built from: [button](https://ui.minidev.pro/docs/button/md), [dropdown-menu](https://ui.minidev.pro/docs/dropdown-menu/md)

## Related

- [Table Toolbar](https://ui.minidev.pro/docs/table-toolbar/md): Toolbar above a data table with a search input on the left and a right slot for filters, view toggles or export buttons. Wraps cleanly on narrow screens.
- [Data Table](https://ui.minidev.pro/docs/data-table/md): React data table with sortable columns, row selection, a floating bulk actions bar, hover row actions, sticky header, three densities and skeleton loading.
- [Saved Views](https://ui.minidev.pro/docs/saved-views/md): Saved views list for filters, rendered as a listbox of named views with item counts and a highlighted active view. Works controlled or uncontrolled in React.
- [Audit Filter Bar](https://ui.minidev.pro/docs/audit-filter-bar/md): Filter toolbar for audit logs combining a search field, actor filter chips for all, admins or system, and a date range picker in one wrapping row.
- [Audit Log Entry](https://ui.minidev.pro/docs/audit-log-entry/md): Single audit log line: actor avatar, actor name, muted action text and the affected target, with a relative timestamp underneath. Stacks into a trail.
- [Audit Trail](https://ui.minidev.pro/docs/audit-trail/md): Bordered audit trail in React that renders a list of actor, action, target and time records as stacked log entries. Useful for security and admin history pages.

---

All 557 components: https://ui.minidev.pro/llms.txt (full text: https://ui.minidev.pro/llms-full.txt).
