Repository navigation
Expand file tree
/
Copy pathsidebar.tsx
More file actions
864 lines (809 loc) · 28.7 KB
/
Copy pathsidebar.tsx
File metadata and controls
864 lines (809 loc) · 28.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
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)] py-1", 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,
};