Docs Tree Sidebar
A documentation sidebar with living navigation: a spring hover rail glides between rows, the active marker slides along the tree, collapsible groups unfold with animated carets and dashed guide lines, NEW badges flag fresh pages, and the whole panel is drag-resizable. Compound API — Sidebar, Header, Content, Section, Group, Item, Separator, Footer.
NavigationReactMotionTailwind CSSSolar Icons
CSSTailwind
Manual
Create a file and paste the following code into it.
docs-tree-sidebar.tsx
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
"use client";
import { createContext, memo, useCallback, useContext, useEffect, useId, useMemo, useRef, useState } from "react";
import { motion, AnimatePresence } from "motion/react";
import { AltArrowRight } from "@solar-icons/react";
import { cn } from "@/lib/cn";
/* ------------------------------------------------------------------ */
/* DocsTreeSidebar */
/* ------------------------------------------------------------------ */
const MotionChevron = motion.create(AltArrowRight);
const EffectsContext = createContext<{ enabled: boolean }>({ enabled: true });
// ─── Hover context ────────────────────────────────────────────────────────────
interface HoverRect {
top: number;
height: number;
left: number;
}
const HoverContext = createContext<{
hovered: string | null;
hoverRect: HoverRect | null;
containerRef: React.RefObject<HTMLDivElement | null>;
setHovered: (id: string | null, rect?: HoverRect | null) => void;
}>({
hovered: null,
hoverRect: null,
containerRef: { current: null },
setHovered: () => {},
});
function HoverProvider({
children,
containerRef,
}: {
children: React.ReactNode;
containerRef: React.RefObject<HTMLDivElement | null>;
}) {
const [hovered, setHoveredId] = useState<string | null>(null);
const [hoverRect, setHoverRect] = useState<HoverRect | null>(null);
const setHovered = useCallback(
(id: string | null, rect?: HoverRect | null) => {
setHoveredId(id);
setHoverRect(rect ?? null);
},
[],
);
const value = useMemo(
() => ({ hovered, hoverRect, containerRef, setHovered }),
[hovered, hoverRect, containerRef, setHovered],
);
return (
<HoverContext.Provider value={value}>{children}</HoverContext.Provider>
);
}
// ─── Scroll to active ─────────────────────────────────────────────────────────
function useScrollToActive(active: boolean) {
const ref = useRef<HTMLDivElement>(null);
const scrolled = useRef(false);
useEffect(() => {
if (!active || scrolled.current || !ref.current) return;
scrolled.current = true;
const el = ref.current;
const schedule =
typeof requestIdleCallback !== "undefined"
? (cb: () => void) => requestIdleCallback(cb)
: (cb: () => void) => setTimeout(cb, 100);
const cancel =
typeof cancelIdleCallback !== "undefined"
? cancelIdleCallback
: clearTimeout;
const id = schedule(() => {
const viewport = el.closest("[data-scroll-viewport]");
if (!(viewport instanceof HTMLElement)) return;
const vpRect = viewport.getBoundingClientRect();
const elRect = el.getBoundingClientRect();
const scale = viewport.offsetHeight
? vpRect.height / viewport.offsetHeight
: 1;
const offset =
(elRect.top - vpRect.top - vpRect.height / 2 + elRect.height / 2) /
scale;
if (Math.abs(offset) > 40)
viewport.scrollBy({ top: offset, behavior: "smooth" });
});
return () => cancel(id as number);
}, [active]);
useEffect(() => {
if (!active) scrolled.current = false;
}, [active]);
return ref;
}
// ─── HoverHighlight ───────────────────────────────────────────────────────────
function HoverHighlight() {
const { hoverRect, hovered } = useContext(HoverContext);
const { enabled } = useContext(EffectsContext);
return (
<AnimatePresence>
{enabled && hovered && hoverRect && (
<motion.div
key="sb001-hover-bg"
className="pointer-events-none absolute z-0 rounded-md bg-zinc-100 dark:bg-zinc-800/50"
style={{ right: 0 }}
initial={false}
animate={{
top: hoverRect.top + 2,
height: hoverRect.height - 4,
left: hoverRect.left,
opacity: 1,
}}
exit={{ opacity: 0 }}
transition={{ type: "spring", stiffness: 300, damping: 30 }}
/>
)}
</AnimatePresence>
);
}
// ─── DocsTreeSidebarItem ───────────────────────────────────────────────────────────
export interface DocsTreeSidebarItemProps {
href: string;
label: React.ReactNode;
isActive: boolean;
isNew?: boolean;
className?: string;
onClick?: React.MouseEventHandler<HTMLAnchorElement>;
}
export const DocsTreeSidebarItem = memo(function DocsTreeSidebarItem({
href,
label,
isActive,
isNew,
className,
onClick,
}: DocsTreeSidebarItemProps) {
const { hovered, setHovered, containerRef } = useContext(HoverContext);
const isHovered = hovered === href;
const itemRef = useScrollToActive(isActive);
const opacity = isActive
? 1
: hovered !== null
? isHovered
? 1
: 0.3
: 0.55;
const x = isActive ? 8 : isHovered ? 6 : 0;
return (
<div className="relative">
{isActive && (
<motion.span
layoutId="sb001-active-bar"
className="pointer-events-none absolute z-10 left-[4px] top-1/2 h-[2.5px] -translate-y-1/2 rounded-full bg-[#F4574D]"
animate={{ width: 26 }}
transition={{ type: "spring", stiffness: 800, damping: 40 }}
/>
)}
<motion.span
className="pointer-events-none absolute left-0 top-1/2 -translate-y-1/2 h-px bg-zinc-900/50 dark:bg-white/50"
animate={{ width: isActive ? 0 : isHovered ? 26 : 18 }}
transition={{ type: "spring", stiffness: 600, damping: 30 }}
/>
<motion.span className="pointer-events-none absolute w-[13px] left-0 top-1/4 h-px bg-zinc-900/30 dark:bg-white/30" />
<motion.span className="pointer-events-none absolute w-[16px] left-0 top-0 h-px bg-zinc-900/30 dark:bg-white/30" />
<motion.span className="pointer-events-none absolute w-[13px] left-0 top-3/4 h-px bg-zinc-900/30 dark:bg-white/30" />
<motion.div
ref={itemRef}
animate={{ opacity, x }}
transition={{ type: "spring", stiffness: 700, damping: 30 }}
style={{ transformOrigin: "left center" }}
>
<a
href={href}
onClick={onClick}
onMouseEnter={() => {
const el = itemRef.current;
const container = containerRef.current;
if (el && container) {
const elRect = el.getBoundingClientRect();
const containerRect = container.getBoundingClientRect();
// Rects are visual px; the rail positions in the container's
// layout px. Divide by the ancestor scale (1 unless the tree
// is embedded in a transform) or the rail lands offset.
const scale = container.offsetHeight
? containerRect.height / container.offsetHeight
: 1;
setHovered(href, {
top: (elRect.top - containerRect.top) / scale,
height: elRect.height / scale,
left: 25,
});
} else {
setHovered(href);
}
}}
onMouseLeave={() => setHovered(null)}
className={cn(
"relative flex items-center gap-2 ml-2 pl-4 py-1.5 text-sm select-none",
className,
)}
>
<span className="relative z-1 truncate">{label}</span>
{isNew && (
<span className="size-1.5 rounded-full bg-[#F4574D]/80 shrink-0" />
)}
</a>
</motion.div>
</div>
);
});
// ─── DocsTreeSidebarSeparator ──────────────────────────────────────────────────────
export function DocsTreeSidebarSeparator({
children,
className,
}: {
children?: React.ReactNode;
className?: string;
}) {
return (
<div
className={cn(
"px-0 py-3.5 mt-2 text-sm font-medium text-zinc-400",
className,
)}
>
{children}
</div>
);
}
// ─── DocsTreeSidebarGroup ──────────────────────────────────────────────────────────
export interface DocsTreeSidebarGroupProps {
label: React.ReactNode;
children: React.ReactNode;
defaultOpen?: boolean;
icon?: React.ReactNode;
className?: string;
}
export function DocsTreeSidebarGroup({
label,
children,
defaultOpen = false,
icon,
className,
}: DocsTreeSidebarGroupProps) {
const [isOpen, setIsOpen] = useState(false);
const id = useId();
const { setHovered, containerRef } = useContext(HoverContext);
const buttonRef = useRef<HTMLButtonElement>(null);
useEffect(() => {
setIsOpen(defaultOpen);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const handleMouseEnter = useCallback(() => {
const el = buttonRef.current;
const container = containerRef.current;
if (el && container) {
const elRect = el.getBoundingClientRect();
const containerRect = container.getBoundingClientRect();
setHovered(id, {
top: elRect.top - containerRect.top,
height: elRect.height,
left: 0,
});
} else {
setHovered(id);
}
}, [id, setHovered, containerRef]);
const handleMouseLeave = useCallback(() => {
setHovered(null);
}, [setHovered]);
return (
<div className={cn("flex flex-col", className)}>
<button
ref={buttonRef}
type="button"
onClick={() => setIsOpen((v) => !v)}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
className="relative z-1 flex items-center gap-1.5 py-1.5 pr-2 select-none text-left w-full group"
>
{icon ? (
<>
<span className="shrink-0 text-zinc-900 dark:text-zinc-100/35 [&_svg]:size-3.5">
{icon}
</span>
<span className="text-sm text-zinc-900 dark:text-zinc-100/45 group-hover:text-zinc-900 dark:text-zinc-100/70 transition-colors duration-150 flex-1">
{label}
</span>
<MotionChevron
size={14}
strokeWidth={2.5}
className="shrink-0 text-zinc-900 dark:text-zinc-100/25 mr-1"
animate={{ rotate: isOpen ? 90 : 0 }}
transition={{ type: "spring", stiffness: 500, damping: 30 }}
/>
</>
) : (
<>
<MotionChevron
size={11}
strokeWidth={2.5}
className="shrink-0 text-zinc-900 dark:text-zinc-100/35"
animate={{ rotate: isOpen ? 90 : 0 }}
transition={{ type: "spring", stiffness: 500, damping: 30 }}
/>
<span className="text-sm text-zinc-900 dark:text-zinc-100/45 group-hover:text-zinc-900 dark:text-zinc-100/70 transition-colors duration-150">
{label}
</span>
</>
)}
</button>
<AnimatePresence initial={false}>
{isOpen && (
<motion.div
initial={{ height: 0, opacity: 0 }}
animate={{ height: "auto", opacity: 1 }}
exit={{ height: 0, opacity: 0 }}
transition={{ type: "spring", stiffness: 420, damping: 34 }}
style={{ overflow: "hidden" }}
>
<div className="flex flex-col pl-3">{children}</div>
</motion.div>
)}
</AnimatePresence>
</div>
);
}
// ─── DocsTreeSidebarSection ────────────────────────────────────────────────────────
export function DocsTreeSidebarSection({
label,
children,
className,
}: {
label?: React.ReactNode;
children: React.ReactNode;
className?: string;
}) {
return (
<div className={cn("flex flex-col", className)}>
{label && <DocsTreeSidebarSeparator>{label}</DocsTreeSidebarSeparator>}
{children}
</div>
);
}
// ─── DocsTreeSidebarContent ────────────────────────────────────────────────────────
export function DocsTreeSidebarContent({
children,
className,
}: {
children: React.ReactNode;
className?: string;
}) {
const containerRef = useContext(HoverContext).containerRef;
return (
<div
className={cn("flex-1 overflow-y-auto py-4 no-scrollbar", className)}
data-scroll-viewport
>
<div ref={containerRef} className="relative px-1">
<HoverHighlight />
{children}
</div>
</div>
);
}
// ─── DocsTreeSidebar (with resize) ─────────────────────────────────────────────────
export interface DocsTreeSidebarProps {
children: React.ReactNode;
className?: string;
/** Initial width in px. Default: 240 */
defaultWidth?: number;
/** Min resize width in px. Default: 160 */
minWidth?: number;
/** Max resize width in px. Default: 400 */
maxWidth?: number;
}
export function DocsTreeSidebar({
children,
className,
defaultWidth = 240,
minWidth = 160,
maxWidth = 400,
}: DocsTreeSidebarProps) {
const containerRef = useRef<HTMLDivElement>(null);
const [width, setWidth] = useState(defaultWidth);
const dragging = useRef(false);
const startX = useRef(0);
const startW = useRef(0);
const onPointerDown = useCallback(
(e: React.PointerEvent) => {
e.preventDefault();
dragging.current = true;
startX.current = e.clientX;
startW.current = width;
(e.target as HTMLElement).setPointerCapture(e.pointerId);
},
[width],
);
const onPointerMove = useCallback(
(e: React.PointerEvent) => {
if (!dragging.current) return;
const next = Math.min(
maxWidth,
Math.max(minWidth, startW.current + e.clientX - startX.current),
);
setWidth(next);
},
[minWidth, maxWidth],
);
const onPointerUp = useCallback(() => {
dragging.current = false;
}, []);
return (
<HoverProvider containerRef={containerRef}>
<aside
className={cn(
"relative flex flex-col h-full shrink-0 bg-white dark:bg-zinc-950",
className,
)}
style={{ width }}
>
{children}
{/* Resize handle */}
<div
className="absolute top-0 right-0 h-full w-1 cursor-col-resize group/handle z-20"
onPointerDown={onPointerDown}
onPointerMove={onPointerMove}
onPointerUp={onPointerUp}
>
<div className="absolute right-0 top-0 h-full w-px bg-zinc-200 dark:bg-zinc-800/50 group-hover/handle:bg-zinc-200 dark:bg-zinc-800 transition-colors duration-150" />
</div>
</aside>
</HoverProvider>
);
}
// ─── DocsTreeSidebarHeader ─────────────────────────────────────────────────────────
export function DocsTreeSidebarHeader({
children,
className,
}: {
children?: React.ReactNode;
className?: string;
}) {
return (
<div className={cn("shrink-0 px-3 pt-4 pb-2", className)}>{children}</div>
);
}
// ─── DocsTreeSidebarFooter ─────────────────────────────────────────────────────────
export function DocsTreeSidebarFooter({
children,
className,
}: {
children?: React.ReactNode;
className?: string;
}) {
return (
<div
className={cn(
"shrink-0 px-3 pb-4 pt-2 border-t border-zinc-200/60 dark:border-zinc-800/60",
className,
)}
>
{children}
</div>
);
}
Update the import paths to match your project setup.
Similar components
Install via CLI
Resource details
PublishedJuly 18, 2026
CategoryNavigation
ReactMotionTailwind CSSSolar Icons
Install via CLI
Resource details
PublishedJuly 18, 2026
CategoryNavigation
ReactMotionTailwind CSSSolar Icons