Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

context-menu

Right-click context menu with keyboard navigation and viewport-aware positioning.

Description

The <context-menu> component provides a custom right-click menu that opens at the cursor position. It supports keyboard navigation, separators, danger items, and disabled states. Shares menu styling patterns with <drop-down>.

It is keyboard-operable too: a trigger that is not already focusable gets tabindex="0", and Shift+F10 or the ContextMenu key opens the menu while focus is on the trigger or on anything inside it. Focus returns to where it was when the menu closes. Any action in a context menu should still be reachable another way — a toolbar button or inline control — because touch users and many screen reader users never right-click.

Basic Usage

Structure

ElementRequiredDescription
any element with data-triggeryesRight-click target area. Made focusable (tabindex="0") when it is not already. A control (button, link, or a focusable element with a widget role) gets aria-haspopup="menu", aria-expanded and aria-controls; any other trigger gets an aria-description instead. These are removed on disconnect
<menu>yesContext menu with <li> items holding <button> or <a> actions; <li data-group> labels and <li role="separator"> rows

Child Attributes

AttributeOnValuesDescription
data-trigger*booleanElement defining the right-click target area
data-grouplibooleanGroup label row. It and the items after it (up to the next label or separator) are wrapped in role="group" named by the label
data-shortcutbutton, astringKeyboard shortcut bound page-wide (hotkey-bind, preventDefault) and shown as a badge, e.g. 'f2' or 'shift+d'. Never use clipboard combos.
data-disabledbutton, abooleanDisabled item: aria-disabled="true", stays in the arrow-key order, cannot be activated
data-valuebutton, astringValue reported in context-menu:select detail.value and the items property

Disabled Items

Mark an item data-disabled. It gets aria-disabled="true", stays in the arrow-key order (so screen reader users learn it exists, per the APG menu pattern), and cannot be activated: no click handlers run, the menu stays open, and its shortcut does nothing. A native disabled button is skipped by the arrow keys because it cannot take focus.

With Icons

Add <icon-wc> or inline <svg> inside buttons. The existing flex layout handles alignment automatically.

Keyboard Shortcuts

Add data-shortcut to any button to display a keyboard shortcut badge and bind a real global keyboard shortcut. Pressing the shortcut anywhere on the page (outside text fields) triggers the button's click. The badge is platform-aware — meta shows ⌘ on Mac and Ctrl elsewhere — and the item gets a matching aria-keyshortcuts. Shortcuts are cleaned up when the component disconnects.

Warning: the binding is page-wide and calls preventDefault(). Binding meta+c, meta+x, meta+v or meta+a would break native copy, cut, paste and select-all everywhere on the page. Choose combos that do not shadow browser or OS shortcuts (function keys and shift+ letters are safe choices).

Group Labels

Use <li data-group> to create labeled sections within the menu. Group labels are non-interactive and visually separated from items. Non-first groups automatically get a top border. On upgrade, each label and the items after it (up to the next label or separator) are wrapped in <li role="group" aria-labelledby> so assistive technology announces the group name; the authored flat list is restored on disconnect, and the items property reads through the wrapper.

Keyboard Navigation

Key Action
Shift+F10 / ContextMenuOpen the menu, with focus on the trigger or on anything inside it
Arrow DownMove to next item
Arrow UpMove to previous item
HomeMove to first item
EndMove to last item
Enter / SpaceActivate focused item
EscapeClose menu and return focus to the trigger
TabClose menu

JavaScript API

Properties

Property Type Description
items Array Get or replace the menu as data. Entries are { label, value?, href?, shortcut?, disabled? }, { group } or { separator: true }. Setting it rebuilds the menu and fires context-menu:items-changed.
isOpen boolean Read-only. Whether the menu is open.

Methods

Method Description
close() Close the context menu

Events

Event Description
context-menu:open Fired when the menu opens
context-menu:close Fired when the menu closes
context-menu:select Fired when an item is activated by click, keyboard or its shortcut. detail.item is the activated element, detail.value its data-value. Not fired for disabled items.
context-menu:items-changed Fired after the items setter rebuilds the menu (detail: { items, source: 'property' })

Accessibility

  • Opens from the keyboard: the trigger is made focusable when needed, and Shift+F10 / ContextMenu open the menu while focus is on the trigger or on a control inside it
  • A trigger that is a control (a button, a link, or a focusable element with a widget role such as role="button") carries aria-haspopup="menu", aria-expanded and aria-controls
  • Any other trigger, such as a <p>, a <section> or an image, keeps its own semantics and gets no popup state, because screen readers ignore or misreport those states on an element that is not a control. It gets aria-description="Context menu available" instead, so the menu is announced when the trigger takes focus. Change the text with data-label-available on <context-menu>; a trigger that already has aria-description or aria-describedby is left alone
  • The attributes the component adds to the trigger are removed when it disconnects
  • Full keyboard navigation (Arrow keys, Home/End, Enter, Escape); focus returns to where it was on close
  • Menu items have role="menuitem", their <li> wrappers role="none", separators role="separator"
  • Group labels name a role="group" through aria-labelledby
  • data-disabled items expose aria-disabled="true" and stay reachable but inert
  • Keyboard focus shows a focus ring, not only a background tint
  • Closes on click outside, Escape, or scroll
  • Menu is positioned within viewport bounds
↑