# Responsive sidebar

import { Badge } from "@pycolors/ui";
import { Callout } from "fumadocs-ui/components/callout";
import { File, Files, Folder } from "fumadocs-ui/components/files";
import { Tab, Tabs } from "fumadocs-ui/components/tabs";
import { Preview } from "@/components/docs/preview";
import { ResponsiveSidebarExample } from "@/components/docs/blocks/canonical-examples";

<div className="flex flex-wrap items-center gap-2">
  <Badge variant="outline">
    Application shells
  </Badge>

  <Badge>
    responsive-sidebar
  </Badge>
</div>

## A reusable frame for application navigation

`ResponsiveSidebar` gives a React application one bounded shell: persistent
desktop navigation at the `md` breakpoint, a left-side mobile Sheet, a header
and action region, and one main-content landmark. Brand, routes, active state,
labels, icons, badges, footer, actions, and page content remain consumer-owned.

It does not include authentication, user identity, billing, dashboard data,
backend calls, or product-specific navigation.

<Callout title="Source-copy Block">
  Copy the complete canonical directory into your application. There is no
  Blocks package, Registry installer, CLI, or automatic update channel for this
  Block today.
</Callout>

## Canonical identity

| Contract                               | Value                                                          |
| -------------------------------------- | -------------------------------------------------------------- |
| Category                               | `Application shells`                                           |
| Category slug                          | `app-shells`                                                   |
| Block slug                             | `responsive-sidebar`                                           |
| Source                                 | `apps/marketing/content/blocks/app-shells/responsive-sidebar/` |
| Export                                 | `ResponsiveSidebar`                                            |
| Informational future Registry identity | `app-shells-responsive-sidebar`                                |

The Registry identity is reserved for future distribution work. It is not an
available installation method and this Block does not create Registry metadata
or generated JSON.

## Copy source

Configure the [public UI package and tokens](/docs/ui/installation), then create
`src/components/blocks/responsive-sidebar/` in your application. Copy all three
files below, keeping their relative imports and client directives. In the Source
tab, choose a file and use its copy button, or select the code manually when
clipboard access is unavailable.

To retrieve all three files at once, open **Code → Download ZIP** in the
[Blocks catalog](/blocks#category-app-shells). Extract the archive and move its
`src/components/blocks/responsive-sidebar/` folder into your application.

### File structure

<Files>
  <Folder name="src/components/blocks/responsive-sidebar" defaultOpen>
    <File name="index.tsx" />

    <File name="sidebar-navigation.tsx" />

    <File name="types.ts" />
  </Folder>
</Files>

| File                     | Responsibility                                          |
| ------------------------ | ------------------------------------------------------- |
| `index.tsx`              | Application layout, mobile Sheet and public exports.    |
| `sidebar-navigation.tsx` | Shared desktop and mobile navigation, brand and footer. |
| `types.ts`               | Navigation items, router adapter and component props.   |

Import `ResponsiveSidebar` and its types from the directory's `index.tsx` entry
point. Customize the navigation without changing the surrounding layout.

<Tabs groupId="blocks-doc-view" items={["Preview", "Source"]}>
  <Tab value="Preview">
    <Preview className="my-0 overflow-hidden">
      <ResponsiveSidebarExample />
    </Preview>
  </Tab>

  <Tab value="Source">
    <Tabs items={["index.tsx", "sidebar-navigation.tsx", "types.ts"]}>
      <Tab value="index.tsx">
        ```tsx title="src/components/blocks/responsive-sidebar/index.tsx"
        "use client";

        import * as React from "react";
        import {
          Button,
          Sheet,
          SheetContent,
          SheetDescription,
          SheetHeader,
          SheetTitle,
          SheetTrigger,
          cn,
        } from "@pycolors/ui";

        import { SidebarContents } from "./sidebar-navigation";
        import type { ResponsiveSidebarProps } from "./types";

        export type {
          ResponsiveSidebarNavItem,
          ResponsiveSidebarNavGroup,
          ResponsiveSidebarRenderLinkProps,
          ResponsiveSidebarProps,
        } from "./types";

        function MenuIcon() {
          return (
            <svg
              aria-hidden="true"
              className="size-4"
              fill="none"
              focusable="false"
              viewBox="0 0 24 24"
            >
              <path
                d="M4 7h16M4 12h16M4 17h16"
                stroke="currentColor"
                strokeLinecap="round"
                strokeWidth="2"
              />
            </svg>
          );
        }

        /**
         * A source-copy application shell with persistent desktop navigation and an
         * accessible mobile Sheet. All product content and routing stay consumer-owned.
         */
        export function ResponsiveSidebar({
          activeItemId,
          brand,
          children,
          className,
          contentId = "main-content",
          contentAs: Content = "main",
          groups = [],
          headerActions,
          headerTitle,
          items,
          mobileDescription = "Navigate the application.",
          mobileTitle,
          mobileTriggerLabel = "Open navigation",
          navigationLabel,
          renderLink,
          sidebarFooter,
          skipToContentLabel = "Skip to content",
        }: ResponsiveSidebarProps) {
          const [mobileOpen, setMobileOpen] = React.useState(false);
          const closeMobileNavigation = () => setMobileOpen(false);

          return (
            <div
              className={cn("flex min-h-dvh bg-background text-foreground", className)}
              data-slot="responsive-sidebar"
            >
              <a
                className={cn(
                  "sr-only z-[60] rounded-md bg-background px-3 py-2 text-sm font-medium text-foreground",
                  "focus:not-sr-only focus:fixed focus:left-3 focus:top-3",
                  "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
                )}
                data-slot="responsive-sidebar-skip-link"
                href={`#${contentId}`}
              >
                {skipToContentLabel}
              </a>

              <aside
                className="sticky top-0 hidden h-dvh w-64 shrink-0 border-r border-border bg-card md:flex"
                data-slot="responsive-sidebar-desktop"
              >
                <SidebarContents
                  activeItemId={activeItemId}
                  brand={brand}
                  groups={groups}
                  items={items}
                  navigationLabel={navigationLabel}
                  onNavigate={() => undefined}
                  renderLink={renderLink}
                  sidebarFooter={sidebarFooter}
                />
              </aside>

              <div className="flex min-w-0 flex-1 flex-col">
                <header
                  className="flex min-h-16 shrink-0 items-center gap-3 border-b border-border bg-background px-4 md:px-6"
                  data-slot="responsive-sidebar-header"
                >
                  <Sheet onOpenChange={setMobileOpen} open={mobileOpen}>
                    <SheetTrigger asChild>
                      <Button
                        aria-label={mobileTriggerLabel}
                        className="size-11 shrink-0 md:hidden"
                        data-slot="responsive-sidebar-mobile-trigger"
                        size="icon"
                        type="button"
                        variant="outline"
                      >
                        <MenuIcon />
                      </Button>
                    </SheetTrigger>

                    <SheetContent
                      className="top-0 flex h-dvh w-[min(18rem,calc(100vw-2rem))] max-w-[calc(100vw-2rem)] flex-col gap-0 rounded-none p-0 [&_[data-slot=responsive-sidebar-brand]]:pr-12 [&>button]:right-3 [&>button]:top-2.5 [&>button]:grid [&>button]:size-11 [&>button]:place-items-center"
                      data-slot="responsive-sidebar-mobile"
                      side="left"
                    >
                      <SheetHeader className="sr-only">
                        <SheetTitle>{mobileTitle}</SheetTitle>
                        <SheetDescription>{mobileDescription}</SheetDescription>
                      </SheetHeader>

                      <SidebarContents
                        activeItemId={activeItemId}
                        brand={brand}
                        groups={groups}
                        items={items}
                        navigationLabel={navigationLabel}
                        onNavigate={closeMobileNavigation}
                        renderLink={renderLink}
                        sidebarFooter={sidebarFooter}
                      />
                    </SheetContent>
                  </Sheet>

                  <div className="min-w-0 flex-1" data-slot="responsive-sidebar-title">
                    {headerTitle}
                  </div>
                  {headerActions ? (
                    <div
                      className="flex min-w-0 max-w-[60%] flex-wrap items-center justify-end gap-2 py-2 [&>*]:max-w-full [&>*]:whitespace-normal [&>*]:[overflow-wrap:anywhere]"
                      data-slot="responsive-sidebar-actions"
                    >
                      {headerActions}
                    </div>
                  ) : null}
                </header>

                <Content
                  className="min-w-0 flex-1 outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"
                  data-slot="responsive-sidebar-main"
                  id={contentId}
                  tabIndex={-1}
                >
                  {children}
                </Content>
              </div>
            </div>
          );
        }

        ```
      </Tab>

      <Tab value="sidebar-navigation.tsx">
        ```tsx title="src/components/blocks/responsive-sidebar/sidebar-navigation.tsx"
        "use client";

        import * as React from "react";
        import { cn } from "@pycolors/ui";
        import type {
          ResponsiveSidebarNavGroup,
          ResponsiveSidebarNavItem,
          ResponsiveSidebarProps,
        } from "./types";

        interface NavigationItemsProps {
          activeItemId?: string;
          items: readonly ResponsiveSidebarNavItem[];
          onNavigate: () => void;
          renderLink?: ResponsiveSidebarProps["renderLink"];
        }

        interface SidebarContentsProps extends NavigationItemsProps {
          brand: React.ReactNode;
          groups: readonly ResponsiveSidebarNavGroup[];
          navigationLabel: string;
          sidebarFooter?: React.ReactNode;
        }

        const linkClassName = cn(
          "flex min-h-11 w-full items-center gap-3 rounded-md px-3 py-2 text-sm font-medium sm:min-h-10",
          "text-muted-foreground transition-colors hover:bg-accent hover:text-accent-foreground",
          "outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
          "focus-visible:ring-offset-background motion-reduce:transition-none",
          "aria-[current=page]:bg-accent aria-[current=page]:text-accent-foreground",
        );

        function NavigationItems({
          activeItemId,
          items,
          onNavigate,
          renderLink,
        }: NavigationItemsProps) {
          return (
            <ul className="space-y-1" role="list">
              {items.map((item) => {
                const active = item.id === activeItemId;
                const children = (
                  <>
                    {item.icon ? (
                      <span
                        aria-hidden="true"
                        className="flex size-4 shrink-0 items-center justify-center"
                        data-slot="responsive-sidebar-item-icon"
                      >
                        {item.icon}
                      </span>
                    ) : null}
                    <span className="min-w-0 flex-1 [overflow-wrap:anywhere]">
                      {item.label}
                    </span>
                    {item.badge ? (
                      <span
                        className="shrink-0 rounded-sm bg-muted px-1.5 py-0.5 text-xs font-medium text-muted-foreground"
                        data-slot="responsive-sidebar-item-badge"
                      >
                        {item.badge}
                      </span>
                    ) : null}
                  </>
                );

                return (
                  <li key={item.id}>
                    {renderLink ? (
                      renderLink({
                        item,
                        active,
                        className: linkClassName,
                        children,
                        onNavigate,
                      })
                    ) : (
                      <a
                        aria-current={active ? "page" : undefined}
                        className={linkClassName}
                        data-slot="responsive-sidebar-link"
                        href={item.href}
                        onClick={onNavigate}
                      >
                        {children}
                      </a>
                    )}
                  </li>
                );
              })}
            </ul>
          );
        }

        export function SidebarContents({
          activeItemId,
          brand,
          groups,
          items,
          navigationLabel,
          onNavigate,
          renderLink,
          sidebarFooter,
        }: SidebarContentsProps) {
          const groupLabelPrefix = React.useId();

          return (
            <div
              className="flex min-h-0 flex-1 flex-col"
              data-slot="responsive-sidebar-contents"
            >
              <div
                className="flex min-h-16 shrink-0 items-center border-b border-border px-4"
                data-slot="responsive-sidebar-brand"
              >
                {brand}
              </div>

              <nav
                aria-label={navigationLabel}
                className="min-h-0 flex-1 space-y-5 overflow-y-auto px-3 py-4"
                data-slot="responsive-sidebar-navigation"
              >
                {items.length > 0 ? (
                  <NavigationItems
                    activeItemId={activeItemId}
                    items={items}
                    onNavigate={onNavigate}
                    renderLink={renderLink}
                  />
                ) : null}

                {groups.map((group, index) => {
                  const labelId = `${groupLabelPrefix}-${index}`;

                  return (
                    <div
                      aria-labelledby={labelId}
                      data-slot="responsive-sidebar-group"
                      key={group.id}
                      role="group"
                    >
                      <p
                        className="mb-2 px-3 text-[11px] font-medium text-muted-foreground [overflow-wrap:anywhere]"
                        id={labelId}
                      >
                        {group.label}
                      </p>
                      <NavigationItems
                        activeItemId={activeItemId}
                        items={group.items}
                        onNavigate={onNavigate}
                        renderLink={renderLink}
                      />
                    </div>
                  );
                })}
              </nav>

              {sidebarFooter ? (
                <div
                  className="shrink-0 border-t border-border p-4"
                  data-slot="responsive-sidebar-footer"
                >
                  {sidebarFooter}
                </div>
              ) : null}
            </div>
          );
        }

        ```
      </Tab>

      <Tab value="types.ts">
        ```ts title="src/components/blocks/responsive-sidebar/types.ts"
        import type * as React from "react";

        export type ResponsiveSidebarNavItem = Readonly<{
          id: string;
          label: string;
          href: string;
          icon?: React.ReactNode;
          badge?: React.ReactNode;
        }>;

        export type ResponsiveSidebarNavGroup = Readonly<{
          id: string;
          label: string;
          items: readonly ResponsiveSidebarNavItem[];
        }>;

        export type ResponsiveSidebarRenderLinkProps = Readonly<{
          item: ResponsiveSidebarNavItem;
          active: boolean;
          className: string;
          children: React.ReactNode;
          onNavigate: () => void;
        }>;

        export type ResponsiveSidebarProps = Readonly<{
          brand: React.ReactNode;
          navigationLabel: string;
          mobileTitle: string;
          mobileDescription?: string;
          mobileTriggerLabel?: string;
          items: readonly ResponsiveSidebarNavItem[];
          groups?: readonly ResponsiveSidebarNavGroup[];
          activeItemId?: string;
          renderLink?: (props: ResponsiveSidebarRenderLinkProps) => React.ReactNode;
          sidebarFooter?: React.ReactNode;
          headerTitle?: React.ReactNode;
          headerActions?: React.ReactNode;
          contentId?: string;
          /** Use div when embedding the shell inside an existing main landmark. */
          contentAs?: "main" | "div";
          skipToContentLabel?: string;
          className?: string;
          children: React.ReactNode;
        }>;

        ```
      </Tab>
    </Tabs>
  </Tab>
</Tabs>

The Preview tab renders the real canonical `ResponsiveSidebar` with fictional
consumer navigation and content. Its links stay inside the preview document and
no network or product service behavior is added. The Source tab includes the
complete canonical files at build time, not a duplicated implementation.

Your application copy does not receive automatic updates. Follow the usage
example below, connect your own behavior, and validate it in your application.
Return to the [Blocks catalog](/blocks) to choose another pattern.

## Install by copying source

1. Copy the complete `responsive-sidebar` directory to an application-owned
   path, such as `src/components/blocks/responsive-sidebar/`.
2. Install React 18 or newer and the public `@pycolors/ui` package.
3. Load the public `@pycolors/tokens/tokens.css` contract and configure the
   existing semantic Tailwind utilities used by your application.
4. Import the Block from its copied local entry point.

```tsx title="app-shell.tsx"
"use client";

import { ResponsiveSidebar } from "./components/blocks/responsive-sidebar";

const items = [
  { id: "overview", label: "Overview", href: "/overview" },
  { id: "projects", label: "Projects", href: "/projects", badge: "4" },
] as const;

export function AppShell({ children }: { children: React.ReactNode }) {
  return (
    <ResponsiveSidebar
      activeItemId="overview"
      brand={<strong>Acme</strong>}
      headerActions={<button type="button">Create project</button>}
      headerTitle={<h1>Overview</h1>}
      items={items}
      mobileDescription="Choose a workspace destination."
      mobileTitle="Workspace navigation"
      navigationLabel="Primary workspace navigation"
      sidebarFooter={<small>Acme workspace</small>}
    >
      {children}
    </ResponsiveSidebar>
  );
}
```

Icons are optional consumer-owned React nodes. The Block treats icon content as
decorative and keeps the visible item label as the link's accessible name.

## Props

The preview includes local Overview, Projects and Team views, grouped navigation,
a workspace identity and a member footer. “Add demo project” adds a fictional row
only in the preview; no project is saved or sent to a backend. The integration
guide links are ordinary navigation. Use the preview's Reset control to return
to the initial state.

When embedding the shell inside an existing page, pass `contentAs="div"` to
avoid nesting main landmarks. The default remains `contentAs="main"` for a full
application shell. The skip link still targets the focusable content element.
The mobile Sheet has a visually hidden accessible title and description so the
workspace brand appears once. Navigation labels wrap, and narrow-screen
navigation links and the menu trigger have at least 44px height.

| Prop                 | Type                                   | Default                     | Purpose                                                      |
| -------------------- | -------------------------------------- | --------------------------- | ------------------------------------------------------------ |
| `brand`              | `ReactNode`                            | required                    | Consumer brand or workspace identity.                        |
| `navigationLabel`    | `string`                               | required                    | Accessible label shared by desktop and mobile navigation.    |
| `mobileTitle`        | `string`                               | required                    | Accessible Sheet title.                                      |
| `mobileDescription`  | `string`                               | `Navigate the application.` | Accessible Sheet description.                                |
| `mobileTriggerLabel` | `string`                               | `Open navigation`           | Accessible name for the icon-only mobile trigger.            |
| `items`              | `readonly ResponsiveSidebarNavItem[]`  | required                    | Primary ungrouped navigation items.                          |
| `groups`             | `readonly ResponsiveSidebarNavGroup[]` | `[]`                        | Optional labelled navigation groups.                         |
| `activeItemId`       | `string`                               | none                        | Stable item ID that receives `aria-current="page"`.          |
| `renderLink`         | `(props) => ReactNode`                 | native anchor               | Router adapter seam.                                         |
| `sidebarFooter`      | `ReactNode`                            | none                        | Optional consumer footer content.                            |
| `headerTitle`        | `ReactNode`                            | none                        | Header title or context.                                     |
| `headerActions`      | `ReactNode`                            | none                        | Consumer actions for the header.                             |
| `contentAs`          | `"main" \| "div"`                      | `main`                      | Content element; use `div` inside an existing main landmark. |
| `contentId`          | `string`                               | `main-content`              | Main landmark ID and skip-link target.                       |
| `skipToContentLabel` | `string`                               | `Skip to content`           | Visible-on-focus skip-link text.                             |
| `className`          | `string`                               | none                        | Root layout customization.                                   |
| `children`           | `ReactNode`                            | required                    | Main application content.                                    |

Navigation item IDs must be unique and stable across `items` and every group.
The Block never derives active behavior from labels or URLs.

## Responsive behavior

* Below `md`, the persistent sidebar is hidden and the labelled header trigger
  opens a left-side Sheet.
* The Sheet is bounded to the viewport width and repeats the same consumer
  brand, navigation, groups, active state, and optional footer.
* Native-anchor navigation closes the Sheet. A custom router must invoke the
  provided `onNavigate` callback after activation.
* At `md` and above, the `w-64` sidebar is sticky, viewport-height, and
  independently scrollable while the header and main content remain flexible
  with `min-w-0`.
* Responsive visibility uses CSS utilities rather than viewport JavaScript.

## Router adaptation

The default renderer uses native anchors. Adapt a client router through
`renderLink`; preserve the supplied classes, active semantics, children, and
mobile close callback.

```tsx title="router-adapter.tsx"
<ResponsiveSidebar
  {...shellProps}
  renderLink={({ active, children, className, item, onNavigate }) => (
    <RouterLink
      aria-current={active ? "page" : undefined}
      className={className}
      href={item.href}
      onClick={onNavigate}
    >
      {children}
    </RouterLink>
  )}
>
  {children}
</ResponsiveSidebar>
```

`RouterLink` is a placeholder for the consumer's router component; the
canonical Block does not import Next.js or another routing library.

## Accessibility and keyboard behavior

* A visible-on-focus skip link targets the single focusable `main` landmark.
* Desktop and mobile navigation expose the required consumer label.
* The active native link uses `aria-current="page"`.
* The mobile trigger is a labelled `Button` with visible focus styling.
* The public `@pycolors/ui` Sheet supplies dialog focus containment, Escape and
  outside-interaction dismissal, a labelled close button, and focus restoration
  to the trigger.
* `SheetTitle` and `SheetDescription` provide the mobile dialog name and
  description.
* Navigation remains native link navigation; there is no custom roving focus or
  arrow-key model.

Consumers must keep item labels visible, preserve `aria-current` in a custom
renderer, provide meaningful navigation and Sheet labels, and avoid placing a
second `main` landmark inside `children`. These guarantees are verified by
focused interaction and axe tests; they are not a claim of full WCAG
certification.

## Styling and customization

The Block uses semantic background, foreground, muted, border, accent, and ring
utilities backed by the public tokens contract. It inherits the consumer's
light or dark token values and introduces no colors, fonts, theme provider,
global CSS, or local UI primitive copies.

After copying, the consumer owns customization and future maintenance. Compare
future canonical changes deliberately and apply only the updates that fit the
application. Rollback is removal or reversion of the copied directory and its
local integration; no package version or remote Block state is involved.

## Explicit non-goals

This Block does not provide user/session state, permissions, billing, metrics,
data fetching, server actions, product navigation, a full dashboard, a package,
a Registry item, generated Registry JSON, a CLI, automatic updates, a release,
or deployment. Starter Free informed the composition but is not imported and
is not source authority.