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

mobile-menu

Breakpoint observer for a header menu: shows your popover trigger and hides the desktop nav below a viewport width.

Overview

The <mobile-menu> element is a breakpoint observer. At or below a configurable viewport width it shows its contents and hides sibling <nav>, <nav-bar> and <site-tools> elements in the parent <header>. Above the breakpoint it hides itself.

It generates nothing. You write the trigger button and the menu panel inside it. The panel is a native HTML popover, so opening, closing, Esc and the backdrop work without any script from this element.

Attributes

AttributeTypeDefaultDescription
breakpoint CSS length 52rem Viewport width at or below which mobile mode activates. Observed: changing it re-evaluates immediately.
data-mobile boolean (state) — Set by the element while mobile mode is active. Style against it; do not set it yourself.

Basic Usage

Place <mobile-menu> as a direct child of a <header>, alongside the desktop navigation. Put both the trigger and the panel inside it: the link, section and current-page styles are scoped to mobile-menu. The trigger needs data-trigger and a popovertarget that names the panel's id. Give the panel an aria-label so its navigation landmark has a name.

For automatic aria-current in the panel, wrap its list in a <nav-bar> inside the popover <nav>.

CSS Tokens

Theme the panel appearance with these custom properties:

TokenDefaultDescription
--mobile-menu-bgvar(--color-surface)Panel background
--mobile-menu-radiusvar(--radius-m)Panel border radius
--mobile-menu-shadowSubtle shadowPanel box shadow
--mobile-menu-paddingvar(--size-l)Panel padding
--mobile-menu-max-width280pxPanel maximum width
--mobile-menu-backdropSemi-transparent blackBackdrop overlay color

How It Works

  1. On connect, and whenever breakpoint changes, the element creates a matchMedia('(max-width: …)') observer. The breakpoint is the viewport width, not a container width, because the menu replaces page-level chrome.
  2. When the viewport crosses the breakpoint, it sets or removes [data-mobile] on itself.
  3. CSS does the rest: header:has(> mobile-menu[data-mobile]) > :is(nav, nav-bar, site-tools) is hidden. No inline styles are written, so removing the element (or leaving mobile mode) always restores the desktop chrome.
  4. When switching back to desktop, it closes the panel if it is open.

Moving a header tool into the menu

Search and a theme picker can simply be repeated inside the panel. A tool that should exist once, such as a notification bell, is marked data-mobile-menu in the header instead. In mobile mode the element moves that sibling into the panel, appending it to the element marked data-mobile-menu-slot (or to the end of the panel when there is no slot), and puts it back in its place in the header on leaving mobile mode or when the menu is removed. It is the same element throughout, so its state and listeners carry over. Before upgrade, and without JavaScript, the marked sibling is hidden at the default breakpoint along with the rest of the desktop chrome.

If the moved tool carries data-unread (<notification-wc> sets it while there are unread items), the menu button shows a dot, since the tool itself is out of sight until the menu opens.

The default 52rem breakpoint is also a CSS media query, so the right state paints before the element upgrades (no layout shift) and even without JavaScript. Because popover and popovertarget are native, the menu still opens and closes with JavaScript off. A custom breakpoint needs JavaScript; without it, the desktop nav stays visible at every width.

Accessibility

  • The trigger button should have aria-label="Menu" for screen readers. popovertarget gives it aria-expanded automatically.
  • Label the panel (aria-label="Site menu"). An unnamed <nav> is a second, anonymous navigation landmark.
  • The native popover gives you Esc to close, light dismiss on an outside click, and focus back on the trigger when it closes. It is not modal: there is no focus trap, the rest of the page stays reachable, and the page still scrolls. Focus does not move into the panel when it opens; the panel's links come next in the tab order after the trigger. If you need a modal menu, use a <dialog> opened with showModal() instead.
  • Include a close button inside the panel with popovertargetaction="hide".
  • The current-page link uses --color-text-on-interactive-auto on the interactive colour. In forced-colors mode it also gets an outline and underline.

Related

↑