# Sidebar Nav

An app's side navigation, which collapses to a rail of icons.

The app's left rail: a workspace switcher, primary links, labelled sections of links (projects, teams) and a collapsible list of recent chats. It collapses to a 52px rail of icons without moving a single glyph: the 224px tree stays put and the shell clips it, while the labels fade and drift away. A highlight glides between rows under the pointer and with keyboard focus; the collapse is instant from the keyboard. The workspace switcher is our menu with a custom trigger (Zag menu machine through Ark UI); the chats section is our collapsible, the toggle our button, the badge our count, and every icon-only control in the rail gets our tooltip; links are native elements.

Kind: block · Category: Navigation · Status: beta · Targets: React, Vue, Svelte

## Add

```bash
npx fndn add sidebar-nav
```

It writes the folder `sidebar-nav/` into the project's ui folder (`index.ts`, `sidebar-nav.tsx`, `SidebarNav.vue`, `SidebarNav.svelte`, `sidebar-nav.recipe.ts`), and what it is built from: [Menu](/components/menu.md), [Collapsible](/components/collapsible.md), [Button](/components/button.md), [Count](/components/count.md), [Empty State](/components/empty-state.md), [Tooltip](/components/tooltip.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { SidebarNav } from "@/components/ui/sidebar-nav";
```

```vue
import { SidebarNav } from "@/components/ui/sidebar-nav";
```

```svelte
import { SidebarNav } from "$lib/components/ui/sidebar-nav";
```

## Parts

In the DOM the root carries `data-slot="sidebar-nav"` and each part `data-slot="sidebar-nav-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `nav` | `sidebar-nav` |  | aria-label; data-collapsed while collapsed; carries kbd-instant. Its width is the only thing that changes size (224px ↔ 52px); it clips the panel |
| `panel` | `div` | `sidebar-nav-panel` |  | The fixed 224px tree; never re-laid out by a collapse |
| `header` | `div` | `sidebar-nav-header` |  |  |
| `switcher` | `div` | `sidebar-nav-switcher` |  | The menu root (the Menu's root); its popup is portaled |
| `workspace` | `button` | `sidebar-nav-workspace` |  | The menu trigger (the Menu's trigger): logo, workspace name and caret Marked data-menu="trigger" (the menu's trigger part, written here). |
| `logo` | `span` | `sidebar-nav-logo` |  |  |
| `workspace-name` | `span` | `sidebar-nav-workspace-name` |  | Truncates; title carries the full name |
| `caret` | `span` | `sidebar-nav-caret` |  |  |
| `toggle` | `button` | `sidebar-nav-toggle` |  | Collapse / expand: a Button (ghost, md, square, rounded, muted) with our tooltip naming its action. Pinned to the root's inline-end edge, so it rides the edge and lands on the logo's spot Marked data-sidebar-nav="toggle". |
| `group` | `div` | `sidebar-nav-group` | repeats | Offset parent of a gliding highlight and its list |
| `highlight` | `span` | `sidebar-nav-highlight` | repeats | Gliding highlight (`glideLayer` of `lib/glide.ts`): follows pointer and focus and glides for both |
| `list` | `ul` | `sidebar-nav-list` | repeats |  |
| `item` | `a` | `sidebar-nav-item` | repeats | A link; aria-current=page on the current one. An optional badge is a plain Count (also data-sidebar-nav="copy"). Collapsed: aria-label carries the name and our tooltip shows it Every link's text starts on one edge: with an icon, the icon sits in the icon column; a chat or section link without one is indented to the label column. |
| `item-icon` | `span` | `sidebar-nav-item-icon` | optional |  |
| `label` | `span` | `sidebar-nav-label` |  | Also data-sidebar-nav="copy" |
| `copy` | `span` | `sidebar-nav-copy` | repeats | Anything that fades out on collapse; aria-hidden while collapsed Every element that fades carries data-sidebar-nav="copy" (labels, the switcher name and caret, a badge Count). |
| `sections` | `div` | `sidebar-nav-sections` |  | Holds the sections and the chats: with sections, one scroller (overscroll contained) where each section takes its own height, 12px apart; without, no box of its own (display: contents). |
| `section` | `div` | `sidebar-nav-section` |  | A Collapsible (variant section, caret at the start): the chats, or one of the sections (its own label, open until folded). The chats' body holds a group of links, or an EmptyState row when there are none. Without sections the chats section scrolls itself; with sections every section is as tall as its rows and the sections part scrolls. Hidden while collapsed |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `workspace` | `string` |  | Bindable (React: `defaultWorkspace` and `onWorkspaceChange`; Vue: `v-model:workspace`; Svelte: `bind:workspace`). Value of the current workspace. |
| `workspaces` | `{ value: string; label: string }[]` |  | Workspaces offered by the switcher (a radio group in the menu). |
| `actions` | `{ value: string; label: string; icon: string? (an icon name) or an icon; tone?: 'danger'; type?: 'separator' }[]` |  | Menu actions under the workspaces. Defaults to New workspace, Workspace settings, a separator and Sign out. |
| `items` | `{ value: string; label: string; href: string; icon: icon name or icon; badge?: string }[]` |  | Primary links, each with an icon. |
| `sections` | `{ value: string; label: string; items: { value, label, href, icon? }[] }[]` |  | Labelled groups of links under the primary ones (projects, teams, saved views): each a collapsible section with its own label, open until the reader folds it. A link with an icon shows it; without, its text lines up with the labels. |
| `chats` | `{ value: string; label: string; href: string; icon?: icon name or icon }[]` |  | Recent chats. A chat with an icon shows it; one without is indented to the label column. Without sections the Chats section is always shown (empty shows the empty text); with sections only when chats is given. |
| `current` | `string` |  | Bindable (React: `defaultCurrent` and `onCurrentChange`; Vue: `v-model:current`; Svelte: `bind:current`). Value of the current link (an item or a chat): aria-current=page. |
| `collapsed` | `boolean` | `false` | Bindable (React: `defaultCollapsed` and `onCollapsedChange`; Vue: `v-model:collapsed`; Svelte: `bind:collapsed`). Rail of icons only. |
| `chatsOpen` | `boolean` | `true` | Bindable (React: `defaultChatsOpen` and `onChatsOpenChange`; Vue: `v-model:chatsOpen`; Svelte: `bind:chatsOpen`). Whether the chats list is shown (the collapsible's open state). |
| `logo` | `string` | `"squares-four"` | Icon in front of the workspace name (an icon name, or an icon). |
| `label` | `string` | `"Workspace navigation"` | Accessible name of the nav. |
| `chatsLabel` | `string` | `"Chats"` |  |
| `emptyLabel` | `string` | `"No chats yet"` |  |
| `size` | `"sm" \| "md"` | `"md"` | Dimensions only (the control scale). md: 32px rows, 14px text, 18px glyphs, the control radius. sm: 28px rows, 13px text, 16px glyphs (and the toggle's), the chip radius on the rows, their fill and ring, the highlight and the switcher (as every control up to 28px), for a dense app whose content is 13px. Both keep the 20px glyph box, so the glyphs' 26px centre, the 52px rail and the 34px label column do not move; collapsed rows stay 36px wide. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `workspaceChange` | (value: string) a workspace was picked; the menu closes |
| `workspaceAction` | (value: string) a menu action was chosen |
| `currentChange` | (value: string) a link was followed |
| `collapsedChange` | (collapsed: boolean) |
| `chatsOpenChange` | (open: boolean) |
| `navigate` | (value: string, href: string, event) a link was followed, before it becomes current; event.preventDefault() lets the app route without loading href |

## States

`hover`, `press`, `focus-visible`, `current`, `collapsed`, `expanded`

## Keyboard and accessibility

Role: navigation.

Keyboard:

- Tab order: workspace switcher, collapse toggle, links, then each section's disclosure and its links, then Chats and the chat links. Collapsed: toggle, then the icon links
- Enter on a link follows it and makes it current (aria-current=page)
- Enter / Space / ArrowDown on the switcher open the menu; arrows move, Enter picks a workspace (the menu closes and focus returns to the switcher), Escape closes
- Enter / Space on the toggle collapse or expand the rail; focus stays on the toggle, which keeps its place under the pointer of the rail
- Enter / Space on Chats show or hide the list; focus stays on the trigger

Notes:

- Collapsed, the labels are aria-hidden and every icon link is named by aria-label; our tooltip shows that name on hover and keyboard focus (on the inline-end side). The toggle's tooltip names its action.
- The collapse and the caret are instant from the keyboard; the highlight glides for keyboard moves too.

## Motion

- collapse: the root's width over duration.move with easing.out-strong (the one layout transition: the page beside it has to reflow; the 224px tree inside never re-lays out). Instant from the keyboard; jumps under reduced motion
- copy: opacity over duration.base and an enter-shift drift over duration.move
- row backgrounds (current, press): a childless pseudo-element shrinks to 36px with the rail
- toggle glyph flips with scaleX over duration.move
- highlight: glides between rows under the pointer and with keyboard focus (duration.move easing.out-strong); hover-gated
- chats: the collapsible's reveal recipe and caret
- picking a link changes state instantly: no motion on navigation

## Tokens

`color.ink`, `color.ink-2`, `color.ink-3`, `color.hover`, `color.hover-2`, `color.accent`, `radius.control`, `radius.chip`, `text.xs`, `text.md`, `duration.fast`, `duration.base`, `duration.press`, `duration.move`, `easing.out-strong`, `motion.press-scale`, `motion.enter-shift`

## Dependencies

- [Menu](/components/menu.md)
- [Collapsible](/components/collapsible.md)
- [Button](/components/button.md)
- [Count](/components/count.md)
- [Empty State](/components/empty-state.md)
- [Tooltip](/components/tooltip.md)

## Examples

### Assistant sidebar

React · `expanded.tsx`

```tsx
import type { ComponentProps } from "react";
import { SidebarNav } from "@/components/ui/sidebar-nav";

type Props = ComponentProps<typeof SidebarNav>;

const workspaces: Props["workspaces"] = [
  { value: "harbor", label: "Harbor" },
  { value: "harbor-eu", label: "Harbor EU" },
  { value: "sandbox", label: "Maya's sandbox" },
];

const items: Props["items"] = [
  { value: "new", label: "New chat", href: "#new", icon: "note-pencil" },
  { value: "home", label: "Home", href: "#home", icon: "house" },
  {
    value: "invite",
    label: "Invite teammates",
    href: "#invite",
    icon: "user-plus",
    badge: "3/10",
  },
];

const chats: Props["chats"] = [
  { value: "deploy", label: "Failed deploy on harbor/api", href: "#deploy" },
  { value: "refund", label: "Refund for Brightline Dental", href: "#refund" },
  { value: "churn", label: "Churn risk: Kestrel Logistics", href: "#churn" },
  {
    value: "webhook",
    label: "Why did billing-service retry the Kestrel invoice webhook 14 times?",
    href: "#webhook",
  },
  { value: "tickets", label: "Summarize this week's support tickets", href: "#tickets" },
  { value: "slow-query", label: "Slow Postgres query on bookings", href: "#slow-query" },
];

/* the rail against the app it belongs to: the content pane starts at its hairline */
export default function AssistantSidebar() {
  return (
    <div className="flex h-[360px] w-[600px] max-w-full">
      <SidebarNav
        defaultWorkspace="harbor"
        workspaces={workspaces}
        logo="calendar"
        items={items}
        chats={chats}
        defaultCurrent="home"
      />
      <main className="flex min-w-0 flex-1 flex-col gap-1 border-s border-line ps-6 pt-2">
        <h2 className="text-md font-medium text-ink">Good morning, Maya</h2>
        <p className="text-sm text-ink-2">Ask Harbor about deploys, billing or your team.</p>
      </main>
    </div>
  );
}
```

Vue · `expanded.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { SidebarNav, type SidebarNavItem } from "@/components/ui/sidebar-nav";

const workspaces = [
  { value: "harbor", label: "Harbor" },
  { value: "harbor-eu", label: "Harbor EU" },
  { value: "sandbox", label: "Maya's sandbox" },
];

const items: SidebarNavItem[] = [
  { value: "new", label: "New chat", href: "#new", icon: "note-pencil" },
  { value: "home", label: "Home", href: "#home", icon: "house" },
  {
    value: "invite",
    label: "Invite teammates",
    href: "#invite",
    icon: "user-plus",
    badge: "3/10",
  },
];

const chats = [
  { value: "deploy", label: "Failed deploy on harbor/api", href: "#deploy" },
  { value: "refund", label: "Refund for Brightline Dental", href: "#refund" },
  { value: "churn", label: "Churn risk: Kestrel Logistics", href: "#churn" },
  {
    value: "webhook",
    label: "Why did billing-service retry the Kestrel invoice webhook 14 times?",
    href: "#webhook",
  },
  { value: "tickets", label: "Summarize this week's support tickets", href: "#tickets" },
  { value: "slow-query", label: "Slow Postgres query on bookings", href: "#slow-query" },
];

const workspace = ref("harbor");

const current = ref("home");
</script>

<!-- the rail against the app it belongs to: the content pane starts at its hairline -->
<template>
  <div class="flex h-[360px] w-[600px] max-w-full">
    <SidebarNav
      v-model:workspace="workspace"
      :workspaces="workspaces"
      logo="calendar"
      :items="items"
      :chats="chats"
      v-model:current="current"
    />
    <main class="flex min-w-0 flex-1 flex-col gap-1 border-s border-line ps-6 pt-2">
      <h2 class="text-md font-medium text-ink">Good morning, Maya</h2>
      <p class="text-sm text-ink-2">Ask Harbor about deploys, billing or your team.</p>
    </main>
  </div>
</template>
```

Svelte · `expanded.svelte`

```svelte
<script lang="ts">
  import { SidebarNav, type SidebarNavItem } from "$lib/components/ui/sidebar-nav";

  const workspaces = [
    { value: "harbor", label: "Harbor" },
    { value: "harbor-eu", label: "Harbor EU" },
    { value: "sandbox", label: "Maya's sandbox" },
  ];

  const items: SidebarNavItem[] = [
    { value: "new", label: "New chat", href: "#new", icon: "note-pencil" },
    { value: "home", label: "Home", href: "#home", icon: "house" },
    {
      value: "invite",
      label: "Invite teammates",
      href: "#invite",
      icon: "user-plus",
      badge: "3/10",
    },
  ];

  const chats = [
    { value: "deploy", label: "Failed deploy on harbor/api", href: "#deploy" },
    { value: "refund", label: "Refund for Brightline Dental", href: "#refund" },
    { value: "churn", label: "Churn risk: Kestrel Logistics", href: "#churn" },
    {
      value: "webhook",
      label: "Why did billing-service retry the Kestrel invoice webhook 14 times?",
      href: "#webhook",
    },
    { value: "tickets", label: "Summarize this week's support tickets", href: "#tickets" },
    { value: "slow-query", label: "Slow Postgres query on bookings", href: "#slow-query" },
  ];

  let workspace = $state("harbor");

  let current = $state("home");
</script>

<!-- the rail against the app it belongs to: the content pane starts at its hairline -->
<div class="flex h-[360px] w-[600px] max-w-full">
  <SidebarNav
    bind:workspace={workspace}
    workspaces={workspaces}
    logo="calendar"
    items={items}
    chats={chats}
    bind:current={current}
  />
  <main class="flex min-w-0 flex-1 flex-col gap-1 border-s border-line ps-6 pt-2">
    <h2 class="text-md font-medium text-ink">Good morning, Maya</h2>
    <p class="text-sm text-ink-2">Ask Harbor about deploys, billing or your team.</p>
  </main>
</div>
```

### Issue tracker with project sections

React · `issue-tracker.tsx`

```tsx
import type { ComponentProps, MouseEvent } from "react";
import { useState } from "react";
import { SidebarNav } from "@/components/ui/sidebar-nav";

type Props = ComponentProps<typeof SidebarNav>;

const workspaces: Props["workspaces"] = [{ value: "northwind", label: "Northwind" }];

const items: Props["items"] = [
  { value: "inbox", label: "Inbox", href: "/inbox", icon: "bell", badge: "4" },
  { value: "my-issues", label: "My issues", href: "/my-issues", icon: "note-pencil" },
];

/* every link has an icon, so the rail keeps one text edge */
const sections: Props["sections"] = [
  {
    value: "projects",
    label: "Projects",
    items: [
      { value: "web-app", label: "web-app", href: "/projects/web-app", icon: "cube" },
      { value: "billing-api", label: "billing-api", href: "/projects/billing-api", icon: "cube" },
      { value: "data-pipeline", label: "data-pipeline", href: "/projects/data-pipeline", icon: "cube" },
    ],
  },
  {
    value: "views",
    label: "Saved views",
    items: [{ value: "triage", label: "Needs triage", href: "/views/triage", icon: "hash" }],
  },
];

/* the app routes itself: onNavigate stops the page load and the sidebar marks the link current */
export default function IssueTracker() {
  const [path, setPath] = useState("/projects/billing-api");
  const navigate = (_value: string, href: string, event: MouseEvent<HTMLAnchorElement>) => {
    event.preventDefault();
    setPath(href);
  };
  return (
    <div className="flex h-[360px] w-[600px] max-w-full">
      <SidebarNav workspaces={workspaces} items={items} sections={sections} defaultCurrent="billing-api" onNavigate={navigate} />
      <main className="flex min-w-0 flex-1 flex-col gap-1 border-s border-line ps-6 pt-2">
        <h2 className="text-md font-medium text-ink">Issues</h2>
        <p className="text-sm text-ink-2" dir="ltr">{path}</p>
      </main>
    </div>
  );
}
```

Vue · `issue-tracker.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { SidebarNav } from "@/components/ui/sidebar-nav";

const workspaces = [{ value: "northwind", label: "Northwind" }];

const items = [
  { value: "inbox", label: "Inbox", href: "/inbox", icon: "bell", badge: "4" },
  { value: "my-issues", label: "My issues", href: "/my-issues", icon: "note-pencil" },
] as const;

/* every link has an icon, so the rail keeps one text edge */
const sections = [
  {
    value: "projects",
    label: "Projects",
    items: [
      { value: "web-app", label: "web-app", href: "/projects/web-app", icon: "cube" },
      { value: "billing-api", label: "billing-api", href: "/projects/billing-api", icon: "cube" },
      { value: "data-pipeline", label: "data-pipeline", href: "/projects/data-pipeline", icon: "cube" },
    ],
  },
  {
    value: "views",
    label: "Saved views",
    items: [{ value: "triage", label: "Needs triage", href: "/views/triage", icon: "hash" }],
  },
] as const;

/* the app routes itself: navigate stops the page load and the sidebar marks the link current */
const current = ref("billing-api");
const path = ref("/projects/billing-api");
function navigate(_value: string, href: string, event: MouseEvent) {
  event.preventDefault();
  path.value = href;
}
</script>

<template>
  <div class="flex h-[360px] w-[600px] max-w-full">
    <SidebarNav :workspaces="workspaces" :items="items" :sections="sections" v-model:current="current" @navigate="navigate" />
    <main class="flex min-w-0 flex-1 flex-col gap-1 border-s border-line ps-6 pt-2">
      <h2 class="text-md font-medium text-ink">Issues</h2>
      <p class="text-sm text-ink-2" dir="ltr">{{ path }}</p>
    </main>
  </div>
</template>
```

Svelte · `issue-tracker.svelte`

```svelte
<script lang="ts">
  import { SidebarNav } from "$lib/components/ui/sidebar-nav";

  const workspaces = [{ value: "northwind", label: "Northwind" }];

  const items = [
    { value: "inbox", label: "Inbox", href: "/inbox", icon: "bell", badge: "4" },
    { value: "my-issues", label: "My issues", href: "/my-issues", icon: "note-pencil" },
  ] as const;

  /* every link has an icon, so the rail keeps one text edge */
  const sections = [
    {
      value: "projects",
      label: "Projects",
      items: [
        { value: "web-app", label: "web-app", href: "/projects/web-app", icon: "cube" },
        { value: "billing-api", label: "billing-api", href: "/projects/billing-api", icon: "cube" },
        { value: "data-pipeline", label: "data-pipeline", href: "/projects/data-pipeline", icon: "cube" },
      ],
    },
    {
      value: "views",
      label: "Saved views",
      items: [{ value: "triage", label: "Needs triage", href: "/views/triage", icon: "hash" }],
    },
  ] as const;

  /* the app routes itself: onnavigate stops the page load and the sidebar marks the link current */
  let current = $state("billing-api");
  let path = $state("/projects/billing-api");
  function navigate(_value: string, href: string, event: MouseEvent) {
    event.preventDefault();
    path = href;
  }
</script>

<div class="flex h-[360px] w-[600px] max-w-full">
  <SidebarNav {workspaces} {items} {sections} bind:current onnavigate={navigate} />
  <main class="flex min-w-0 flex-1 flex-col gap-1 border-s border-line ps-6 pt-2">
    <h2 class="text-md font-medium text-ink">Issues</h2>
    <p class="text-sm text-ink-2" dir="ltr">{path}</p>
  </main>
</div>
```

### Collapsed to an icon rail

React · `collapsed.tsx`

```tsx
import type { ComponentProps } from "react";
import { SidebarNav } from "@/components/ui/sidebar-nav";

type Props = ComponentProps<typeof SidebarNav>;

const workspaces: Props["workspaces"] = [
  { value: "harbor", label: "Harbor" },
  { value: "harbor-eu", label: "Harbor EU" },
  { value: "sandbox", label: "Maya's sandbox" },
];

const items: Props["items"] = [
  { value: "new", label: "New chat", href: "#new", icon: "note-pencil" },
  { value: "home", label: "Home", href: "#home", icon: "house" },
  {
    value: "invite",
    label: "Invite teammates",
    href: "#invite",
    icon: "user-plus",
    badge: "3/10",
  },
];

const chats: Props["chats"] = [
  { value: "deploy", label: "Failed deploy on harbor/api", href: "#deploy" },
  { value: "refund", label: "Refund for Brightline Dental", href: "#refund" },
];

/* the rail against the app it belongs to: the content pane starts at its hairline */
export default function CollapsedToRail() {
  return (
    <div className="flex h-[360px] w-[600px] max-w-full">
      <SidebarNav
        defaultWorkspace="harbor"
        workspaces={workspaces}
        logo="calendar"
        items={items}
        chats={chats}
        defaultCurrent="home"
        defaultCollapsed
      />
      <main className="flex min-w-0 flex-1 flex-col gap-1 border-s border-line ps-6 pt-2">
        <h2 className="text-md font-medium text-ink">Good morning, Maya</h2>
        <p className="text-sm text-ink-2">The rail keeps every link one press away.</p>
      </main>
    </div>
  );
}
```

Vue · `collapsed.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { SidebarNav, type SidebarNavItem } from "@/components/ui/sidebar-nav";

const workspaces = [
  { value: "harbor", label: "Harbor" },
  { value: "harbor-eu", label: "Harbor EU" },
  { value: "sandbox", label: "Maya's sandbox" },
];

const items: SidebarNavItem[] = [
  { value: "new", label: "New chat", href: "#new", icon: "note-pencil" },
  { value: "home", label: "Home", href: "#home", icon: "house" },
  {
    value: "invite",
    label: "Invite teammates",
    href: "#invite",
    icon: "user-plus",
    badge: "3/10",
  },
];

const chats = [
  { value: "deploy", label: "Failed deploy on harbor/api", href: "#deploy" },
  { value: "refund", label: "Refund for Brightline Dental", href: "#refund" },
];

const workspace = ref("harbor");

const current = ref("home");

const collapsed = ref(true);
</script>

<!-- the rail against the app it belongs to: the content pane starts at its hairline -->
<template>
  <div class="flex h-[360px] w-[600px] max-w-full">
    <SidebarNav
      v-model:workspace="workspace"
      :workspaces="workspaces"
      logo="calendar"
      :items="items"
      :chats="chats"
      v-model:current="current"
      v-model:collapsed="collapsed"
    />
    <main class="flex min-w-0 flex-1 flex-col gap-1 border-s border-line ps-6 pt-2">
      <h2 class="text-md font-medium text-ink">Good morning, Maya</h2>
      <p class="text-sm text-ink-2">The rail keeps every link one press away.</p>
    </main>
  </div>
</template>
```

Svelte · `collapsed.svelte`

```svelte
<script lang="ts">
  import { SidebarNav, type SidebarNavItem } from "$lib/components/ui/sidebar-nav";

  const workspaces = [
    { value: "harbor", label: "Harbor" },
    { value: "harbor-eu", label: "Harbor EU" },
    { value: "sandbox", label: "Maya's sandbox" },
  ];

  const items: SidebarNavItem[] = [
    { value: "new", label: "New chat", href: "#new", icon: "note-pencil" },
    { value: "home", label: "Home", href: "#home", icon: "house" },
    {
      value: "invite",
      label: "Invite teammates",
      href: "#invite",
      icon: "user-plus",
      badge: "3/10",
    },
  ];

  const chats = [
    { value: "deploy", label: "Failed deploy on harbor/api", href: "#deploy" },
    { value: "refund", label: "Refund for Brightline Dental", href: "#refund" },
  ];

  let workspace = $state("harbor");

  let current = $state("home");

  let collapsed = $state(true);
</script>

<!-- the rail against the app it belongs to: the content pane starts at its hairline -->
<div class="flex h-[360px] w-[600px] max-w-full">
  <SidebarNav
    bind:workspace={workspace}
    workspaces={workspaces}
    logo="calendar"
    items={items}
    chats={chats}
    bind:current={current}
    bind:collapsed={collapsed}
  />
  <main class="flex min-w-0 flex-1 flex-col gap-1 border-s border-line ps-6 pt-2">
    <h2 class="text-md font-medium text-ink">Good morning, Maya</h2>
    <p class="text-sm text-ink-2">The rail keeps every link one press away.</p>
  </main>
</div>
```

### First day in a new workspace

React · `no-chats.tsx`

```tsx
import type { ComponentProps } from "react";
import { SidebarNav } from "@/components/ui/sidebar-nav";

type Props = ComponentProps<typeof SidebarNav>;

const workspaces: Props["workspaces"] = [
  { value: "harbor", label: "Harbor" },
  { value: "harbor-eu", label: "Harbor EU" },
  { value: "sandbox", label: "Maya's sandbox" },
];

const items: Props["items"] = [
  { value: "new", label: "New chat", href: "#new", icon: "note-pencil" },
  { value: "home", label: "Home", href: "#home", icon: "house" },
  {
    value: "invite",
    label: "Invite teammates",
    href: "#invite",
    icon: "user-plus",
    badge: "3/10",
  },
];

/* the rail against the app it belongs to: the content pane starts at its hairline */
export default function FirstDayInWorkspace() {
  return (
    <div className="flex h-[360px] w-[600px] max-w-full">
      <SidebarNav
        defaultWorkspace="harbor"
        workspaces={workspaces}
        logo="calendar"
        items={items}
        chats={[]}
        defaultCurrent="home"
      />
      <main className="flex min-w-0 flex-1 flex-col gap-1 border-s border-line ps-6 pt-2">
        <h2 className="text-md font-medium text-ink">Welcome to Harbor</h2>
        <p className="text-sm text-ink-2">Your chats will be listed in the sidebar once you start one.</p>
      </main>
    </div>
  );
}
```

Vue · `no-chats.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { SidebarNav, type SidebarNavItem } from "@/components/ui/sidebar-nav";

const workspaces = [
  { value: "harbor", label: "Harbor" },
  { value: "harbor-eu", label: "Harbor EU" },
  { value: "sandbox", label: "Maya's sandbox" },
];

const items: SidebarNavItem[] = [
  { value: "new", label: "New chat", href: "#new", icon: "note-pencil" },
  { value: "home", label: "Home", href: "#home", icon: "house" },
  {
    value: "invite",
    label: "Invite teammates",
    href: "#invite",
    icon: "user-plus",
    badge: "3/10",
  },
];

const workspace = ref("harbor");

const current = ref("home");
</script>

<!-- the rail against the app it belongs to: the content pane starts at its hairline -->
<template>
  <div class="flex h-[360px] w-[600px] max-w-full">
    <SidebarNav
      v-model:workspace="workspace"
      :workspaces="workspaces"
      logo="calendar"
      :items="items"
      :chats="[]"
      v-model:current="current"
    />
    <main class="flex min-w-0 flex-1 flex-col gap-1 border-s border-line ps-6 pt-2">
      <h2 class="text-md font-medium text-ink">Welcome to Harbor</h2>
      <p class="text-sm text-ink-2">Your chats will be listed in the sidebar once you start one.</p>
    </main>
  </div>
</template>
```

Svelte · `no-chats.svelte`

```svelte
<script lang="ts">
  import { SidebarNav, type SidebarNavItem } from "$lib/components/ui/sidebar-nav";

  const workspaces = [
    { value: "harbor", label: "Harbor" },
    { value: "harbor-eu", label: "Harbor EU" },
    { value: "sandbox", label: "Maya's sandbox" },
  ];

  const items: SidebarNavItem[] = [
    { value: "new", label: "New chat", href: "#new", icon: "note-pencil" },
    { value: "home", label: "Home", href: "#home", icon: "house" },
    {
      value: "invite",
      label: "Invite teammates",
      href: "#invite",
      icon: "user-plus",
      badge: "3/10",
    },
  ];

  let workspace = $state("harbor");

  let current = $state("home");
</script>

<!-- the rail against the app it belongs to: the content pane starts at its hairline -->
<div class="flex h-[360px] w-[600px] max-w-full">
  <SidebarNav
    bind:workspace={workspace}
    workspaces={workspaces}
    logo="calendar"
    items={items}
    chats={[]}
    bind:current={current}
  />
  <main class="flex min-w-0 flex-1 flex-col gap-1 border-s border-line ps-6 pt-2">
    <h2 class="text-md font-medium text-ink">Welcome to Harbor</h2>
    <p class="text-sm text-ink-2">Your chats will be listed in the sidebar once you start one.</p>
  </main>
</div>
```

## Recipe

`sidebar-nav.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";
import { glideLayer } from "../lib/glide";

/* sidebar-nav — workspace switcher, primary links and recent chats; collapses to a 52px rail.
 *
 * The panel is one fixed 224px tree. Collapsing changes only the root's width, which clips the
 * panel, so every glyph keeps its x position (26px from the inline start) in both states.
 * Labels fade and drift; row backgrounds are childless pseudo-elements that shrink to 36px.
 * The workspace switcher is the real Menu (its trigger carries the menu's trigger classes and
 * data-menu="trigger"); the toggle is the real Button (data-sidebar-nav="toggle"); the chats are the
 * real Collapsible (section); "No chats yet" is the real EmptyState. Whatever fades on collapse
 * carries the copy classes and data-sidebar-nav="copy". Collapsed is a variant of every part it
 * changes. Rows carry the shared gliding highlight (glideLayer of lib/glide.ts).
 *
 * Sections (projects, teams): each is the real Collapsible (section) with its own label, below the
 * primary links; with sections, they and the chats share one scroller (`sections`) and each takes
 * its own height. Every link's text starts on one edge: a link with an icon shows it in the icon
 * column, one without is indented to the label column (indent), so the rail never has two text
 * edges.
 *
 * Motion (`docs motion`), all here:
 *   collapse  the root's width at duration.move easing.out-strong (the sidebar's footprint must
 *             change so the page beside it reflows; the 224px panel inside is never re-laid out);
 *             the copy fades (base) and drifts by motion.enter-shift (move); a row's background
 *             narrows to 36px (width, a childless layer: scaleX would distort its radius); the row
 *             itself switches to 36px at the end of the collapse (a discrete 0s switch), back at once
 *   keyboard  the root is kbd-instant (its own transition is skipped); its moving
 *             parts follow with kbd:duration-0 kbd:delay-0
 *   press     the switcher scales (press); a row darkens its background
 *
 * size (dimensions only, the control scale): md (default) 32px rows, 14px text, 18px glyphs, the
 * control radius; sm 28px rows, 13px text, 16px glyphs, the chip radius (rows, their fill and ring,
 * the highlight and the switcher), for a dense app whose content is 13px (an issue tracker, an
 * editor). The glyph box stays 20px, so every glyph keeps its 26px centre and the 52px rail, the
 * toggle's landing place and the label column (34px) are the same at both sizes; collapsed rows
 * stay 36px wide (as tall as the size). */

export const sidebarNav = tv({
  slots: {
    /* rows and the toggle stack above the highlight; isolation keeps that inside, below portaled popups */
    base: "kbd-instant relative isolate flex flex-col shrink-0 w-56 max-h-full overflow-clip motion-size-move",
    panel: "flex flex-col flex-[1_1_auto] w-56 min-h-0 pb-2",
    /* switcher + toggle */
    header: "flex items-center shrink-0 h-10 mb-2.5 px-2",
    switcher: "min-w-0",
    /* added to the menu's trigger classes. Ark stamps dir="ltr" on the menu trigger; the switcher
     * follows the page instead. An open switcher keeps the pressed fill, hovered or not. */
    workspace: [
      "flex items-center gap-1.5 w-[164px] h-8 px-2 rounded-control text-md font-medium leading-none text-ink-2 text-start",
      "opacity-100 hover:not-data-[state=open]:bg-hover data-[state=open]:bg-hover-2 active:scale-press",
      "[[data-slot=sidebar-nav]:dir(rtl)_&]:[direction:rtl]",
      "motion-colors-fast motion-transform-press motion-opacity-base motion-also-[visibility_var(--fndn-duration-base)] kbd:duration-0 kbd:delay-0",
    ],
    logo: "flex items-center justify-center shrink-0 size-5 text-[18px] text-ink",
    /* names take their direction from their own first letter (a name in the other script keeps its
     * words in order and its ellipsis at its own end), but still start on the page's side */
    workspaceName: "flex-1 min-w-0 leading-[1.5] overflow-hidden text-ellipsis whitespace-nowrap [unicode-bidi:plaintext] [&:dir(ltr)]:text-left [&:dir(rtl)]:text-right",
    caret: "flex shrink-0 text-[14px] text-ink-3",
    /* added to the Button (md square ghost rounded muted), pinned to the root's inline-end edge: it
     * rides the edge as the rail closes and lands where the logo was (its glyph centre is 26px from
     * the start when collapsed). Its glyph mirrors to point the way the rail will move. */
    toggle: [
      "absolute top-1 end-2.5 z-2",
      "[&_[data-slot=icon]]:motion-transform-move kbd:[&_[data-slot=icon]]:duration-0 kbd:[&_[data-slot=icon]]:delay-0",
      "[[data-slot=sidebar-nav]:dir(rtl)_&_[data-slot=icon]]:[transform:scaleX(-1)]",
    ],
    /* the chats' list gets room for the focus ring of its first and last row inside the clipping region */
    group: "relative shrink-0 mx-2",
    /* the gliding highlight: hover-gated (a tap would leave it stuck); it follows pointer and focus
     * and glides for both */
    highlight: [
      glideLayer, "rounded-control bg-hover invisible [@media(hover:hover)_and_(pointer:fine)]:visible",
    ],
    list: "flex flex-col gap-px m-0 p-0 list-none",
    /* a width, not auto, so the collapsed 36px can be switched at a delay. Collapsed, the link
     * itself is the 36px square once the rail has closed, so its tooltip sits next to the rail (a
     * tooltip anchors to its trigger's box) and nothing is reachable in the clipped part; it
     * switches after the collapse, when the label has faded, and back at once.
     * The background and focus ring are a childless ::before that narrows to 36px with the rail. */
    item: [
      "relative z-1 flex w-full items-center gap-1.5 h-8 px-2 rounded-control pointer-coarse:h-10",
      "text-md font-medium leading-none text-ink-2 no-underline outline-none select-none aria-[current=page]:text-ink",
      "motion-colors-fast kbd:duration-0 kbd:delay-0",
      "before:content-[''] before:absolute before:inset-y-0 before:start-0 before:-z-1 before:w-full before:rounded-control before:bg-transparent",
      "aria-[current=page]:before:bg-hover-2 active:before:bg-hover-2",
      "focus-visible:before:outline-2 focus-visible:before:outline-solid focus-visible:before:outline-accent focus-visible:before:outline-offset-2",
      "before:motion-colors-fast before:motion-size-move kbd:before:duration-0 kbd:before:delay-0",
    ],
    /* holds the sections and the chats: one scroller when there are sections, otherwise no box of
     * its own (contents), so the chats section scrolls itself */
    sections: "contents",
    itemIcon: "flex items-center justify-center shrink-0 size-5 text-[18px]",
    /* the row is 32px with line-height 1; truncation clips, so descenders (Arabic, Devanagari) get room */
    label: "flex-1 min-w-0 leading-[1.5] overflow-hidden text-ellipsis whitespace-nowrap [unicode-bidi:plaintext] [&:dir(ltr)]:text-left [&:dir(rtl)]:text-right",
    /* what fades on collapse: added to the labels, the switcher's name and caret, and a badge */
    copy: [
      "opacity-100 [transform:none] motion-opacity-base motion-transform-move kbd:duration-0 kbd:delay-0",
    ],
    /* chats: the Collapsible's root (variant section), which owns the trigger, the caret, the
     * reveal and the press colour. Collapsed, the section is visibility: hidden and the open region
     * inherits it (the collapsible never sets visibility on an open region), so the chats leave the
     * tab order with the rest of the copy. Its header sits at the top of the scrolling group, which
     * clips: the ring is drawn inside. "No chats yet" lines up with the chat labels. */
    section: [
      "flex flex-col flex-[1_1_auto] min-h-0 mt-3 overflow-y-auto overscroll-contain",
      "opacity-100",
      "motion-opacity-base motion-also-[visibility_var(--fndn-duration-base)] kbd:duration-0 kbd:delay-0",
      "[&>[data-slot=collapsible-trigger]]:shrink-0 [&>[data-slot=collapsible-trigger]]:mx-2",
      "[&>[data-slot=collapsible-trigger]:focus-visible]:outline-offset-[-2px]",
      "[&_[data-slot=empty-state]]:mx-2",
    ],
  },
  variants: {
    size: {
      md: {},
      /* 28px rows take the chip radius, as every control up to 28px (md stays control) */
      sm: {
        workspace: "h-7 rounded-chip text-sm",
        logo: "text-[16px]",
        highlight: "rounded-chip",
        item: "h-7 rounded-chip before:rounded-chip text-sm pointer-coarse:h-10",
        itemIcon: "text-[16px]",
      },
    },
    /* the 52px rail: the root narrows; the copy, the switcher and the chats fade (and leave the tab
     * order); rows and the highlight become 36px squares (rows at the end of the collapse); the
     * toggle's glyph turns to point the way the rail will open */
    collapsed: {
      true: {
        base: "w-13",
        workspace: "opacity-0 invisible",
        toggle: "[&_[data-slot=icon]]:[transform:scaleX(-1)] [[data-slot=sidebar-nav]:dir(rtl)_&_[data-slot=icon]]:[transform:none]",
        highlight: "end-auto w-9",
        item: "w-9 motion-also-[width_0s_linear_var(--fndn-duration-move)] before:w-9",
        copy: "opacity-0 [transform:translateX(calc(var(--fndn-motion-enter-shift)*-1))] [[data-slot=sidebar-nav]:dir(rtl)_&]:[transform:translateX(var(--fndn-motion-enter-shift))]",
        section: "opacity-0 invisible",
      },
    },
    inSection: {
      true: { group: "py-1" },
    },
    /* a link without an icon: its text starts where the labels of icon links do (8 + 20 + 6px) */
    indent: {
      true: { item: "ps-[34px]" },
    },
    /* a section among others in the scroller: as tall as its rows, the scroller scrolls */
    stacked: {
      true: {
        sections: "flex flex-col flex-[1_1_auto] min-h-0 mt-3 gap-3 overflow-y-auto overscroll-contain",
        section: "flex-none mt-0 min-h-auto overflow-visible",
      },
    },
    /* the copy class on a Count: only the fade and drift are timed there (the badge's
     * own colour and shadow easing is switched off; it never changes state in the rail) */
    badge: {
      true: { copy: "[--fndn-m-colors:initial]! [--fndn-m-shadow:initial]!" },
    },
  },
  defaultVariants: { collapsed: false, size: "md" },
});

export type SidebarNavRecipeProps = VariantProps<typeof sidebarNav>;
```
