# TabSwitcher

Row of tabs where one is selected (`bind:selected`). Tabs are `Tab` objects, `{ name: string; label: string; icon?: string; badge?: string | number; href?: string; disabled?: boolean; panelId?: string }`: `name` identifies the tab and is what `selected` holds, `label` is shown, `icon` is an SVG path, `badge` a short count in a pill, `panelId` the `id` of the panel the tab shows (`aria-controls`). By default it is an ARIA tab list: the arrow keys, Home and End move the focus between the tabs, Enter and Space select the focused one, and only one tab is in the Tab order. When the tabs have `href` it is a `<nav>` of links with `aria-current="page"` on the selected one, for tabs that switch page. Two looks, `underline` (default) and `segmented`, with an indicator that slides to the selected tab. Tabs that do not fit scroll sideways, and the selected one is scrolled into view. Its state is exposed as `data-variant` on the root and `data-selected` on the selected tab for app CSS.

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

Category: simple. Source: `src/lib/components/simple/navigation/TabSwitcher.svelte`.

## Examples

### Tabs and panels

An ARIA tab list: click a tab, or focus it and use the arrow keys, Home and End, then Enter or Space. `panelId` links each tab to its panel with `aria-controls`; the panel is markup of the app.

```svelte
<script lang="ts">
	import {
		mdiAccountOutline,
		mdiContentCut,
		mdiFileDocumentOutline,
		mdiHistory
	} from '@mdi/js';
	import { TabSwitcher, type Tab } from '@likable-hair/svelte';

	const tabs: Tab[] = [
		{ name: 'overview', label: 'Overview', icon: mdiAccountOutline, panelId: 'client-overview' },
		{ name: 'services', label: 'Services', icon: mdiContentCut, panelId: 'client-services' },
		{ name: 'history', label: 'History', icon: mdiHistory, badge: 12, panelId: 'client-history' },
		{ name: 'documents', label: 'Documents', icon: mdiFileDocumentOutline, disabled: true }
	];
	const text: Record<string, string> = {
		overview: 'Giulia Marini, client since 2021. Prefers morning appointments.',
		services: 'Cut, color and blow-dry: usually 1 hour and 45 minutes.',
		history: '12 visits in the last year, the latest on 2 October.'
	};

	let selected = $state('overview');
	const panel = $derived(tabs.find((tab) => tab.name === selected));
</script>

<div class="client">
	<TabSwitcher {tabs} bind:selected aria-label="Client" />
	{#if panel}
		<div id={panel.panelId} role="tabpanel" aria-label={panel.label} class="panel">
			{text[panel.name]}
		</div>
	{/if}
</div>

<style>
	.client {
		display: grid;
		gap: 16px;
		width: 100%;
	}

	.panel {
		font-size: 14px;
	}
</style>
```

### Segmented

`variant="segmented"`: a pill with a thumb that slides to the selected option, for switching views. `badge` adds a short count.

```svelte
<script lang="ts">
	import { mdiCalendarToday } from '@mdi/js';
	import { TabSwitcher } from '@likable-hair/svelte';

	let view = $state('day');
</script>

<div class="calendar">
	<TabSwitcher
		variant="segmented"
		aria-label="Calendar view"
		tabs={[
			{ name: 'day', label: 'Day', icon: mdiCalendarToday },
			{ name: 'week', label: 'Week' },
			{ name: 'month', label: 'Month', badge: 12 }
		]}
		bind:selected={view}
	/>
	<p>Showing the <strong>{view}</strong> view.</p>
</div>

<style>
	.calendar {
		display: grid;
		justify-items: center;
		gap: 12px;
		width: 100%;
	}

	p {
		margin: 0;
		font-size: 14px;
	}
</style>
```

### Tabs that switch page

With `href` the tabs are links in a `<nav>`: they open in a new browser tab with a middle click and SvelteKit can preload them. The selected one has `aria-current="page"`. In an app, derive `selected` from the current URL (`page.url.pathname`).

```svelte
<script lang="ts">
	import { TabSwitcher } from '@likable-hair/svelte';

	let selected = $state('general');
</script>

<TabSwitcher
	aria-label="User settings"
	tabs={[
		{ name: 'general', label: 'General', href: '#general' },
		{ name: 'permissions', label: 'Permissions', href: '#permissions' },
		{ name: 'notifications', label: 'Notifications', href: '#notifications' },
		{ name: 'billing', label: 'Billing', href: '#billing', disabled: true }
	]}
	bind:selected
/>
```

### Overflow and actions

Tabs that do not fit scroll sideways, and the selected tab is scrolled into view. `appendSnippet` puts content at the end of the row, outside the tabs.

```svelte
<script lang="ts">
	import { mdiPlus } from '@mdi/js';
	import { Button, TabSwitcher } from '@likable-hair/svelte';

	let months = $state(['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August']);
	let selected = $state('August');

	function add() {
		const next = new Date(2026, months.length, 1).toLocaleString('en', { month: 'long' });
		months.push(next);
		selected = next;
	}
</script>

<div class="narrow">
	<TabSwitcher
		aria-label="Month"
		tabs={months.map((month) => ({ name: month, label: month }))}
		bind:selected
	>
		{#snippet appendSnippet()}
			<Button
				buttonType="icon"
				variant="secondary"
				size="sm"
				icon={mdiPlus}
				aria-label="Add month"
				disabled={months.length >= 12}
				onclick={add}
			/>
		{/snippet}
	</TabSwitcher>
</div>

<style>
	.narrow {
		width: 100%;
		max-width: 420px;
	}
</style>
```

### Custom content

`tabSnippet` replaces the content of every tab and receives the tab and whether it is selected; clicks, keyboard and the indicator stay the same.

```svelte
<script lang="ts">
	import { TabSwitcher, type Tab } from '@likable-hair/svelte';

	const status: Record<string, string> = {
		open: 'var(--global-color-success)',
		waiting: 'var(--global-color-warning)',
		closed: 'var(--global-color-text-3)'
	};
	const tabs: Tab[] = [
		{ name: 'open', label: 'Open', badge: 8 },
		{ name: 'waiting', label: 'Waiting', badge: 3 },
		{ name: 'closed', label: 'Closed', badge: 41 }
	];

	let selected = $state('open');
</script>

<TabSwitcher {tabs} bind:selected aria-label="Tickets">
	{#snippet tabSnippet({ tab, selected })}
		<span class="dot" style:background={status[tab.name]}></span>
		{tab.label}
		<span class={['count', selected && 'count-selected']}>{tab.badge}</span>
	{/snippet}
</TabSwitcher>

<style>
	.dot {
		width: 8px;
		height: 8px;
		border-radius: 50%;
	}

	.count {
		font-size: 12px;
		font-variant-numeric: tabular-nums;
		opacity: 0.6;
	}

	.count-selected {
		opacity: 1;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `tabs` | `Tab[]` | `[]` | Tabs, in order. |
| `selected` (bindable) | `string` |  | `name` of the selected tab. |
| `mandatory` | `boolean` | `true` | While `selected` is empty, the first enabled tab is selected. |
| `variant` | `'underline' \| 'segmented'` | `'underline'` | Look: `underline` (text tabs on a line) or `segmented` (a pill with a sliding thumb). |
| `aria-label` | `string \| null` |  | Accessible name of the tab list, or of the `<nav>` with links. Not shown. |
| `aria-labelledby` | `string \| null` |  | `id` of the element that names the tab list. |
| `class` | `{ container?: ClassValue; tab?: ClassValue; selected?: ClassValue; indicator?: ClassValue; }` | `{}` | Classes of the parts: `container` the root, `tab` every tab, `selected` the selected tab, `indicator` the sliding indicator. |
| `tabSwitcherElement` (bindable) | `HTMLDivElement` |  | The root element. |

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

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `tabSnippet` | `{ tab: Tab; selected: boolean }` | Replaces the content of every tab (icon, label and badge). |
| `appendSnippet` |  | Content at the end of the row, after the tabs, such as an action button. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `ontabClick` | `(event: { tab: Tab; nativeEvent: MouseEvent }) => void` | Called on a click on a tab (Enter and Space included), after `selected` has changed. Links navigate on their own. |

## Types

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

### Tab

`type Tab = { … }`

| Name | Type | Description |
| --- | --- | --- |
| `name` (required) | `string` | Unique value: `selected` holds the name of the selected tab. |
| `label` (required) | `string` | Visible text. |
| `icon` | `string` | SVG path of an icon before the text, for example from `@mdi/js`. |
| `badge` | `string \| number` | Short count or text after the label, in a pill. |
| `href` | `string` | Makes the tab a link. With at least one `href` the tabs are a `<nav>` of links. |
| `disabled` | `boolean` | The tab cannot be selected. |
| `panelId` | `string` | `id` of the panel the tab shows, set as `aria-controls`. |

## CSS variables

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

| Per-instance variable | Default |
| --- | --- |
| `--tab-switcher-indicator-height` | `--tab-switcher-default-underline-indicator-height`: `2px` |
| `--tab-switcher-focus-ring-width` | `--tab-switcher-default-focus-ring-width`: `var(--global-focus-ring-width)` |
| `--tab-switcher-focus-ring-offset` | `--tab-switcher-default-focus-ring-offset`: `var(--global-focus-ring-offset)` |
| `--tab-switcher-append-gap` | `--tab-switcher-default-append-gap`: `8px` |
| `--tab-switcher-width` | `--tab-switcher-default-width`: `100%` |
| `--tab-switcher-guide-width` | `--tab-switcher-default-guide-width`: `var(--global-border-width)` |
| `--tab-switcher-guide-color` | `--tab-switcher-default-guide-color`: `var(--global-color-border)` |
| `--tab-switcher-font-family` | `--tab-switcher-default-font-family`: `inherit` |
| `--tab-switcher-font-size` | `--tab-switcher-default-underline-font-size`: `14px`<br>`--tab-switcher-default-segmented-font-size`: `13px` |
| `--tab-switcher-font-weight` | `--tab-switcher-default-font-weight`: `500` |
| `--tab-switcher-gap` | `--tab-switcher-default-underline-gap`: `22px`<br>`--tab-switcher-default-segmented-gap`: `0px` |
| `--tab-switcher-list-padding` | `--tab-switcher-default-list-padding`: `3px` |
| `--tab-switcher-list-border-width` | `--tab-switcher-default-list-border-width`: `var(--global-border-width)` |
| `--tab-switcher-list-border-color` | `--tab-switcher-default-list-border-color`: `var(--global-color-border)` |
| `--tab-switcher-list-border-radius` | `--tab-switcher-default-list-border-radius`: `var(--global-radius-md)` |
| `--tab-switcher-list-background` | `--tab-switcher-default-list-background`: `var(--global-color-surface-2)` |
| `--tab-switcher-icon-size` | `--tab-switcher-default-icon-size`: `16px` |
| `--tab-switcher-icon-gap` | `--tab-switcher-default-icon-gap`: `6px` |
| `--tab-switcher-padding` | `--tab-switcher-default-underline-padding`: `8px 0 10px`<br>`--tab-switcher-default-segmented-padding`: `5px 12px` |
| `--tab-switcher-border-radius` | `--tab-switcher-default-underline-border-radius`: `var(--global-radius-xs)`<br>`--tab-switcher-default-segmented-border-radius`: `var(--global-radius-sm)` |
| `--tab-switcher-color` | `--tab-switcher-default-underline-color`: `var(--global-color-text-3)`<br>`--tab-switcher-default-segmented-color`: `var(--global-color-text-2)` |
| `--tab-switcher-selected-color` | `--tab-switcher-default-underline-selected-color`: `var(--global-color-text)`<br>`--tab-switcher-default-segmented-selected-color`: `var(--global-color-text)` |
| `--tab-switcher-hover-color` | `--tab-switcher-default-underline-hover-color`: `var(--global-color-text)`<br>`--tab-switcher-default-segmented-hover-color`: `var(--global-color-text)` |
| `--tab-switcher-focus-ring-color` | `--tab-switcher-default-focus-ring-color`: `var(--global-focus-ring-color)` |
| `--tab-switcher-disabled-opacity` | `--tab-switcher-default-disabled-opacity`: `0.4` |
| `--tab-switcher-badge-padding` | `--tab-switcher-default-badge-padding`: `1px 6px` |
| `--tab-switcher-badge-border-radius` | `--tab-switcher-default-badge-border-radius`: `var(--global-radius-full)` |
| `--tab-switcher-badge-background` | `--tab-switcher-default-badge-background`: `var(--global-color-primary-soft)` |
| `--tab-switcher-badge-color` | `--tab-switcher-default-badge-color`: `var(--global-color-primary)` |
| `--tab-switcher-badge-font-family` | `--tab-switcher-default-badge-font-family`: `var(--global-font-family-mono)` |
| `--tab-switcher-badge-font-size` | `--tab-switcher-default-badge-font-size`: `10.5px` |
| `--tab-switcher-indicator-border-width` | `--tab-switcher-default-underline-indicator-border-width`: `0px`<br>`--tab-switcher-default-segmented-indicator-border-width`: `var(--global-border-width)` |
| `--tab-switcher-indicator-border-color` | `--tab-switcher-default-underline-indicator-border-color`: `transparent`<br>`--tab-switcher-default-segmented-indicator-border-color`: `var(--global-color-border-strong)` |
| `--tab-switcher-indicator-border-radius` | `--tab-switcher-default-underline-indicator-border-radius`: `2px`<br>`--tab-switcher-default-segmented-indicator-border-radius`: `var(--global-radius-sm)` |
| `--tab-switcher-indicator-background` | `--tab-switcher-default-underline-indicator-background`: `var(--global-gradient)`<br>`--tab-switcher-default-segmented-indicator-background`: `var(--global-color-surface-pop)` |
| `--tab-switcher-indicator-box-shadow` | `--tab-switcher-default-underline-indicator-box-shadow`: `0 0 12px var(--global-color-primary-glow)`<br>`--tab-switcher-default-segmented-indicator-box-shadow`: `var(--global-shadow-sm)` |
| `--tab-switcher-duration` | `--tab-switcher-default-duration`: `var(--global-duration-slow)` |
| `--tab-switcher-easing` | `--tab-switcher-default-underline-easing`: `var(--global-ease)`<br>`--tab-switcher-default-segmented-easing`: `var(--global-ease-spring)` |
