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