本文へ移動
日本語

仕組み

Items with an href render as <a>, others as <button>; parents are disclosure buttons and never selected themselves. The selected item sits on one pill element behind the items, moved with transform: it glides only when the selected element changes, and follows layout changes (a submenu opening above, the rail collapsing) instantly through a ResizeObserver. Until it is measured the selected item paints the pill itself, so the server render already looks right. Submenus open with grid-template-rows 0fr to 1fr. It is a list of navigation links, not role="menu": every item is in the Tab order and the arrow keys are a shortcut. When collapsed, text turns transparent so names stay readable, while the tooltip and flyout hang under the root with a measured top, so scrolling cannot clip them.

向いている場面

  • The main navigation of an app or workspace: five or more destinations, with groups or one level of submenus.
  • Pages that need room for content (wide tables, dashboards), where the nav collapses to an icon rail or collapses automatically in narrow windows.
  • The sidebar slot of an app shell: pass the shell's collapsed state through.

向かない場面

  • Five or fewer destinations that will not grow: use a top bar or tabs.
  • More than one level of nesting: use a tree, or rethink the structure.
  • Actions that run on click (export, delete): use a dropdown menu, which is role="menu".
  • The primary navigation of a phone app: use a bottom tab bar.

使い方

import { SideNav } from './SideNav.jsx'

const items = [
  {
    type: 'group',
    label: 'Workspace',
    children: [
      { key: 'overview', label: 'Overview', icon: <GridIcon />, href: '/overview' },
      {
        key: 'projects',
        label: 'Projects',
        icon: <BoxIcon />,
        children: [
          { key: 'active', label: 'Active', href: '/projects/active' },
          { key: 'archived', label: 'Archived', href: '/projects/archived' },
        ],
      },
      { key: 'tasks', label: 'Tasks', icon: <ListIcon />, href: '/tasks', badge: 12 },
    ],
  },
  { key: 'settings', label: 'Settings', icon: <GearIcon />, href: '/settings' },
]

export function AppNav({ collapsed }) {
  const { pathname } = useLocation()
  const navigate = useNavigate()
  return (
    <SideNav
      items={items}
      collapsed={collapsed}
      selectedKey={keyOfPath(pathname)}
      header={
        <button type="button" className="ds-sidenav__brand" onClick={openWorkspaces}>
          <span className="ds-sidenav__brand-logo">H</span>
          <span className="ds-sidenav__brand-text">
            <span className="ds-sidenav__brand-name">Harbor Studio</span>
            <span className="ds-sidenav__brand-meta">Team · 8 seats</span>
          </span>
        </button>
      }
      onSelect={(_key, item, event) => {
        // Link items: stop the full page load and hand over to the router
        if (item.href) {
          event.preventDefault()
          navigate(item.href)
        }
      }}
    />
  )
}

// The width comes from outside: 220px expanded, 64px as an icon rail
<aside style={{ width: collapsed ? 64 : 220, height: '100vh' }}>
  <AppNav collapsed={collapsed} />
</aside>

プロパティ

プロパティ型既定値説明
itemsSideNavEntry[]—Menu content. Item: { key, label, icon?, href?, target?, badge?, disabled?, children? } where children is a submenu (one level). Group: { type: 'group', label, children }.
selectedKeystring | null—Controlled: key of the selected item (null for none). Usually derived from the current route.
defaultSelectedKeystring—Uncontrolled: key of the initially selected item.
onSelect(key, item, event) => void—Called when an item is clicked. To switch pages without a full load, call event.preventDefault() and hand over to your router. Modifier-clicks on links do not call it.
openKeysstring[]—Controlled: keys of the open parents. Use with onOpenChange.
defaultOpenKeysstring[]—Uncontrolled: initially open parents. By default the parent of the selected item opens.
onOpenChange(openKeys) => void—Called when the user opens or closes a submenu.
collapsedbooleanfalseCollapses to an icon rail: text becomes a hover or focus tooltip and submenus become flyouts. The outer element sets the width (64px).
tone'ink' | 'tint''ink'Look of the selected pill: ink (body text colour with surface text) or the soft accent tint.
headerReactNode | ({ collapsed }) => ReactNode—Top slot, 56px high to line up with a top bar. Put a workspace switcher or a logo here; the ds-sidenav__brand helper classes (__brand-logo, __brand-text, __brand-name, __brand-meta, __brand-caret) give it the look, and their text fades out in the icon rail.
footerReactNode | ({ collapsed }) => ReactNode—Bottom slot, fixed and not scrolling with the menu. The ds-sidenav__item, __icon and __label classes work inside it.
labelstring'Main navigation'Accessible name of the <nav>. Give each navigation on a page its own name.
...restHTMLAttributes—Other attributes go to the root <nav>. Set --ds-sidenav-z to change the tooltip and flyout layer (default 30).

アクセシビリティ

  • The root is <nav aria-label> with <ul> lists; each group list points to its title with aria-labelledby.
  • Links are <a>, the rest <button>; the selected item has aria-current="page", and parents have aria-expanded and aria-controls. Closed submenus are hidden from Tab.
  • Every item is in the Tab order; Up/Down move between visible items, Home/End jump to the ends, Right opens a submenu or enters it, Left closes it or returns to the parent.
  • In the icon rail, names remain for screen readers; the tooltip appears on hover and keyboard focus, and Escape closes it.
  • A flyout is a titled group: keyboard opening moves focus inside, Escape or Left closes it and returns to the parent, and Escape does not close an outer drawer or dialog.
  • Items have a focus ring; on touch screens each is 44px tall. Reduced motion moves the pill without gliding, opens submenus directly and only fades tooltips.

モーション

The selection pill glides to the new item in 300ms ease-out while the new label turns to the pill colour a moment later; it never glides when only the layout moves. Submenus open in 240ms (grid rows 0fr to 1fr) with the children settling 4px down, and the chevron turns 90°. Collapsing fades text out in 100ms; expanding fades it in after a 100ms wait. The tooltip (an ink pill with a caret) and the flyout scale from 0.96 next to the item; the flyout leaves in 140ms. Press scales an item to 0.98.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

SideNav.jsxside-nav.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

ナビゲーション