Open History to see the last commit
1import { mergeProps } from "@base-ui/react/merge-props";
2import { useRender } from "@base-ui/react/use-render";
3import { cva, type VariantProps } from "class-variance-authority";
4import { PanelLeftCloseIcon, PanelLeftIcon } from "lucide-react";
5import * as React from "react";
6import { cn } from "~/lib/utils";
7import { Button } from "~/components/ui/button";
8import { Input, type InputProps } from "~/components/ui/input";
9import { ScrollArea } from "~/components/ui/scroll-area";
10import {
11 Sheet,
12 SheetDescription,
13 SheetHeader,
14 SheetPopup,
15 SheetTitle,
16} from "~/components/ui/sheet";
17import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
18import { useResizeDrag } from "~/hooks/useResizeDrag";
19import { useIsMobile } from "~/hooks/useMediaQuery";
20import { getLocalStorageItem, setLocalStorageItem } from "~/hooks/useLocalStorage";
21import { resolveSidebarState, type ResponsiveSidebarState } from "./sidebarState";
22import * as Schema from "effect/Schema";
23
24const SIDEBAR_COOKIE_NAME = "sidebar_state";
25const SIDEBAR_COOKIE_MAX_AGE = 60 * 60 * 24 * 7;
26const SIDEBAR_WIDTH = "16rem";
27const SIDEBAR_WIDTH_MOBILE = "calc(100vw - var(--spacing(3)))";
28const SIDEBAR_WIDTH_ICON = "3rem";
29const SIDEBAR_RESIZE_DEFAULT_MIN_WIDTH = 16 * 16;
30
31type SidebarContextProps = {
32 state: ResponsiveSidebarState;
33 open: boolean;
34 setOpen: (open: boolean) => void;
35 openMobile: boolean;
36 setOpenMobile: (open: boolean) => void;
37 isMobile: boolean;
38 toggleSidebar: () => void;
39};
40
41type SidebarResizableOptions = {
42 maxWidth?: number;
43 minWidth?: number;
44 onResize?: (width: number) => void;
45 shouldAcceptWidth?: (context: {
46 currentWidth: number;
47 nextWidth: number;
48 rail: HTMLButtonElement;
49 side: "left" | "right";
50 sidebarRoot: HTMLElement;
51 wrapper: HTMLElement;
52 }) => boolean;
53 storageKey?: string;
54};
55
56type SidebarResolvedResizableOptions = {
57 maxWidth: number;
58 minWidth: number;
59 onResize?: (width: number) => void;
60 shouldAcceptWidth?: (context: {
61 currentWidth: number;
62 nextWidth: number;
63 rail: HTMLButtonElement;
64 side: "left" | "right";
65 sidebarRoot: HTMLElement;
66 wrapper: HTMLElement;
67 }) => boolean;
68 storageKey: string | null;
69};
70
71type SidebarInstanceContextProps = {
72 resizable: SidebarResolvedResizableOptions | null;
73 side: "left" | "right";
74};
75
76const SidebarContext = React.createContext<SidebarContextProps | null>(null);
77const SidebarInstanceContext = React.createContext<SidebarInstanceContextProps | null>(null);
78
79function useSidebar() {
80 const context = React.use(SidebarContext);
81 if (!context) {
82 throw new Error("useSidebar must be used within a SidebarProvider.");
83 }
84
85 return context;
86}
87
88function useSidebarVisibility() {
89 const { isMobile, open, openMobile } = useSidebar();
90 return isMobile ? openMobile : open;
91}
92
93function SidebarProvider({
94 defaultOpen = true,
95 open: openProp,
96 onOpenChange: setOpenProp,
97 className,
98 style,
99 children,
100 ...props
101}: React.ComponentProps<"div"> & {
102 defaultOpen?: boolean;
103 open?: boolean;
104 onOpenChange?: (open: boolean) => void;
105}) {
106 const isMobile = useIsMobile();
107 const [openMobile, setOpenMobile] = React.useState(false);
108
109 // This is the internal state of the sidebar.
110 // We use openProp and setOpenProp for control from outside the component.
111 const [_open, _setOpen] = React.useState(defaultOpen);
112 const open = openProp ?? _open;
113 const setOpen = React.useCallback(
114 async (value: boolean | ((value: boolean) => boolean)) => {
115 const openState = typeof value === "function" ? value(open) : value;
116 if (setOpenProp) {
117 setOpenProp(openState);
118 } else {
119 _setOpen(openState);
120 }
121
122 // This sets the cookie to keep the sidebar state.
123 await cookieStore.set({
124 expires: Date.now() + SIDEBAR_COOKIE_MAX_AGE * 1000,
125 name: SIDEBAR_COOKIE_NAME,
126 path: "/",
127 value: String(openState),
128 });
129 },
130 [setOpenProp, open],
131 );
132
133 // Helper to toggle the sidebar.
134 const toggleSidebar = React.useCallback(() => {
135 return isMobile ? setOpenMobile((open) => !open) : setOpen((open) => !open);
136 }, [isMobile, setOpen]);
137
138 // We add a state so that we can do data-state="expanded" or "collapsed".
139 // This makes it easier to style the sidebar with Tailwind classes.
140 const state = resolveSidebarState({ isMobile, open, openMobile });
141
142 const contextValue = React.useMemo<SidebarContextProps>(
143 () => ({
144 isMobile,
145 open,
146 openMobile,
147 setOpen,
148 setOpenMobile,
149 state,
150 toggleSidebar,
151 }),
152 [state, open, setOpen, isMobile, openMobile, toggleSidebar],
153 );
154
155 return (
156 <SidebarContext value={contextValue}>
157 <div
158 // Inset layouts opt into bg-sidebar through className.
159 className={cn(
160 "group/sidebar-wrapper flex min-h-svh w-full max-sm:[--workspace-titlebar-control-size:--spacing(8)]",
161 className,
162 )}
163 data-sidebar-state={state}
164 data-slot="sidebar-wrapper"
165 style={
166 {
167 "--sidebar-width": SIDEBAR_WIDTH,
168 "--sidebar-width-icon": SIDEBAR_WIDTH_ICON,
169 "--workspace-titlebar-content-left":
170 "calc(var(--workspace-controls-left) + var(--workspace-titlebar-control-size) + var(--workspace-titlebar-control-gap))",
171 ...style,
172 } as React.CSSProperties
173 }
174 {...props}
175 >
176 {children}
177 </div>
178 </SidebarContext>
179 );
180}
181
182function Sidebar({
183 side = "left",
184 variant = "sidebar",
185 collapsible = "offcanvas",
186 resizable = false,
187 className,
188 children,
189 ...props
190}: React.ComponentProps<"div"> & {
191 side?: "left" | "right";
192 variant?: "sidebar" | "floating" | "inset";
193 collapsible?: "offcanvas" | "icon" | "none";
194 resizable?: boolean | SidebarResizableOptions;
195}) {
196 const { isMobile, state, openMobile, setOpenMobile } = useSidebar();
197 const resolvedResizable = React.useMemo<SidebarResolvedResizableOptions | null>(() => {
198 if (isMobile || collapsible === "none" || !resizable) {
199 return null;
200 }
201
202 const options = typeof resizable === "boolean" ? {} : resizable;
203 return {
204 maxWidth: options.maxWidth ?? Number.POSITIVE_INFINITY,
205 minWidth: options.minWidth ?? SIDEBAR_RESIZE_DEFAULT_MIN_WIDTH,
206 storageKey: options.storageKey ?? null,
207 ...(options.onResize ? { onResize: options.onResize } : {}),
208 ...(options.shouldAcceptWidth ? { shouldAcceptWidth: options.shouldAcceptWidth } : {}),
209 };
210 }, [collapsible, isMobile, resizable]);
211 const instanceContextValue = React.useMemo<SidebarInstanceContextProps>(
212 () => ({ side, resizable: resolvedResizable }),
213 [resolvedResizable, side],
214 );
215
216 if (collapsible === "none") {
217 return (
218 <SidebarInstanceContext value={instanceContextValue}>
219 <div
220 className={cn(
221 "flex h-full w-(--sidebar-width) flex-col bg-sidebar surface-grain text-sidebar-foreground",
222 className,
223 )}
224 data-slot="sidebar"
225 {...props}
226 >
227 {children}
228 </div>
229 </SidebarInstanceContext>
230 );
231 }
232
233 if (isMobile) {
234 return (
235 <SidebarInstanceContext value={instanceContextValue}>
236 <Sheet onOpenChange={setOpenMobile} open={openMobile} {...props}>
237 <SheetPopup
238 className={cn(
239 "w-(--sidebar-width) max-w-none bg-sidebar surface-grain p-0 text-sidebar-foreground",
240 className,
241 )}
242 data-mobile="true"
243 data-sidebar="sidebar"
244 data-slot="sidebar"
245 showCloseButton={false}
246 side={side}
247 style={
248 {
249 "--sidebar-width": SIDEBAR_WIDTH_MOBILE,
250 } as React.CSSProperties
251 }
252 >
253 <SheetHeader className="sr-only">
254 <SheetTitle>Sidebar</SheetTitle>
255 <SheetDescription>Displays the mobile sidebar.</SheetDescription>
256 </SheetHeader>
257 <div
258 className={cn(
259 "flex h-full w-full flex-col pb-safe pt-safe",
260 side === "left" ? "pl-safe" : "pr-safe",
261 )}
262 >
263 {children}
264 </div>
265 </SheetPopup>
266 </Sheet>
267 </SidebarInstanceContext>
268 );
269 }
270
271 return (
272 <SidebarInstanceContext value={instanceContextValue}>
273 <div
274 className="group peer hidden text-sidebar-foreground md:block"
275 data-collapsible={state === "collapsed" ? collapsible : ""}
276 data-side={side}
277 data-slot="sidebar"
278 data-state={state}
279 data-variant={variant}
280 >
281 {/* This is what handles the sidebar gap on desktop */}
282 <div
283 className={cn(
284 "relative w-(--sidebar-width) bg-transparent",
285 "[[data-panel-animations=true]_&]:transition-[width] [[data-panel-animations=true]_&]:[transition-duration:var(--panel-animation-duration)] [[data-panel-animations=true]_&]:ease-out",
286 "group-data-[collapsible=offcanvas]:w-0",
287 "group-data-[side=right]:rotate-180",
288 variant === "floating" || variant === "inset"
289 ? "group-data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)+(--spacing(4)))]"
290 : "group-data-[collapsible=icon]:w-(--sidebar-width-icon)",
291 )}
292 data-slot="sidebar-gap"
293 />
294 <div
295 className={cn(
296 "fixed inset-y-0 z-10 hidden h-svh w-(--sidebar-width) md:flex",
297 "[[data-panel-animations=true]_&]:transition-[left,right,width] [[data-panel-animations=true]_&]:[transition-duration:var(--panel-animation-duration)] [[data-panel-animations=true]_&]:ease-out",
298 side === "left"
299 ? "left-0 group-data-[collapsible=offcanvas]:left-[calc(var(--sidebar-width)*-1)]"
300 : "right-0 group-data-[collapsible=offcanvas]:right-[calc(var(--sidebar-width)*-1)]",
301 // Adjust the padding for floating and inset variants.
302 variant === "floating" || variant === "inset"
303 ? "p-2 group-data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)+(--spacing(4))+2px)]"
304 : "group-data-[collapsible=icon]:w-(--sidebar-width-icon) group-data-[side=left]:border-r group-data-[side=right]:border-l",
305 className,
306 )}
307 data-slot="sidebar-container"
308 {...props}
309 >
310 <div
311 className="flex h-full w-full flex-col bg-sidebar surface-grain group-data-[variant=floating]:rounded-lg group-data-[variant=floating]:border group-data-[variant=floating]:border-sidebar-border group-data-[variant=floating]:shadow-sm/5"
312 data-sidebar="sidebar"
313 data-slot="sidebar-inner"
314 >
315 {children}
316 </div>
317 </div>
318 </div>
319 </SidebarInstanceContext>
320 );
321}
322
323function SidebarTrigger({ className, onClick, ...props }: React.ComponentProps<typeof Button>) {
324 const { toggleSidebar } = useSidebar();
325 const isOpen = useSidebarVisibility();
326
327 return (
328 <Button
329 className={cn(
330 "size-[var(--workspace-titlebar-control-size)]! [-webkit-app-region:no-drag]",
331 className,
332 )}
333 data-sidebar="trigger"
334 data-slot="sidebar-trigger"
335 aria-pressed={isOpen}
336 onClick={(event) => {
337 onClick?.(event);
338 toggleSidebar();
339 }}
340 size="icon"
341 variant="ghost"
342 {...props}
343 >
344 {isOpen ? <PanelLeftCloseIcon className="size-4" /> : <PanelLeftIcon className="size-4" />}
345 <span className="sr-only">Toggle Sidebar</span>
346 </Button>
347 );
348}
349
350function clampSidebarWidth(width: number, options: SidebarResolvedResizableOptions): number {
351 return Math.max(options.minWidth, Math.min(width, options.maxWidth));
352}
353
354function SidebarRail({
355 className,
356 onClick,
357 onPointerCancel,
358 onLostPointerCapture,
359 onPointerDown,
360 onPointerMove,
361 onPointerUp,
362 ...props
363}: React.ComponentProps<"button">) {
364 const { open, toggleSidebar } = useSidebar();
365 const sidebarInstance = React.use(SidebarInstanceContext);
366 const railRef = React.useRef<HTMLButtonElement | null>(null);
367 const suppressClickRef = React.useRef(false);
368 const resolvedResizable = sidebarInstance?.resizable ?? null;
369 const latestResizable = React.useRef(resolvedResizable);
370 React.useLayoutEffect(() => {
371 latestResizable.current = resolvedResizable;
372 }, [resolvedResizable]);
373 const canResize = resolvedResizable !== null && open;
374 const railLabel = canResize ? "Resize Sidebar" : "Toggle Sidebar";
375 const railTitle = canResize ? "Drag to resize sidebar" : "Toggle Sidebar";
376 const resize = useResizeDrag<HTMLButtonElement>((event) => {
377 if (!resolvedResizable || !open) return null;
378 const rail = event.currentTarget;
379 const wrapper = rail.closest<HTMLElement>("[data-slot='sidebar-wrapper']");
380 const sidebarRoot = rail.closest<HTMLElement>("[data-slot='sidebar']");
381 const sidebarContainer = sidebarRoot?.querySelector<HTMLElement>(
382 "[data-slot='sidebar-container']",
383 );
384 if (!wrapper || !sidebarRoot || !sidebarContainer) return null;
385
386 const side = sidebarInstance?.side ?? "left";
387 let width = clampSidebarWidth(
388 sidebarContainer.getBoundingClientRect().width,
389 resolvedResizable,
390 );
391 const transitionTargets = [
392 sidebarRoot.querySelector<HTMLElement>("[data-slot='sidebar-gap']"),
393 sidebarContainer,
394 ].filter((element): element is HTMLElement => element !== null);
395 transitionTargets.forEach((element) => {
396 element.style.setProperty("transition-duration", "0ms");
397 });
398 wrapper.style.setProperty("--sidebar-width", `${width}px`);
399
400 return {
401 width,
402 edge: side === "left" ? "right" : "left",
403 resize(value) {
404 const options = latestResizable.current;
405 if (!options) return width;
406 const nextWidth = clampSidebarWidth(value, options);
407 const accepted =
408 options.shouldAcceptWidth?.({
409 currentWidth: width,
410 nextWidth,
411 rail,
412 side,
413 sidebarRoot,
414 wrapper,
415 }) ?? true;
416 if (accepted) {
417 wrapper.style.setProperty("--sidebar-width", `${nextWidth}px`);
418 width = nextWidth;
419 }
420 return width;
421 },
422 finish(finalWidth, moved) {
423 suppressClickRef.current = moved;
424 const options = latestResizable.current;
425 if (options?.storageKey) {
426 try {
427 setLocalStorageItem(options.storageKey, finalWidth, Schema.Finite);
428 } catch (error) {
429 console.error("Could not persist sidebar width.", error);
430 }
431 }
432 options?.onResize?.(finalWidth);
433 },
434 cleanup() {
435 transitionTargets.forEach((element) => {
436 element.style.removeProperty("transition-duration");
437 });
438 },
439 };
440 });
441
442 const handleClick = React.useCallback(
443 (event: React.MouseEvent<HTMLButtonElement>) => {
444 onClick?.(event);
445 if (event.defaultPrevented) return;
446 if (suppressClickRef.current) {
447 suppressClickRef.current = false;
448 event.preventDefault();
449 return;
450 }
451 if (resolvedResizable && open) {
452 event.preventDefault();
453 return;
454 }
455 toggleSidebar();
456 },
457 [onClick, open, resolvedResizable, toggleSidebar],
458 );
459
460 React.useLayoutEffect(() => {
461 if (!resolvedResizable?.storageKey || typeof window === "undefined") return;
462 const rail = railRef.current;
463 if (!rail) return;
464 const wrapper = rail.closest<HTMLElement>("[data-slot='sidebar-wrapper']");
465 if (!wrapper) return;
466
467 let storedWidth: number | null;
468 try {
469 storedWidth = getLocalStorageItem(resolvedResizable.storageKey, Schema.Finite);
470 } catch (error) {
471 console.error("Could not restore persisted sidebar width.", error);
472 return;
473 }
474 if (storedWidth === null) return;
475 const clampedWidth = clampSidebarWidth(storedWidth, resolvedResizable);
476 // Hydrate the CSS variable before the browser paints so a restored sidebar
477 // never flashes at the default width first.
478 wrapper.style.setProperty("--sidebar-width", `${clampedWidth}px`);
479 resolvedResizable.onResize?.(clampedWidth);
480 }, [resolvedResizable]);
481
482 return (
483 <Tooltip>
484 <TooltipTrigger
485 render={
486 <button
487 aria-label={railLabel}
488 className={cn(
489 /* disable pointer events only when offcanvas sidebar is collapsed, that's when the rail sits over the native scrollbar on windows and linux. icon mode stays fully clickable. */
490 "-translate-x-1/2 group-data-[side=left]:-right-4 absolute inset-y-0 z-20 hidden w-4 after:absolute after:inset-y-0 after:left-1/2 after:w-[2px] hover:after:bg-sidebar-border group-data-[side=right]:left-0 sm:flex [[data-collapsible=offcanvas][data-state=collapsed]_&]:pointer-events-none",
491 "[[data-panel-animations=true]_&]:transition-all [[data-panel-animations=true]_&]:[transition-duration:var(--panel-animation-duration)] [[data-panel-animations=true]_&]:ease-out",
492 "in-data-[side=left]:cursor-w-resize in-data-[side=right]:cursor-e-resize",
493 "[[data-side=left][data-state=collapsed]_&]:cursor-e-resize [[data-side=right][data-state=collapsed]_&]:cursor-w-resize",
494 "group-data-[collapsible=offcanvas]:translate-x-0 hover:group-data-[collapsible=offcanvas]:bg-sidebar group-data-[collapsible=offcanvas]:after:left-full",
495 "[[data-side=left][data-collapsible=offcanvas]_&]:-right-2",
496 "[[data-side=right][data-collapsible=offcanvas]_&]:-left-2",
497 className,
498 )}
499 data-sidebar="rail"
500 data-slot="sidebar-rail"
501 onClick={handleClick}
502 onLostPointerCapture={(event) => {
503 onLostPointerCapture?.(event);
504 if (!event.defaultPrevented) resize.onLostPointerCapture(event);
505 }}
506 onPointerCancel={(event) => {
507 onPointerCancel?.(event);
508 if (!event.defaultPrevented) resize.onPointerCancel(event);
509 }}
510 onPointerDown={(event) => {
511 onPointerDown?.(event);
512 if (!event.defaultPrevented) resize.onPointerDown(event);
513 }}
514 onPointerMove={(event) => {
515 onPointerMove?.(event);
516 if (!event.defaultPrevented) resize.onPointerMove(event);
517 }}
518 onPointerUp={(event) => {
519 onPointerUp?.(event);
520 if (!event.defaultPrevented) resize.onPointerUp(event);
521 }}
522 ref={railRef}
523 tabIndex={-1}
524 type="button"
525 {...props}
526 />
527 }
528 />
529 <TooltipPopup side="right">{railTitle}</TooltipPopup>
530 </Tooltip>
531 );
532}
533
534/** A bare search or rename field that sits in a row of sidebar chrome. */
535function SidebarInput({ className, ...props }: Omit<InputProps, "unstyled" | "variant">) {
536 return (
537 <Input
538 className={cn(
539 "[&_[data-slot=input]]:h-auto [&_[data-slot=input]]:p-0 [&_[data-slot=input]]:font-medium [&_[data-slot=input]]:text-sidebar-foreground [&_[data-slot=input]]:text-sm [&_[data-slot=input]]:leading-normal [&_[data-slot=input]]:placeholder:text-sidebar-muted-foreground",
540 className,
541 )}
542 unstyled
543 {...props}
544 />
545 );
546}
547
548function SidebarInset({ className, ...props }: React.ComponentProps<"main">) {
549 return (
550 <main
551 className={cn(
552 "relative flex min-w-0 w-full flex-1 flex-col bg-background surface-grain",
553 "md:peer-data-[variant=inset]:peer-data-[state=collapsed]:ms-2 md:peer-data-[variant=inset]:m-2 md:peer-data-[variant=inset]:ms-0 md:peer-data-[variant=inset]:rounded-xl md:peer-data-[variant=inset]:shadow-sm/5",
554 className,
555 )}
556 data-slot="sidebar-inset"
557 {...props}
558 />
559 );
560}
561
562function SidebarHeader({ className, ...props }: React.ComponentProps<"div">) {
563 return (
564 <div
565 className={cn("flex flex-col gap-2 p-2", className)}
566 data-sidebar="header"
567 data-slot="sidebar-header"
568 {...props}
569 />
570 );
571}
572
573function SidebarFooter({ className, ...props }: React.ComponentProps<"div">) {
574 return (
575 <div
576 className={cn("flex flex-col gap-2 px-[var(--sidebar-content-inset)] py-1", className)}
577 data-sidebar="footer"
578 data-slot="sidebar-footer"
579 {...props}
580 />
581 );
582}
583
584function SidebarContent({
585 className,
586 fixedHeader,
587 ...props
588}: React.ComponentProps<"div"> & {
589 fixedHeader?: React.ReactNode;
590}) {
591 return (
592 <>
593 {fixedHeader ? <div className="w-full shrink-0">{fixedHeader}</div> : null}
594 {/* Rows take focus on click. Scroll padding would make the browser nudge
595 the list whenever a focused row sits under the fade. */}
596 <ScrollArea
597 hideScrollbars
598 scrollFade
599 scrollFadePadding={false}
600 className="h-auto min-h-0 flex-1 [&>[data-slot=scroll-area-viewport]]:[--fade-size:0.75rem]"
601 >
602 <div
603 // Reordered rows must not pull the viewport to their new position.
604 className={cn(
605 // Stacked groups share one inset between them instead of doubling
606 // it, including across the fixed header's boundary.
607 "flex w-full min-w-0 flex-col [overflow-anchor:none] group-data-[collapsible=icon]:overflow-hidden [&>[data-sidebar=group]+[data-sidebar=group]]:pt-0",
608 fixedHeader && "[&>[data-sidebar=group]:first-child]:pt-0",
609 className,
610 )}
611 data-sidebar="content"
612 data-slot="sidebar-content"
613 {...props}
614 />
615 </ScrollArea>
616 </>
617 );
618}
619
620function SidebarGroup({ className, ...props }: React.ComponentProps<"div">) {
621 return (
622 <div
623 className={cn(
624 "relative flex w-full min-w-0 flex-col p-[var(--sidebar-content-inset)]",
625 className,
626 )}
627 data-sidebar="group"
628 data-slot="sidebar-group"
629 {...props}
630 />
631 );
632}
633
634function SidebarMenu({ className, ...props }: React.ComponentProps<"ul">) {
635 return (
636 <ul
637 className={cn("flex w-full min-w-0 flex-col gap-1", className)}
638 data-sidebar="menu"
639 data-slot="sidebar-menu"
640 {...props}
641 />
642 );
643}
644
645function SidebarMenuItem({ className, ...props }: React.ComponentProps<"li">) {
646 return (
647 <li
648 className={cn("group/menu-item relative", className)}
649 data-sidebar="menu-item"
650 data-slot="sidebar-menu-item"
651 {...props}
652 />
653 );
654}
655
656const sidebarMenuButtonVariants = cva(
657 "peer/menu-button flex w-full cursor-pointer items-center gap-[var(--sidebar-control-gap)] overflow-hidden text-left outline-hidden ring-ring transition-[width,height,padding] hover:bg-sidebar-row-hover hover:text-sidebar-foreground focus-visible:ring-2 active:bg-sidebar-row-active active:text-sidebar-foreground disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50 data-[active=true]:bg-sidebar-row-selected data-[active=true]:font-medium data-[active=true]:text-sidebar-foreground data-[state=open]:hover:bg-sidebar-row-hover data-[state=open]:hover:text-sidebar-foreground group-data-[collapsible=icon]:size-8! group-data-[collapsible=icon]:p-[var(--sidebar-content-inset)]! [&>span:last-child]:truncate [&>svg:not([class*='size-'])]:size-4 [&>svg]:shrink-0 [&>svg]:text-[var(--sidebar-icon-color)] hover:[&>svg]:text-sidebar-foreground active:[&>svg]:text-sidebar-foreground data-[active=true]:[&>svg]:text-sidebar-foreground",
658 {
659 defaultVariants: {
660 size: "default",
661 variant: "default",
662 },
663 variants: {
664 size: {
665 default:
666 "h-8 rounded-[var(--control-radius)] px-[var(--sidebar-row-content-inset)] py-1.5 text-sm",
667 icon: "size-8 justify-center rounded-[var(--control-radius)] p-0",
668 lg: "h-12 rounded-lg p-2 text-sm group-data-[collapsible=icon]:p-0!",
669 sm: "h-7 rounded-lg p-2 text-xs",
670 },
671 variant: {
672 default: "font-medium text-sidebar-muted-foreground/80",
673 outline: "bg-sidebar-control-surface ring-1 ring-sidebar-border",
674 },
675 },
676 },
677);
678
679function SidebarMenuButton({
680 isActive = false,
681 variant = "default",
682 size = "default",
683 tooltip,
684 className,
685 render,
686 ...props
687}: useRender.ComponentProps<"button"> & {
688 isActive?: boolean;
689 tooltip?: string | React.ComponentProps<typeof TooltipPopup>;
690} & VariantProps<typeof sidebarMenuButtonVariants>) {
691 const { isMobile, state } = useSidebar();
692
693 const defaultProps = {
694 className: cn(sidebarMenuButtonVariants({ size, variant }), className),
695 "data-active": isActive,
696 "data-sidebar": "menu-button",
697 "data-size": size,
698 "data-slot": "sidebar-menu-button",
699 };
700
701 const buttonProps = mergeProps<"button">(defaultProps, props);
702
703 const buttonElement = useRender({
704 defaultTagName: "button",
705 props: buttonProps,
706 render,
707 });
708
709 if (!tooltip) {
710 return buttonElement;
711 }
712
713 if (typeof tooltip === "string") {
714 tooltip = {
715 children: tooltip,
716 };
717 }
718
719 return (
720 <Tooltip>
721 <TooltipTrigger render={buttonElement as React.ReactElement<Record<string, unknown>>} />
722 <TooltipPopup
723 align="center"
724 hidden={state !== "collapsed" || isMobile}
725 side="right"
726 {...tooltip}
727 />
728 </Tooltip>
729 );
730}
731
732function SidebarMenuSub({ className, ...props }: React.ComponentProps<"ul">) {
733 return (
734 <ul
735 className={cn(
736 "mx-3.5 flex min-w-0 translate-x-px flex-col gap-1 border-sidebar-border border-l px-2.5 py-0.5",
737 "group-data-[collapsible=icon]:hidden",
738 className,
739 )}
740 data-sidebar="menu-sub"
741 data-slot="sidebar-menu-sub"
742 {...props}
743 />
744 );
745}
746
747function SidebarMenuSubItem({ className, ...props }: React.ComponentProps<"li">) {
748 return (
749 <li
750 className={cn("group/menu-sub-item relative", className)}
751 data-sidebar="menu-sub-item"
752 data-slot="sidebar-menu-sub-item"
753 {...props}
754 />
755 );
756}
757
758function SidebarMenuSubButton({
759 size = "md",
760 isActive = false,
761 className,
762 render,
763 ...props
764}: useRender.ComponentProps<"a"> & {
765 size?: "sm" | "md";
766 isActive?: boolean;
767}) {
768 const defaultProps = {
769 className: cn(
770 "-translate-x-px flex h-7 min-w-0 cursor-pointer items-center gap-2 overflow-hidden rounded-lg px-2 text-sidebar-foreground outline-hidden ring-ring hover:bg-sidebar-row-hover hover:text-sidebar-foreground focus-visible:ring-2 active:bg-sidebar-row-active active:text-sidebar-foreground disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50 [&>span:last-child]:truncate [&>svg:not([class*='size-'])]:size-4 [&>svg]:shrink-0 [&>svg]:text-sidebar-muted-foreground",
771 "data-[active=true]:bg-sidebar-row-selected data-[active=true]:text-sidebar-foreground",
772 size === "sm" && "text-xs",
773 size === "md" && "text-sm",
774 "group-data-[collapsible=icon]:hidden",
775 className,
776 ),
777 "data-active": isActive,
778 "data-sidebar": "menu-sub-button",
779 "data-size": size,
780 "data-slot": "sidebar-menu-sub-button",
781 };
782
783 return useRender({
784 defaultTagName: "a",
785 props: mergeProps<"a">(defaultProps, props),
786 render,
787 });
788}
789
790export {
791 SidebarInput,
792 Sidebar,
793 SidebarContent,
794 SidebarFooter,
795 SidebarGroup,
796 SidebarHeader,
797 SidebarInset,
798 SidebarMenu,
799 SidebarMenuButton,
800 SidebarMenuItem,
801 SidebarMenuSub,
802 SidebarMenuSubButton,
803 SidebarMenuSubItem,
804 SidebarProvider,
805 SidebarRail,
806 SidebarTrigger,
807 useSidebar,
808 useSidebarVisibility,
809};