sveltelean Primitives
Version v0.18.0
Editor

Last command: none

Code
<script lang="ts">
	// With @svelte-lean/vite the menubar and menu behaviors register themselves.
	import '@svelte-lean/styles/menu.css'; // brings popover.css
	import '@svelte-lean/styles/menubar.css';
	import type { MenuSelectDetail } from '@svelte-lean/primitives/menu';
	import { on } from 'svelte/events';

	const menus = {
		File: ['New file', 'Open…', 'Save'],
		Edit: ['Undo', 'Redo', 'Find'],
		View: ['Zoom in', 'Zoom out', 'Full screen']
	};
	let last = $state('none');

	function onSelect(event: Event) {
		last = (event as CustomEvent<MenuSelectDetail>).detail.value ?? '';
	}
</script>

<ul
	role="menubar"
	aria-label="Editor"
	data-slean="menubar"
	{@attach (node) => on(node, 'slean:select', onSelect)}
>
	{#each Object.entries(menus) as [label, items], i (label)}
		<li role="none">
			<button
				type="button"
				id="{label}-item"
				role="menuitem"
				popovertarget="{label}-menu"
				tabindex={i === 0 ? 0 : -1}
			>
				{label}
			</button>
			<div
				id="{label}-menu"
				popover
				data-slean="menu"
				data-slean-align="start"
			>
				{#each items as item (item)}
					<button type="button" data-slean-value={item}>{item}</button>
				{/each}
			</div>
		</li>
	{/each}
</ul>
<p>Last command: {last}</p>
Tab to File, use the side arrows along the bar and ArrowDown to open; inside a menu the side arrows move to the next menu.

Examples

Checkbox and radio items

Checkbox and radio items

The quick brown fox jumps over the lazy dog.

Code
<script lang="ts">
	import '@svelte-lean/styles/menu.css';
	import '@svelte-lean/styles/menubar.css';
	import type { MenuSelectDetail } from '@svelte-lean/primitives/menu';
	import { on } from 'svelte/events';

	const aligns = ['Left', 'Center', 'Right'];
	let bold = $state(false);
	let italic = $state(true);
	let align = $state('Left');

	function onFormat(event: Event) {
		const { value, checked } = (event as CustomEvent<MenuSelectDetail>).detail;
		if (value === 'bold') bold = checked === true;
		else if (value === 'italic') italic = checked === true;
		else if (value) align = value;
	}
</script>

<ul
	role="menubar"
	aria-label="Format"
	data-slean="menubar"
	{@attach (node) => on(node, 'slean:select', onFormat)}
>
	<li role="none">
		<button type="button" id="text-item" role="menuitem" popovertarget="text-menu" tabindex="0">
			Text
		</button>
		<div id="text-menu" popover data-slean="menu">
			<button type="button" role="menuitemcheckbox" aria-checked={bold} data-slean-value="bold">
				Bold <kbd>Ctrl+B</kbd>
			</button>
			<button type="button" role="menuitemcheckbox" aria-checked={italic} data-slean-value="italic">
				Italic <kbd>Ctrl+I</kbd>
			</button>
		</div>
	</li>
	<li role="none">
		<button type="button" id="align-item" role="menuitem" popovertarget="align-menu" tabindex="-1">
			Align
		</button>
		<div id="align-menu" popover data-slean="menu">
			<div role="group" aria-label="Alignment">
				{#each aligns as option (option)}
					<button
						type="button"
						role="menuitemradio"
						aria-checked={align === option}
						data-slean-value={option}
					>
						{option}
					</button>
				{/each}
			</div>
		</div>
	</li>
</ul>
<p
	style:font-weight={bold ? 600 : 400}
	style:font-style={italic ? 'italic' : 'normal'}
	style:text-align={align.toLowerCase()}
>
	The quick brown fox jumps over the lazy dog.
</p>
The menus keep the menu contract: checkbox and radio items, slean:select on the bar.

Usage

Install @svelte-lean/primitives and @svelte-lean/styles. With @svelte-lean/vite the behavior registers itself for every data-slean="menubar"; without it, import the register module once.

+layout.svelte or any client module
import '@svelte-lean/primitives/menubar/register';
stylesheets
import '@svelte-lean/styles/popover.css';
import '@svelte-lean/styles/menu.css';
import '@svelte-lean/styles/menubar.css';

Render a role="menubar" list of role="none" items, each holding a role="menuitem" button with popovertarget and, right after it, its menu: a data-slean="menu" popover of buttons. The bar is visible at load, so its roles and its one tab stop (tabindex="0" on the first item, -1 on the others) are markup; each menu gets its roles and is labelled by its item when it opens.

Listen to slean:select on the bar for the chosen item of any menu. A bar item with aria-disabled="true" is skipped by the arrows and does not open on hover.

Anatomy

PartElementdata-slean-partRequiredNotes
root<ul role="menubar" aria-label="…" data-slean="menubar">–yesNo options.
item<button role="menuitem" popovertarget="…" tabindex="0|-1">–yesInside <li role="none">, before its menu; one has tabindex="0".
menu<div popover data-slean="menu">–yesThe menu contract: role="menu" and aria-labelledby (its item) are written when it opens.

Keyboard

KeyWhenResult
ArrowRight/ArrowLeftfocus on the barNext or previous item, wrapping; swapped under dir="rtl"
Home/Endfocus on the barFirst or last item
ArrowDown/Enter/Spacefocus on an itemOpens its menu; focus moves to the first menu item
ArrowRight/ArrowLeftfocus in a menuCloses it and opens the next or previous menu
Escapefocus in a menuCloses it; focus returns to its item

Styling

menubar.css lays the items out in a bar and draws the item whose menu is open with the current color's soft tint, with :has() on the list item; data-color on the bar or an item picks the color. The menus are drawn by popover.css and menu.css. The tokens it reads:

TokenDefault (light)Applies to
--slean-space-1calc(var(--slean-spacing) * 1)padding
--slean-bordervar(--slean-neutral-6)border
--slean-radius-mdvar(--slean-radius)border-radius
--slean-surfaceoklch(100% 0 0)background
--slean-control-height-sm2remmin-block-size
--slean-space-3calc(var(--slean-spacing) * 3)padding-inline
--slean-radius-smcalc(var(--slean-radius) * 0.6)border-radius
--slean-fgvar(--slean-neutral-12)color
--slean-text-sm0.875remfont-size
--slean-font-weight-medium500font-weight
--slean-mutedvar(--slean-neutral-3)background
--slean-focus-ringvar(--slean-focus-ring-width) solid var(--slean-focus-ring-color)outline
--slean-focus-ring-width2pxoutline-offset
--slean-color-softvar(--slean-primary-soft)background
--slean-color-soft-textvar(--slean-primary-soft-text)color
--slean-control-height-md2.25remmin-block-size

State selectors the stylesheet targets, all from the platform or ARIA: :focus-visible, :hover, :popover-open, [aria-disabled="true"], [role="menuitem"].

Under the hood

Runtime and size
Tier
1 · Delegated micro-behavior
Behavior JS
2063 B brotli · 2324 B gzip · packages/primitives/artifacts/size.json · method
Platform features
role="menubar", popovertarget, showPopover(), :popover-open, :has()
Shared listeners
keydown, focusin, pointerover
Per-instance listeners
none
Lazy state
none
Native base
role="menubar" of popovertarget menu buttons

Keydown, focusin and pointerover handlers with the shared router, plus the menu behavior’s own; a thousand bars keep one listener per type (tests/menubar.test.ts). The open menu is the popover matching :popover-open and the focusable item the one with tabindex="0", so no state is kept.

Why this implementation

Each menu is already a popover opened by its item’s popovertarget, with light dismiss, Escape, focus return and the top layer, and the Menu primitive gives each menu its keys. What makes a row of menus a menubar in the WAI-ARIA pattern is the movement between them: one tab stop for the bar, the side arrows along it, the side arrows inside an open menu moving to the neighbouring menu, and hover switching while a menu is open.

None of that needs memory: the menus are found through their items’ popovertarget, and three shared listeners cover every bar on the page.

The browser owns

  • each menu: showing, light dismiss, Escape, focus return and the top layer (popovertarget)
  • Enter and Space on an item open its menu (native button activation)
  • the expanded state of each item, exposed from popovertarget

Svelte Lean owns

  • one tab stop for the bar; the side arrows, Home and End between its items; RTL
  • ArrowDown to open a menu; the side arrows inside a menu to move to the neighbouring one
  • switching the open menu when the pointer moves to another item
  • the menu behavior inside each menu (the menu contract)
  • menubar.css: the bar, its items and the item whose menu is open, in the current color
Accessibility
  • role="menubar" named by aria-label; each item a menuitem whose expanded state the platform exposes from popovertarget; each menu labelled by its item.
  • One tab stop for the bar; the side arrows, Home and End move along it, mirrored under dir="rtl".
  • ArrowDown, Enter or Space opens a menu and focus moves to its first item; Escape closes it and focus returns to the item.
  • A menubar is for application commands. Site navigation is a <nav> with links.
Platform features
FeatureBaselineOutside the target
Popover APIBaseline 2024 (Chrome 114, Safari 17, Firefox 125)The menus render inline; the bar still lists its items
:has()Baseline 2023The item whose menu is open is not drawn pressed
Without JavaScript

Each item opens its menu natively through popovertarget; the menus' keys and the bar's arrows need the runtime, and Tab reaches only the first item.

Server rendering and hydration

Render the bar with one item at tabindex 0 and every menu closed.

Before the behaviors load, a click opens a menu (native) but the arrow keys do nothing. The first key after hydration works on the server's markup.

Compatibility

The Popover API is Baseline 2024 and :has() Baseline 2023. Submenus and mnemonics are not part of the primitive.

Tests and source