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

sidebar.tsx

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