Sidebar
An app's sidebar: it folds away or to icons, becomes a sheet on a phone and toggles from anywhere with ⌘B. Its edge is ruled by hand, and the active page is shaded in with the pen.
"use client";
import { InkIcon } from "@/components/ui/ink-icons";
import {
Sidebar,
SidebarContent,
SidebarFooter,
SidebarGroup,
SidebarGroupContent,
SidebarGroupLabel,
SidebarHeader,
SidebarInset,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
SidebarProvider,
SidebarTrigger,
} from "@/components/ui/sidebar";
const items = [
{ title: "Home", icon: "home", active: true },
{ title: "Inbox", icon: "inbox" },
{ title: "Calendar", icon: "calendar" },
{ title: "Search", icon: "search" },
{ title: "Settings", icon: "settings" },
] as const;
export default function SidebarDemo() {
return (
<SidebarProvider contained className="h-96 w-full">
<Sidebar seed="sb-demo">
<SidebarHeader>
<div className="flex items-center gap-2 px-2 py-1 text-lg font-bold">
<InkIcon name="notebook" className="size-5" /> Notebook
</div>
</SidebarHeader>
<SidebarContent>
<SidebarGroup>
<SidebarGroupLabel>Application</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu>
{items.map((item) => (
<SidebarMenuItem key={item.title}>
<SidebarMenuButton isActive={"active" in item} render={<a href="#" />}>
<InkIcon name={item.icon} />
<span>{item.title}</span>
</SidebarMenuButton>
</SidebarMenuItem>
))}
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
</SidebarContent>
<SidebarFooter>
<div className="px-2 text-sm text-ink-3">ada@example.com</div>
</SidebarFooter>
</Sidebar>
<SidebarInset>
<header className="flex h-12 items-center gap-2 px-3">
<SidebarTrigger seed="sb-demo-trigger" />
<span className="text-ink-2">Home</span>
</header>
<div className="flex-1 p-4 text-ink-3">Press ⌘B, or the button, to fold the sidebar away.</div>
</SidebarInset>
</SidebarProvider>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ sidebar"use client";
import { createContext, useCallback, useContext, useEffect, useId, useMemo, useState, type ComponentProps, type CSSProperties, type ReactElement } from "react";
import { mergeProps } from "@base-ui/react/merge-props";
import { useRender } from "@base-ui/react/use-render";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/utils";
import { useIsMobile } from "@/hooks/use-mobile";
import { usePen, type Pen } from "@/hooks/use-ink-box";
import { inkWash } from "@/lib/ink";
import { hashSeed } from "@/lib/ink-sketch";
import { InkOutline } from "@/lib/ink-outline";
import { InkPanel } from "@/lib/ink-panel";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { InkIcon } from "@/components/ui/ink-icons";
import { Separator } from "@/components/ui/separator";
import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle } from "@/components/ui/sheet";
import { Skeleton } from "@/components/ui/skeleton";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
// An app's sidebar: it opens and folds away (or folds to icons), shows as a
// sheet on a phone, and is toggled from anywhere with ⌘B or Ctrl+B. Its
// edge is ruled by hand, or the whole panel is pencilled round when it
// "floats". The active page is shaded in the way a highlighted menu item is.
const WIDTH = "16rem";
const WIDTH_MOBILE = "18rem";
const WIDTH_ICON = "3rem";
type SidebarContextValue = {
state: "expanded" | "collapsed";
open: boolean;
setOpen: (open: boolean) => void;
openMobile: boolean;
setOpenMobile: (open: boolean) => void;
isMobile: boolean;
toggleSidebar: () => void;
contained: boolean;
};
const SidebarContext = createContext<SidebarContextValue | null>(null);
/** The sidebar's state and how to change it. */
function useSidebar() {
const context = useContext(SidebarContext);
if (!context) throw new Error("useSidebar must be used inside <SidebarProvider>.");
return context;
}
/**
* Wrap the sidebar and the page beside it. `open` and `onOpenChange` control
* it from outside. With `contained` the sidebar stays inside this box instead
* of the window, for a demo or a panel within a page.
*/
function SidebarProvider({
defaultOpen = true,
open: openProp,
onOpenChange,
contained = false,
className,
style,
children,
...props
}: ComponentProps<"div"> & { defaultOpen?: boolean; open?: boolean; onOpenChange?: (open: boolean) => void; contained?: boolean }) {
const isMobile = useIsMobile();
const [openMobile, setOpenMobile] = useState(false);
const [own, setOwn] = useState(defaultOpen);
const open = openProp ?? own;
const setOpen = useCallback(
(next: boolean) => {
if (openProp === undefined) setOwn(next);
onOpenChange?.(next);
},
[openProp, onOpenChange],
);
const toggleSidebar = useCallback(() => (isMobile ? setOpenMobile((o) => !o) : setOpen(!open)), [isMobile, open, setOpen]);
useEffect(() => {
const onKey = (event: KeyboardEvent) => {
if (event.key.toLowerCase() === "b" && (event.metaKey || event.ctrlKey)) {
event.preventDefault();
toggleSidebar();
}
};
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, [toggleSidebar]);
const value = useMemo<SidebarContextValue>(
() => ({ state: open ? "expanded" : "collapsed", open, setOpen, openMobile, setOpenMobile, isMobile, toggleSidebar, contained }),
[open, setOpen, openMobile, isMobile, toggleSidebar, contained],
);
return (
<SidebarContext value={value}>
<TooltipProvider delay={0}>
<div
data-slot="sidebar-wrapper"
data-contained={contained ? "" : undefined}
style={{ "--sidebar-width": WIDTH, "--sidebar-width-icon": WIDTH_ICON, ...inkWash, ...style } as CSSProperties}
className={cn("group/sidebar-wrapper flex min-h-svh w-full text-foreground has-data-[variant=inset]:bg-ink-5/40", contained && "relative min-h-0 overflow-hidden", className)}
{...props}
>
{children}
</div>
</TooltipProvider>
</SidebarContext>
);
}
/**
* The panel itself. `collapsible` is how it folds: "offcanvas" slides it away,
* "icon" folds it to a strip of icons, "none" keeps it open. `variant` is
* "sidebar" (an edge ruled by hand), "floating" (a pencilled panel set off
* from the page) or "inset" (the page is the pencilled panel).
*/
function Sidebar({
side = "left",
variant = "sidebar",
collapsible = "offcanvas",
className,
children,
seed,
roughness,
passes,
radius,
corners,
shadow,
draw,
weight,
speed,
...props
}: ComponentProps<"div"> & { side?: "left" | "right"; variant?: "sidebar" | "floating" | "inset"; collapsible?: "offcanvas" | "icon" | "none" } & Omit<Pen, "fill"> & { seed?: string | number }) {
const { isMobile, state, openMobile, setOpenMobile, contained } = useSidebar();
const pen = usePen({ roughness, passes, radius, corners, shadow, draw, weight, speed });
const boxed = variant === "floating";
if (collapsible === "none") {
return (
<div data-slot="sidebar" className={cn("relative flex h-full w-(--sidebar-width) flex-col text-foreground", className)} {...props}>
{children}
<Separator orientation="vertical" className="absolute inset-y-0 right-0 text-ink-line" seed={seed === undefined ? undefined : `${seed}-edge`} />
</div>
);
}
if (isMobile) {
return (
<Sheet open={openMobile} onOpenChange={setOpenMobile}>
<SheetContent
data-sidebar="sidebar"
data-slot="sidebar"
data-mobile="true"
side={side}
showCloseButton={false}
seed={seed}
style={{ "--sidebar-width": WIDTH_MOBILE, ...inkWash } as CSSProperties}
className="gap-0 p-0 data-[side=left]:w-(--sidebar-width) data-[side=left]:sm:max-w-none data-[side=right]:w-(--sidebar-width) data-[side=right]:sm:max-w-none"
>
<SheetHeader className="sr-only">
<SheetTitle>Sidebar</SheetTitle>
<SheetDescription>Shows the navigation for the app.</SheetDescription>
</SheetHeader>
<div className="flex h-full w-full flex-col">{children}</div>
</SheetContent>
</Sheet>
);
}
const fixed = contained ? "absolute h-full" : "fixed h-svh";
const floats = variant !== "sidebar";
return (
<div
className="group peer hidden text-foreground md:block"
data-state={state}
data-collapsible={state === "collapsed" ? collapsible : ""}
data-variant={variant}
data-side={side}
data-slot="sidebar"
>
{/* The space it takes up in the layout, which slides shut as it folds. */}
<div
data-slot="sidebar-gap"
className={cn(
"relative w-(--sidebar-width) bg-transparent transition-[width] duration-200 ease-linear motion-reduce:transition-none",
"group-data-[collapsible=offcanvas]:w-0 group-data-[side=right]:rotate-180",
floats ? "group-data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)+--spacing(4))]" : "group-data-[collapsible=icon]:w-(--sidebar-width-icon)",
)}
/>
<div
data-slot="sidebar-container"
className={cn(
"inset-y-0 z-10 hidden w-(--sidebar-width) transition-[left,right,width] duration-200 ease-linear motion-reduce:transition-none md:flex",
fixed,
side === "left" ? "left-0 group-data-[collapsible=offcanvas]:left-[calc(var(--sidebar-width)*-1)]" : "right-0 group-data-[collapsible=offcanvas]:right-[calc(var(--sidebar-width)*-1)]",
floats ? "p-2 group-data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)+--spacing(4)+2px)]" : "group-data-[collapsible=icon]:w-(--sidebar-width-icon)",
className,
)}
{...props}
>
<div data-sidebar="sidebar" data-slot="sidebar-inner" className={cn("relative isolate flex h-full w-full flex-col", boxed && "ink-paper")}>
{boxed && <InkPanel pen={pen} seed={seed} estimate={[256, 600]} offset={5} maxRadius={14} className="text-ink-line" />}
{children}
{variant === "sidebar" && (
<Separator
orientation="vertical"
className={cn("absolute inset-y-0 text-ink-line", side === "left" ? "right-0" : "left-0")}
seed={seed === undefined ? undefined : `${seed}-edge`}
draw={pen.draw}
/>
)}
</div>
</div>
</div>
);
}
/** A button that opens and folds the sidebar. */
function SidebarTrigger({ className, onClick, ...props }: ComponentProps<typeof Button>) {
const { toggleSidebar } = useSidebar();
return (
<Button
data-sidebar="trigger"
data-slot="sidebar-trigger"
variant="ghost"
size="icon-sm"
className={className}
onClick={(event) => {
onClick?.(event);
toggleSidebar();
}}
{...props}
>
<InkIcon name="sidebar" />
<span className="sr-only">Toggle sidebar</span>
</Button>
);
}
/** A strip down the sidebar's edge: click it to fold the sidebar. */
function SidebarRail({ className, ...props }: ComponentProps<"button">) {
const { toggleSidebar } = useSidebar();
return (
<button
data-sidebar="rail"
data-slot="sidebar-rail"
aria-label="Toggle sidebar"
tabIndex={-1}
onClick={toggleSidebar}
title="Toggle sidebar"
className={cn(
"absolute inset-y-0 z-20 hidden w-4 -translate-x-1/2 transition-all ease-linear after:absolute after:inset-y-0 after:left-1/2 after:w-0.5 hover:after:bg-ink-4 group-data-[side=left]:-right-4 group-data-[side=right]:left-0 sm:flex",
"in-data-[side=left]:cursor-w-resize in-data-[side=right]:cursor-e-resize [[data-side=left][data-state=collapsed]_&]:cursor-e-resize [[data-side=right][data-state=collapsed]_&]:cursor-w-resize",
"group-data-[collapsible=offcanvas]:translate-x-0 group-data-[collapsible=offcanvas]:after:left-full",
className,
)}
{...props}
/>
);
}
/** The page beside the sidebar. With an `inset` sidebar it is the pencilled panel. */
function SidebarInset({ className, children, ...props }: ComponentProps<"main">) {
const pen = usePen({});
return (
<main
data-slot="sidebar-inset"
className={cn(
"relative isolate flex w-full min-w-0 flex-1 flex-col bg-background [--inset-ink:0] md:peer-data-[variant=inset]:m-2 md:peer-data-[variant=inset]:ml-0 md:peer-data-[variant=inset]:[--inset-ink:1] md:peer-data-[variant=inset]:peer-data-[state=collapsed]:ml-2",
className,
)}
{...props}
>
<InkOutline pen={pen} estimate={[640, 480]} maxRadius={14} pad={4} className="text-ink-line opacity-(--inset-ink)" />
{children}
</main>
);
}
/** A search field for the top of the sidebar. */
function SidebarInput({ className, ...props }: ComponentProps<typeof Input>) {
return <Input data-slot="sidebar-input" data-sidebar="input" className={cn("h-8 w-full", className)} {...props} />;
}
function SidebarHeader({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="sidebar-header" data-sidebar="header" className={cn("flex flex-col gap-2 p-3", className)} {...props} />;
}
function SidebarFooter({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="sidebar-footer" data-sidebar="footer" className={cn("flex flex-col gap-2 p-3", className)} {...props} />;
}
function SidebarSeparator({ className, ...props }: ComponentProps<typeof Separator>) {
return <Separator data-slot="sidebar-separator" data-sidebar="separator" className={cn("mx-3 w-auto", className)} {...props} />;
}
function SidebarContent({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="sidebar-content" data-sidebar="content" className={cn("flex min-h-0 flex-1 flex-col gap-2 overflow-auto group-data-[collapsible=icon]:overflow-hidden", className)} {...props} />;
}
function SidebarGroup({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="sidebar-group" data-sidebar="group" className={cn("relative flex w-full min-w-0 flex-col p-2", className)} {...props} />;
}
/** A heading over a group of links. Fades out when the sidebar folds to icons. */
function SidebarGroupLabel({ className, render, ...props }: useRender.ComponentProps<"div">) {
return useRender({
defaultTagName: "div",
props: mergeProps<"div">(
{
className: cn(
"flex h-8 shrink-0 items-center px-2 text-sm text-ink-3 outline-hidden transition-[margin,opacity] duration-200 ease-linear",
"focus-visible:outline-solid focus-visible:outline-[1.5px] focus-visible:outline-ring [&>svg]:size-4 [&>svg]:shrink-0",
"group-data-[collapsible=icon]:-mt-8 group-data-[collapsible=icon]:opacity-0",
className,
),
},
props,
),
render,
state: { slot: "sidebar-group-label", sidebar: "group-label" },
});
}
function SidebarGroupAction({ className, render, ...props }: useRender.ComponentProps<"button">) {
return useRender({
defaultTagName: "button",
props: mergeProps<"button">(
{
className: cn(
"absolute top-3.5 right-3 flex aspect-square w-5 cursor-pointer items-center justify-center p-0 text-ink-3 outline-hidden transition-colors hover:text-ink",
"focus-visible:outline-solid focus-visible:outline-[1.5px] focus-visible:outline-ring [&>svg]:size-4 [&>svg]:shrink-0",
"after:absolute after:-inset-2 md:after:hidden group-data-[collapsible=icon]:hidden",
className,
),
},
props,
),
render,
state: { slot: "sidebar-group-action", sidebar: "group-action" },
});
}
function SidebarGroupContent({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="sidebar-group-content" data-sidebar="group-content" className={cn("w-full text-base", className)} {...props} />;
}
function SidebarMenu({ className, ...props }: ComponentProps<"ul">) {
return <ul data-slot="sidebar-menu" data-sidebar="menu" className={cn("flex w-full min-w-0 flex-col gap-1", className)} {...props} />;
}
function SidebarMenuItem({ className, ...props }: ComponentProps<"li">) {
return <li data-slot="sidebar-menu-item" data-sidebar="menu-item" className={cn("group/menu-item relative", className)} {...props} />;
}
const menuButtonVariants = cva(
[
"peer/menu-button group/menu-button relative isolate flex w-full cursor-pointer items-center gap-2 overflow-hidden p-2 text-left text-base text-ink-2 outline-hidden transition-[width,height,padding,color] duration-(--dur-hover)",
"before:pointer-events-none before:absolute before:inset-x-0 before:inset-y-0 before:-z-10 before:bg-current before:opacity-0 before:transition-opacity before:duration-(--dur-press)",
"before:[mask-image:var(--ink-wash-1)] before:[mask-size:100%_100%] before:[mask-repeat:no-repeat]",
"hover:text-ink hover:before:opacity-[0.13] focus-visible:text-ink focus-visible:outline-solid focus-visible:outline-[1.5px] focus-visible:-outline-offset-2 focus-visible:outline-ring",
"data-[active=true]:font-bold data-[active=true]:text-ink data-[active=true]:before:opacity-[0.16] data-popup-open:text-ink data-popup-open:before:opacity-[0.13]",
"disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50",
"group-has-data-[sidebar=menu-action]/menu-item:pr-8",
"group-data-[collapsible=icon]:size-8! group-data-[collapsible=icon]:p-2! [&>span:last-child]:truncate [&>svg]:size-4 [&>svg]:shrink-0",
],
{
variants: {
variant: { default: "", outline: "" },
size: {
default: "h-9",
sm: "h-7 text-sm group-data-[collapsible=icon]:p-0!",
lg: "h-12 group-data-[collapsible=icon]:p-0!",
},
},
defaultVariants: { variant: "default", size: "default" },
},
);
/**
* A link or button in the sidebar. `isActive` shades it in, `tooltip` names it
* when the sidebar is folded to icons, and `render` makes it your framework's link.
*/
function SidebarMenuButton({
render,
isActive = false,
variant = "default",
size = "default",
tooltip,
className,
seed,
...props
}: useRender.ComponentProps<"button"> & { isActive?: boolean; tooltip?: string | ComponentProps<typeof TooltipContent>; seed?: string | number } & VariantProps<typeof menuButtonVariants>) {
const { isMobile, state } = useSidebar();
const pen = usePen({ passes: 1, corners: "joined" });
const { children, ...rest } = props;
const button = useRender({
defaultTagName: "button",
props: mergeProps<"button">(
{
className: cn(menuButtonVariants({ variant, size }), className),
children: (
<>
{variant === "outline" && <InkOutline pen={pen} seed={seed} estimate={[200, 36]} className="text-ink-line" />}
{children}
</>
),
},
rest,
),
render,
state: { slot: "sidebar-menu-button", sidebar: "menu-button", size, active: isActive },
}) as ReactElement;
if (!tooltip) return button;
const content = typeof tooltip === "string" ? { children: tooltip } : tooltip;
return (
<Tooltip disabled={state !== "collapsed" || isMobile}>
<TooltipTrigger render={button} />
<TooltipContent side="right" align="center" {...content} />
</Tooltip>
);
}
function SidebarMenuAction({ className, render, showOnHover = false, ...props }: useRender.ComponentProps<"button"> & { showOnHover?: boolean }) {
return useRender({
defaultTagName: "button",
props: mergeProps<"button">(
{
className: cn(
"absolute top-1.5 right-1 flex aspect-square w-6 cursor-pointer items-center justify-center p-0 text-ink-3 outline-hidden transition-colors hover:text-ink",
"focus-visible:outline-solid focus-visible:outline-[1.5px] focus-visible:outline-ring peer-data-[size=default]/menu-button:top-2 peer-data-[size=lg]/menu-button:top-3.5 peer-data-[size=sm]/menu-button:top-1",
"after:absolute after:-inset-2 md:after:hidden group-data-[collapsible=icon]:hidden [&>svg]:size-4 [&>svg]:shrink-0",
showOnHover && "group-focus-within/menu-item:opacity-100 group-hover/menu-item:opacity-100 peer-data-[active=true]/menu-button:text-ink aria-expanded:opacity-100 md:opacity-0",
className,
),
},
props,
),
render,
state: { slot: "sidebar-menu-action", sidebar: "menu-action" },
});
}
function SidebarMenuBadge({ className, ...props }: ComponentProps<"div">) {
return (
<div
data-slot="sidebar-menu-badge"
data-sidebar="menu-badge"
className={cn(
"pointer-events-none absolute right-2 flex h-5 min-w-5 items-center justify-center px-1 text-sm text-ink-3 tabular-nums select-none",
"peer-data-[size=default]/menu-button:top-2 peer-data-[size=lg]/menu-button:top-3.5 peer-data-[size=sm]/menu-button:top-1 peer-data-[active=true]/menu-button:text-ink group-data-[collapsible=icon]:hidden",
className,
)}
{...props}
/>
);
}
/** A placeholder for a menu item that is still loading. */
function SidebarMenuSkeleton({ className, showIcon = false, ...props }: ComponentProps<"div"> & { showIcon?: boolean }) {
// Each skeleton keeps its own width, from its place in the tree rather than at random, so server and browser agree.
const id = useId();
const width = `${50 + (hashSeed(id) % 40)}%`;
return (
<div data-slot="sidebar-menu-skeleton" data-sidebar="menu-skeleton" className={cn("flex h-9 items-center gap-2 px-2", className)} {...props}>
{showIcon && <Skeleton className="size-4" seed={`${id}-icon`} />}
<Skeleton className="h-4 max-w-(--skeleton-width) flex-1" style={{ "--skeleton-width": width } as CSSProperties} seed={`${id}-text`} />
</div>
);
}
function SidebarMenuSub({ className, ...props }: ComponentProps<"ul">) {
return (
<ul
data-slot="sidebar-menu-sub"
data-sidebar="menu-sub"
className={cn("mx-3.5 flex min-w-0 translate-x-px flex-col gap-1 border-l border-ink-4/70 px-2.5 py-0.5 group-data-[collapsible=icon]:hidden", className)}
{...props}
/>
);
}
function SidebarMenuSubItem({ className, ...props }: ComponentProps<"li">) {
return <li data-slot="sidebar-menu-sub-item" data-sidebar="menu-sub-item" className={cn("group/menu-sub-item relative", className)} {...props} />;
}
function SidebarMenuSubButton({
render,
size = "md",
isActive = false,
className,
...props
}: useRender.ComponentProps<"a"> & { size?: "sm" | "md"; isActive?: boolean }) {
return useRender({
defaultTagName: "a",
props: mergeProps<"a">(
{
className: cn(
"relative isolate flex h-7 min-w-0 -translate-x-px cursor-pointer items-center gap-2 overflow-hidden px-2 text-ink-2 outline-hidden transition-colors duration-(--dur-hover)",
"before:pointer-events-none before:absolute before:inset-0 before:-z-10 before:bg-current before:opacity-0 before:transition-opacity before:duration-(--dur-press) before:[mask-image:var(--ink-wash-2)] before:[mask-size:100%_100%] before:[mask-repeat:no-repeat]",
"hover:text-ink hover:before:opacity-[0.13] focus-visible:outline-solid focus-visible:outline-[1.5px] focus-visible:outline-ring",
"disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50 [&>span:last-child]:truncate [&>svg]:size-4 [&>svg]:shrink-0",
"data-[active=true]:font-bold data-[active=true]:text-ink data-[active=true]:before:opacity-[0.16]",
size === "sm" ? "text-sm" : "text-base",
"group-data-[collapsible=icon]:hidden",
className,
),
},
props,
),
render,
state: { slot: "sidebar-menu-sub-button", sidebar: "menu-sub-button", size, active: isActive },
});
}
export {
Sidebar,
SidebarContent,
SidebarFooter,
SidebarGroup,
SidebarGroupAction,
SidebarGroupContent,
SidebarGroupLabel,
SidebarHeader,
SidebarInput,
SidebarInset,
SidebarMenu,
SidebarMenuAction,
SidebarMenuBadge,
SidebarMenuButton,
SidebarMenuItem,
SidebarMenuSkeleton,
SidebarMenuSub,
SidebarMenuSubButton,
SidebarMenuSubItem,
SidebarProvider,
SidebarRail,
SidebarSeparator,
SidebarTrigger,
useSidebar,
};import { useSyncExternalStore } from "react";
const query = "(max-width: 767px)";
function subscribe(onChange: () => void) {
const list = window.matchMedia(query);
list.addEventListener("change", onChange);
return () => list.removeEventListener("change", onChange);
}
/** Whether the window is phone-sized (under 768px). False on the server, and until the browser has been asked. */
export function useIsMobile() {
return useSyncExternalStore(
subscribe,
() => window.matchMedia(query).matches,
() => false,
);
}Usage
import { Sidebar, SidebarContent, SidebarGroup, SidebarInset, SidebarMenu, SidebarMenuButton, SidebarMenuItem, SidebarProvider, SidebarTrigger } from "@/components/ui/sidebar"
<SidebarProvider>
<Sidebar>
<SidebarContent>
<SidebarGroup>
<SidebarMenu>
<SidebarMenuItem>
<SidebarMenuButton isActive render={<a href="/" />}>Home</SidebarMenuButton>
</SidebarMenuItem>
</SidebarMenu>
</SidebarGroup>
</SidebarContent>
</Sidebar>
<SidebarInset>
<SidebarTrigger />
…
</SidebarInset>
</SidebarProvider>Examples
Folds to icons
Tooltips name each item while the sidebar is a strip of icons; the rail on its edge folds it too.
"use client";
import { InkIcon } from "@/components/ui/ink-icons";
import {
Sidebar,
SidebarContent,
SidebarGroup,
SidebarGroupContent,
SidebarHeader,
SidebarInset,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
SidebarProvider,
SidebarRail,
SidebarTrigger,
} from "@/components/ui/sidebar";
const items = [
{ title: "Pages", icon: "file-text", active: true },
{ title: "Sketches", icon: "pen" },
{ title: "Maps", icon: "map" },
{ title: "Archive", icon: "archive" },
] as const;
export default function SidebarIcons() {
return (
<SidebarProvider contained className="h-80 w-full">
<Sidebar collapsible="icon" seed="sb-icons">
<SidebarHeader>
<SidebarMenu>
<SidebarMenuItem>
<SidebarMenuButton size="lg" tooltip="Notebook">
<InkIcon name="notebook" />
<span className="font-bold">Notebook</span>
</SidebarMenuButton>
</SidebarMenuItem>
</SidebarMenu>
</SidebarHeader>
<SidebarContent>
<SidebarGroup>
<SidebarGroupContent>
<SidebarMenu>
{items.map((item) => (
<SidebarMenuItem key={item.title}>
<SidebarMenuButton isActive={"active" in item} tooltip={item.title}>
<InkIcon name={item.icon} />
<span>{item.title}</span>
</SidebarMenuButton>
</SidebarMenuItem>
))}
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
</SidebarContent>
<SidebarRail />
</Sidebar>
<SidebarInset>
<header className="flex h-12 items-center gap-2 px-3">
<SidebarTrigger seed="sb-icons-trigger" />
<span className="text-ink-2">Folds to icons</span>
</header>
</SidebarInset>
</SidebarProvider>
);
}Floating, with groups
A pencilled panel set off from the page, a badge, and a submenu in a Collapsible.
"use client";
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible";
import { InkIcon } from "@/components/ui/ink-icons";
import {
Sidebar,
SidebarContent,
SidebarGroup,
SidebarGroupContent,
SidebarGroupLabel,
SidebarInset,
SidebarMenu,
SidebarMenuBadge,
SidebarMenuButton,
SidebarMenuItem,
SidebarMenuSub,
SidebarMenuSubButton,
SidebarMenuSubItem,
SidebarProvider,
SidebarTrigger,
} from "@/components/ui/sidebar";
export default function SidebarFloating() {
return (
<SidebarProvider contained className="h-96 w-full">
<Sidebar variant="floating" seed="sb-float">
<SidebarContent>
<SidebarGroup>
<SidebarGroupLabel>Notebooks</SidebarGroupLabel>
<SidebarGroupContent>
<SidebarMenu>
<SidebarMenuItem>
<SidebarMenuButton isActive>
<InkIcon name="inbox" />
<span>Inbox</span>
</SidebarMenuButton>
<SidebarMenuBadge>24</SidebarMenuBadge>
</SidebarMenuItem>
<Collapsible defaultOpen render={<SidebarMenuItem />}>
<CollapsibleTrigger render={<SidebarMenuButton />}>
<InkIcon name="folder" />
<span>Projects</span>
<InkIcon name="chevron-down" className="ml-auto" />
</CollapsibleTrigger>
<CollapsibleContent>
<SidebarMenuSub>
<SidebarMenuSubItem>
<SidebarMenuSubButton render={<a href="#" />}>
<span>Field sketches</span>
</SidebarMenuSubButton>
</SidebarMenuSubItem>
<SidebarMenuSubItem>
<SidebarMenuSubButton render={<a href="#" />} isActive>
<span>Letters</span>
</SidebarMenuSubButton>
</SidebarMenuSubItem>
</SidebarMenuSub>
</CollapsibleContent>
</Collapsible>
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
</SidebarContent>
</Sidebar>
<SidebarInset className="bg-transparent">
<header className="flex h-12 items-center gap-2 px-3">
<SidebarTrigger seed="sb-float-trigger" />
</header>
</SidebarInset>
</SidebarProvider>
);
}Inset
The page itself is the pencilled panel; skeletons stand in for items still loading.
"use client";
import { InkIcon } from "@/components/ui/ink-icons";
import {
Sidebar,
SidebarContent,
SidebarGroup,
SidebarGroupContent,
SidebarInset,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
SidebarMenuSkeleton,
SidebarProvider,
SidebarTrigger,
} from "@/components/ui/sidebar";
export default function SidebarInsetExample() {
return (
<SidebarProvider contained className="h-96 w-full">
<Sidebar variant="inset">
<SidebarContent>
<SidebarGroup>
<SidebarGroupContent>
<SidebarMenu>
<SidebarMenuItem>
<SidebarMenuButton isActive>
<InkIcon name="home" />
<span>Dashboard</span>
</SidebarMenuButton>
</SidebarMenuItem>
{[0, 1, 2].map((i) => (
<SidebarMenuItem key={i}>
<SidebarMenuSkeleton showIcon />
</SidebarMenuItem>
))}
</SidebarMenu>
</SidebarGroupContent>
</SidebarGroup>
</SidebarContent>
</Sidebar>
<SidebarInset>
<header className="flex min-h-12 items-center gap-2 px-3 py-2">
<SidebarTrigger seed="sb-inset-trigger" />
<span className="text-ink-2">Dashboard</span>
</header>
<p className="px-4 text-ink-3">The page is the pencilled panel, set in from the sidebar.</p>
</SidebarInset>
</SidebarProvider>
);
}API reference
- SidebarProvider defaultOpen / open / onOpenChangedefault
true booleanOpen or folded, controlled or not.
- SidebarProvider containeddefault
false booleanKeep the sidebar inside its box instead of the window, for a demo or a panel within a page.
- Sidebar sidedefault
"left" "left" | "right"Which edge it sits on.
- Sidebar variantdefault
"sidebar" "sidebar" | "floating" | "inset"An edge ruled by hand, a pencilled panel set off from the page, or the page as the pencilled panel.
- Sidebar collapsibledefault
"offcanvas" "offcanvas" | "icon" | "none"Slides away, folds to a strip of icons, or stays open.
- SidebarMenuButton isActive / tooltip / render
boolean / string | props / ReactElementShades the page you are on; names the item when the sidebar is folded to icons; renders your framework's link.
- SidebarMenuButton variant / size
"default" | "outline" / "default" | "sm" | "lg"Plain, or pencilled round; and its height.
- useSidebar()
{ state, open, setOpen, openMobile, setOpenMobile, isMobile, toggleSidebar }Read and change the sidebar from anywhere inside the provider.
- keyboard
⌘B or Ctrl+BToggles the sidebar, or its sheet on a phone.
Pen settings
The drawing also takes draw, roughness, passes, radius, corners, shadow, weight and speed, as props or from the nearest InkProvider. What each one does.