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