Open History to see the last commit
1import { useEffect, useRef, useState } from "react";
2
3import { cn } from "~/lib/utils";
4
5export function CompactSidebarPreview({
6 railEnabled,
7 compactRows,
8}: {
9 railEnabled: boolean;
10 compactRows: boolean;
11}) {
12 const [collapsed, setCollapsed] = useState(false);
13 const sidebarRef = useRef<HTMLSpanElement>(null);
14
15 useEffect(() => {
16 if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
17 const animation = sidebarRef.current?.animate(
18 [
19 { width: "36px", offset: 0 },
20 { width: railEnabled ? "12px" : "0px", offset: 0.45 },
21 { width: railEnabled ? "12px" : "0px", offset: 0.6 },
22 { width: "36px", offset: 1 },
23 ],
24 { duration: 800, easing: "ease-in-out" },
25 );
26 return () => animation?.cancel();
27 }, [railEnabled, compactRows]);
28
29 return (
30 <button
31 type="button"
32 aria-label="Toggle compact sidebar preview"
33 aria-pressed={collapsed}
34 className="group/compact-preview relative flex h-10 w-full cursor-pointer overflow-hidden rounded-lg border border-border bg-background p-1 shadow-xs/5 outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background"
35 onClick={() => {
36 sidebarRef.current?.getAnimations().forEach((animation) => animation.cancel());
37 setCollapsed((value) => !value);
38 }}
39 >
40 <span
41 ref={sidebarRef}
42 aria-hidden
43 className={cn(
44 "flex h-full shrink-0 flex-col justify-center gap-1 overflow-hidden rounded-md bg-sidebar transition-[width] duration-250 ease-out motion-reduce:transition-none",
45 collapsed ? (railEnabled ? "w-3" : "w-0") : "w-9",
46 )}
47 >
48 {(compactRows ? [0, 1, 2, 3] : [0, 1, 2]).map((row) => (
49 <span key={row} className="flex shrink-0 items-start gap-1 px-1">
50 <span className="size-1 shrink-0 rounded-[1px] bg-muted-foreground/50" />
51 <span
52 className={cn(
53 "flex shrink-0 flex-col gap-0.5 transition-opacity duration-250 ease-out motion-reduce:transition-none",
54 collapsed && "opacity-0",
55 )}
56 >
57 <span className="h-0.5 w-4 rounded-full bg-muted-foreground/25" />
58 {!compactRows && <span className="h-0.5 w-2 rounded-full bg-muted-foreground/15" />}
59 </span>
60 </span>
61 ))}
62 </span>
63 <span aria-hidden className="flex min-w-0 flex-1 flex-col gap-1 px-1 pt-1">
64 <span className="h-0.5 w-full rounded-full bg-muted-foreground/25" />
65 <span className="h-0.5 w-4/5 rounded-full bg-muted-foreground/20" />
66 <span className="h-0.5 w-3/5 rounded-full bg-muted-foreground/15" />
67 </span>
68 <span
69 aria-hidden
70 className={cn(
71 "pointer-events-none absolute top-3 left-5 flex w-9 flex-col gap-1 rounded-sm border border-border bg-popover p-1 opacity-0 shadow-xs transition-opacity duration-150 ease-out motion-reduce:transition-none",
72 collapsed &&
73 railEnabled &&
74 "group-hover/compact-preview:opacity-100 group-focus-visible/compact-preview:opacity-100",
75 )}
76 >
77 <span className="h-0.5 w-full rounded-full bg-popover-foreground/45" />
78 <span className="h-0.5 w-2/3 rounded-full bg-popover-foreground/25" />
79 </span>
80 </button>
81 );
82}