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

sidebar.tsx

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