# Sidebar

> The main navigation of a dashboard.

The sidebar is the main navigation of a Sosialoka dashboard, on the left of every page.

## When to use

- Use it for the 4 to 8 main sections of a product.
- Group related items under a label when there are more than six.
- Put page-level actions in the page, not in the sidebar.

_Live preview of the React and Vue versions: https://design.sosialoka.tech/components/sidebar/_

## Anatomy

1. Product logo
2. Active item
3. Search card
4. Content

This is the sidebar of the dashboard shell (Alternative A): a white panel (`sidebar`) 16rem wide, with icon and label items on `radius-md`. The active item uses `sidebar-accent` with `sidebar-accent-foreground` text, the product's soft tint. On small screens it becomes a sheet that slides in.

## Variants

- `collapsible="offcanvas"` (default): slides away on small screens.
- `collapsible="icon"`: collapses to icons.
- `collapsible="none"`: always shown, as in the preview.

## Props

| React | Vue | Flutter | Type | Default | Use |
| --- | --- | --- | --- | --- | --- |
| `SidebarProvider` | `SidebarProvider` | none | element | none | Holds the open state; wraps the layout. |
| `collapsible` | `collapsible` | none | see Variants | `offcanvas` | How it collapses. |
| `SidebarMenuButton isActive` | `SidebarMenuButton :is-active` | `backgroundColor` from `sidebar-accent` | boolean | `false` | Marks the current section. |
| `SidebarMenuButton render` | `SidebarMenuButton as-child` | `onPressed` | element | button | Renders the item as a link. |

## Code

**React**
```tsx
import { BarChart3Icon, DiscIcon, FileTextIcon, HomeIcon, SettingsIcon } from "lucide-react"
import {
  Sidebar,
  SidebarContent,
  SidebarGroup,
  SidebarGroupContent,
  SidebarMenu,
  SidebarMenuButton,
  SidebarMenuItem,
  SidebarProvider,
} from "@/components/ui/sidebar"

const items = [
  { title: "Dasbor", icon: HomeIcon, active: true },
  { title: "Rilis", icon: DiscIcon },
  { title: "Royalti", icon: BarChart3Icon },
  { title: "Dokumen", icon: FileTextIcon },
  { title: "Pengaturan", icon: SettingsIcon },
]

export default function SidebarDemo() {
  return (
    <SidebarProvider className="min-h-0 w-auto">
      <div className="h-96 overflow-hidden rounded-lg border border-border">
        <Sidebar collapsible="none">
          <SidebarContent>
            <SidebarGroup>
              <SidebarGroupContent>
                <SidebarMenu>
                  {items.map((item) => (
                    <SidebarMenuItem key={item.title}>
                      <SidebarMenuButton
                        isActive={item.active}
                        render={<a href="#" aria-current={item.active ? "page" : undefined} />}
                      >
                        <item.icon />
                        <span>{item.title}</span>
                      </SidebarMenuButton>
                    </SidebarMenuItem>
                  ))}
                </SidebarMenu>
              </SidebarGroupContent>
            </SidebarGroup>
          </SidebarContent>
        </Sidebar>
      </div>
    </SidebarProvider>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import { BarChart3, Disc, FileText, Home, Settings } from "@lucide/vue"
import {
  Sidebar,
  SidebarContent,
  SidebarGroup,
  SidebarGroupContent,
  SidebarMenu,
  SidebarMenuButton,
  SidebarMenuItem,
  SidebarProvider,
} from "@/components/ui/sidebar"

const items = [
  { title: "Dasbor", icon: Home, active: true },
  { title: "Rilis", icon: Disc },
  { title: "Royalti", icon: BarChart3 },
  { title: "Dokumen", icon: FileText },
  { title: "Pengaturan", icon: Settings },
]
</script>

<template>
  <SidebarProvider class="min-h-0 w-auto">
    <div class="h-96 overflow-hidden rounded-lg border border-border">
      <Sidebar collapsible="none">
        <SidebarContent>
          <SidebarGroup>
            <SidebarGroupContent>
              <SidebarMenu>
                <SidebarMenuItem v-for="item in items" :key="item.title">
                  <SidebarMenuButton as-child :is-active="item.active">
                    <a href="#" :aria-current="item.active ? 'page' : undefined">
                      <component :is="item.icon" />
                      <span>{{ item.title }}</span>
                    </a>
                  </SidebarMenuButton>
                </SidebarMenuItem>
              </SidebarMenu>
            </SidebarGroupContent>
          </SidebarGroup>
        </SidebarContent>
      </Sidebar>
    </div>
  </SidebarProvider>
</template>
```

**Flutter**
```dart
// shadcn_ui has no sidebar widget: build it from a Column of ShadButton.ghost items,
// and mark the active one with the primary-soft and primary-text colors of the Sosialoka theme.
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:sosialoka_ui/sosialoka_ui.dart';

const items = ['Dasbor', 'Rilis', 'Royalti', 'Dokumen', 'Pengaturan'];

class SidebarDemo extends StatelessWidget {
  const SidebarDemo({super.key, this.active = 'Dasbor'});

  final String active;

  @override
  Widget build(BuildContext context) {
    final scheme = ShadTheme.of(context).colorScheme;
    final c = context.sosialoka;
    return Container(
      width: 256,
      color: scheme.card,
      padding: const EdgeInsets.all(8),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          for (final item in items)
            ShadButton.ghost(
              mainAxisAlignment: MainAxisAlignment.start,
              backgroundColor: item == active ? c.primarySoft : null,
              foregroundColor: item == active ? c.primaryText : scheme.foreground,
              onPressed: () {},
              child: Text(item),
            ),
        ],
      ),
    );
  }
}
```

## Install

**React**
```bash
npx shadcn@latest add https://design.sosialoka.tech/r/react/sidebar.json
```

**Vue**
```bash
npx shadcn-vue@latest add https://design.sosialoka.tech/r/vue/sidebar.json
```

It also installs the Sosialoka sheet, separator, skeleton, tooltip, input and button.

## Accessibility

- Render items as links, and set `aria-current="page"` on the active one.
- The collapse button needs an `aria-label`; the sidebar can be toggled with ⌘B or Ctrl+B.
