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

sidebar.tsx

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