# Sidebar

Use a sidebar navigation layout to keep your application content front and center.

```tsx
import { Dropdown, Header, Heading, Main, Profile, Separator, Sidebar, Spacer, Text } from '@fluxui-dev/react';
import {
    ArrowRightStartOnRectangleIcon,
    Bars2Icon,
    CalendarIcon,
    Cog6ToothIcon,
    DocumentTextIcon,
    HomeIcon,
    InboxIcon,
    InformationCircleIcon,
} from '@fluxui-dev/react/icons';

<div className="min-h-screen bg-white dark:bg-zinc-800 antialiased">
    <Sidebar sticky collapsible="mobile" className="bg-zinc-50 dark:bg-zinc-900 border-r border-zinc-200 dark:border-zinc-700">
        <Sidebar.Header>
            <Sidebar.Brand href="#" logo="/img/demo/logo.png" logoDark="/img/demo/dark-mode-logo.png" name="Acme Inc." />

            <Sidebar.Collapse className="lg:hidden" />
        </Sidebar.Header>

        <Sidebar.Search placeholder="Search..." />

        <Sidebar.Nav>
            <Sidebar.Item icon={HomeIcon} href="#" current>Home</Sidebar.Item>
            <Sidebar.Item icon={InboxIcon} badge="12" href="#">Inbox</Sidebar.Item>
            <Sidebar.Item icon={DocumentTextIcon} href="#">Documents</Sidebar.Item>
            <Sidebar.Item icon={CalendarIcon} href="#">Calendar</Sidebar.Item>

            <Sidebar.Group expandable heading="Favorites" className="grid">
                <Sidebar.Item href="#">Marketing site</Sidebar.Item>
                <Sidebar.Item href="#">Android app</Sidebar.Item>
                <Sidebar.Item href="#">Brand guidelines</Sidebar.Item>
            </Sidebar.Group>
        </Sidebar.Nav>

        <Sidebar.Spacer />

        <Sidebar.Nav>
            <Sidebar.Item icon={Cog6ToothIcon} href="#">Settings</Sidebar.Item>
            <Sidebar.Item icon={InformationCircleIcon} href="#">Help</Sidebar.Item>
        </Sidebar.Nav>

        <Dropdown position="top" align="start" className="max-lg:hidden">
            <Dropdown.Trigger>
                <Sidebar.Profile avatar="/img/demo/user.png" name="Olivia Martin" />
            </Dropdown.Trigger>

            <Dropdown.Menu>
                <Dropdown.RadioGroup defaultValue="olivia">
                    <Dropdown.Radio value="olivia">Olivia Martin</Dropdown.Radio>
                    <Dropdown.Radio value="truly">Truly Delta</Dropdown.Radio>
                </Dropdown.RadioGroup>

                <Dropdown.Separator />

                <Dropdown.Item icon={ArrowRightStartOnRectangleIcon}>Logout</Dropdown.Item>
            </Dropdown.Menu>
        </Dropdown>
    </Sidebar>

    <Header className="lg:hidden">
        <Sidebar.Toggle className="lg:hidden" icon={Bars2Icon} inset="left" />

        <Spacer />

        <Dropdown position="top" align="start">
            <Dropdown.Trigger>
                <Profile avatar="/img/demo/user.png" />
            </Dropdown.Trigger>

            <Dropdown.Menu>
                <Dropdown.RadioGroup defaultValue="olivia">
                    <Dropdown.Radio value="olivia">Olivia Martin</Dropdown.Radio>
                    <Dropdown.Radio value="truly">Truly Delta</Dropdown.Radio>
                </Dropdown.RadioGroup>

                <Dropdown.Separator />

                <Dropdown.Item icon={ArrowRightStartOnRectangleIcon}>Logout</Dropdown.Item>
            </Dropdown.Menu>
        </Dropdown>
    </Header>

    <Main>
        <Heading size="xl" level={1}>Good afternoon, Olivia</Heading>

        <Text className="mb-6 mt-2 text-base">Here's what's new today</Text>

        <Separator variant="subtle" />
    </Main>
</div>
```

## Inset main

Add `inset` to `Main` to place your content in a bordered panel with rounded corners.

```tsx
import { StarIcon } from '@fluxui-dev/react/icons';

<div className="min-h-dvh bg-zinc-50 dark:bg-zinc-900">
    <Sidebar sticky collapsible className="bg-zinc-50 dark:bg-zinc-900">
        <Sidebar.Header>
            <Sidebar.Brand href="#" logo="/img/demo/logo.png" logoDark="/img/demo/dark-mode-logo.png" name="Acme Inc." />

            <Sidebar.Collapse className="in-data-flux-sidebar-on-desktop:not-in-data-flux-sidebar-collapsed-desktop:-mr-2" />
        </Sidebar.Header>

        <Sidebar.Nav>
            <Sidebar.Item icon={HomeIcon} href="#" current>Home</Sidebar.Item>
            <Sidebar.Item icon={InboxIcon} badge="12" href="#">Inbox</Sidebar.Item>
            <Sidebar.Item icon={DocumentTextIcon} href="#">Documents</Sidebar.Item>
            <Sidebar.Item icon={CalendarIcon} href="#">Calendar</Sidebar.Item>

            <Sidebar.Group expandable icon={StarIcon} heading="Favorites" className="grid">
                <Sidebar.Item href="#">Marketing site</Sidebar.Item>
                <Sidebar.Item href="#">Android app</Sidebar.Item>
                <Sidebar.Item href="#">Brand guidelines</Sidebar.Item>
            </Sidebar.Group>
        </Sidebar.Nav>

        <Sidebar.Spacer />

        <Sidebar.Nav>
            <Sidebar.Item icon={Cog6ToothIcon} href="#">Settings</Sidebar.Item>
            <Sidebar.Item icon={InformationCircleIcon} href="#">Help</Sidebar.Item>
        </Sidebar.Nav>

        <Dropdown position="top" align="start" className="max-lg:hidden">
            <Dropdown.Trigger>
                <Sidebar.Profile avatar="/img/demo/user.png" name="Olivia Martin" />
            </Dropdown.Trigger>

            <Dropdown.Menu>
                <Dropdown.RadioGroup defaultValue="olivia">
                    <Dropdown.Radio value="olivia">Olivia Martin</Dropdown.Radio>
                    <Dropdown.Radio value="truly">Truly Delta</Dropdown.Radio>
                </Dropdown.RadioGroup>

                <Dropdown.Separator />

                <Dropdown.Item icon={ArrowRightStartOnRectangleIcon}>Logout</Dropdown.Item>
            </Dropdown.Menu>
        </Dropdown>
    </Sidebar>

    <Header className="lg:hidden bg-white dark:bg-zinc-800">
        <Sidebar.Toggle className="lg:hidden" icon={Bars2Icon} inset="left" />

        <Spacer />

        <Dropdown position="top" align="start">
            <Dropdown.Trigger>
                <Profile avatar="/img/demo/user.png" />
            </Dropdown.Trigger>

            <Dropdown.Menu>
                <Dropdown.RadioGroup defaultValue="olivia">
                    <Dropdown.Radio value="olivia">Olivia Martin</Dropdown.Radio>
                    <Dropdown.Radio value="truly">Truly Delta</Dropdown.Radio>
                </Dropdown.RadioGroup>

                <Dropdown.Separator />

                <Dropdown.Item icon={ArrowRightStartOnRectangleIcon}>Logout</Dropdown.Item>
            </Dropdown.Menu>
        </Dropdown>
    </Header>

    <Main inset className="lg:ms-0">
        <Heading size="xl" level={1}>Good afternoon, Olivia</Heading>

        <Text className="mt-2 mb-6 text-base">Here's what's new today</Text>

        <Separator variant="subtle" />
    </Main>
</div>
```

Set the background around the panel on your layout and sidebar. Add `lg:ms-0` to the main to remove the extra margin beside the sidebar.

## Secondary header

Use a top header for secondary navigation.

```tsx
import { Navbar } from '@fluxui-dev/react';

<div className="min-h-screen bg-white dark:bg-zinc-800 antialiased">
    <Sidebar sticky collapsible="mobile" className="bg-zinc-50 dark:bg-zinc-900 border-r border-zinc-200 dark:border-zinc-700">
        <Sidebar.Header>
            <Sidebar.Brand href="#" logo="/img/demo/logo.png" logoDark="/img/demo/dark-mode-logo.png" name="Acme Inc." />

            <Sidebar.Collapse className="lg:hidden" />
        </Sidebar.Header>

        <Sidebar.Search placeholder="Search..." />

        <Sidebar.Nav>
            <Sidebar.Item icon={HomeIcon} href="#" current>Home</Sidebar.Item>
            <Sidebar.Item icon={InboxIcon} badge="12" href="#">Inbox</Sidebar.Item>
            <Sidebar.Item icon={DocumentTextIcon} href="#">Documents</Sidebar.Item>
            <Sidebar.Item icon={CalendarIcon} href="#">Calendar</Sidebar.Item>

            <Sidebar.Group expandable heading="Favorites" className="grid">
                <Sidebar.Item href="#">Marketing site</Sidebar.Item>
                <Sidebar.Item href="#">Android app</Sidebar.Item>
                <Sidebar.Item href="#">Brand guidelines</Sidebar.Item>
            </Sidebar.Group>
        </Sidebar.Nav>

        <Sidebar.Spacer />

        <Sidebar.Nav>
            <Sidebar.Item icon={Cog6ToothIcon} href="#">Settings</Sidebar.Item>
            <Sidebar.Item icon={InformationCircleIcon} href="#">Help</Sidebar.Item>
        </Sidebar.Nav>

        <Dropdown position="top" align="start" className="max-lg:hidden">
            <Dropdown.Trigger>
                <Sidebar.Profile avatar="/img/demo/user.png" name="Olivia Martin" />
            </Dropdown.Trigger>

            <Dropdown.Menu>
                <Dropdown.RadioGroup defaultValue="olivia">
                    <Dropdown.Radio value="olivia">Olivia Martin</Dropdown.Radio>
                    <Dropdown.Radio value="truly">Truly Delta</Dropdown.Radio>
                </Dropdown.RadioGroup>

                <Dropdown.Separator />

                <Dropdown.Item icon={ArrowRightStartOnRectangleIcon}>Logout</Dropdown.Item>
            </Dropdown.Menu>
        </Dropdown>
    </Sidebar>

    <Header className="block! bg-white lg:bg-zinc-50 dark:bg-zinc-900 border-b border-zinc-200 dark:border-zinc-700">
        <Navbar className="lg:hidden w-full">
            <Sidebar.Toggle className="lg:hidden" icon={Bars2Icon} inset="left" />

            <Spacer />

            <Dropdown position="top" align="start">
                <Dropdown.Trigger>
                    <Profile avatar="/img/demo/user.png" />
                </Dropdown.Trigger>

                <Dropdown.Menu>
                    <Dropdown.RadioGroup defaultValue="olivia">
                        <Dropdown.Radio value="olivia">Olivia Martin</Dropdown.Radio>
                        <Dropdown.Radio value="truly">Truly Delta</Dropdown.Radio>
                    </Dropdown.RadioGroup>

                    <Dropdown.Separator />

                    <Dropdown.Item icon={ArrowRightStartOnRectangleIcon}>Logout</Dropdown.Item>
                </Dropdown.Menu>
            </Dropdown>
        </Navbar>

        <Navbar scrollable>
            <Navbar.Item href="#" current>Dashboard</Navbar.Item>
            <Navbar.Item badge="32" href="#">Orders</Navbar.Item>
            <Navbar.Item href="#">Catalog</Navbar.Item>
            <Navbar.Item href="#">Configuration</Navbar.Item>
        </Navbar>
    </Header>

    <Main>
        <Heading size="xl" level={1}>Good afternoon, Olivia</Heading>

        <Text className="mb-6 mt-2 text-base">Here's what's new today</Text>

        <Separator variant="subtle" />
    </Main>
</div>
```

## Collapsible sidebar

Make your app feel more spacious with a collapsible sidebar.

```tsx
<div className="min-h-screen bg-white dark:bg-zinc-800 antialiased">
    <Sidebar sticky collapsible className="bg-zinc-50 dark:bg-zinc-900 border-r border-zinc-200 dark:border-zinc-700">
        <Sidebar.Header>
            <Sidebar.Brand href="#" logo="/img/demo/logo.png" logoDark="/img/demo/dark-mode-logo.png" name="Acme Inc." />

            <Sidebar.Collapse className="in-data-flux-sidebar-on-desktop:not-in-data-flux-sidebar-collapsed-desktop:-mr-2" />
        </Sidebar.Header>

        <Sidebar.Nav>
            <Sidebar.Item icon={HomeIcon} href="#" current>Home</Sidebar.Item>
            <Sidebar.Item icon={InboxIcon} badge="12" href="#">Inbox</Sidebar.Item>
            <Sidebar.Item icon={DocumentTextIcon} href="#">Documents</Sidebar.Item>
            <Sidebar.Item icon={CalendarIcon} href="#">Calendar</Sidebar.Item>

            <Sidebar.Group expandable icon={StarIcon} heading="Favorites" className="grid">
                <Sidebar.Item href="#">Marketing site</Sidebar.Item>
                <Sidebar.Item href="#">Android app</Sidebar.Item>
                <Sidebar.Item href="#">Brand guidelines</Sidebar.Item>
            </Sidebar.Group>
        </Sidebar.Nav>

        <Sidebar.Spacer />

        <Sidebar.Nav>
            <Sidebar.Item icon={Cog6ToothIcon} href="#">Settings</Sidebar.Item>
            <Sidebar.Item icon={InformationCircleIcon} href="#">Help</Sidebar.Item>
        </Sidebar.Nav>

        <Dropdown position="top" align="start" className="max-lg:hidden">
            <Dropdown.Trigger>
                <Sidebar.Profile avatar="/img/demo/user.png" name="Olivia Martin" />
            </Dropdown.Trigger>

            <Dropdown.Menu>
                <Dropdown.RadioGroup defaultValue="olivia">
                    <Dropdown.Radio value="olivia">Olivia Martin</Dropdown.Radio>
                    <Dropdown.Radio value="truly">Truly Delta</Dropdown.Radio>
                </Dropdown.RadioGroup>

                <Dropdown.Separator />

                <Dropdown.Item icon={ArrowRightStartOnRectangleIcon}>Logout</Dropdown.Item>
            </Dropdown.Menu>
        </Dropdown>
    </Sidebar>

    <Header className="lg:hidden">
        <Sidebar.Toggle className="lg:hidden" icon={Bars2Icon} inset="left" />

        <Spacer />

        <Dropdown position="top" align="start">
            <Dropdown.Trigger>
                <Profile avatar="/img/demo/user.png" />
            </Dropdown.Trigger>

            <Dropdown.Menu>
                <Dropdown.RadioGroup defaultValue="olivia">
                    <Dropdown.Radio value="olivia">Olivia Martin</Dropdown.Radio>
                    <Dropdown.Radio value="truly">Truly Delta</Dropdown.Radio>
                </Dropdown.RadioGroup>

                <Dropdown.Separator />

                <Dropdown.Item icon={ArrowRightStartOnRectangleIcon}>Logout</Dropdown.Item>
            </Dropdown.Menu>
        </Dropdown>
    </Header>

    <Main>
        <Heading size="xl" level={1}>Good afternoon, Olivia</Heading>

        <Text className="mt-2 mb-6 text-base">Here's what's new today</Text>

        <Separator variant="subtle" />
    </Main>
</div>
```

## Persisting the collapsed state

A collapsible sidebar remembers whether it's collapsed in a cookie and restores it before the page paints, with no server code. To store it yourself, such as in the user's settings, pass `persist={false}` and control it:

```tsx
const [collapsed, setCollapsed] = useState(user.sidebarCollapsed);

<Sidebar collapsible persist={false} collapsed={collapsed} onCollapsedChange={setCollapsed}>
    ...
</Sidebar>
```

## JavaScript methods

You can import the `sidebar()` function from `@fluxui-dev/react` to toggle, open, and close the sidebar from anywhere, such as a keyboard shortcut. On desktop, open and close expand and collapse a collapsible sidebar; on mobile, they open and close the off-canvas sidebar.

```tsx
import { sidebar } from '@fluxui-dev/react';

sidebar().toggle();
sidebar().open();
sidebar().close();

// Toggle the sidebar with ⌘B or Ctrl+B...
useEffect(() => {
    const onKeyDown = (event: KeyboardEvent) => {
        if (event.key === 'b' && (event.metaKey || event.ctrlKey)) {
            event.preventDefault();
            sidebar().toggle();
        }
    };

    document.addEventListener('keydown', onKeyDown);

    return () => document.removeEventListener('keydown', onKeyDown);
}, []);
```

## Reference

### Sidebar

**Prop:**

- `sticky` - When present, makes the sidebar sticky when scrolling.
- `collapsible` - Controls sidebar collapse behavior. Values: `"mobile"` (mobile-only), `true` (both mobile and desktop), `false` (never collapsible).
- `breakpoint` - The viewport breakpoint for mobile/desktop behavior. Accepts pixel values (`1024`, `"1024px"`) or rem values (`"64rem"`). Defaults to `1024`.
- `persist` - Controls whether the collapsed state is saved to a `flux_sidebar_collapsed` cookie, restored before the page paints with no server setup. Defaults to `true` (desktop collapsed state persists across sessions). Set to `false` to disable persistence.
- `collapsed` - Controlled collapsed state on desktop. Pair with `onCollapsedChange`.
- `defaultCollapsed` - Initial collapsed state on desktop when uncontrolled. Default: `false`.
- `onCollapsedChange` - Callback fired when the desktop collapsed state changes, including when a saved state is restored, receiving the new state and `eventDetails`, whose `reason` is `trigger-press`, `imperative-action` for a call to `sidebar()`, or `initial` for a restored state.

**Attribute:**

- `data-flux-sidebar` - Applied to the root element for styling and identification.
- `data-flux-sidebar-on-desktop` - Applied when the viewport is at or above the breakpoint.
- `data-flux-sidebar-on-mobile` - Applied when the viewport is below the breakpoint.
- `data-flux-sidebar-collapsed-desktop` - Applied while the sidebar is collapsed to icons on desktop.
- `data-flux-sidebar-collapsed-mobile` - Applied while the off-canvas sidebar is closed on mobile.

### Sidebar.Header

Content for the sidebar header, typically containing brand and collapse button.

### Sidebar.Brand

**Prop:**

- `href` - The URL to navigate to when the brand is clicked. Default: `/`. An in-app URL navigates through the link registered in `FluxProvider`.
- `logo` - The URL or path to the logo image, or an element such as an SVG or icon.
- `logoDark` - The URL or path to the logo image for dark mode, used when `logo` is a URL.
- `logoClassName` - CSS classes applied to the box around a `logo` element, such as its size, shape, and background.
- `name` - The brand name to display next to the logo, hidden while the sidebar is collapsed.
- `alt` - Alternative text for the logo.
- `iconTrailing` - Icon to display after the name, such as a chevron when the brand opens a dropdown: a component or an element.
- `iconVariant` - Variant of the trailing icon: `outline`, `solid`, `mini`, or `micro`. Default: `micro`.
- `render` - Element to render instead of the `a` tag (such as `<button type="button" />` for a brand that opens a dropdown, which adds a hover background), with the brand's props merged in. Also accepts a function that receives the props and returns an element.

### Sidebar.Collapse

**Prop:**

- `inset` - Positioning of the collapse button. Space-separated values like `"left"`, `"right"`, `"top"`, `"bottom"`, or combinations like `"left top bottom"`, or `true` for all sides.
- `tooltip` - Tooltip text shown on hover, also used as the button's accessible name. Defaults to `"Toggle sidebar"`; an empty string disables it. Pass a `Tooltip` element without children, such as `<Tooltip kbd="⌘B" />`, to set its options; it shows on the right by default.

### Sidebar.Search

Button styled as a search field for opening your own search, such as a command palette.

**Prop:**

- `placeholder` - Text displayed in the button. Default: `"Search..."`.
- `kbd` - Keyboard shortcut hint displayed at the end of the button, such as `"⌘K"`.
- `tooltip` - Tooltip text shown when the sidebar is collapsed. Defaults to `placeholder`, with `kbd` at its end. Pass a `Tooltip` element without children to set its options; it shows on the right by default.
- `render` - Element to render instead of the `button`, with the search button's props merged in. Also accepts a function that receives the props and returns an element.

### Sidebar.Nav

Navigation items and groups to display.

### Sidebar.Item

**Prop:**

- `href` - The URL to navigate to when the item is clicked. When set, the item renders as an `a` tag. An in-app URL navigates through the link registered in `FluxProvider`.
- `render` - Element to render instead of the item (such as a router link with props `href` can't carry), with the item's props merged in. Also accepts a function that receives the props and returns an element.
- `icon` - The icon to display before the item text, a component or an element such as `<img />`, and the only part shown while the sidebar is collapsed.
- `iconTrailing` - Icon to display at the end of the item: a component or an element.
- `iconVariant` - Variant of the item's icons: `outline`, `solid`, `mini`, or `micro`. Default: `outline`.
- `iconDot` - If `true`, displays a small dot on the icon, such as for unread notifications.
- `badge` - A badge value to display on the right side of the item: text or a number renders in a zinc badge, an element such as `Navlist.Badge` renders as is, and nothing renders for `undefined`, `null`, `false`, or an empty string.
- `current` - When present, indicates this is the currently active item and sets `aria-current="page"`. Without it, the item is current when its `href` matches `FluxProvider`'s `currentUrl`.
- `accent` - If `false`, the current item uses zinc instead of the accent color. Default: `true`.
- `tooltip` - Tooltip text shown when sidebar is collapsed. Defaults to the item's text content. Pass a `Tooltip` element without children, such as `<Tooltip kbd="G I" />`, to set its options; it shows on the right by default.

**Attribute:**

- `data-flux-sidebar-item` - Applied to the root element for styling and identification.
- `data-current` - Applied when the item is active/current.

### Sidebar.Group

**Prop:**

- `heading` - The heading text for the group.
- `expandable` - When present, allows the group to be expanded/collapsed.
- `icon` - The icon to display before the group heading instead of the default chevron icon, a component or an element. Groups without an icon specified will be hidden when the sidebar is collapsed, and groups with one open their items in a menu.
- `iconVariant` - Variant of the icon: `outline`, `solid`, `mini`, or `micro`. Default: `outline`.
- `defaultOpen` - Controls the initial expanded state when uncontrolled. Defaults to `true`.
- `open` - Controlled expanded state of an expandable group. Pair with `onOpenChange`.
- `onOpenChange` - Callback fired when an expandable group expands or collapses, receiving the new open state and `eventDetails`, whose `reason` tells why.

### Sidebar.Spacer

Fills the free space in the sidebar, pushing what follows to the bottom.

### Sidebar.Profile

**Prop:**

- `avatar` - The URL or path to the user's avatar image, or a custom `Avatar` element.
- `name` - The user's display name, hidden while the sidebar is collapsed.
- `initials` - Custom initials to display when no avatar image is provided. Automatically generated from name if not provided.
- `circle` - Whether to display a circular avatar. Default: `false`.
- `chevron` - Whether to display a chevron icon (dropdown indicator). Default: `true`.
- `iconTrailing` - Icon to display instead of the chevron: a component, such as `ChevronUpDownIcon` from `@fluxui-dev/react/icons`, or an element.
- `iconVariant` - Variant of the trailing icon: `outline`, `solid`, `mini`, or `micro`. Default: `micro`.
- `render` - Element to render instead of the profile button, with its props merged in.

### Sidebar.Toggle

Button that opens and closes the sidebar from anywhere on the page, such as a header menu button, accepting all `Button` props except `href`.

**Prop:**

- `icon` - The icon to display in the toggle button (e.g., `Bars2Icon`, `XMarkIcon`).
- `inset` - Positioning of the toggle button (e.g., `left`).

### sidebar()

Controls every sidebar on the page from anywhere, and does nothing if no sidebar is mounted. A controlled sidebar receives the change through `onCollapsedChange`.

**Method:**

- `toggle()` - Does what `Sidebar.Toggle` does: collapses or expands the sidebar on desktop, and opens or closes it on mobile.
- `open()` - Expands the sidebar on desktop, or opens it on mobile and moves focus onto it.
- `close()` - Collapses the sidebar on desktop, or closes it on mobile.

### Main

**Prop:**

- `inset` - Displays the main content in a bordered surface with rounded corners and outer gutters on large screens. The layout fills the viewport and the main scrolls internally. On smaller screens, the main is full width and uses normal page scrolling.
- `container` - When present, constrains the main content to a container width. With `inset`, the content is centered inside the full-width surface.

**Attribute:**

- `data-flux-main` - Applied to the root element for styling and identification.
