Skip to content
Likable Aurora @likable-hair/svelte 5

Menu

Floating surface anchored to an activator, for dropdowns, autocompletes, pickers, action and context menus. It opens on the browser's top layer (popover), so it is never clipped by overflow containers and needs no z-index. It flips and shifts to stay in the viewport and follows the activator wherever it moves. It closes on outside click (the activator excluded) and on Escape, which returns focus to the activator; with menus open inside it, a click outside closes only the innermost one, one level per click, as Escape does. Opening a menu closes the other open menus, except nested ones and those with closeOnClickOutside={false}. The content decides the role (role="menu", role="listbox", ...). The current side is exposed as data-side on the menu element.

import { Menu } from '@likable-hair/svelte';

src/lib/components/simple/common/Menu.svelte · Markdown for LLMs

Action menu

A button opens the menu below it. It closes on outside click, on Escape (focus goes back to the button) and when an action is chosen.

Last action: none
Code
<script lang="ts">
	import {
		mdiArchiveOutline,
		mdiContentCopy,
		mdiDotsHorizontal,
		mdiPencilOutline,
		mdiTrashCanOutline
	} from '@mdi/js';
	import { Button, Icon, Menu } from '@likable-hair/svelte';

	let open = $state(false);
	let activator = $state<HTMLButtonElement>();
	let last = $state('none');

	const actions = [
		{ label: 'Edit', icon: mdiPencilOutline },
		{ label: 'Duplicate', icon: mdiContentCopy },
		{ label: 'Archive', icon: mdiArchiveOutline }
	];

	function choose(label: string) {
		last = label;
		open = false;
	}
</script>

<Button
	variant="secondary"
	icon={mdiDotsHorizontal}
	aria-haspopup="menu"
	aria-expanded={open}
	bind:buttonElement={activator}
	onclick={() => (open = !open)}
>
	Actions
</Button>
<span class="last">Last action: {last}</span>

<Menu bind:open {activator} role="menu" aria-label="Actions" --menu-min-width="200px">
	{#each actions as action (action.label)}
		<button class="item" role="menuitem" onclick={() => choose(action.label)}>
			<Icon path={action.icon} />{action.label}
		</button>
	{/each}
	<hr />
	<button class="item danger" role="menuitem" onclick={() => choose('Delete')}>
		<Icon path={mdiTrashCanOutline} />Delete
	</button>
</Menu>

<style>
	.last {
		color: var(--global-color-text-3);
		font-size: 13px;
	}

	.item {
		display: flex;
		align-items: center;
		gap: 8px;
		width: 100%;
		padding: 7px 10px;
		border: 0;
		border-radius: var(--global-radius-sm);
		background: none;
		color: var(--global-color-text-2);
		font: inherit;
		font-size: 13px;
		text-align: left;
		cursor: pointer;
		--icon-size: 17px;
	}

	.item:hover,
	.item:focus-visible {
		background: var(--global-color-surface-2);
		color: var(--global-color-text);
		outline: none;
	}

	.item.danger {
		color: var(--global-color-error);
	}

	.item.danger:hover,
	.item.danger:focus-visible {
		background: var(--global-color-error-soft);
	}

	hr {
		height: 1px;
		margin: 4px 6px;
		border: 0;
		background: var(--global-color-border);
	}
</style>

Placement

placement picks the side and the alignment. When there is not enough space the menu flips to the opposite side and shifts to stay in the viewport. Opening one menu closes the others.

Code
<script lang="ts">
	import { Button, Menu } from '@likable-hair/svelte';

	const placements = ['bottom-start', 'bottom-end', 'top', 'right', 'left'] as const;
	let open = $state<Record<string, boolean>>(Object.fromEntries(placements.map((p) => [p, false])));
	let buttons = $state<Record<string, HTMLButtonElement>>({});
</script>

{#each placements as placement (placement)}
	<Button
		variant="secondary"
		size="sm"
		bind:buttonElement={buttons[placement]}
		onclick={() => (open[placement] = !open[placement])}
	>
		{placement}
	</Button>
	<Menu bind:open={open[placement]} activator={buttons[placement]} {placement} --menu-padding="10px 14px">
		<code>placement="{placement}"</code>
	</Menu>
{/each}

Inside a scrolling container

The menu opens on the top layer, so a parent with overflow: hidden or auto cannot clip it, and it follows the activator while the container scrolls.

This box has overflow: auto and a fixed height.

Scroll inside the box: the menu stays attached to the button.

Code
<script lang="ts">
	import { Button, Menu } from '@likable-hair/svelte';

	let open = $state(false);
	let activator = $state<HTMLButtonElement>();
</script>

<div class="box">
	<p>This box has <code>overflow: auto</code> and a fixed height.</p>
	<Button size="sm" bind:buttonElement={activator} onclick={() => (open = !open)}>Open menu</Button>
	<p>Scroll inside the box: the menu stays attached to the button.</p>
	<div class="spacer"></div>
</div>

<Menu bind:open {activator} --menu-padding="12px" --menu-width="220px">
	Not clipped by the box, even though it overflows it.
</Menu>

<style>
	.box {
		height: 140px;
		width: 260px;
		overflow: auto;
		padding: 12px;
		border: var(--global-border-width) dashed var(--global-color-border-strong);
		border-radius: var(--global-radius-md);
		font-size: 13px;
		color: var(--global-color-text-2);
	}

	.spacer {
		height: 200px;
	}
</style>

Context menu and activator width

activator can be any object with getBoundingClientRect(): here the point of a right click. matchActivatorWidth makes the menu as wide as its activator.

Code
<script lang="ts">
	import { Button, Menu } from '@likable-hair/svelte';

	let contextOpen = $state(false);
	let point = $state<{ getBoundingClientRect(): DOMRect }>();

	function openAt(event: MouseEvent) {
		event.preventDefault();
		const { clientX, clientY } = event;
		point = { getBoundingClientRect: () => new DOMRect(clientX, clientY, 0, 0) };
		contextOpen = true;
	}

	let wideOpen = $state(false);
	let wideButton = $state<HTMLButtonElement>();
</script>

<div class="area" role="presentation" oncontextmenu={openAt}>Right click anywhere in this area</div>
<Menu bind:open={contextOpen} activator={point} --menu-padding="10px 14px">Opened at the cursor</Menu>

<Button variant="secondary" bind:buttonElement={wideButton} onclick={() => (wideOpen = !wideOpen)} --button-width="260px">
	Menu as wide as this button
</Button>
<Menu bind:open={wideOpen} activator={wideButton} matchActivatorWidth --menu-padding="10px 14px">
	Same width as the button.
</Menu>

<style>
	.area {
		display: grid;
		place-items: center;
		width: 260px;
		height: 90px;
		border: var(--global-border-width) dashed var(--global-color-border-strong);
		border-radius: var(--global-radius-md);
		color: var(--global-color-text-3);
		font-size: 13px;
	}
</style>

Props

NameTypeDefaultDescription
open bindable booleanWhether the menu is open. undefined counts as closed.
activator bindable HTMLElement | VirtualElementWhat the menu is anchored to: usually the button or field that opens it, or any object with getBoundingClientRect() (for example the mouse position of a context menu). Without it the menu is centered in the viewport.
placement 'bottom-start' | 'bottom' | 'bottom-end' | 'top-start' | 'top' | 'top-end' | 'left-start' | 'left' | 'left-end' | 'right-start' | 'right' | 'right-end''bottom-start'Side of the activator and alignment. Flips to the opposite side when there is not enough space.
offset number6Distance from the activator, in pixels.
matchActivatorWidth booleanfalseMakes the menu as wide as the activator, as in autocompletes and selects.
closeOnClickOutside booleantrueCloses the menu on a click outside it and outside the activator. When false the menu also stays open when other menus open.
menuElement bindable HTMLDivElementThe menu 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

NameParametersDescription
childrenMenu content.

CSS variables

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

Per-instance variableDefault
--menu-duration
--menu-default-duration var(--global-duration)
--menu-transition-distance
--menu-default-transition-distance 6px
--menu-transition-scale
--menu-default-transition-scale 0.98
--menu-width
--menu-default-width auto
--menu-min-width
--menu-default-min-width 0
--menu-max-width
--menu-default-max-width calc(100vw - 16px)
--menu-height
--menu-default-height auto
--menu-max-height
--menu-default-max-height calc(100dvh - 16px)
--menu-overflow
--menu-default-overflow auto
--menu-padding
--menu-default-padding 4px
--menu-background
--menu-default-background var(--global-color-surface-pop)
--menu-color
--menu-default-color var(--global-color-text)
--menu-border-width
--menu-default-border-width var(--global-border-width)
--menu-border-color
--menu-default-border-color var(--global-color-border-strong)
--menu-border-radius
--menu-default-border-radius var(--global-radius-lg)
--menu-box-shadow
--menu-default-box-shadow var(--global-shadow-lg)
--menu-backdrop-filter
--menu-default-backdrop-filter blur(24px)