Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

sidebar.tsx

847 lines28.2 KB

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 { useMediaQuery } 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;
91​
92function useSidebarVisibility() {
93 const { isMobile, open, openMobile } = useSidebar();
94 return isMobile ? openMobile : open;
96​
97function SidebarProvider({
98 defaultOpen = true,
99 mobileBreakpoint,
100 open: openProp,
101 onOpenChange: setOpenProp,
102 className,
103 style,
104 children,
105 ...props
106}: React.ComponentProps<"div"> & {
107 defaultOpen?: boolean;
108 mobileBreakpoint?: number;
109 open?: boolean;
110 onOpenChange?: (open: boolean) => void;
111}) {
112 const isMobile = useMediaQuery(
113 mobileBreakpoint === undefined ? "max-md" : { max: mobileBreakpoint },
114 );
115 const [openMobile, setOpenMobile] = React.useState(false);
116 if (!isMobile && openMobile) {
117 setOpenMobile(false);
118 }
119 const [mobileSheetMounted, setMobileSheetMounted] = React.useState(false);
120 const setMobileSheetElement = (element: HTMLDivElement | null) => {
121 setMobileSheetMounted(element !== null);
122 };
123​
124 const [_open, _setOpen] = React.useState(defaultOpen);
125 const open = openProp ?? _open;
126 const setOpen = async (value: boolean | ((value: boolean) => boolean)) => {
127 const openState = typeof value === "function" ? value(open) : value;
128 if (setOpenProp) {
129 setOpenProp(openState);
130 } else {
131 _setOpen(openState);
132 }
133​
134 await cookieStore.set({
135 expires: Date.now() + SIDEBAR_COOKIE_MAX_AGE * 1000,
136 name: SIDEBAR_COOKIE_NAME,
137 path: "/",
138 value: String(openState),
139 });
140 };
141​
142 const toggleSidebar = () => {
143 return isMobile ? setOpenMobile((open) => !open) : setOpen((open) => !open);
144 };
145​
146 const state = resolveSidebarState({ isMobile, open, openMobile });
147​
148 const contextValue: SidebarContextProps = {
149 isMobile,
150 open,
151 openMobile,
152 mobileSheetMounted,
153 setMobileSheetElement,
154 setOpen,
155 setOpenMobile,
156 state,
157 toggleSidebar,
158 };
159​
160 return (
161 <SidebarContext value={contextValue}>
162 <div
163 className={cn(
164 "group/sidebar-wrapper flex min-h-svh w-full",
165 SIDEBAR_TITLEBAR_CONTROL_SIZE,
166 className,
167 )}
168 data-sidebar-mobile={isMobile}
169 data-sidebar-state={state}
170 data-slot="sidebar-wrapper"
171 style={
172 {
173 "--sidebar-width": SIDEBAR_WIDTH,
174 "--sidebar-width-icon": SIDEBAR_WIDTH_ICON,
175 "--workspace-titlebar-content-left":
176 "calc(var(--workspace-controls-left) + var(--workspace-titlebar-control-size) + var(--workspace-titlebar-control-gap))",
177 ...style,
178 } as React.CSSProperties
179 }
180 {...props}
181 >
182 {children}
183 </div>
184 </SidebarContext>
185 );
187​
188function resolveSidebarResizable({
189 collapsible,
190 isMobile,
191 resizable,
192}: {
193 collapsible: "offcanvas" | "icon" | "none";
194 isMobile: boolean;
195 resizable: boolean | SidebarResizableOptions;
196}): 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 };
210​
211function Sidebar({
212 side = "left",
213 variant = "sidebar",
214 collapsible = "offcanvas",
215 resizable = false,
216 className,
217 children,
218 ...props
219}: React.ComponentProps<"div"> & {
220 side?: "left" | "right";
221 variant?: "sidebar" | "floating" | "inset";
222 collapsible?: "offcanvas" | "icon" | "none";
223 resizable?: boolean | SidebarResizableOptions;
224}) {
225 const { isMobile, state, openMobile, setOpenMobile, setMobileSheetElement } = useSidebar();
226 const desktopSidebarRef = React.useRef<HTMLDivElement>(null);
227 const offcanvasCollapsed = !isMobile && collapsible === "offcanvas" && state === "collapsed";
228​
229 React.useLayoutEffect(() => {
230 const sidebar = desktopSidebarRef.current;
231 if (!sidebar) return;
232​
233 // Transfer focus before inert causes the browser to clear the focused descendant.
234 if (offcanvasCollapsed && sidebar.contains(document.activeElement)) {
235 const wrapper = sidebar.closest('[data-slot="sidebar-wrapper"]');
236 const trigger = Array.from(
237 wrapper?.querySelectorAll<HTMLElement>('[data-sidebar="trigger"]') ?? [],
238 ).find((element) => !sidebar.contains(element));
239 trigger?.focus({ preventScroll: true });
240 }
241 sidebar.toggleAttribute("inert", offcanvasCollapsed);
242 }, [offcanvasCollapsed]);
243​
244 const resolvedResizable = resolveSidebarResizable({ collapsible, isMobile, resizable });
245 const instanceContextValue: SidebarInstanceContextProps = { side, resizable: resolvedResizable };
246​
247 if (collapsible === "none") {
248 return (
249 <SidebarInstanceContext value={instanceContextValue}>
250 <div
251 className={cn(
252 "flex h-full w-(--sidebar-width) flex-col bg-sidebar surface-grain text-sidebar-foreground",
253 className,
254 )}
255 data-slot="sidebar"
256 {...props}
257 >
258 {children}
259 </div>
260 </SidebarInstanceContext>
261 );
262 }
263​
264 if (isMobile) {
265 return (
266 <SidebarInstanceContext value={instanceContextValue}>
267 <Sheet onOpenChange={setOpenMobile} open={openMobile}>
268 <SheetPopup
269 ref={setMobileSheetElement}
270 className={cn(
271 "w-(--sidebar-width) max-w-none bg-sidebar surface-grain p-0 text-sidebar-foreground",
272 className,
273 )}
274 data-mobile="true"
275 data-sidebar="sidebar"
276 data-slot="sidebar"
277 showCloseButton={false}
278 side={side}
279 style={
280 {
281 "--sidebar-width": SIDEBAR_WIDTH_MOBILE,
282 } as React.CSSProperties
283 }
284 >
285 <SheetHeader className="sr-only">
286 <SheetTitle>Sidebar</SheetTitle>
287 <SheetDescription>Displays the mobile sidebar.</SheetDescription>
288 </SheetHeader>
289 <div
290 className={cn(
291 "flex h-full w-full flex-col pb-safe pt-safe",
292 SIDEBAR_TITLEBAR_CONTROL_SIZE,
293 side === "left" ? "pl-safe" : "pr-safe",
294 )}
295 {...props}
296 >
297 {children}
298 </div>
299 </SheetPopup>
300 </Sheet>
301 </SidebarInstanceContext>
302 );
303 }
304​
305 return (
306 <SidebarInstanceContext value={instanceContextValue}>
307 <div
308 ref={desktopSidebarRef}
309 className="group peer text-sidebar-foreground"
310 data-collapsible={state === "collapsed" ? collapsible : ""}
311 data-side={side}
312 data-slot="sidebar"
313 data-state={state}
314 data-variant={variant}
315 >
316 <div
317 className={cn(
318 "relative w-(--sidebar-width) bg-transparent",
319 "[[data-panel-animations=true]_&]:transition-[width] [[data-panel-animations=true]_&]:[transition-duration:var(--panel-animation-duration)] [[data-panel-animations=true]_&]:ease-out",
320 "group-data-[collapsible=offcanvas]:w-0",
321 "group-data-[side=right]:rotate-180",
322 variant === "floating" || variant === "inset"
323 ? "group-data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)+(--spacing(4)))]"
324 : "group-data-[collapsible=icon]:w-(--sidebar-width-icon)",
325 )}
326 data-slot="sidebar-gap"
327 />
328 <div
329 className={cn(
330 "fixed inset-y-0 z-10 flex h-svh w-(--sidebar-width)",
331 "[[data-panel-animations=true]_&]:transition-[left,right,width] [[data-panel-animations=true]_&]:[transition-duration:var(--panel-animation-duration)] [[data-panel-animations=true]_&]:ease-out",
332 side === "left"
333 ? "left-0 group-data-[collapsible=offcanvas]:left-[calc(var(--sidebar-width)*-1)]"
334 : "right-0 group-data-[collapsible=offcanvas]:right-[calc(var(--sidebar-width)*-1)]",
335 variant === "floating" || variant === "inset"
336 ? "p-2 group-data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)+(--spacing(4))+2px)]"
337 : "group-data-[collapsible=icon]:w-(--sidebar-width-icon) group-data-[side=left]:border-r group-data-[side=right]:border-l",
338 className,
339 )}
340 data-slot="sidebar-container"
341 {...props}
342 >
343 <div
344 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"
345 data-sidebar="sidebar"
346 data-slot="sidebar-inner"
347 >
348 {children}
349 </div>
350 </div>
351 </div>
352 </SidebarInstanceContext>
353 );
355​
356function SidebarTrigger({ className, onClick, ...props }: React.ComponentProps<typeof Button>) {
357 const { toggleSidebar } = useSidebar();
358 const isOpen = useSidebarVisibility();
359​
360 return (
361 <Button
362 className={cn(
363 "size-[var(--workspace-titlebar-control-size)]! [-webkit-app-region:no-drag]",
364 className,
365 )}
366 data-sidebar="trigger"
367 data-slot="sidebar-trigger"
368 aria-pressed={isOpen}
369 onClick={(event) => {
370 onClick?.(event);
371 toggleSidebar();
372 }}
373 size="icon"
374 variant="ghost"
375 {...props}
376 >
377 <MorphIcon className="size-4" icon={isOpen ? PanelLeftClose : PanelLeft} />
378 <span className="sr-only">Toggle Sidebar</span>
379 </Button>
380 );
382​
383function clampSidebarWidth(width: number, options: SidebarResolvedResizableOptions): number {
384 return Math.max(options.minWidth, Math.min(width, options.maxWidth));
386​
387function SidebarRail({
388 className,
389 onClick,
390 onPointerCancel,
391 onLostPointerCapture,
392 onPointerDown,
393 onPointerMove,
394 onPointerUp,
395 ...props
396}: React.ComponentProps<"button">) {
397 const { open, toggleSidebar } = useSidebar();
398 const sidebarInstance = React.use(SidebarInstanceContext);
399 const railRef = React.useRef<HTMLButtonElement | null>(null);
400 const suppressClickRef = React.useRef(false);
401 const resolvedResizable = sidebarInstance?.resizable ?? null;
402 const latestResizable = React.useRef(resolvedResizable);
403 const canResize = resolvedResizable !== null && open;
404 const railLabel = canResize ? "Resize Sidebar" : "Toggle Sidebar";
405 const railTitle = canResize ? "Drag to resize sidebar" : "Toggle Sidebar";
406 const resize = useResizeDrag<HTMLButtonElement>((event) => {
407 if (!resolvedResizable || !open) return null;
408 const rail = event.currentTarget;
409 const wrapper = rail.closest<HTMLElement>("[data-slot='sidebar-wrapper']");
410 const sidebarRoot = rail.closest<HTMLElement>("[data-slot='sidebar']");
411 const sidebarContainer = sidebarRoot?.querySelector<HTMLElement>(
412 "[data-slot='sidebar-container']",
413 );
414 if (!wrapper || !sidebarRoot || !sidebarContainer) return null;
415​
416 const side = sidebarInstance?.side ?? "left";
417 let width = clampSidebarWidth(
418 sidebarContainer.getBoundingClientRect().width,
419 resolvedResizable,
420 );
421 // Drag frames write width to the gap and container only. Changing the
422 // inherited --sidebar-width on the wrapper restyles the whole app, so it is
423 // written once when the drag commits.
424 const widthTargets = [
425 sidebarRoot.querySelector<HTMLElement>("[data-slot='sidebar-gap']"),
426 sidebarContainer,
427 ].filter((element): element is HTMLElement => element !== null);
428 const setTargetWidth = (value: number) => {
429 widthTargets.forEach((element) => {
430 element.style.setProperty("width", `${value}px`);
431 });
432 };
433 widthTargets.forEach((element) => {
434 element.style.setProperty("transition-duration", "0ms");
435 });
436 setTargetWidth(width);
437​
438 return {
439 width,
440 edge: side === "left" ? "right" : "left",
441 resize(value) {
442 const options = latestResizable.current;
443 if (!options) return width;
444 const nextWidth = clampSidebarWidth(value, options);
445 const accepted =
446 options.shouldAcceptWidth?.({
447 currentWidth: width,
448 nextWidth,
449 rail,
450 side,
451 sidebarRoot,
452 wrapper,
453 }) ?? true;
454 if (accepted) {
455 setTargetWidth(nextWidth);
456 width = nextWidth;
457 }
458 return width;
459 },
460 finish(finalWidth, moved) {
461 wrapper.style.setProperty("--sidebar-width", `${finalWidth}px`);
462 suppressClickRef.current = moved;
463 const options = latestResizable.current;
464 if (options?.storageKey) {
465 try {
466 setLocalStorageItem(options.storageKey, finalWidth, Schema.Finite);
467 } catch (error) {
468 console.error("Could not persist sidebar width.", error);
469 }
470 }
471 options?.onResize?.(finalWidth);
472 },
473 cleanup() {
474 widthTargets.forEach((element) => {
475 element.style.removeProperty("width");
476 element.style.removeProperty("transition-duration");
477 });
478 },
479 };
480 }, String(open));
481 const refreshResize = React.useEffectEvent(() => resize.refresh());
482 React.useLayoutEffect(() => {
483 latestResizable.current = resolvedResizable;
484 refreshResize();
485 }, [resolvedResizable]);
486​
487 const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
488 onClick?.(event);
489 if (event.defaultPrevented) return;
490 if (suppressClickRef.current) {
491 suppressClickRef.current = false;
492 event.preventDefault();
493 return;
494 }
495 if (resolvedResizable && open) {
496 event.preventDefault();
497 return;
498 }
499 toggleSidebar();
500 };
501​
502 React.useLayoutEffect(() => {
503 if (!resolvedResizable?.storageKey || typeof window === "undefined") return;
504 const rail = railRef.current;
505 if (!rail) return;
506 const wrapper = rail.closest<HTMLElement>("[data-slot='sidebar-wrapper']");
507 if (!wrapper) return;
508​
509 let storedWidth: number | null;
510 try {
511 storedWidth = getLocalStorageItem(resolvedResizable.storageKey, Schema.Finite);
512 } catch (error) {
513 console.error("Could not restore persisted sidebar width.", error);
514 return;
515 }
516 if (storedWidth === null) return;
517 const clampedWidth = clampSidebarWidth(storedWidth, resolvedResizable);
518 wrapper.style.setProperty("--sidebar-width", `${clampedWidth}px`);
519 resolvedResizable.onResize?.(clampedWidth);
520 }, [resolvedResizable]);
521​
522 return (
523 <Tooltip>
524 <TooltipTrigger
525 render={
526 <button
527 aria-label={railLabel}
528 className={cn(
529 /* 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. */
530 "-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",
531 "[[data-panel-animations=true]_&]:transition-all [[data-panel-animations=true]_&]:[transition-duration:var(--panel-animation-duration)] [[data-panel-animations=true]_&]:ease-out",
532 "in-data-[side=left]:cursor-w-resize in-data-[side=right]:cursor-e-resize",
533 "[[data-side=left][data-state=collapsed]_&]:cursor-e-resize [[data-side=right][data-state=collapsed]_&]:cursor-w-resize",
534 "group-data-[collapsible=offcanvas]:translate-x-0 hover:group-data-[collapsible=offcanvas]:bg-sidebar group-data-[collapsible=offcanvas]:after:left-full",
535 "[[data-side=left][data-collapsible=offcanvas]_&]:-right-2",
536 "[[data-side=right][data-collapsible=offcanvas]_&]:-left-2",
537 className,
538 )}
539 data-sidebar="rail"
540 data-slot="sidebar-rail"
541 onClick={handleClick}
542 onLostPointerCapture={(event) => {
543 onLostPointerCapture?.(event);
544 if (!event.defaultPrevented) resize.onLostPointerCapture(event);
545 }}
546 onPointerCancel={(event) => {
547 onPointerCancel?.(event);
548 if (!event.defaultPrevented) resize.onPointerCancel(event);
549 }}
550 onPointerDown={(event) => {
551 onPointerDown?.(event);
552 if (!event.defaultPrevented) resize.onPointerDown(event);
553 }}
554 onPointerMove={(event) => {
555 onPointerMove?.(event);
556 if (!event.defaultPrevented) resize.onPointerMove(event);
557 }}
558 onPointerUp={(event) => {
559 onPointerUp?.(event);
560 if (!event.defaultPrevented) resize.onPointerUp(event);
561 }}
562 ref={railRef}
563 tabIndex={-1}
564 type="button"
565 {...props}
566 />
567 }
568 />
569 <TooltipPopup side="right">{railTitle}</TooltipPopup>
570 </Tooltip>
571 );
573​
574function SidebarInput({ className, ...props }: Omit<InputProps, "unstyled" | "variant">) {
575 return (
576 <Input
577 className={cn(
578 "[&_[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",
579 className,
580 )}
581 unstyled
582 {...props}
583 />
584 );
586​
587function SidebarInset({ className, ...props }: React.ComponentProps<"main">) {
588 return (
589 <main
590 className={cn(
591 "relative flex min-w-0 w-full flex-1 flex-col bg-background surface-grain",
592 "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",
593 className,
594 )}
595 data-slot="sidebar-inset"
596 {...props}
597 />
598 );
600​
601function SidebarHeader({ className, ...props }: React.ComponentProps<"div">) {
602 return (
603 <div
604 className={cn("flex flex-col gap-2 p-2", className)}
605 data-sidebar="header"
606 data-slot="sidebar-header"
607 {...props}
608 />
609 );
611​
612function SidebarFooter({ className, ...props }: React.ComponentProps<"div">) {
613 return (
614 <div
615 className={cn("flex flex-col gap-2 px-[var(--sidebar-content-inset)] py-1", className)}
616 data-sidebar="footer"
617 data-slot="sidebar-footer"
618 {...props}
619 />
620 );
622​
623function SidebarContent({
624 className,
625 fixedHeader,
626 ...props
627}: React.ComponentProps<"div"> & {
628 fixedHeader?: React.ReactNode;
629}) {
630 return (
631 <>
632 {fixedHeader ? <div className="w-full shrink-0">{fixedHeader}</div> : null}
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 className={cn(
645 "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",
646 fixedHeader && "[&>[data-sidebar=group]:first-child]:pt-0",
647 className,
648 )}
649 data-sidebar="content"
650 data-slot="sidebar-content"
651 {...props}
652 />
653 </ScrollArea>
654 </>
655 );
657​
658function SidebarGroup({ className, ...props }: React.ComponentProps<"div">) {
659 return (
660 <div
661 className={cn(
662 "relative flex w-full min-w-0 flex-col p-[var(--sidebar-content-inset)]",
663 className,
664 )}
665 data-sidebar="group"
666 data-slot="sidebar-group"
667 {...props}
668 />
669 );
671​
672function SidebarMenu({ className, ...props }: React.ComponentProps<"ul">) {
673 return (
674 <ul
675 className={cn("flex w-full min-w-0 flex-col gap-1", className)}
676 data-sidebar="menu"
677 data-slot="sidebar-menu"
678 {...props}
679 />
680 );
682​
683function SidebarMenuItem({ className, ...props }: React.ComponentProps<"li">) {
684 return (
685 <li
686 className={cn("group/menu-item relative", className)}
687 data-sidebar="menu-item"
688 data-slot="sidebar-menu-item"
689 {...props}
690 />
691 );
693​
694const sidebarMenuButtonVariants = cva(
695 "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",
696 {
697 defaultVariants: {
698 size: "default",
699 variant: "default",
700 },
701 variants: {
702 size: {
703 default:
704 "h-8 rounded-[var(--control-radius)] px-[var(--sidebar-row-content-inset)] py-1.5 text-sm",
705 icon: "size-8 justify-center rounded-[var(--control-radius)] p-0",
706 lg: "h-12 rounded-lg p-2 text-sm group-data-[collapsible=icon]:p-0!",
707 sm: "h-7 rounded-lg p-2 text-xs",
708 },
709 variant: {
710 default: "font-medium text-sidebar-muted-foreground/80",
711 outline: "bg-sidebar-control-surface ring-1 ring-sidebar-border",
712 },
713 },
714 },
715);
716​
717function SidebarMenuButton({
718 isActive = false,
719 variant = "default",
720 size = "default",
721 tooltip,
722 className,
723 render,
724 ...props
725}: useRender.ComponentProps<"button"> & {
726 isActive?: boolean;
727 tooltip?: string | React.ComponentProps<typeof TooltipPopup>;
728} & VariantProps<typeof sidebarMenuButtonVariants>) {
729 const { isMobile, state } = useSidebar();
730​
731 const defaultProps = {
732 className: cn(sidebarMenuButtonVariants({ size, variant }), className),
733 "data-active": isActive,
734 "data-sidebar": "menu-button",
735 "data-size": size,
736 "data-slot": "sidebar-menu-button",
737 };
738​
739 const buttonProps = mergeProps<"button">(defaultProps, props);
740​
741 const buttonElement = useRender({
742 defaultTagName: "button",
743 props: buttonProps,
744 render,
745 });
746​
747 if (!tooltip) {
748 return buttonElement;
749 }
750​
751 if (typeof tooltip === "string") {
752 tooltip = {
753 children: tooltip,
754 };
755 }
756​
757 return (
758 <Tooltip>
759 <TooltipTrigger render={buttonElement as React.ReactElement<Record<string, unknown>>} />
760 <TooltipPopup
761 align="center"
762 hidden={state !== "collapsed" || isMobile}
763 side="right"
764 {...tooltip}
765 />
766 </Tooltip>
767 );
769​
770function SidebarMenuSub({ className, ...props }: React.ComponentProps<"ul">) {
771 return (
772 <ul
773 className={cn(
774 "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",
775 "group-data-[collapsible=icon]:hidden",
776 className,
777 )}
778 data-sidebar="menu-sub"
779 data-slot="sidebar-menu-sub"
780 {...props}
781 />
782 );
784​
785function SidebarMenuSubItem({ className, ...props }: React.ComponentProps<"li">) {
786 return (
787 <li
788 className={cn("group/menu-sub-item relative", className)}
789 data-sidebar="menu-sub-item"
790 data-slot="sidebar-menu-sub-item"
791 {...props}
792 />
793 );
795​
796function SidebarMenuSubButton({
797 size = "md",
798 isActive = false,
799 className,
800 render,
801 ...props
802}: useRender.ComponentProps<"a"> & {
803 size?: "sm" | "md";
804 isActive?: boolean;
805}) {
806 const defaultProps = {
807 className: cn(
808 "-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",
809 "data-[active=true]:bg-sidebar-row-selected data-[active=true]:text-sidebar-foreground",
810 size === "sm" && "text-xs",
811 size === "md" && "text-sm",
812 "group-data-[collapsible=icon]:hidden",
813 className,
814 ),
815 "data-active": isActive,
816 "data-sidebar": "menu-sub-button",
817 "data-size": size,
818 "data-slot": "sidebar-menu-sub-button",
819 };
820​
821 return useRender({
822 defaultTagName: "a",
823 props: mergeProps<"a">(defaultProps, props),
824 render,
825 });
827​
828export {
829 SidebarInput,
830 Sidebar,
831 SidebarContent,
832 SidebarFooter,
833 SidebarGroup,
834 SidebarHeader,
835 SidebarInset,
836 SidebarMenu,
837 SidebarMenuButton,
838 SidebarMenuItem,
839 SidebarMenuSub,
840 SidebarMenuSubButton,
841 SidebarMenuSubItem,
842 SidebarProvider,
843 SidebarRail,
844 SidebarTrigger,
845 useSidebar,
846 useSidebarVisibility,
847};