# Sidebar

```tsx
import { Sidebar } from '@tangible/ui';
```

Interactive docs and examples: [Sidebar in Storybook](https://design.tangible.one/?path=/docs/components-sidebar--docs).

## Sidebar props

| Prop | Type | Default | Description |
| - | - | - | - |
| `children` | `ReactNode` | | Sidebar content (Header, Nav, etc.). |
| `aria-label?` | `string` | | Accessible label for the sidebar landmark (static) or dialog (drawer). When a page has multiple sidebars, each must have a unique label so screen reader users can distinguish between landmarks (e.g. "Course navigation", "Lesson settings"). |
| `className?` | `string` | | Additional CSS class names. |
| `collapsed?` | `boolean` | | Collapsed state (controlled). Pair with `onCollapsedChange`. Leave undefined to let the sidebar own the state. |
| `collapsible?` | `boolean` | `false` | Opt into the collapsed rail: the sidebar can shrink to `--tui-sidebar-collapsed-width`, hiding Header, Nav and Footer while leaving any other child — a `Sidebar.Toggle`, an icon rail — visible. Deliberately absent from drawer mode: a drawer is already a show/hide overlay, and a rail inside one has nothing to sit beside. |
| `defaultCollapsed?` | `boolean` | `false` | Initial collapsed state when uncontrolled. |
| `drawer?` | `boolean` | `false` | Enable drawer mode (mobile overlay). |
| `initialFocusSelector?` | `string` | | CSS selector, matched inside the drawer when it opens, for the element that takes focus: say, the current lesson in a long outline, so keyboard and screen reader users start where they are. Escape still closes and the close button is a Shift+Tab away. Don't point it at a destructive action. The target must be able to take focus (a control, a link, or anything with `tabindex`, -1 included) and be rendered at open. Otherwise a dev warning fires and focus goes to the default: an element marked `data-autofocus`, else the first control. It's read once, when the drawer opens; changing it while open doesn't move focus. Same prop as `Modal`'s, but the default differs: Modal skips its own close button, while a drawer has none, so its first control is often your Header's close button. That's what this prop is for. |
| `onClose?` | `() => void` | | Callback to close the drawer. Required when `drawer={true}`. Called when ESC is pressed or backdrop is clicked. Omitting this in drawer mode means the user cannot dismiss the overlay, which is a WCAG 2.1.1 (Keyboard) failure. |
| `onCollapsedChange?` | `(collapsed: boolean) => void` | | Called with the next collapsed state whenever `Sidebar.Toggle` is pressed — in both controlled and uncontrolled mode. |
| `open?` | `boolean` | | Drawer open state. |
| `position?` | `"left" \| "right"` | `'left'` | Sidebar position. Affects border placement and drawer slide direction. |
| `restoreFocusTo?` | `RefObject<HTMLElement \| null> \| (() => HTMLElement \| null)` | | Where focus goes when the drawer closes, read once it has finished closing: usually the button that opens it. Defaults to whatever had focus when it opened, which in Safari is often nothing: a clicked button doesn't take focus there, so focus would fall to <body>. Same prop as `Modal`'s. A function suits a target that only exists by close time. |

### Sidebar.Header

| Prop | Type | Default | Description |
| - | - | - | - |
| `children` | `ReactNode` | | Header content (back button, title, progress, CTAs). |
| `className?` | `string` | | Additional CSS class names. |

### Sidebar.Footer

| Prop | Type | Default | Description |
| - | - | - | - |
| `children` | `ReactNode` | | Footer content (CTAs, user info, etc.). Sticks to the bottom of the sidebar. |
| `className?` | `string` | | Additional CSS class names. |

### Sidebar.Nav

| Prop | Type | Default | Description |
| - | - | - | - |
| `children` | `ReactNode` | | Nav content (typically Accordion with StepList). |
| `aria-label?` | `string` | | Accessible label for the nav landmark. Use this for the primary label since nav is the actionable region. |
| `aria-labelledby?` | `string` | | ID of a visible heading that labels the navigation. |
| `className?` | `string` | | Additional CSS class names. |

### Sidebar.Toggle

| Prop | Type | Default | Description |
| - | - | - | - |
| `badge?` | `number` | | A count on the button's corner: unread notifications, items in a cart. Above `badgeMax` it shows "99+". `0`, a negative or a non-number shows nothing, so `badge={items.length}` needs no guard. The count is part of the button's name, not only its look: "Notifications, 5 new" (`labels.badgeCount`). The full count is spoken even when the badge is capped. Anything that replaces the name replaces the count with it: an `aria-label` or `aria-labelledby` (a dev warning fires), and `loadingLabel`, which is the whole busy name. A change of count is NOT announced: the name changes silently on a button the user isn't on. If people need to know about an arrival right away, tell them with your own status message (a Toast). The badge overhangs the button's end edge (a "99+" by about 10px at `sm`) and paints above later siblings, so leave a gap after a badged button. Its colour doesn't follow `theme`: it is `--tui-icon-button-badge-bg` (primary by default) on every button. Set `--tui-icon-button-badge-ring` to the surface the button sits on (a header) so its cut-out blends in. |
| `badgeDot?` | `boolean` | `false` | Show the badge as a dot instead of a number: "there's something new" without a count, or a quieter look for one. With a `badge` count as well, the dot still speaks the count in the name. |
| `badgeMax?` | `number` | `99` | The largest count the badge shows; above it, the badge reads "99+". At `xs`, consider 9: a wide count overhangs a small button a long way. Whole numbers of 1 or more. |
| `className?` | `string` | | Additional CSS class names. |
| `disabled?` | `boolean` | | Disabled state. |
| `href?` | `never` | | |
| `labels?` | `import("/home/runner/work/tangible-ui/tangible-ui/src/components/index").IconButtonLabels & import("/home/runner/work/tangible-ui/tangible-ui/src/components/index").SidebarToggleLabels` | | Override default strings for i18n, and opt in to loading announcements (`loadingStart` / `loadingEnd`, empty by default). The button's name is `label`; the busy name is `loadingLabel`. Accessible names for the two states. Defaults are English and are exported as `defaultSidebarToggleLabels`; a partial object overrides only the keys it names. |
| `loading?` | `boolean` | | Loading state: shows a spinner and blocks activation while keeping the button focusable (`aria-disabled`, not native `disabled`), so a keyboard user who triggered it keeps their place. Only `onClick` is guarded: put the action there, not in `onKeyDown`/`onPointerDown`. By default the start and end of loading aren't announced: the busy name ("Save, loading") is set on the focused button, which screen readers rarely re-read, and it reverts silently. Opt in with `labels={{ loadingStart: 'Saving…', loadingEnd: 'Saved' }}` to speak each transition through a pre-mounted polite live region (portaled, so it never becomes a DOM sibling of the button). If the button unmounts when the action completes, the end can't be spoken from here: confirm through your own status message (a Toast). With `loadingStart`, don't set `aria-busy` on an ancestor of the button: NVDA and Chromium hold live-region output under a busy ancestor. Test with a screen reader: some also re-read the busy name ("Save, loading"), which can double up with `loadingStart`. While loading, the button stays focusable, so it mutes its colors rather than dimming the whole element: the focus ring keeps full contrast. |
| `loadingLabel?` | `string` | `` `${name}, loading` (English), where `name` includes any badge `` | Accessible label override during loading state. For i18n support. It is the whole busy name: include the badge count yourself if it matters. |
| `shape?` | `"circle" \| "square"` | `'square'` | Button shape. Use `'circle'` for avatar-adjacent actions, FABs, or anywhere a round affordance is expected. |
| `showTooltip?` | `boolean` | `false` | Show the label as a tooltip on hover. Since icon-only buttons have no visible text, enabling this helps sighted users discover what the button does. Recommended for most standalone icon buttons. Can be omitted in toolbars where a shared Tooltip provider handles it, or where adjacent visible text already communicates the action. |
| `size?` | `"lg" \| "md" \| "sm" \| "xs"` | `'sm'` | Size of the button. `'xs'`: 24px — below WCAG 2.5.8 minimum target size (24×24 CSS px). Use only in dense, mouse-first UIs (toolbars, table rows) where touch is not expected. Consider pairing with adequate spacing or a larger touch target via padding/margin. `'sm'`: 32px (default); `'md'`: 40px; `'lg'`: 48px |
| `theme?` | `"danger" \| "info" \| "neutral" \| "primary" \| "secondary" \| "success" \| "warning"` | `'secondary'` | Semantic color theme. Note: Defaults to 'secondary' (unlike Button which defaults to 'primary') because IconButtons are typically used for toolbar/utility actions. |
| `tooltipSide?` | `"bottom" \| "left" \| "right" \| "top"` | `'top'` | Tooltip placement side. |
| `variant?` | `"ghost" \| "outline" \| "solid"` | `'ghost'` | Visual style variant. `'solid'`: Filled background; `'outline'`: Border only, transparent background; `'ghost'`: No border or background, icon only — has no at-rest visual affordance. Best suited for toolbar groups, icon bars, or other contexts where surrounding UI makes the interactive nature obvious. |

Also accepts the standard attributes of `ButtonHTMLAttributes` (`data-*`, `aria-*`, event handlers), passed to the element it renders.

## Other exports

- `defaultSidebarToggleLabels`: `Required<SidebarToggleLabels>`

## Examples

- [Region Padding (Edge-to-Edge Nav)](https://design.tangible.one/?path=/story/components-sidebar--region-padding): Each region has its own padding token, falling back to
  `--tui-sidebar-padding`: `--tui-sidebar-header-padding`,
  `--tui-sidebar-nav-padding`, `--tui-sidebar-footer-padding`. Here the nav
  keeps its block padding but drops the inline, so NavMenu rows run edge to
  edge; the footer keeps the default. NavMenu's own knobs square the rows
  (`--tui-nav-menu-item-radius`) and run the current-page bar the full row
  height (`--tui-nav-menu-indicator-inset-block` and `-radius` at `0`).

- [Collapsible](https://design.tangible.one/?path=/story/components-sidebar--collapsible): `collapsible` lets the sidebar shrink to a rail. Header, Nav and Footer go
  with it; anything else placed directly in the sidebar stays — which is why
  `Sidebar.Toggle` belongs there and not inside the Header.

- [Collapsed Rail](https://design.tangible.one/?path=/story/components-sidebar--collapsed-rail): Starts as a rail. The toggle is the only thing left to bring it back.

- [Controlled Collapse](https://design.tangible.one/?path=/story/components-sidebar--controlled-collapse): Controlled: the parent owns `collapsed` and the sidebar only reports intent
  through `onCollapsedChange`. Worth a story of its own because a consumer that
  forgets to write the value back gets a toggle that announces a state change
  and then does nothing — the sidebar deliberately will not move itself.

- [Collapsible (Right)](https://design.tangible.one/?path=/story/components-sidebar--collapsible-right): On a right-hand sidebar the glyph mirrors: both directions collapse away from the content.

- [Drawer Open Interaction](https://design.tangible.one/?path=/story/components-sidebar--drawer-open-interaction): Drawer opens with focus inside, role="dialog", and aria-modal.

- [Drawer Initial Focus](https://design.tangible.one/?path=/story/components-sidebar--drawer-initial-focus): `initialFocusSelector` sends focus to a chosen element when the drawer
  opens: here the current lesson, so a keyboard or screen reader user starts
  where they are. The close button is a Shift+Tab away and Escape still
  closes. Marking the element `data-autofocus` does the same without a
  selector.

- [Drawer Escape Interaction](https://design.tangible.one/?path=/story/components-sidebar--drawer-escape-interaction): Escape closes the drawer and restores focus to the trigger.

- [Drawer Backdrop Interaction](https://design.tangible.one/?path=/story/components-sidebar--drawer-backdrop-interaction): Clicking the backdrop closes the drawer.

- [Drawer Focus Trap Interaction](https://design.tangible.one/?path=/story/components-sidebar--drawer-focus-trap-interaction): Focus trap cycles Tab within the drawer.

- [Drawer Scroll Lock Interaction](https://design.tangible.one/?path=/story/components-sidebar--drawer-scroll-lock-interaction): Body scroll lock class is applied while the drawer is open.

- [Collapse Focus Recovery Interaction](https://design.tangible.one/?path=/story/components-sidebar--collapse-focus-recovery-interaction): Regression cover for the focus hole in a *programmatic* collapse: when
  `collapsed` flips from outside while focus sits in a region the rail hides,
  the browser drops focus to `<body>` and the user's place in the page is
  gone silently. Clicking the toggle never hits this — focus is already on the
  one element that survives — so only an external control reproduces it.

  This has to live in a browser. jsdom has no layout, so `offsetParent` is
  null for every element and the guard that separates "hidden under the
  cursor" from "user clicked away" cannot be exercised there at all.

- [Collapse Interaction](https://design.tangible.one/?path=/story/components-sidebar--collapse-interaction): The toggle survives its own collapse and keeps the sidebar reachable from
  the keyboard — the regions it hides leave the tab order with it.

Other stories: Static, With Header and Nav Slots, With Footer, Right Position, Drawer Mode, Drawer (Right Position), Drawer Keyboard Interaction, Course Viewer (Full Integration) ([interactive page](https://design.tangible.one/?path=/docs/components-sidebar--docs)).

## Component API tokens

Read via fallback, never declared. Set one on an ancestor to restyle every instance under it.

- `--tui-sidebar-width`
- `--tui-sidebar-collapsed-width`
- `--tui-sidebar-bg`
- `--tui-sidebar-fg`
- `--tui-sidebar-border`
- `--tui-sidebar-divider`
- `--tui-sidebar-padding`
- `--tui-sidebar-header-padding`
- `--tui-sidebar-nav-padding`
- `--tui-sidebar-footer-padding`
- `--tui-sidebar-z`

---

Interactive version: https://design.tangible.one/?path=/docs/components-sidebar--docs
