Sidebar
Vertical navigation + sidebar. 4 states × 2 types.
import * as React from "react";
import { House, Inbox, Star, User } from "@/components/ui/icons";
import { Sidebar, SidebarItem } from "@/components/ui/sidebar-item";
export default function Example() {
const [here, setHere] = React.useState("orders");
const go = (id: string) => (e: React.MouseEvent) => { e.preventDefault(); setHere(id); };
return (
<Sidebar style={{ width: 240, maxWidth: "100%" }}>
<SidebarItem href="#home" icon={<House />} active={here === "home"} onClick={go("home")}>Home</SidebarItem>
<SidebarItem href="#orders" icon={<Inbox />} badge={12} active={here === "orders"} onClick={go("orders")}>Orders</SidebarItem>
<SidebarItem href="#customers" icon={<User />} trailingChevron active={here === "customers"} onClick={go("customers")}>Customers</SidebarItem>
<SidebarItem href="#reports" icon={<Star />} disabled>Reports</SidebarItem>
</Sidebar>
);
}
Installation
$ npx uix-ui@latest add sidebar-itemThis copies the file into components/ui/sidebar-item.tsx in your project. npx shadcn@latest add @uix/sidebar-item works too.
Usage
import { Sidebar, SidebarItem } from "@/components/ui/sidebar-item";
<Sidebar style={{ width: 240, maxWidth: "100%" }}>
<SidebarItem href="#home" icon={<House />} active={here === "home"} onClick={go("home")}>Home</SidebarItem>
<SidebarItem href="#orders" icon={<Inbox />} badge={12} active={here === "orders"} onClick={go("orders")}>Orders</SidebarItem>
<SidebarItem href="#customers" icon={<User />} trailingChevron active={here === "customers"} onClick={go("customers")}>Customers</SidebarItem>
<SidebarItem href="#reports" icon={<Star />} disabled>Reports</SidebarItem>
</Sidebar>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
active | boolean | false | Marks the current page: sets aria-current and the active look. |
disabled | boolean | false | |
icon | React.ReactNode | – | Icon before the label. |
badge | React.ReactNode | – | Count or tag after the label. |
trailingChevron | boolean | false | Chevron at the end. |
Everything else is passed to the root element, so className, aria-* and event handlers work as usual.
Dependencies
Installed for you by the CLI.
class-variance-authorityclsxtailwind-merge
lib/utils.tscomponents/ui/icons.tsx
This file exports 2 parts: Sidebar, SidebarItem. Use only the ones you need.