Panel Nav

Titled sections of links with icon tiles, for a side panel.

The current page’s row is shaded and its icon tile filled. Section titles are small capitals.

Preview
basic.tsx
import { LayoutPanelTop, Megaphone, Newspaper, Type } from "lucide-react"

import { PanelNav, PanelNavItem, PanelNavSection } from "~/components/panel-nav"

export default function Example() {
  return (
    <div className="w-80 border border-border bg-background">
      <PanelNav aria-label="Print">
        <PanelNavSection title="Product · Daily">
          <PanelNavItem href="#editions" icon=<Newspaper />>
            Editions
          </PanelNavItem>
          <PanelNavItem href="#styles" icon=<Type />>
            Paragraph styles
          </PanelNavItem>
        </PanelNavSection>
        <PanelNavSection title="Edition · Weekend">
          <PanelNavItem href="#pages" icon=<LayoutPanelTop /> active>
            Pages
          </PanelNavItem>
          <PanelNavItem href="#ads" icon=<Megaphone />>
            Ads
          </PanelNavItem>
        </PanelNavSection>
      </PanelNav>
    </div>
  )
}

Dependencies

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

UsesTextprimitive
Used byNothing yet.
Packages None.

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/panel-nav.json

Manually

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/panel-nav.tsx
import type * as React from "react"

import { textVariants } from "~/components/ui/text"
import { cn } from "~/lib/utils"

/*
 * Navigation inside a side panel: titled sections of links, each with an
 * optional icon in a bordered tile. The current page's row is shaded and its
 * tile filled. Rows without an icon are shorter, for long lists.
 *
 * <PanelNav aria-label="…">
 *   <PanelNavSection title="…">
 *     <PanelNavItem href="…" icon={…} active>…</PanelNavItem>
 *   </PanelNavSection>
 * </PanelNav>
 */

/** The panel's navigation landmark. Name it with `aria-label`. */
function PanelNav({ className, ...props }: React.ComponentProps<"nav">) {
  return <nav data-slot="panel-nav" className={cn("flex flex-col", className)} {...props} />
}

type PanelNavSectionProps = Omit<React.ComponentProps<"section">, "title"> & {
  /** A short heading, shown small and in capitals. */
  title: React.ReactNode
}

/** A titled group of links, ruled off from the next. */
function PanelNavSection({ title, className, children, ...props }: PanelNavSectionProps) {
  return (
    <section
      data-slot="panel-nav-section"
      className={cn("border-b border-border px-md py-lg last:border-b-0", className)}
      {...props}
    >
      <h3
        className={cn(
          textVariants({ size: "xs", weight: "medium", caps: true, tone: "muted" }),
          "mb-xs px-md",
        )}
      >
        {title}
      </h3>
      <ul className="grid gap-3xs">{children}</ul>
    </section>
  )
}

type PanelNavItemProps = React.ComponentProps<"a"> & {
  /** An icon, shown in a bordered tile. Leave it out for a compact row of text. */
  icon?: React.ReactNode
  /** Whether this is the page you're on. */
  active?: boolean
}

/** A link, with its icon tile when it has an icon. */
function PanelNavItem({ icon, active = false, className, children, ...props }: PanelNavItemProps) {
  return (
    <li>
      <a
        data-slot="panel-nav-item"
        aria-current={active ? "page" : undefined}
        className={cn(
          textVariants({ size: "sm", weight: active ? "medium" : "normal" }),
          "group/item flex h-8 items-center gap-sm rounded-lg px-md text-foreground outline-none has-data-[slot=panel-nav-icon]:h-9",
          "hover:bg-muted focus-visible:ring-3 focus-visible:ring-ring/30 aria-[current=page]:bg-muted",
          className,
        )}
        {...props}
      >
        {icon !== undefined && (
          <span
            data-slot="panel-nav-icon"
            aria-hidden="true"
            className="flex size-6 shrink-0 items-center justify-center rounded-md border border-border text-muted-foreground group-aria-[current=page]/item:border-primary group-aria-[current=page]/item:bg-primary group-aria-[current=page]/item:text-primary-foreground [&_svg]:size-3.5"
          >
            {icon}
          </span>
        )}
        <span className="min-w-0 truncate">{children}</span>
      </a>
    </li>
  )
}

export { PanelNav, PanelNavItem, PanelNavSection }
export type { PanelNavItemProps, PanelNavSectionProps }

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.

PanelNav

The panel's navigation landmark. Name it with aria-label.

Takes the props of a native div.

PanelNavItem

A link, with its icon tile when it has an icon.

activeours

Whether this is the page you're on.

Type boolean · Default false

iconours

An icon, shown in a bordered tile. Leave it out for a compact row of text.

Type ReactNode

PanelNavSection

A titled group of links, ruled off from the next.

title*ours

A short heading, shown small and in capitals.

Type ReactNode