Nav Sheet

A site's main navigation on a small screen, as a sheet of large links.

The sheet covers the screen on phones and slides in as a column on wider ones. Its top row is as tall as Workspace’s header, so the close button lands where the menu button was. Each link is a full-width row with an optional line under its label; the current one is shaded and marked at its edge. Open it from the menu button below.

Preview
basic.tsx
import {
  NavSheet,
  NavSheetContent,
  NavSheetFooter,
  NavSheetLink,
  NavSheetList,
  NavSheetTrigger,
} from "~/components/nav-sheet"
import { Button } from "~/components/ui/button"

export default function Example() {
  return (
    <NavSheet>
      <NavSheetTrigger />
      <NavSheetContent title="Newsroom">
        <NavSheetList aria-label="Sections">
          <NavSheetLink href="#news" description="Today's stories, as they're filed." active>
            News
          </NavSheetLink>
          <NavSheetLink href="#opinion" description="Columns and letters.">
            Opinion
          </NavSheetLink>
          <NavSheetLink href="#sport">Sport</NavSheetLink>
          <NavSheetLink href="#archive">Archive</NavSheetLink>
        </NavSheetList>
        <NavSheetFooter>
          <Button variant="outline" className="flex-1">
            Sign in
          </Button>
        </NavSheetFooter>
      </NavSheetContent>
    </NavSheet>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesButtonprimitiveScroll AreaprimitiveSheetTextprimitive
Used byNothing yet.
Packages lucide-react@1.73.0

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/nav-sheet.json

Manually

Install its packages:

terminal
npm install lucide-react@1.73.0

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/nav-sheet.tsx
import { ChevronRight, Menu, X } from "lucide-react"
import type * as React from "react"

import { Button } from "~/components/ui/button"
import { ScrollArea } from "~/components/ui/scroll-area"
import {
  Sheet,
  SheetBackdrop,
  SheetClose,
  SheetContent,
  SheetTitle,
  SheetTrigger,
} from "~/components/ui/sheet"
import { textVariants } from "~/components/ui/text"
import { cn } from "~/lib/utils"

/*
 * A site's main navigation on a small screen: a sheet as wide as the screen
 * whose top row sits where the page header was, with the close button where
 * the menu button was, then a list of large links.
 *
 * <NavSheet>
 *   <NavSheetTrigger />
 *   <NavSheetContent title="…">
 *     <NavSheetList aria-label="…">
 *       <NavSheetLink href="…" description="…" active>…</NavSheetLink>
 *     </NavSheetList>
 *     <NavSheetFooter>…</NavSheetFooter>
 *   </NavSheetContent>
 * </NavSheet>
 */

/** Holds the sheet's open state. It slides in from the right. */
function NavSheet(props: React.ComponentProps<typeof Sheet>) {
  return <Sheet side="right" {...props} />
}

/** The menu button that opens the sheet. Named "Menu" unless you name it. */
function NavSheetTrigger({ children, ...props }: React.ComponentProps<typeof SheetTrigger>) {
  return (
    <SheetTrigger aria-label="Menu" render={<Button variant="ghost" size="icon-sm" />} {...props}>
      {children ?? <Menu />}
    </SheetTrigger>
  )
}

type NavSheetContentProps = Omit<
  React.ComponentProps<typeof SheetContent>,
  "title" | "children"
> & {
  children?: React.ReactNode
  /** The site's name, in the top row. */
  title: React.ReactNode
}

/**
 * The sheet: a top row as tall as Workspace's header, with the title and a
 * close button, then the links, which scroll when there are many.
 */
function NavSheetContent({ title, className, children, ...props }: NavSheetContentProps) {
  return (
    <>
      <SheetBackdrop />
      <SheetContent
        data-slot="nav-sheet-content"
        className={cn(
          "gap-0 data-[side=right]:w-full data-[side=right]:border-l-0 sm:data-[side=right]:max-w-sm sm:data-[side=right]:border-l",
          className,
        )}
        {...props}
      >
        <div className="flex h-11 shrink-0 items-center justify-between gap-sm border-b border-border px-lg">
          <SheetTitle className="min-w-0">{title}</SheetTitle>
          <SheetClose render={<Button variant="ghost" size="icon-sm" aria-label="Close menu" />}>
            <X />
          </SheetClose>
        </div>
        <ScrollArea className="min-h-0 flex-1">
          <div className="flex flex-1 flex-col">{children}</div>
        </ScrollArea>
      </SheetContent>
    </>
  )
}

type NavSheetListProps = Omit<React.ComponentProps<"nav">, "title"> & {
  /** A quiet heading above the links, when there's more than one list. */
  title?: React.ReactNode
}

/** A navigation landmark of links, one to a row. Name it with `aria-label`. */
function NavSheetList({ title, className, children, ...props }: NavSheetListProps) {
  return (
    <nav data-slot="nav-sheet-list" className={cn("flex flex-col", className)} {...props}>
      {title !== undefined && (
        <h2 className={cn(textVariants({ size: "sm", tone: "muted" }), "px-lg pt-xl pb-sm")}>
          {title}
        </h2>
      )}
      <ul className="flex flex-col">{children}</ul>
    </nav>
  )
}

type NavSheetLinkProps = React.ComponentProps<"a"> & {
  /** A line under the label, saying what's there. */
  description?: React.ReactNode
  /** Whether this is where you are. */
  active?: boolean
}

/** A large link across the sheet, ruled off from the next. The current one is marked at its edge. */
function NavSheetLink({
  description,
  active = false,
  className,
  children,
  ...props
}: NavSheetLinkProps) {
  return (
    <li className="border-b border-border">
      <a
        data-slot="nav-sheet-link"
        aria-current={active ? "page" : undefined}
        className={cn(
          "group/link relative flex items-center gap-md px-lg py-lg outline-none",
          "hover:bg-muted focus-visible:bg-muted aria-[current=page]:bg-muted",
          "before:absolute before:inset-y-0 before:left-0 before:w-0.5 before:bg-primary before:opacity-0 aria-[current=page]:before:opacity-100",
          className,
        )}
        {...props}
      >
        <span className="flex min-w-0 flex-1 flex-col gap-3xs">
          <span className={textVariants({ size: "2xl", weight: "medium", tone: "default" })}>
            {children}
          </span>
          {description !== undefined && (
            <span className={cn(textVariants({ size: "sm", tone: "muted" }), "line-clamp-2")}>
              {description}
            </span>
          )}
        </span>
        <ChevronRight
          aria-hidden="true"
          className="size-5 shrink-0 text-muted-foreground transition-transform group-hover/link:translate-x-0.5 group-aria-[current=page]/link:text-foreground"
        />
      </a>
    </li>
  )
}

/** The bottom of the sheet, for secondary things like settings or a version. */
function NavSheetFooter({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="nav-sheet-footer"
      className={cn("mt-auto flex items-center gap-sm px-lg py-lg", className)}
      {...props}
    />
  )
}

export { NavSheet, NavSheetContent, NavSheetFooter, NavSheetLink, NavSheetList, NavSheetTrigger }
export type { NavSheetContentProps, NavSheetLinkProps, NavSheetListProps }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

NavSheet

Holds the sheet's open state. It slides in from the right.

closeOnOutsidePress

Whether pressing outside the drawer, like on its backdrop, closes it.

Type boolean · Default true, or false while the drawer isn't modal

container

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type Element | DocumentFragment | null

controller

Opens, closes and moves the drawer from anywhere, and connects triggers and outlets outside it. Create one with createDrawerController().

Type DrawerController<any, any>

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

defaultOpen

Whether the dialog starts open, when uncontrolled. Focus moves into it once it's on the page.

Type boolean · Default false

defaultSnapPoint

The snap point it opens at, when uncontrolled. It opens there again each time.

Type SnapPoint · Default the first of `snapPoints`

dismissible

Whether Escape, an outside press and Android's back gesture can close the dialog. Close parts, triggers and the controller always can. Pass false while a form has unsaved changes, and ask in onDismissAttempt.

Type boolean · Default true

driver

How it moves. "pointer" follows touch, mouse and pen with a spring fitted to iOS. "scroll-snap" uses the browser's own scrolling for bottom drawers. "auto" picks scroll snap for a bottom drawer on a touch screen, unless modalAbove is set or expandOnScroll is false, and the pointer driver otherwise.

Type SheetDriverName · Default "auto"

expandOnScroll

Whether dragging toward a larger snap point on scrollable content moves the drawer first, until it's at its largest.

Type boolean · Default true

modal

true: the rest of the page is inert and doesn't scroll, focus is trapped, and an outside press only closes the drawer. "trap-focus": traps focus and leaves the page usable. false: a drawer beside the page. See modalAbove to make it modal only above a snap point.

Type boolean | "trap-focus" · Default true

modalAbove

At or below this snap point the page stays usable and undimmed, like Maps: no inert page, no scroll lock, no aria-modal, and presses beside the drawer reach the page. Above it, modal applies. Changes once the drawer comes to rest.

Type SnapPoint

onDismissAttempt

Runs when a dismissal was refused because dismissible is false, including a swipe. The drawer resists and springs back. Ask "Discard changes?" here.

Type (details: DrawerDismissAttemptDetails) => void

onOpenChange

Runs when the drawer wants to open or close. details.reason says why, "swipe" included, details.cancel() stops it, and details.returnValue carries what closed it.

Type (open: boolean, details: DialogOpenChangeDetails<DrawerOpenChangeReason>) => void

onOpenChangeComplete

Runs once the dialog has finished opening or closing, after any animation.

Type (open: boolean) => void

onPayloadChange

Runs when the payload the content shows changes: a trigger with another payload opens the drawer, or takes a non-modal drawer over while it's open, which fires no onOpenChange, and so does a controller's open({ payload }) on the open drawer. details.cancel() keeps the previous payload.

Type (payload: unknown, details: ChangeDetails<DialogPayloadChangeReason>) => void

onSnapPointChange

Runs when the drawer moves to another snap point. details.reason says why: a swipe, the handle, the keyboard, a resize or the controller.

Type (snapPoint: SnapPoint, details: ChangeDetails<DrawerSnapPointChangeReason>) => void

open

Whether the dialog is open. Pass it to control the dialog.

Type boolean

pendingAnnouncement

What screen readers hear once a Close part's action has been running for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.

Type string · Default "Loading"

persistentElements

Elements that stay usable while the dialog makes the page inert, like a chat launcher or a password manager's button. Elements, CSS selectors, or a function that returns elements, read when the dialog opens. Presses on them never close it.

Type (string | Element)[] | (() => Element[])

physics

Overrides the motion: spring, projection, rubber band and dim. Starts from sheetPhysics.ios.

Type Partial<SheetPhysics>

preventEdgeSwipe

Whether presses on the drawer near the screen's left and right edges are kept from starting iOS Safari's back and forward swipe. Taps right at those edges then don't click. Read when the content mounts.

Type boolean · Default false

side

The edge it comes from. "start" and "end" follow the writing direction.

Type Side · Default "bottom"

snapPoint

The snap point it rests on. Pass it to control it.

Type SnapPoint | null

snapPoints

Where the drawer can rest, from the edge it comes from: a fraction of the screen up to 1, pixels above 1, "…px", "…rem", "…%", or "content" for its own size. None can be larger than the drawer, which you size with CSS.

Type SnapPoint[] · Default ["content"]

themeColor

Whether <meta name="theme-color"> dims with the backdrop, so Safari's toolbars darken too. Written where motion starts and ends, never per frame.

Type boolean · Default false

NavSheetContent

The sheet: a top row as tall as Workspace's header, with the title and a close button, then the links, which scroll when there are many.

title*ours

The site's name, in the top row.

Type ReactNode

className

Class names, or a function of the part's state that returns them.

Type string | ((state: DrawerContentState<unknown>) => string)

container

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type Element | DocumentFragment | null

finalFocus

Where focus goes when the dialog closes while focus is inside it.

Type PopupFocusTarget · Default "trigger"

initialFocus

Where focus goes when the dialog opens. Opening with touch focuses the dialog itself, so no on-screen keyboard appears, unless this is set to something other than "auto".

Type DialogInitialFocus · Default "auto"

keepMounted

Whether the dialog stays in the page, hidden, while closed. Its content keeps its state between opens.

Type boolean · Default false

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<DrawerContentState<unknown>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: DrawerContentState<unknown>) => CSSProperties)

NavSheetFooter

The bottom of the sheet, for secondary things like settings or a version.

Takes the props of a native div.

A large link across the sheet, ruled off from the next. The current one is marked at its edge.

activeours

Whether this is where you are.

Type boolean · Default false

descriptionours

A line under the label, saying what's there.

Type ReactNode

NavSheetList

A navigation landmark of links, one to a row. Name it with aria-label.

titleours

A quiet heading above the links, when there's more than one list.

Type ReactNode

NavSheetTrigger

The menu button that opens the sheet. Named "Menu" unless you name it.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: DialogTriggerState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: DialogTriggerState) => string)

controller

Connects a trigger outside the drawer, like one per table row, to the drawer with this controller.

Type DrawerController<any, any>

disabled

Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless focusableWhenDisabled is set.

Type boolean · Default false

focusableWhenDisabled

Keeps a disabled trigger in the tab order with aria-disabled instead of the native attribute, so it can host a tooltip that says why. Presses still do nothing.

Type boolean · Default false

payload

Data for the content to render when this trigger opens the dialog, like the row it belongs to.

Type unknown

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<DialogTriggerState>

shortcut

A keyboard shortcut that presses the trigger, like "mod+k". mod is Command on Apple platforms and Control elsewhere. Sets aria-keyshortcuts, and a Shortcut part inside shows it. A modal dialog pauses the page's shortcuts while it's open.

Type string

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: DialogTriggerState) => CSSProperties)