Getting started

Actions

Forms

Layout

Navigation

Overlays

Feedback

Data display

Controllers

Helpers

Recipes

Popover menu

A button that opens a menu beside it, walked with the arrow keys and closed with Escape or a press outside.

Goal

An actions menu on a row, a card or a toolbar. Opening moves focus into the menu, the arrow keys move between items, and every way of closing it ends with focus back on the button that opened it when the user did not choose to go elsewhere.

Primitives

  • PopoverController shows the panel beside the button and keeps it positioned. With onDismiss set, it also reports an outside press, focus leaving and Escape through a DismissController it owns, so the recipe does not add a second one. portal: false keeps the panel where it is.
  • RovingController gives the menu items one tab stop and moves it with the arrows, Home and End.

Left to the recipe: the open flag, moving focus into the menu on open, and returning focus to the button. The popover reports why it was dismissed and does not restore focus, because that depends on the reason: after Escape or choosing an item the button gets focus, after a press elsewhere the press already moved it.

Example

Open the menu with the button, move with the arrow keys, choose with Enter, or press Escape to close it. Pick an authoring pattern below; they all behave the same, and the one you pick is remembered on the other recipes.

The same behavior as a RadiantElement. The markup is authored inside the element and the element finds its parts with @query; there is nothing to mount, clean up or run again.

Menu closed

import { RadiantElement, customElement, onEvent, query, queryAll } from '@ecopages/radiant';
import { createPopoverController, createRovingController } from '@ecopages/radiant-ui/controllers';

@customElement('popover-menu-element')
export class PopoverMenuElement extends RadiantElement {
	@query('button') private trigger!: HTMLElement;
	@query('[role="menu"]') private menu!: HTMLElement;
	@queryAll('[role="menuitem"]') private items!: HTMLElement[];
	@query('[data-status]') private status!: HTMLElement;

	private open = false;

	private readonly menuPopover = createPopoverController(this, {
		getAnchor: () => this.trigger,
		getFloating: () => this.menu,
		getOpen: () => this.open,
		getPlacement: () => 'bottom-start',
		portal: false,
		onDismiss: (reason) => this.setOpen(false, reason === 'escape'),
	});

	private readonly roving = createRovingController(this, {
		getItems: () => this.items,
		getTarget: () => this.menu,
		orientation: 'vertical',
	});

	override onConnected(): void {
		this.roving.sync();
	}

	private setOpen(next: boolean, restoreFocus = false): void {
		this.open = next;
		this.menuPopover.sync();
		this.trigger.setAttribute('aria-expanded', String(next));
		this.status.textContent = next ? 'Menu open' : 'Menu closed';
		if (next) {
			this.roving.sync();
			this.roving.focus(this.items[0]);
		} else if (restoreFocus) {
			this.trigger.focus();
		}
	}

	@onEvent({ selector: 'button[aria-haspopup]', type: 'click' })
	toggle(): void {
		this.setOpen(!this.open, true);
	}

	@onEvent({ selector: '[role="menuitem"]', type: 'click' })
	choose(event: Event): void {
		this.setOpen(false, true);
		this.status.textContent = `Chose ${(event.target as HTMLElement).textContent}`;
	}
}

createPopoverController(this, ...) and createRovingController(this, ...) are held in fields because the element calls them when the menu opens; both attach when the element connects and are destroyed when it disconnects, as are the @onEvent listeners. The popover still reports the dismissal reason, so the element decides whether focus returns to the button. sync() goes in onConnected because the items exist only after the element has its children.

What the Radiant form removes: the mount function and its registration, the cleanup to return and call, and the re-run after a client-side navigation.

Styles

The menu hides with hidden and sits in the flow under the button; add position: absolute to float it over what follows. The stylesheet is the popover-menu.css tab in the files above.