# ColumnManager

Panel where the user picks the columns of a table: which ones are shown, in which order and which ones are pinned on the left. It edits the same `columnState` bound to `Table`, live, so the table behind shows every change at once; "Save view" calls `onsave({ columnState })` (save it there, for example in the user's settings) and closes, while closing in any other way (Escape, the backdrop, the close button, `open = false`) puts back the state the panel opened with. "Reset" goes back to the default view of `columns` (order, visibility, pins and widths). Visible columns are reordered by dragging their handle (mouse or touch) or with the up and down arrows on it; pinned columns stay first and are reordered among themselves; `required` columns and the last visible one cannot be hidden. Hidden columns are listed under "Available" with their icon, the name of their type and their `description` (or `info`). With more than 7 columns a search field filters both lists by name, type and description. It is a `Drawer` on the right, a bottom sheet below 640px. While a column is dragged it gets `data-dragging`; pinned ones have `data-pinned`.

```ts
import { ColumnManager } from '@likable-hair/svelte';
```

Category: composed. Source: `src/lib/components/composed/tables/ColumnManager.svelte`.

## Examples

### Choose the columns

A "Columns" button opens the manager on the same `columnState` bound to the table: the table changes while the user edits, "Save view" calls `onsave` (here the app would store the view in the user's settings) and closing in any other way puts the table back as it was.

```svelte
<script lang="ts">
	import { mdiViewColumnOutline } from '@mdi/js';
	import { Button, ColumnManager, Table, type TableColumn, type TableColumnState } from '@likable-hair/svelte';

	type Appointment = {
		id: number;
		client: string;
		service: string;
		date: Date;
		amount: number;
		staff: string;
		duration: number;
	};

	const rows: Appointment[] = [
		{ id: 1, client: 'Luca Rossi', service: 'Cut', date: new Date(2026, 8, 24, 9, 30), amount: 28, staff: 'Giulia', duration: 30 },
		{ id: 2, client: 'Sara Conti', service: 'Colour', date: new Date(2026, 8, 24, 11), amount: 64, staff: 'Marco', duration: 90 },
		{ id: 3, client: 'Anna Galli', service: 'Blow-dry', date: new Date(2026, 8, 25, 15), amount: 22, staff: 'Giulia', duration: 45 }
	];

	const columns: TableColumn<Appointment>[] = [
		{ key: 'client', label: 'Client', pinned: true },
		{ key: 'service', label: 'Service' },
		{ key: 'date', label: 'Date', type: 'datetime' },
		{ key: 'amount', label: 'Amount', type: 'number', numberFormat: { style: 'currency', currency: 'EUR' } },
		{ key: 'staff', label: 'Staff', hidden: true },
		{ key: 'duration', label: 'Minutes', type: 'number', hidden: true }
	];

	let columnState = $state<TableColumnState[]>();
	let open = $state(false);
	let saved = $state<string>();
</script>

<div class="stack">
	<div class="toolbar">
		<Button variant="secondary" size="sm" icon={mdiViewColumnOutline} onclick={() => (open = true)}>
			Columns
		</Button>
	</div>
	<Table {rows} {columns} bind:columnState cardBreakpoint={false} />
	<code>{saved ?? 'No view saved yet'}</code>
</div>

<ColumnManager
	bind:open
	{columns}
	bind:columnState
	onsave={({ columnState }) => (saved = JSON.stringify(columnState.map(({ key, visible }) => (visible ? key : `-${key}`))))}
/>

<style>
	.stack {
		display: flex;
		width: 100%;
		max-width: 620px;
		flex-direction: column;
		gap: 12px;
	}

	.toolbar {
		display: flex;
		justify-content: flex-end;
	}

	code {
		color: var(--global-color-text-2);
		font-family: var(--global-font-family-mono);
		font-size: 12px;
		overflow-wrap: anywhere;
	}
</style>
```

### Many columns

With more than 7 columns a search field filters both lists by name, type and description. Each column shows the icon and the name of its `type` (a currency `numberFormat` reads "Currency"), or its own `icon`, and its `description` (or its `info`, the tooltip in the header). `required` columns have no button to hide them, and the last visible column cannot be hidden either.

```svelte
<script lang="ts">
	import { mdiAccountOutline, mdiListStatus, mdiPhoneOutline, mdiViewColumnOutline } from '@mdi/js';
	import { Button, ColumnManager, Table, type TableColumn, type TableColumnState } from '@likable-hair/svelte';

	type Appointment = {
		id: number;
		client: string;
		service: string;
		date: Date;
		amount: number;
		status: string;
		staff: string;
		duration: number;
		phone: string;
		lastVisit: Date;
		payment: string;
		location: string;
		notes: string;
	};

	const rows: Appointment[] = [
		{
			id: 1,
			client: 'Luca Rossi',
			service: 'Cut',
			date: new Date(2026, 8, 24, 9, 30),
			amount: 28,
			status: 'Confirmed',
			staff: 'Giulia',
			duration: 30,
			phone: '+39 333 123 4567',
			lastVisit: new Date(2026, 7, 20),
			payment: 'Card',
			location: 'Milan Brera',
			notes: 'Prefers mornings'
		},
		{
			id: 2,
			client: 'Sara Conti',
			service: 'Colour',
			date: new Date(2026, 8, 24, 11),
			amount: 64,
			status: 'Pending',
			staff: 'Marco',
			duration: 90,
			phone: '+39 347 765 4321',
			lastVisit: new Date(2026, 6, 2),
			payment: 'Cash',
			location: 'Turin Centre',
			notes: ''
		}
	];

	const columns: TableColumn<Appointment>[] = [
		{ key: 'client', label: 'Client', icon: mdiAccountOutline, description: 'Name of the client', required: true, pinned: true },
		{ key: 'service', label: 'Service', description: 'For example cut, colour, blow-dry' },
		{ key: 'date', label: 'Date', type: 'datetime', description: 'Day and time of the appointment' },
		{ key: 'amount', label: 'Amount', type: 'number', numberFormat: { style: 'currency', currency: 'EUR' }, description: 'Total with VAT' },
		{ key: 'status', label: 'Status', icon: mdiListStatus, description: 'Confirmed, pending or cancelled' },
		{ key: 'staff', label: 'Staff', icon: mdiAccountOutline, description: 'Who does the service', hidden: true },
		{ key: 'duration', label: 'Duration', type: 'number', description: 'Expected minutes', hidden: true },
		{ key: 'phone', label: 'Phone', icon: mdiPhoneOutline, description: 'Main contact of the client', hidden: true },
		{ key: 'lastVisit', label: 'Last visit', type: 'date', description: 'Last completed appointment', hidden: true },
		{ key: 'payment', label: 'Payment method', description: 'Cash, card or transfer', hidden: true },
		{ key: 'location', label: 'Location', description: 'Shop of the appointment', hidden: true },
		{ key: 'notes', label: 'Internal notes', description: 'Visible to the staff only', hidden: true }
	];

	let columnState = $state<TableColumnState[]>();
	let open = $state(false);
</script>

<div class="stack">
	<div class="toolbar">
		<Button variant="secondary" size="sm" icon={mdiViewColumnOutline} onclick={() => (open = true)}>
			Columns
		</Button>
	</div>
	<Table {rows} {columns} bind:columnState cardBreakpoint={false} />
</div>

<ColumnManager bind:open {columns} bind:columnState />

<style>
	.stack {
		display: flex;
		width: 100%;
		max-width: 620px;
		flex-direction: column;
		gap: 12px;
	}

	.toolbar {
		display: flex;
		justify-content: flex-end;
	}
</style>
```

### Texts, pins and style

`pinnable={false}` removes the pin buttons (columns pinned by `columns` stay pinned), `labels` changes any text on top of those of `locale`, and the `--column-manager-*` variables change the look of this instance only.

```svelte
<script lang="ts">
	import { mdiTableCog } from '@mdi/js';
	import { Button, ColumnManager, Table, type TableColumn, type TableColumnState } from '@likable-hair/svelte';

	type Product = { id: number; sku: string; name: string; price: number; stock: number; active: boolean };

	const rows: Product[] = [
		{ id: 1, sku: 'SH-001', name: 'Shampoo', price: 12.5, stock: 40, active: true },
		{ id: 2, sku: 'CO-014', name: 'Conditioner', price: 14, stock: 0, active: false }
	];

	const columns: TableColumn<Product>[] = [
		{ key: 'sku', label: 'Code', pinned: true },
		{ key: 'name', label: 'Product' },
		{ key: 'price', label: 'Price', type: 'number', numberFormat: { style: 'currency', currency: 'EUR' } },
		{ key: 'stock', label: 'Stock', type: 'number' },
		{ key: 'active', label: 'On sale', type: 'boolean' }
	];

	let columnState = $state<TableColumnState[]>();
	let open = $state(false);
</script>

<div class="stack">
	<div class="toolbar">
		<Button variant="secondary" size="sm" icon={mdiTableCog} onclick={() => (open = true)}>Layout</Button>
	</div>
	<Table {rows} {columns} bind:columnState cardBreakpoint={false} />
</div>

<ColumnManager
	bind:open
	{columns}
	bind:columnState
	pinnable={false}
	labels={{ title: 'Product list layout', save: 'Apply' }}
	--column-manager-width="320px"
	--column-manager-type-icon-background="var(--global-color-surface-2)"
	--column-manager-type-icon-color="var(--global-color-text-2)"
	--column-manager-item-border-radius="var(--global-radius-sm)"
/>

<style>
	.stack {
		display: flex;
		width: 100%;
		max-width: 520px;
		flex-direction: column;
		gap: 12px;
	}

	.toolbar {
		display: flex;
		justify-content: flex-end;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `open` (bindable) | `boolean` |  | Whether the panel is open. `undefined` counts as closed. |
| `columns` (required) | `TableColumn<Row>[]` |  | The columns of the table, the same passed to `Table`: their `label`, `type`, `icon` and `description` describe them in the panel, `required` ones cannot be hidden. |
| `columnState` (bindable) | `TableColumnState[]` |  | Order, visibility, widths and pins of the columns, the same bound to `Table`. It changes while the user edits, so the table shows each change at once; closing without saving puts back the state the panel opened with. Empty means the default view of `columns`. |
| `pinnable` | `boolean` | `true` | Lets the user pin columns on the left. Columns pinned by `columns` or by `columnState` stay pinned either way. |
| `searchable` | `boolean` |  | Shows a field that filters the columns by name, type and description. Defaults to on with more than 7 columns. |
| `locale` | `string` |  | BCP 47 locale of the texts: `it` and `en` are built in, other languages fall back to English. Defaults to the one set with `setDefaultLocale`, else `en`. |
| `labels` | `{ title?: string; count?: string; search?: string; visible?: string; reorderHint?: string; available?: string; availableCount?: string; availableCountOne?: string; results?: string; resultsOne?: string; noMatch?: string; allVisible?: string; reset?: string; save?: string; close?: string; move?: string; moveHint?: string; moved?: string; pin?: string; pinTip?: string; unpinTip?: string; pinned?: string; hide?: string; hideTip?: string; required?: string; lastVisible?: string; add?: string; typeText?: string; typeNumber?: string; typeCurrency?: string; typePercent?: string; typeDate?: string; typeDatetime?: string; typeBoolean?: string; }` |  | Texts to change, on top of those of `locale`. `{column}`, `{count}`, `{visible}`, `{total}`, `{position}` and `{query}` are replaced by the label of the column, a number of columns, the visible and the total columns, the new position of a moved column and the search text. |
| `drawerElement` (bindable) | `HTMLDialogElement` |  | The `<dialog>` element. |
| `class` | `{ drawer?: string; panel?: string; item?: string; card?: string; }` | `{}` | Extra classes for each part: `drawer` the `<dialog>`, `panel` the sliding panel, `item` the rows of the visible columns, `card` the cards of the available ones. |

It also accepts the native attributes and events of `<dialog>` (for example `id`, `title`, `aria-label`, `onkeydown`), which are forwarded to the element.

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `columnSnippet` | `{ column: TableColumn<Row>; visible: boolean; query: string }` | Content of a column in both lists, instead of its icon, its label, its type and its description. The handle and the buttons stay. |
| `emptySnippet` | `{ query: string }` | Replaces the text shown when the search matches no column. |
| `actionsSnippet` | `{ reset: () => void; save: () => void; cancel: () => void }` | Replaces the buttons at the bottom: `reset` goes back to the default view, `save` saves and closes, `cancel` closes without saving. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onsave` | `(event: { columnState: TableColumnState[] }) => void` | Called when the user saves, with the new state, just before the panel closes: save it here, for example in the user's settings on the backend. |
| `oncancel` | `() => void` | Called when the panel closes without saving, after `columnState` has been put back. |

## Types

Exported by the package: `import type { TableColumn, TableColumnState } from '@likable-hair/svelte';`

### TableColumn

A column of a `Table`: what it shows, how it formats, sorts and sizes it.

`interface TableColumn<Row = any>`

| Name | Type | Description |
| --- | --- | --- |
| `key` (required) | `string` | Unique key of the column. Without `value` it is also the property of the row that the column shows. |
| `label` | `string` | Text of the header. |
| `type` | `'text' \| 'number' \| 'date' \| 'datetime' \| 'boolean' \| 'custom'` | How the value is formatted and sorted: `text` as is, `number` with `Intl.NumberFormat` (right-aligned, monospaced digits), `date` and `datetime` with `Intl.DateTimeFormat` (a `Date`, an ISO string, a timestamp or an object with `toJSDate()` such as a luxon `DateTime`), `boolean` as a check mark when true, `custom` with the `cellSnippet` of the table (sorted as text). Defaults to `text`. |
| `value` | `(row: Row) => unknown` | Reads the value of the column from a row, for nested or computed values. Defaults to `row[key]`. |
| `format` | `(value: any, row: Row) => string` | Turns the value into the text of the cell, instead of the formatting of `type`. Not called for empty values. |
| `numberFormat` | `Intl.NumberFormatOptions` | Options of `Intl.NumberFormat` for `number` columns, for example `{ style: 'currency', currency: 'EUR' }`. |
| `dateFormat` | `Intl.DateTimeFormatOptions` | Options of `Intl.DateTimeFormat` for `date` and `datetime` columns. Defaults to the day, the month and the year in digits, plus hours and minutes for `datetime`. |
| `empty` | `string` | Text of an empty cell (`null`, `undefined`, an empty string). Defaults to the `empty` of the table. |
| `align` | `'start' \| 'center' \| 'end'` | Horizontal alignment of the header and the cells. Defaults to `end` for numbers, else `start`. |
| `width` | `string` | Preferred width of the column, any CSS length (`120px`, `20%`): columns still shrink to fit the table, `minWidth` keeps them wider and makes the table scroll. A width set by resizing wins. |
| `minWidth` | `string` | Minimum width of the column, any CSS length. |
| `maxWidth` | `string` | Maximum width of the column, any CSS length: longer text is cut with an ellipsis. |
| `sortable` | `boolean` | The header becomes a button that sorts by this column: ascending, descending, then unsorted. |
| `compare` | `(a: Row, b: Row) => number` | Compares two rows when the table sorts by this column on its own, instead of comparing the values. Return a negative number when `a` comes first in ascending order. |
| `sort` | `(params: { builder: FilterBuilder; direction: 'asc' \| 'desc' }) => FilterBuilder` | Adds the sorting by this column to the query sent to the backend, instead of `orderBy(key, direction)`. Used when the table does not sort on its own (`sorting="manual"`, `DataTable`). |
| `pinned` | `boolean` | Keeps the column visible on the left while the table scrolls sideways. Pinned columns are moved before the others; they scroll like the others while, together with the checkboxes and the actions, they would take more than half of the table. |
| `hidden` | `boolean` | Hides the column until it is shown through `columnState` (for example with a `ColumnManager`). |
| `required` | `boolean` | The column is always shown: `columnState` cannot hide it and the `ColumnManager` has no button to hide it. |
| `icon` | `string` | SVG path of the icon of the column in the `ColumnManager`. Defaults to an icon of its `type`. |
| `description` | `string` | Description of the column in the `ColumnManager`, also matched by its search. Defaults to `info`. |
| `resizable` | `boolean` | With `resizable` on the table, `false` keeps this column at its size. |
| `info` | `string` | Explanation of the column, shown in a tooltip next to the header. |
| `footer` | `string \| Snippet<[{ rows: Row[]; column: TableColumn<Row> }]>` | Content of the column in the footer of the table: a text, or a snippet that receives every row, for example to show a total. |
| `cell` | `Snippet<[{ row: Row; value: any; column: TableColumn<Row> }]>` | Content of the cells of this column, instead of the formatted value. |
| `header` | `Snippet<[{ column: TableColumn<Row> }]>` | Content of the header of this column, instead of `label`. The sort arrow and the info icon stay. |

### TableColumnState

What the user changed about a column, in display order: plain data, ready to be saved.

`interface TableColumnState`

| Name | Type | Description |
| --- | --- | --- |
| `key` (required) | `string` | Key of the column. |
| `visible` (required) | `boolean` | Whether the column is shown. |
| `width` | `number` | Width in pixels set by resizing. |
| `pinned` | `boolean` | Whether the column is pinned on the left. |

## CSS variables

For a single instance, pass them as style props: `<ColumnManager --column-manager-count-color="tomato">`. The `--column-manager-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--column-manager-width` | `--column-manager-default-width`: `372px` |
| `--column-manager-backdrop-background` | `--column-manager-default-backdrop-background`: `color-mix(in srgb, var(--global-color-overlay) 45%, transparent)` |
| `--column-manager-backdrop-filter` | `--column-manager-default-backdrop-filter`: `none` |
| `--column-manager-sheet-max-height` | `--column-manager-default-sheet-max-height`: `90dvh` |
| `--column-manager-count-color` | `--column-manager-default-count-color`: `var(--global-color-text-3)` |
| `--column-manager-count-font-size` | `--column-manager-default-count-font-size`: `12px` |
| `--column-manager-duration` | `--column-manager-default-duration`: `var(--global-duration)` |
| `--column-manager-easing` | `--column-manager-default-easing`: `var(--global-ease)` |
| `--column-manager-focus-ring-width` | `--column-manager-default-focus-ring-width`: `var(--global-focus-ring-width)` |
| `--column-manager-focus-ring-color` | `--column-manager-default-focus-ring-color`: `var(--global-focus-ring-color)` |
| `--column-manager-focus-ring-offset` | `--column-manager-default-focus-ring-offset`: `var(--global-focus-ring-offset)` |
| `--column-manager-section-margin` | `--column-manager-default-section-margin`: `14px 0 6px` |
| `--column-manager-section-color` | `--column-manager-default-section-color`: `var(--global-color-text-3)` |
| `--column-manager-section-font-size` | `--column-manager-default-section-font-size`: `11px` |
| `--column-manager-section-hint-font-size` | `--column-manager-default-section-hint-font-size`: `11.5px` |
| `--column-manager-item-gap` | `--column-manager-default-item-gap`: `4px` |
| `--column-manager-item-border-width` | `--column-manager-default-item-border-width`: `var(--global-border-width)` |
| `--column-manager-item-border-radius` | `--column-manager-default-item-border-radius`: `var(--global-radius-md)` |
| `--column-manager-item-background` | `--column-manager-default-item-background`: `var(--global-color-surface-solid)` |
| `--column-manager-item-color` | `--column-manager-default-item-color`: `var(--global-color-text)` |
| `--column-manager-item-font-size` | `--column-manager-default-item-font-size`: `13px` |
| `--column-manager-item-border-color` | `--column-manager-default-item-border-color`: `var(--global-color-border)` |
| `--column-manager-item-padding` | `--column-manager-default-item-padding`: `5px 6px 5px 4px` |
| `--column-manager-item-dragging-border-color` | `--column-manager-default-item-dragging-border-color`: `var(--global-color-primary)` |
| `--column-manager-item-dragging-box-shadow` | `--column-manager-default-item-dragging-box-shadow`: `var(--global-shadow-md)` |
| `--column-manager-card-border-color` | `--column-manager-default-card-border-color`: `var(--global-color-border)` |
| `--column-manager-card-gap` | `--column-manager-default-card-gap`: `10px` |
| `--column-manager-card-padding` | `--column-manager-default-card-padding`: `8px` |
| `--column-manager-item-hover-border-color` | `--column-manager-default-item-hover-border-color`: `var(--global-color-border-strong)` |
| `--column-manager-card-hover-border-color` | `--column-manager-default-card-hover-border-color`: `var(--global-color-border-strong)` |
| `--column-manager-add-hover-background` | `--column-manager-default-add-hover-background`: `var(--global-color-primary)` |
| `--column-manager-add-hover-color` | `--column-manager-default-add-hover-color`: `var(--global-color-on-primary)` |
| `--column-manager-button-hover-background` | `--column-manager-default-button-hover-background`: `var(--global-color-surface-3)` |
| `--column-manager-button-hover-color` | `--column-manager-default-button-hover-color`: `var(--global-color-text)` |
| `--column-manager-button-size` | `--column-manager-default-button-size`: `26px` |
| `--column-manager-button-border-radius` | `--column-manager-default-button-border-radius`: `var(--global-radius-xs)` |
| `--column-manager-grip-color` | `--column-manager-default-grip-color`: `var(--global-color-text-3)` |
| `--column-manager-button-disabled-opacity` | `--column-manager-default-button-disabled-opacity`: `0.4` |
| `--column-manager-type-icon-size` | `--column-manager-default-type-icon-size`: `24px` |
| `--column-manager-type-icon-border-radius` | `--column-manager-default-type-icon-border-radius`: `var(--global-radius-xs)` |
| `--column-manager-type-icon-background` | `--column-manager-default-type-icon-background`: `var(--global-color-primary-soft)` |
| `--column-manager-type-icon-color` | `--column-manager-default-type-icon-color`: `var(--global-color-primary)` |
| `--column-manager-card-icon-background` | `--column-manager-default-card-icon-background`: `var(--global-color-surface-2)` |
| `--column-manager-card-icon-color` | `--column-manager-default-card-icon-color`: `var(--global-color-text-2)` |
| `--column-manager-item-font-weight` | `--column-manager-default-item-font-weight`: `500` |
| `--column-manager-button-color` | `--column-manager-default-button-color`: `var(--global-color-text-3)` |
| `--column-manager-button-pinned-color` | `--column-manager-default-button-pinned-color`: `var(--global-color-primary)` |
| `--column-manager-tag-padding` | `--column-manager-default-tag-padding`: `1px 5px` |
| `--column-manager-tag-border-radius` | `--column-manager-default-tag-border-radius`: `var(--global-radius-xs)` |
| `--column-manager-tag-background` | `--column-manager-default-tag-background`: `var(--global-color-surface-2)` |
| `--column-manager-tag-color` | `--column-manager-default-tag-color`: `var(--global-color-text-3)` |
| `--column-manager-tag-font-family` | `--column-manager-default-tag-font-family`: `var(--global-font-family-mono)` |
| `--column-manager-tag-font-size` | `--column-manager-default-tag-font-size`: `10px` |
| `--column-manager-description-color` | `--column-manager-default-description-color`: `var(--global-color-text-3)` |
| `--column-manager-description-font-size` | `--column-manager-default-description-font-size`: `11.5px` |
| `--column-manager-add-size` | `--column-manager-default-add-size`: `28px` |
| `--column-manager-add-border-color` | `--column-manager-default-add-border-color`: `var(--global-color-border-strong)` |
| `--column-manager-add-background` | `--column-manager-default-add-background`: `var(--global-color-surface-2)` |
| `--column-manager-add-color` | `--column-manager-default-add-color`: `var(--global-color-text-2)` |
| `--column-manager-mark-color` | `--column-manager-default-mark-color`: `var(--global-color-primary)` |
| `--column-manager-mark-font-weight` | `--column-manager-default-mark-font-weight`: `600` |
| `--column-manager-empty-color` | `--column-manager-default-empty-color`: `var(--global-color-text-3)` |
| `--column-manager-empty-font-size` | `--column-manager-default-empty-font-size`: `12.5px` |
