Skip to Content
Neshel-UI
Components

MobileMenu

A navigation sheet for small screens that closes after navigation, on Escape and at its breakpoint.

Client component ("use client")

Purpose

MobileMenu puts navigation links into a sheet below a breakpoint. Its trigger is hidden at and above the breakpoint, and an open menu closes automatically when the viewport reaches it.

Import

import.tsx
import {
  MobileMenu,
  MobileMenuContent,
  MobileMenuLink,
  MobileMenuNav,
  MobileMenuTrigger,
} from "@neshel-ui/components";

Usage

The demo uses breakpoint="2xl" so you can open it at most widths; in a real navbar pair it with NavbarNav hideBelow (see Navbar).

Preview: mobile-menu-basic
Preview theme
open: false
mobile-menu-basic.tsx
"use client";
import {
  MobileMenu,
  MobileMenuContent,
  MobileMenuLink,
  MobileMenuNav,
  MobileMenuTrigger,
} from "@neshel-ui/components";
import { Inline } from "@neshel-ui/layout";
import { Text } from "@neshel-ui/typography";
import { useState } from "react";

/**
 * A MobileMenu that stays available at every width (breakpoint "2xl" hides it only from 96rem),
 * controlled so the page can show its state. It closes after a link is activated.
 */
export default function MobileMenuBasic() {
  const [open, setOpen] = useState(false);
  return (
    <Inline space="3" align="center">
      <MobileMenu breakpoint="2xl" open={open} onOpenChange={setOpen}>
        <MobileMenuTrigger label="Open navigation" />
        <MobileMenuContent title="Navigation">
          <MobileMenuNav>
            <MobileMenuLink href="#home" active>
              Home
            </MobileMenuLink>
            <MobileMenuLink href="#pricing">Pricing</MobileMenuLink>
            <MobileMenuLink href="#contact">Contact</MobileMenuLink>
          </MobileMenuNav>
        </MobileMenuContent>
      </MobileMenu>
      <Text tone="muted" size="sm">
        open: {String(open)}
      </Text>
    </Inline>
  );
}

API

MobileMenuProps

MobileMenuProps
PropTypeDefaultDescription
breakpointBreakpoint"md"At and above this breakpoint the trigger is hidden and the menu closes (default "md").
childrenReactNode——
defaultOpenboolean——
onOpenChange(open: boolean) => void——
openboolean——

MobileMenuTriggerProps

MobileMenuTriggerProps
PropTypeDefaultDescription
labelstring"Open menu"Accessible name (default "Open menu").
refRef<HTMLButtonElement>——

Also accepts the props of: React DOM attributes.

MobileMenuContentProps

MobileMenuContentProps
PropTypeDefaultDescription
closeLabelstring"Close"Accessible name of the close button (default "Close").
containerHTMLElement | nulldocument.bodyPortal target (default document.body, wrapped with the trigger's theme).
refRef<HTMLDivElement>——
showClosebooleantrueRender a close button with an accessible label (default true).
sideSheetSide"left"Edge the menu slides from (default "left").
size"sm" | "md" | "lg""md"Width (left/right) or height (top/bottom) of the sheet (default "md").
titleReactNode"Menu"Dialog title (default "Menu").
titleHiddenbooleantrueKeep the title for assistive technology only (default true).

Also accepts the props of: @radix-ui/react-dialog, @radix-ui/react-dismissable-layer, @radix-ui/react-primitive, React DOM attributes.

MobileMenuNavProps

MobileMenuNavProps
PropTypeDefaultDescription
refRef<HTMLElement>——

Also accepts the props of: React DOM attributes.

MobileMenuLinkProps

MobileMenuLinkProps
PropTypeDefaultDescription
activeboolean—Current page: sets aria-current="page".
asChildboolean—Render the single child element (e.g. a router link) with the link styles.
refRef<HTMLAnchorElement>——

Also accepts the props of: React DOM attributes.

  • MobileMenu: breakpoint (default "md", any configured name), open / defaultOpen / onOpenChange.
  • MobileMenuTrigger: a button with a default icon and the accessible label ("Open menu").
  • MobileMenuContent: a sheet (side default "left") titled by title ("Menu"), visually hidden by default (titleHidden).
  • MobileMenuLink: active (aria-current="page"), asChild for router links. Activating a link closes the menu.

Accessibility

The content is a modal dialog: focus is trapped and returns to the trigger. Translate label and title for non-English interfaces.

Keyboard

KeyAction
Enter / Space on the triggerOpen the menu
TabMove through the links
EscapeClose

Common mistakes

  • Different breakpoints on MobileMenu and NavbarNav hideBelow.
  • Putting the whole page navigation only in a MobileMenu with a breakpoint larger than common desktops: desktop users would lose it.
Edit on GitHub

On this page