import { mergeProps } from "@base-ui/react/merge-props";
import { useRender } from "@base-ui/react/use-render";
import { cva, type VariantProps } from "class-variance-authority";
import { PanelLeft, PanelLeftClose } from "lucide";
import * as React from "react";
import { cn } from "~/lib/utils";
import { Button } from "~/components/ui/button";
import { Input, type InputProps } from "~/components/ui/input";
import { MorphIcon } from "~/components/MorphIcon";
import { ScrollArea } from "~/components/ui/scroll-area";
import {
Sheet,
SheetDescription,
SheetHeader,
SheetPopup,
SheetTitle,
} from "~/components/ui/sheet";
import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
import { useResizeDrag } from "~/hooks/useResizeDrag";
import { useMediaQuery } from "~/hooks/useMediaQuery";
import { setLocalStorageItem } from "~/hooks/useLocalStorage";
import { resolveSidebarState, type ResponsiveSidebarState } from "./sidebarState";
import * as Schema from "effect/Schema";
import { announceSidebarVisibility } from "~/lib/accessibilityAnnouncements";
const SIDEBAR_COOKIE_NAME = "sidebar_state";
const SIDEBAR_COOKIE_MAX_AGE = 60 * 60 * 24 * 7;
const SIDEBAR_WIDTH = "16rem";
const SIDEBAR_WIDTH_MOBILE = "calc(100vw - var(--spacing(3)))";
const SIDEBAR_WIDTH_ICON = "3rem";
const SIDEBAR_RESIZE_DEFAULT_MIN_WIDTH = 16 * 16;
const SIDEBAR_TITLEBAR_CONTROL_SIZE = "max-sm:[--workspace-titlebar-control-size:--spacing(8)]";
type SidebarContextProps = {
state: ResponsiveSidebarState;
open: boolean;
setOpen: (open: boolean) => void;
openMobile: boolean;
mobileSheetMounted: boolean;
setMobileSheetElement: (element: HTMLDivElement | null) => void;
setOpenMobile: (open: boolean) => void;
isMobile: boolean;
toggleSidebar: () => void;
/** Runs before the desktop sidebar collapses. */
onCollapse: (listener: () => void) => () => void;
};
type SidebarResizableOptions = {
maxWidth?: number;
minWidth?: number;
onResize?: (width: number) => void;
shouldAcceptWidth?: (context: {
currentWidth: number;
nextWidth: number;
rail: HTMLButtonElement;
side: "left" | "right";
sidebarRoot: HTMLElement;
wrapper: HTMLElement;
}) => boolean;
storageKey?: string;
};
type SidebarResolvedResizableOptions = {
maxWidth: number;
minWidth: number;
onResize?: (width: number) => void;
shouldAcceptWidth?: (context: {
currentWidth: number;
nextWidth: number;
rail: HTMLButtonElement;
side: "left" | "right";
sidebarRoot: HTMLElement;
wrapper: HTMLElement;
}) => boolean;
storageKey: string | null;
};
type SidebarInstanceContextProps = {
resizable: SidebarResolvedResizableOptions | null;
side: "left" | "right";
};
const SidebarContext = React.createContext<SidebarContextProps | null>(null);
const SidebarInstanceContext = React.createContext<SidebarInstanceContextProps | null>(null);
function useSidebar() {
const context = React.use(SidebarContext);
if (!context) {
throw new Error("useSidebar must be used within a SidebarProvider.");
}
return context;
}
function useSidebarVisibility() {
const { isMobile, open, openMobile } = useSidebar();
return isMobile ? openMobile : open;
}
// The `max-md` breakpoint, also read when a shortcut fires.
const SIDEBAR_MOBILE_QUERY = "(max-width: 767px)";
function isMobileViewport(): boolean {
return typeof window !== "undefined" && window.matchMedia(SIDEBAR_MOBILE_QUERY).matches;
}
async function persistSidebarOpen(open: boolean) {
await cookieStore.set({
expires: Date.now() + SIDEBAR_COOKIE_MAX_AGE * 1000,
name: SIDEBAR_COOKIE_NAME,
path: "/",
value: String(open),
});
}
// An offcanvas sidebar turns inert once collapsed, which drops focus to the body.
function moveFocusOutOfCollapsingSidebar() {
const sidebar = document.activeElement?.closest<HTMLElement>(
'[data-slot="sidebar"][data-collapsible-mode="offcanvas"]',
);
const wrapper = sidebar?.closest<HTMLElement>('[data-slot="sidebar-wrapper"]');
if (!sidebar || !wrapper) return;
const trigger = Array.from(
wrapper.querySelectorAll<HTMLElement>('[data-sidebar="trigger"]'),
).find((element) => !sidebar.contains(element));
trigger?.focus({ preventScroll: true });
}
interface SidebarOpenState {
readonly open: boolean;
readonly openMobile: boolean;
}
/** Per-provider open state, kept outside React so shortcuts can toggle it at event time. */
function createSidebarOpenStore(defaultOpen: boolean) {
let state: SidebarOpenState = { open: defaultOpen, openMobile: false };
const listeners = new Set<() => void>();
const collapseListeners = new Set<() => void>();
const publish = (next: SidebarOpenState) => {
state = next;
for (const listener of listeners) listener();
};
const setOpen = (open: boolean) => {
if (!open && state.open) {
for (const listener of collapseListeners) listener();
moveFocusOutOfCollapsingSidebar();
}
if (open !== state.open) {
if (!isMobileViewport()) announceSidebarVisibility(open);
publish({ ...state, open });
}
void persistSidebarOpen(open);
};
const setOpenMobile = (openMobile: boolean) => {
if (openMobile !== state.openMobile) publish({ ...state, openMobile });
};
return {
subscribe: (listener: () => void) => {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
},
getSnapshot: () => state,
setOpen,
setOpenMobile,
toggle: () => (isMobileViewport() ? setOpenMobile(!state.openMobile) : setOpen(!state.open)),
onCollapse: (listener: () => void) => {
collapseListeners.add(listener);
return () => {
collapseListeners.delete(listener);
};
},
};
}
function SidebarProvider({
defaultOpen = true,
className,
style,
children,
...props
}: React.ComponentProps<"div"> & {
defaultOpen?: boolean;
}) {
const isMobile = useMediaQuery(SIDEBAR_MOBILE_QUERY);
const [store] = React.useState(() => createSidebarOpenStore(defaultOpen));
const { open, openMobile } = React.useSyncExternalStore(
store.subscribe,
store.getSnapshot,
store.getSnapshot,
);
const [mobileSheetMounted, setMobileSheetMounted] = React.useState(false);
const setMobileSheetElement = (element: HTMLDivElement | null) => {
setMobileSheetMounted(element !== null);
};
const state = resolveSidebarState({ isMobile, open, openMobile });
const contextValue: SidebarContextProps = {
isMobile,
open,
openMobile,
mobileSheetMounted,
setMobileSheetElement,
setOpen: store.setOpen,
setOpenMobile: store.setOpenMobile,
state,
toggleSidebar: store.toggle,
onCollapse: store.onCollapse,
};
return (
<SidebarContext value={contextValue}>
<div
className={cn(
"group/sidebar-wrapper flex min-h-svh w-full",
SIDEBAR_TITLEBAR_CONTROL_SIZE,
className,
)}
data-sidebar-state={state}
data-slot="sidebar-wrapper"
style={
{
"--sidebar-width": SIDEBAR_WIDTH,
"--sidebar-width-icon": SIDEBAR_WIDTH_ICON,
"--workspace-titlebar-content-left":
"calc(var(--workspace-controls-left) + var(--workspace-titlebar-control-size) + var(--workspace-titlebar-control-gap))",
...style,
} as React.CSSProperties
}
{...props}
>
{children}
</div>
</SidebarContext>
);
}
function resolveSidebarResizable({
collapsible,
isMobile,
resizable,
}: {
collapsible: "offcanvas" | "icon" | "none";
isMobile: boolean;
resizable: boolean | SidebarResizableOptions;
}): SidebarResolvedResizableOptions | null {
if (isMobile || collapsible === "none" || !resizable) {
return null;
}
const options = typeof resizable === "boolean" ? {} : resizable;
return {
maxWidth: options.maxWidth ?? Number.POSITIVE_INFINITY,
minWidth: options.minWidth ?? SIDEBAR_RESIZE_DEFAULT_MIN_WIDTH,
storageKey: options.storageKey ?? null,
...(options.onResize ? { onResize: options.onResize } : {}),
...(options.shouldAcceptWidth ? { shouldAcceptWidth: options.shouldAcceptWidth } : {}),
};
}
function Sidebar({
side = "left",
variant = "sidebar",
collapsible = "offcanvas",
resizable = false,
className,
children,
...props
}: React.ComponentProps<"div"> & {
side?: "left" | "right";
variant?: "sidebar" | "floating" | "inset";
collapsible?: "offcanvas" | "icon" | "none";
resizable?: boolean | SidebarResizableOptions;
}) {
const { isMobile, state, openMobile, setOpenMobile, setMobileSheetElement } = useSidebar();
const offcanvasCollapsed = !isMobile && collapsible === "offcanvas" && state === "collapsed";
const resolvedResizable = resolveSidebarResizable({ collapsible, isMobile, resizable });
const instanceContextValue: SidebarInstanceContextProps = { side, resizable: resolvedResizable };
if (collapsible === "none") {
return (
<SidebarInstanceContext value={instanceContextValue}>
<div
className={cn(
"flex h-full w-(--sidebar-width) flex-col bg-sidebar surface-grain text-sidebar-foreground",
className,
)}
data-slot="sidebar"
{...props}
>
{children}
</div>
</SidebarInstanceContext>
);
}
if (isMobile) {
return (
<SidebarInstanceContext value={instanceContextValue}>
<Sheet onOpenChange={setOpenMobile} open={openMobile}>
<SheetPopup
ref={setMobileSheetElement}
className={cn(
"w-(--sidebar-width) max-w-none bg-sidebar surface-grain p-0 text-sidebar-foreground",
className,
)}
data-mobile="true"
data-sidebar="sidebar"
data-slot="sidebar"
showCloseButton={false}
side={side}
style={
{
"--sidebar-width": SIDEBAR_WIDTH_MOBILE,
} as React.CSSProperties
}
>
<SheetHeader className="sr-only">
<SheetTitle>{props["aria-label"] ?? "Sidebar"}</SheetTitle>
<SheetDescription>Displays the mobile sidebar.</SheetDescription>
</SheetHeader>
<div
className={cn(
"flex h-full w-full flex-col pb-safe pt-safe",
SIDEBAR_TITLEBAR_CONTROL_SIZE,
side === "left" ? "pl-safe" : "pr-safe",
)}
{...props}
>
{children}
</div>
</SheetPopup>
</Sheet>
</SidebarInstanceContext>
);
}
return (
<SidebarInstanceContext value={instanceContextValue}>
<div
className="group peer hidden text-sidebar-foreground md:block"
data-collapsible={state === "collapsed" ? collapsible : ""}
data-collapsible-mode={collapsible}
inert={offcanvasCollapsed}
data-side={side}
data-slot="sidebar"
data-state={state}
data-variant={variant}
>
<div
className={cn(
"relative w-(--sidebar-width) bg-transparent",
"[[data-panel-animations=true]_&]:transition-[width] [[data-panel-animations=true]_&]:[transition-duration:var(--panel-animation-duration)] [[data-panel-animations=true]_&]:ease-out",
"group-data-[collapsible=offcanvas]:w-0",
"group-data-[side=right]:rotate-180",
variant === "floating" || variant === "inset"
? "group-data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)+(--spacing(4)))]"
: "group-data-[collapsible=icon]:w-(--sidebar-width-icon)",
)}
data-slot="sidebar-gap"
/>
<div
className={cn(
"fixed inset-y-0 z-10 hidden h-svh w-(--sidebar-width) md:flex",
"[[data-panel-animations=true]_&]:transition-[left,right,width] [[data-panel-animations=true]_&]:[transition-duration:var(--panel-animation-duration)] [[data-panel-animations=true]_&]:ease-out",
side === "left"
? "left-0 group-data-[collapsible=offcanvas]:left-[calc(var(--sidebar-width)*-1)]"
: "right-0 group-data-[collapsible=offcanvas]:right-[calc(var(--sidebar-width)*-1)]",
variant === "floating" || variant === "inset"
? "p-2 group-data-[collapsible=icon]:w-[calc(var(--sidebar-width-icon)+(--spacing(4))+2px)]"
: "group-data-[collapsible=icon]:w-(--sidebar-width-icon) group-data-[side=left]:border-r group-data-[side=right]:border-l",
className,
)}
data-slot="sidebar-container"
{...props}
>
<div
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"
data-sidebar="sidebar"
data-slot="sidebar-inner"
>
{children}
</div>
</div>
</div>
</SidebarInstanceContext>
);
}
function SidebarTrigger({ className, onClick, ...props }: React.ComponentProps<typeof Button>) {
const { toggleSidebar } = useSidebar();
const isOpen = useSidebarVisibility();
return (
<Button
className={cn(
"size-[var(--workspace-titlebar-control-size)]! [-webkit-app-region:no-drag]",
className,
)}
data-sidebar="trigger"
data-slot="sidebar-trigger"
aria-pressed={isOpen}
onClick={(event) => {
onClick?.(event);
toggleSidebar();
}}
size="icon"
variant="ghost"
{...props}
>
<MorphIcon className="size-4" icon={isOpen ? PanelLeftClose : PanelLeft} />
<span className="sr-only">Toggle Sidebar</span>
</Button>
);
}
function clampSidebarWidth(width: number, options: SidebarResolvedResizableOptions): number {
return Math.max(options.minWidth, Math.min(width, options.maxWidth));
}
function SidebarRail({
className,
onClick,
onPointerCancel,
onLostPointerCapture,
onPointerDown,
onPointerMove,
onPointerUp,
...props
}: React.ComponentProps<"button">) {
const { open, toggleSidebar, onCollapse } = useSidebar();
const sidebarInstance = React.use(SidebarInstanceContext);
const suppressClickRef = React.useRef(false);
const resolvedResizable = sidebarInstance?.resizable ?? null;
const canResize = resolvedResizable !== null && open;
const railLabel = canResize ? "Resize Sidebar" : "Toggle Sidebar";
const railTitle = canResize ? "Drag to resize sidebar" : "Toggle Sidebar";
const resize = useResizeDrag<HTMLButtonElement>((event) => {
const options = resolvedResizable;
if (!options || !open) return null;
const rail = event.currentTarget;
const wrapper = rail.closest<HTMLElement>("[data-slot='sidebar-wrapper']");
const sidebarRoot = rail.closest<HTMLElement>("[data-slot='sidebar']");
const sidebarContainer = sidebarRoot?.querySelector<HTMLElement>(
"[data-slot='sidebar-container']",
);
if (!wrapper || !sidebarRoot || !sidebarContainer) return null;
const side = sidebarInstance?.side ?? "left";
let width = clampSidebarWidth(sidebarContainer.getBoundingClientRect().width, options);
// Write width only to gap/container during drag; inherited --sidebar-width restyles the whole app, so
// update it at commit.
const widthTargets = [
sidebarRoot.querySelector<HTMLElement>("[data-slot='sidebar-gap']"),
sidebarContainer,
].filter((element): element is HTMLElement => element !== null);
const setTargetWidth = (value: number) => {
widthTargets.forEach((element) => {
element.style.setProperty("width", `${value}px`);
});
};
widthTargets.forEach((element) => {
element.style.setProperty("transition-duration", "0ms");
});
setTargetWidth(width);
return {
width,
edge: side === "left" ? "right" : "left",
bounds: { min: options.minWidth, max: options.maxWidth },
resize(nextWidth) {
const accepted =
options.shouldAcceptWidth?.({
currentWidth: width,
nextWidth,
rail,
side,
sidebarRoot,
wrapper,
}) ?? true;
if (accepted) {
setTargetWidth(nextWidth);
width = nextWidth;
}
return width;
},
finish(finalWidth, moved) {
wrapper.style.setProperty("--sidebar-width", `${finalWidth}px`);
suppressClickRef.current = moved;
if (options.storageKey) {
try {
setLocalStorageItem(options.storageKey, finalWidth, Schema.Finite);
} catch (error) {
console.error("Could not persist sidebar width.", error);
}
}
options.onResize?.(finalWidth);
},
cleanup() {
widthTargets.forEach((element) => {
element.style.removeProperty("width");
element.style.removeProperty("transition-duration");
});
},
};
});
// Collapsing mid-drag abandons the drag instead of committing a width to a hidden sidebar.
// Created once: a new ref callback would run the old one's cleanup, which ends a live drag.
const [railRef] = React.useState(() => {
const { ref: dragRef, cancel } = resize;
return (node: HTMLButtonElement | null) => {
const releaseDrag = dragRef(node);
if (!releaseDrag) return undefined;
const stopCancelOnCollapse = onCollapse(cancel);
return () => {
stopCancelOnCollapse();
releaseDrag();
};
};
});
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
onClick?.(event);
if (event.defaultPrevented) return;
if (suppressClickRef.current) {
suppressClickRef.current = false;
event.preventDefault();
return;
}
if (resolvedResizable && open) {
event.preventDefault();
return;
}
toggleSidebar();
};
return (
<Tooltip>
<TooltipTrigger
render={
<button
aria-label={railLabel}
className={cn(
/* A collapsed offcanvas rail overlaps the native scrollbar on Windows and Linux. */
"-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",
"[[data-panel-animations=true]_&]:transition-all [[data-panel-animations=true]_&]:[transition-duration:var(--panel-animation-duration)] [[data-panel-animations=true]_&]:ease-out",
"in-data-[side=left]:cursor-w-resize in-data-[side=right]:cursor-e-resize",
"[[data-side=left][data-state=collapsed]_&]:cursor-e-resize [[data-side=right][data-state=collapsed]_&]:cursor-w-resize",
"group-data-[collapsible=offcanvas]:translate-x-0 hover:group-data-[collapsible=offcanvas]:bg-sidebar group-data-[collapsible=offcanvas]:after:left-full",
"[[data-side=left][data-collapsible=offcanvas]_&]:-right-2",
"[[data-side=right][data-collapsible=offcanvas]_&]:-left-2",
className,
)}
data-sidebar="rail"
data-slot="sidebar-rail"
onClick={handleClick}
onLostPointerCapture={(event) => {
onLostPointerCapture?.(event);
if (!event.defaultPrevented) resize.onLostPointerCapture(event);
}}
onPointerCancel={(event) => {
onPointerCancel?.(event);
if (!event.defaultPrevented) resize.onPointerCancel(event);
}}
onPointerDown={(event) => {
onPointerDown?.(event);
if (!event.defaultPrevented) resize.onPointerDown(event);
}}
onPointerMove={(event) => {
onPointerMove?.(event);
if (!event.defaultPrevented) resize.onPointerMove(event);
}}
onPointerUp={(event) => {
onPointerUp?.(event);
if (!event.defaultPrevented) resize.onPointerUp(event);
}}
ref={railRef}
tabIndex={-1}
type="button"
{...props}
/>
}
/>
<TooltipPopup side="right">{railTitle}</TooltipPopup>
</Tooltip>
);
}
function SidebarInput({ className, ...props }: Omit<InputProps, "unstyled" | "variant">) {
return (
<Input
className={cn(
"[&_[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",
className,
)}
unstyled
{...props}
/>
);
}
function SidebarInset({ className, ...props }: React.ComponentProps<"main">) {
return (
<main
className={cn(
"relative flex min-w-0 w-full flex-1 flex-col bg-background surface-grain",
"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",
className,
)}
data-slot="sidebar-inset"
{...props}
/>
);
}
function SidebarHeader({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
className={cn("flex flex-col gap-2 p-2", className)}
data-sidebar="header"
data-slot="sidebar-header"
{...props}
/>
);
}
function SidebarFooter({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
className={cn(
"flex flex-col gap-2 px-[var(--sidebar-content-inset)] pt-1 pb-[var(--sidebar-content-inset)]",
className,
)}
data-sidebar="footer"
data-slot="sidebar-footer"
{...props}
/>
);
}
function SidebarContent({
className,
fixedHeader,
...props
}: React.ComponentProps<"div"> & {
fixedHeader?: React.ReactNode;
}) {
return (
<>
{fixedHeader ? <div className="w-full shrink-0">{fixedHeader}</div> : null}
<ScrollArea
hideScrollbars
scrollFade
scrollFadePadding={false}
// Rows provide keyboard access; exclude the viewport from tab order to flatten VoiceOver's WebKit
// interaction boundary.
viewportTabIndex={-1}
className="h-auto min-h-0 flex-1 [&>[data-slot=scroll-area-viewport]]:[--fade-size:0.75rem]"
>
<div
className={cn(
"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",
fixedHeader && "[&>[data-sidebar=group]:first-child]:pt-0",
className,
)}
data-sidebar="content"
data-slot="sidebar-content"
{...props}
/>
</ScrollArea>
</>
);
}
function SidebarGroup({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
className={cn(
"relative flex w-full min-w-0 flex-col p-[var(--sidebar-content-inset)]",
className,
)}
data-sidebar="group"
data-slot="sidebar-group"
{...props}
/>
);
}
function SidebarMenu({ className, ...props }: React.ComponentProps<"ul">) {
return (
<ul
className={cn("flex w-full min-w-0 flex-col gap-1", className)}
data-sidebar="menu"
data-slot="sidebar-menu"
{...props}
/>
);
}
function SidebarMenuItem({ className, ...props }: React.ComponentProps<"li">) {
return (
<li
className={cn("group/menu-item relative", className)}
data-sidebar="menu-item"
data-slot="sidebar-menu-item"
{...props}
/>
);
}
const sidebarMenuButtonVariants = cva(
"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",
{
defaultVariants: {
size: "default",
variant: "default",
},
variants: {
size: {
default:
"h-8 rounded-[var(--control-radius)] px-[var(--sidebar-row-content-inset)] py-1.5 text-sm",
icon: "size-8 justify-center rounded-[var(--control-radius)] p-0",
lg: "h-12 rounded-[var(--control-radius)] px-[var(--sidebar-row-content-inset)] py-2 text-sm group-data-[collapsible=icon]:p-0!",
sm: "h-7 rounded-[var(--control-radius)] px-[var(--sidebar-row-content-inset)] py-2 text-xs",
},
variant: {
default: "font-medium text-sidebar-muted-foreground/80",
outline: "bg-sidebar-control-surface ring-1 ring-sidebar-border",
},
},
},
);
function SidebarMenuButton({
isActive = false,
variant = "default",
size = "default",
tooltip,
className,
render,
...props
}: useRender.ComponentProps<"button"> & {
isActive?: boolean;
tooltip?: string | React.ComponentProps<typeof TooltipPopup>;
} & VariantProps<typeof sidebarMenuButtonVariants>) {
const { isMobile, state } = useSidebar();
const defaultProps = {
className: cn(sidebarMenuButtonVariants({ size, variant }), className),
"data-active": isActive,
"data-sidebar": "menu-button",
"data-size": size,
"data-slot": "sidebar-menu-button",
};
const buttonProps = mergeProps<"button">(defaultProps, props);
const buttonElement = useRender({
defaultTagName: "button",
props: buttonProps,
render,
});
if (!tooltip) {
return buttonElement;
}
if (typeof tooltip === "string") {
tooltip = {
children: tooltip,
};
}
return (
<Tooltip>
<TooltipTrigger render={buttonElement as React.ReactElement<Record<string, unknown>>} />
<TooltipPopup
align="center"
hidden={state !== "collapsed" || isMobile}
side="right"
{...tooltip}
/>
</Tooltip>
);
}
function SidebarMenuSub({ className, ...props }: React.ComponentProps<"ul">) {
return (
<ul
className={cn(
"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",
"group-data-[collapsible=icon]:hidden",
className,
)}
data-sidebar="menu-sub"
data-slot="sidebar-menu-sub"
{...props}
/>
);
}
function SidebarMenuSubItem({ className, ...props }: React.ComponentProps<"li">) {
return (
<li
className={cn("group/menu-sub-item relative", className)}
data-sidebar="menu-sub-item"
data-slot="sidebar-menu-sub-item"
{...props}
/>
);
}
function SidebarMenuSubButton({
size = "md",
isActive = false,
className,
render,
...props
}: useRender.ComponentProps<"a"> & {
size?: "sm" | "md";
isActive?: boolean;
}) {
const defaultProps = {
className: cn(
"-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",
"data-[active=true]:bg-sidebar-row-selected data-[active=true]:text-sidebar-foreground",
size === "sm" && "text-xs",
size === "md" && "text-sm",
"group-data-[collapsible=icon]:hidden",
className,
),
"data-active": isActive,
"data-sidebar": "menu-sub-button",
"data-size": size,
"data-slot": "sidebar-menu-sub-button",
};
return useRender({
defaultTagName: "a",
props: mergeProps<"a">(defaultProps, props),
render,
});
}
export {
SidebarInput,
Sidebar,
SidebarContent,
SidebarFooter,
SidebarGroup,
SidebarHeader,
SidebarInset,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
SidebarMenuSub,
SidebarMenuSubButton,
SidebarMenuSubItem,
SidebarProvider,
SidebarRail,
SidebarTrigger,
useSidebar,
useSidebarVisibility,
};