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