---
title: "Sidebar"
description: "Sidebar with accessible presentation and local state."
---

# Sidebar

Sidebar with accessible presentation and local state.

## Installation

Install the shared package once in your Exact app. This version is a beta.

```sh
bun add --exact @rightui/ui@0.4.0-beta.4
```

Or choose another package manager:

```sh
npm install --save-exact @rightui/ui@0.4.0-beta.4
pnpm add --save-exact @rightui/ui@0.4.0-beta.4
yarn add --exact @rightui/ui@0.4.0-beta.4
```

Exact compilation uses Bun 1.4.2. Register Geist and Newsreader, declare the font marker and add the root provider using [Installation](/docs/installation).

### Manual source setup

Install an editable copy of the complete package:

```sh
mkdir -p vendor/rightui vendor/rightui-ui
npm pack @rightui/ui@0.4.0-beta.4 --pack-destination vendor/rightui
tar -xzf ./vendor/rightui/rightui-ui-0.4.0-beta.4.tgz -C vendor/rightui-ui --strip-components=1
bun add file:./vendor/rightui-ui
```

Keep package.json, the Contract files, ui.tamber, assets and licenses together.

### Import

```contract
use Sidebar, SidebarEntry, SidebarTrigger from "@rightui/ui"
```

## Usage

Import Sidebar, SidebarEntry from @rightui/ui. Store its value in state and update it through the provided action callback.

## Example

```contract
use Sidebar, SidebarEntry from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  derive narrow = width < 600
  state location = "projects"
  state collapsed = false
  action navigate(value: string)
    location = value
  action toggleSidebar
    collapsed = not collapsed
  view
    column gap="ui.space-16" min-width=0 max-width="100%"
      Sidebar(id="desktop-sidebar", label="Workspace navigation", entries=[SidebarEntry(id="projects", label="Projets", symbol="◈"), SidebarEntry(id="media", label="Media", symbol="▧"), SidebarEntry(id="favorites", label="Favoris", symbol="☆")], selected=location, collapsed=collapsed, reduced=reduced, choose=navigate, toggle=toggleSidebar)

```

## API Reference

| Property | Type |
| --- | --- |
| Sidebar.id | string |
| Sidebar.label | string |
| Sidebar.entries | SidebarEntry[] |
| Sidebar.selected | string |
| Sidebar.collapsed | bool |
| Sidebar.reduced | bool |
| Sidebar.choose | action |
| Sidebar.toggle | action |
| SidebarTrigger.id | string |
| SidebarTrigger.expanded | bool |
| SidebarTrigger.controls | string |
| SidebarTrigger.press | action |

## Source

File: packages/ui/sidebar.contract

```contract
use FontWeights from "./typography-context.contract"
use uiFontFamily from "./typography-context.contract"
use Control from "./styles.contract"
use DisclosureButton from "./button.contract"

shape SidebarEntry
  id: string
  label: string
  symbol: string

component Sidebar
  props
    id: string
    label: string
    entries: SidebarEntry[]
    selected: string
    collapsed: bool
    reduced: bool
    choose: action
    toggle: action
  inject
    uiWeights: FontWeights
    uiFont: string
  view
    nav id=id testId=id aria-label=label display="flex" flex-direction="column" gap="ui.space-8" padding="ui.space-8" box-sizing="border-box"
      width=(collapsed ? "ui.space-48" : "ui.sidebar-width") max-width="100%" flex-shrink=0 min-width="0px"
      -exact-layout-transition=(reduced ? "none" : "ui.duration-normal ui.easing-linear")
      background-color="ui.muted" border-width="ui.border-width" border-style="solid" border-color="ui.border" border-radius="ui.panel-radius"
      SidebarTrigger(id=id + "-toggle", expanded=(not collapsed), controls=id + "-items", press=toggle)
      column id=(id + "-items") gap="ui.space-4" min-width="0px"
        for item in entries key=item.id
          button id=(id + "-item-" + item.id) testId=(id + "-item-" + item.id) class=Control font-weight=uiWeights.medium press=choose(item.id)
            aria-current=(selected == item.id ? "page" : "false") aria-label=item.label title=item.label
            display="flex" align-items="center" gap="ui.space-8" min-height="ui.space-32" padding="ui.space-8" border="0"
            background-color=(selected == item.id ? "ui.surface" : "transparent") color="ui.foreground"
            transition=(reduced ? "none" : "background-color ui.duration-fast ui.easing")
            text item.symbol aria-hidden=true flex-shrink=0 font-family=uiFontFamily(uiFont) data-rightui-font="body"
            if not collapsed
              text item.label min-width="0px" text-align="left" font-family=uiFontFamily(uiFont) data-rightui-font="body"

component SidebarTrigger
  props
    id: string
    expanded: bool
    controls: string
    press: action
  view
    DisclosureButton(id=id, label="Toggle sidebar", variant="ghost", controls=controls, expanded=expanded, icon=true, press=press)
      svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.stroke-standard" aria-hidden=true
        rect x=3 y=3 width=18 height=18 rx=2
        path d="M9 3v18"

```
