Vanilla Breeze

mobile-menu

Responsive mobile navigation menu with hamburger toggle and popover panel.

Overview

The <mobile-menu> element provides responsive navigation that activates at a configurable viewport breakpoint. Below the breakpoint, it shows a hamburger toggle and hides sibling <nav> and <site-tools> elements in the parent header. Above the breakpoint, it hides itself completely.

The menu panel uses the native HTML popover API for the overlay, with backdrop support and keyboard accessibility built in.

Attributes

AttributeTypeDefaultDescription
breakpoint CSS length 52rem Viewport width below which mobile mode activates

Basic Usage

Place <mobile-menu> inside a <header> alongside the desktop navigation. The trigger button must have data-trigger and popovertarget pointing to the panel.

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 connectedCallback, the component creates a matchMedia observer for the configured breakpoint.
  2. When the viewport crosses the breakpoint, it sets or removes [data-mobile] on itself.
  3. In mobile mode, the component hides sibling <nav> and <site-tools> elements in the parent header.
  4. When switching back to desktop, it restores sibling visibility and closes any open popover.

Without JavaScript, the element stays hidden (display: none by default). This is progressive enhancement — the desktop nav remains functional.

Accessibility

  • The trigger button should have aria-label="Menu" for screen readers.
  • The popover panel uses the native popover API which provides focus trapping and Esc to close.
  • Include a close button inside the panel with popovertargetaction="hide".

Related