# HierarchyMenu

Navigation menu of nested options, as in the sidebar of an app (`SidebarLayout` draws one from its `options`). Options are `HierarchyMenuOption` objects, `{ name: string; label: string; icon?: string; href?: string; options?: HierarchyMenuOption[]; badge?: string | number; disabled?: boolean }`, nested to any depth. An option with `href` is a link; with `currentPath` (pass `page.url.pathname`) the option whose `href` is the longest match of the path is selected, and so are the pages below it; when no `href` matches, no link is selected (with one menu per group, only the menu of the current page shows a selection). Without links, `selected` holds the `name` of the selected option, as `bind:selected`. The options above the selected one are highlighted and opened. A parent opens and closes its children with a click; a parent with `href` is a link with a separate arrow button. With `iconsOnly` (a collapsed sidebar) only the icons show, each with a tooltip, and a parent opens a menu with its children. Keyboard: Tab, plus the arrow keys to move between the options, Right and Left to open and close a parent, Home and End. The state is exposed as `data-selected`, `data-ancestor`, `data-open` and `data-depth` on every row, for app CSS.

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

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

## Examples

### Nested options

Parents open and close their children with a click; the options above the selected one are highlighted and opened. Without links, `bind:selected` holds the `name` of the chosen option. Use the arrow keys to move, Right and Left to open and close.

```svelte
<script lang="ts">
	import {
		mdiAccountMultipleOutline,
		mdiCalendarOutline,
		mdiCogOutline,
		mdiShieldAccountOutline,
		mdiViewDashboardOutline
	} from '@mdi/js';
	import { HierarchyMenu, type HierarchyMenuOption } from '@likable-hair/svelte';

	const options: HierarchyMenuOption[] = [
		{ name: 'dashboard', label: 'Dashboard', icon: mdiViewDashboardOutline },
		{ name: 'agenda', label: 'Agenda', icon: mdiCalendarOutline },
		{ name: 'clients', label: 'Clients', icon: mdiAccountMultipleOutline },
		{
			name: 'settings',
			label: 'Settings',
			icon: mdiCogOutline,
			options: [
				{ name: 'general', label: 'General' },
				{ name: 'hours', label: 'Opening hours' },
				{
					name: 'billing',
					label: 'Billing',
					options: [
						{ name: 'methods', label: 'Payment methods' },
						{ name: 'invoices', label: 'Invoices' }
					]
				}
			]
		},
		{
			name: 'permissions',
			label: 'Permissions',
			icon: mdiShieldAccountOutline,
			options: [
				{ name: 'roles', label: 'Roles' },
				{ name: 'users', label: 'Users' }
			]
		}
	];

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

<div class="menu">
	<HierarchyMenu {options} bind:selected aria-label="Main menu" />
	<p>Selected: <code>{selected}</code></p>
</div>

<style>
	.menu {
		display: grid;
		gap: 16px;
		width: 260px;
		padding-left: 10px;
	}

	p {
		margin: 0;
		color: var(--global-color-text-3);
		font-size: 13px;
	}
</style>
```

### Links and the current page

Options with `href` are links. Pass the path of the page as `currentPath`, in SvelteKit `page.url.pathname` from `$app/state`: the option whose `href` is the longest match is selected, also on the pages below it (`/clients` on `/clients/12`). Here the links only change `path` instead of navigating.

```svelte
<script lang="ts">
	import {
		mdiAccountMultipleOutline,
		mdiCalendarOutline,
		mdiChartBoxOutline,
		mdiCogOutline,
		mdiViewDashboardOutline
	} from '@mdi/js';
	import { HierarchyMenu, type HierarchyMenuOption } from '@likable-hair/svelte';

	const options: HierarchyMenuOption[] = [
		{ name: 'dashboard', label: 'Dashboard', icon: mdiViewDashboardOutline, href: '/' },
		{ name: 'agenda', label: 'Agenda', icon: mdiCalendarOutline, href: '/agenda' },
		{ name: 'clients', label: 'Clients', icon: mdiAccountMultipleOutline, href: '/clients' },
		{ name: 'reports', label: 'Reports', icon: mdiChartBoxOutline, href: '/reports' },
		{
			name: 'settings',
			label: 'Settings',
			icon: mdiCogOutline,
			href: '/settings',
			options: [
				{ name: 'general', label: 'General', href: '/settings/general' },
				{ name: 'hours', label: 'Opening hours', href: '/settings/hours' }
			]
		}
	];

	let path = $state('/clients/12');
</script>

<div class="menu">
	<HierarchyMenu
		{options}
		currentPath={path}
		aria-label="Main menu"
		onoptionClick={({ option, nativeEvent }) => {
			nativeEvent.preventDefault();
			path = option.href ?? path;
		}}
	/>
	<p>Path: <code>{path}</code></p>
</div>

<style>
	.menu {
		display: grid;
		gap: 16px;
		width: 260px;
		padding-left: 10px;
	}

	p {
		margin: 0;
		color: var(--global-color-text-3);
		font-size: 13px;
	}
</style>
```

### Icons only

With `iconsOnly`, as in a collapsed sidebar, the labels become tooltips, a badge becomes a dot and a parent opens a menu with its children. Switch it off to see the full menu.

```svelte
<script lang="ts">
	import {
		mdiAccountMultipleOutline,
		mdiCalendarOutline,
		mdiCogOutline,
		mdiPackageVariantClosed,
		mdiViewDashboardOutline
	} from '@mdi/js';
	import { HierarchyMenu, Switch, type HierarchyMenuOption } from '@likable-hair/svelte';

	const options: HierarchyMenuOption[] = [
		{ name: 'dashboard', label: 'Dashboard', icon: mdiViewDashboardOutline },
		{ name: 'agenda', label: 'Agenda', icon: mdiCalendarOutline, badge: 12 },
		{ name: 'clients', label: 'Clients', icon: mdiAccountMultipleOutline },
		{ name: 'warehouse', label: 'Warehouse', icon: mdiPackageVariantClosed, badge: 3 },
		{
			name: 'settings',
			label: 'Settings',
			icon: mdiCogOutline,
			options: [
				{ name: 'general', label: 'General' },
				{ name: 'hours', label: 'Opening hours' }
			]
		}
	];

	let iconsOnly = $state(true);
	let selected = $state('hours');
</script>

<div class="demo">
	<Switch bind:checked={iconsOnly} label="Icons only" />
	<div class="rail" data-narrow={iconsOnly ? '' : undefined}>
		<HierarchyMenu {options} bind:selected {iconsOnly} aria-label="Main menu" />
	</div>
</div>

<style>
	.demo {
		display: grid;
		gap: 16px;
		justify-items: start;
	}

	.rail {
		box-sizing: border-box;
		width: 260px;
		padding: 12px 10px;
		border: 1px solid var(--global-color-border);
		border-radius: var(--global-radius-md);
		background: var(--global-color-surface);
	}

	.rail[data-narrow] {
		width: 64px;
	}
</style>
```

### Badges, disabled options and extra content

`badge` shows a short count in a pill, `disabled` keeps an option visible but not selectable. `appendSnippet` adds content after each option, outside the link or button, so it can hold its own buttons.

```svelte
<script lang="ts">
	import { mdiCalendarOutline, mdiEmailOutline, mdiLockOutline, mdiPlus } from '@mdi/js';
	import { Button, HierarchyMenu, type HierarchyMenuOption } from '@likable-hair/svelte';

	const options: HierarchyMenuOption[] = [
		{ name: 'inbox', label: 'Inbox', icon: mdiEmailOutline, badge: 28 },
		{ name: 'agenda', label: 'Agenda', icon: mdiCalendarOutline, badge: 'new' },
		{ name: 'admin', label: 'Administration', icon: mdiLockOutline, disabled: true }
	];

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

<div class="menu">
	<HierarchyMenu {options} bind:selected aria-label="Mail">
		{#snippet appendSnippet({ option })}
			{#if option.name === 'agenda'}
				<Button buttonType="icon" size="sm" icon={mdiPlus} aria-label="New appointment" />
			{/if}
		{/snippet}
	</HierarchyMenu>
</div>

<style>
	.menu {
		width: 260px;
		padding-left: 10px;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `options` | `HierarchyMenuOption[]` | `[]` | Options of the menu, in order, each with its nested `options`. |
| `selected` (bindable) | `string` |  | `name` of the selected option. It changes when an option is chosen and, with `currentPath`, when the path matches an option's `href`. |
| `currentPath` | `string` |  | Path of the current page, usually `page.url.pathname`. The option whose `href` is the longest match is selected: equal to the path, or a parent folder of it (`/clients` matches `/clients/12`). `/` matches only itself. When no option matches, no link option is selected. |
| `expanded` (bindable) | `string[]` |  | `name`s of the open parents. The parents of the selected option open by themselves. |
| `iconsOnly` | `boolean` | `false` | Shows only the icons, as in a collapsed sidebar: labels become tooltips and a parent opens a menu with its children. |
| `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`. |
| `aria-label` | `string \| null` |  | Accessible name of the `<nav>`. Defaults to "Menu", in the language of `locale`, unless `aria-labelledby` names it. |
| `class` | `{ container?: ClassValue; option?: ClassValue; selected?: ClassValue; children?: ClassValue; }` | `{}` | Classes of the parts: `container` the `<nav>`, `option` every option, `selected` the selected option, `children` every list of nested options. |
| `hierarchyMenuElement` (bindable) | `HTMLElement` |  | The `<nav>` element. |

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `prependSnippet` | `{ option: HierarchyMenuOption; selected: boolean; depth: number }` | Replaces the icon of every option. |
| `optionSnippet` | `{ option: HierarchyMenuOption; selected: boolean; depth: number }` | Replaces the label of every option. |
| `appendSnippet` | `{ option: HierarchyMenuOption; selected: boolean; depth: number }` | Content after every option, outside the link or button, so it can hold its own buttons. Hidden with `iconsOnly`. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onoptionClick` | `(event: { option: HierarchyMenuOption; nativeEvent: MouseEvent }) => void` | Called when an option is chosen with a click, Enter or Space, after `selected` has changed. Links then navigate on their own. Not called for a parent without `href`, which only opens and closes. |

## Types

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

### HierarchyMenuOption

`type HierarchyMenuOption = { … }`

| Name | Type | Description |
| --- | --- | --- |
| `name` (required) | `string` | Unique value among all the options of the menu: `selected` holds it. |
| `label` (required) | `string` | Visible text. |
| `icon` | `string` | SVG path of the icon before the text, for example from `@mdi/js`. |
| `href` | `string` | Makes the option a link. With `currentPath`, the option whose `href` best matches the path is selected. |
| `options` | `HierarchyMenuOption[]` | Options shown below this one when it is open, nested to any depth. |
| `badge` | `string \| number` | Short count or text after the label, in a pill. A dot on the icon when the menu shows only icons. |
| `disabled` | `boolean` | The option cannot be chosen. |

## CSS variables

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

| Per-instance variable | Default |
| --- | --- |
| `--hierarchy-menu-duration` | `--hierarchy-menu-default-duration`: `var(--global-duration-slow)` |
| `--hierarchy-menu-easing` | `--hierarchy-menu-default-easing`: `var(--global-ease)` |
| `--hierarchy-menu-focus-ring-width` | `--hierarchy-menu-default-focus-ring-width`: `var(--global-focus-ring-width)` |
| `--hierarchy-menu-focus-ring-color` | `--hierarchy-menu-default-focus-ring-color`: `var(--global-focus-ring-color)` |
| `--hierarchy-menu-icon-size` | `--hierarchy-menu-default-icon-size`: `18px` |
| `--hierarchy-menu-gap` | `--hierarchy-menu-default-gap`: `2px` |
| `--hierarchy-menu-width` | `--hierarchy-menu-default-width`: `100%` |
| `--hierarchy-menu-font-family` | `--hierarchy-menu-default-font-family`: `inherit` |
| `--hierarchy-menu-option-background` | `--hierarchy-menu-default-option-background`: `transparent` |
| `--hierarchy-menu-option-color` | `--hierarchy-menu-default-option-color`: `var(--global-color-text-2)` |
| `--hierarchy-menu-option-height` | `--hierarchy-menu-default-option-height`: `38px` |
| `--hierarchy-menu-option-font-size` | `--hierarchy-menu-default-option-font-size`: `14px` |
| `--hierarchy-menu-option-border-radius` | `--hierarchy-menu-default-option-border-radius`: `var(--global-radius-sm)` |
| `--hierarchy-menu-child-height` | `--hierarchy-menu-default-child-height`: `34px` |
| `--hierarchy-menu-child-font-size` | `--hierarchy-menu-default-child-font-size`: `13px` |
| `--hierarchy-menu-option-ancestor-color` | `--hierarchy-menu-default-option-ancestor-color`: `var(--global-color-text)` |
| `--hierarchy-menu-option-selected-background` | `--hierarchy-menu-default-option-selected-background`: `linear-gradient(90deg, var(--global-color-primary-soft), transparent)` |
| `--hierarchy-menu-option-selected-color` | `--hierarchy-menu-default-option-selected-color`: `var(--global-color-text)` |
| `--hierarchy-menu-indicator-offset` | `--hierarchy-menu-default-indicator-offset`: `-10px` |
| `--hierarchy-menu-indicator-inset` | `--hierarchy-menu-default-indicator-inset`: `9px` |
| `--hierarchy-menu-indicator-width` | `--hierarchy-menu-default-indicator-width`: `3px` |
| `--hierarchy-menu-indicator-background` | `--hierarchy-menu-default-indicator-background`: `var(--global-gradient)` |
| `--hierarchy-menu-indicator-box-shadow` | `--hierarchy-menu-default-indicator-box-shadow`: `0 0 12px var(--global-color-primary)` |
| `--hierarchy-menu-option-hover-background` | `--hierarchy-menu-default-option-hover-background`: `var(--global-color-surface-2)` |
| `--hierarchy-menu-option-hover-color` | `--hierarchy-menu-default-option-hover-color`: `var(--global-color-text)` |
| `--hierarchy-menu-disabled-opacity` | `--hierarchy-menu-default-disabled-opacity`: `0.45` |
| `--hierarchy-menu-option-gap` | `--hierarchy-menu-default-option-gap`: `10px` |
| `--hierarchy-menu-option-padding` | `--hierarchy-menu-default-option-padding`: `0 13px` |
| `--hierarchy-menu-option-font-weight` | `--hierarchy-menu-default-option-font-weight`: `500` |
| `--hierarchy-menu-option-selected-font-weight` | `--hierarchy-menu-default-option-selected-font-weight`: `500` |
| `--hierarchy-menu-icon-color` | `--hierarchy-menu-default-icon-color`: `currentColor` |
| `--hierarchy-menu-icon-selected-color` | `--hierarchy-menu-default-icon-selected-color`: `var(--global-color-primary)` |
| `--hierarchy-menu-badge-padding` | `--hierarchy-menu-default-badge-padding`: `1px 7px` |
| `--hierarchy-menu-badge-border-radius` | `--hierarchy-menu-default-badge-border-radius`: `var(--global-radius-full)` |
| `--hierarchy-menu-badge-background` | `--hierarchy-menu-default-badge-background`: `var(--global-color-surface-3)` |
| `--hierarchy-menu-badge-color` | `--hierarchy-menu-default-badge-color`: `var(--global-color-text-2)` |
| `--hierarchy-menu-badge-font-family` | `--hierarchy-menu-default-badge-font-family`: `var(--global-font-family-mono)` |
| `--hierarchy-menu-badge-font-size` | `--hierarchy-menu-default-badge-font-size`: `11px` |
| `--hierarchy-menu-badge-selected-background` | `--hierarchy-menu-default-badge-selected-background`: `var(--global-color-primary)` |
| `--hierarchy-menu-badge-selected-color` | `--hierarchy-menu-default-badge-selected-color`: `var(--global-color-on-primary)` |
| `--hierarchy-menu-dot-size` | `--hierarchy-menu-default-dot-size`: `7px` |
| `--hierarchy-menu-dot-color` | `--hierarchy-menu-default-dot-color`: `var(--global-color-primary)` |
| `--hierarchy-menu-caret-size` | `--hierarchy-menu-default-caret-size`: `16px` |
| `--hierarchy-menu-caret-color` | `--hierarchy-menu-default-caret-color`: `var(--global-color-text-3)` |
| `--hierarchy-menu-children-offset` | `--hierarchy-menu-default-children-offset`: `21px` |
| `--hierarchy-menu-children-indent` | `--hierarchy-menu-default-children-indent`: `12px` |
| `--hierarchy-menu-children-border` | `--hierarchy-menu-default-children-border`: `1px dashed var(--global-color-border-strong)` |
| `--hierarchy-menu-flyout-min-width` | `--hierarchy-menu-default-flyout-min-width`: `200px` |
| `--hierarchy-menu-flyout-padding` | `--hierarchy-menu-default-flyout-padding`: `6px` |
| `--hierarchy-menu-flyout-title-color` | `--hierarchy-menu-default-flyout-title-color`: `var(--global-color-text-3)` |
| `--hierarchy-menu-flyout-title-font-size` | `--hierarchy-menu-default-flyout-title-font-size`: `11px` |
