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

sidebar.tsx

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