Skip to main content

Edge Dock Sidebar

A left-edge drawer that springs in over the page and dismisses on escape, an outside pointer-down, or a scroll. Rows are lit by animated marker lines and a shared active bar, hovering one floats a cursor-tracked video preview, and the active row scrolls itself toward center. A provider adds a single-key shortcut and an optional left-edge hover trigger.

NavigationReactMotionTailwind CSS
CSSTailwind

Manual

Create a file and paste the following code into it.

edge-dock-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
"use client";

import { createContext, memo, type ReactNode, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
import { AnimatePresence, motion, useMotionValue, useSpring } from "motion/react";
import { cn } from "@/lib/cn";

/* ------------------------------------------------------------------ */
/*  EdgeDockSidebar — context + provider                                    */
/* ------------------------------------------------------------------ */

interface SidebarState {
  open: boolean;
  toggle: () => void;
  close: () => void;
}

const SidebarContext = createContext<SidebarState>({
  open: false,
  toggle: () => {},
  close: () => {},
});

/** Read the sidebar's open state and controls from anywhere inside the provider. */
export function useEdgeDockSidebar() {
  return useContext(SidebarContext);
}

interface ProviderProps {
  children: ReactNode;
  /** Start opened (handy for demos/thumbnails) [Optional, default: false] */
  defaultOpen?: boolean;
  /** Single-key shortcut that toggles the drawer [Optional, default: "s"] */
  keyboardShortcut?: string;
  /** Open when the pointer rests at the left edge [Optional, default: true] */
  edgeTrigger?: boolean;
  edgeDelay?: number;
}

/**
 * Wraps the app, holding the drawer's open state. It binds a single-key
 * shortcut (ignored inside inputs) and an optional left-edge hover trigger
 * that opens the drawer after a short dwell.
 * @param {boolean} defaultOpen - Start opened [Optional, default: false]
 * @param {string} keyboardShortcut - Toggle key [Optional, default: "s"]
 */
export export function EdgeDockSidebarProvider({
  children,
  defaultOpen = false,
  keyboardShortcut = "s",
  edgeTrigger = true,
  edgeDelay = 500,
}: ProviderProps) {
  const [open, setOpen] = useState(defaultOpen);
  const toggle = useCallback(() => setOpen((v) => !v), []);
  const close = useCallback(() => setOpen(false), []);
  const value = useMemo(() => ({ open, toggle, close }), [open, toggle, close]);

  useEffect(() => {
    if (!keyboardShortcut) return;
    const onKey = (e: KeyboardEvent) => {
      const t = e.target as HTMLElement;
      if (t.tagName === "INPUT" || t.tagName === "TEXTAREA" || t.isContentEditable) return;
      if (e.key === keyboardShortcut) toggle();
    };
    window.addEventListener("keydown", onKey);
    return () => window.removeEventListener("keydown", onKey);
  }, [keyboardShortcut, toggle]);

  useEffect(() => {
    if (!edgeTrigger) return;
    let timer: ReturnType<typeof setTimeout> | null = null;
    const onMove = (e: MouseEvent) => {
      if (!open && e.clientX <= 16) {
        if (!timer) timer = setTimeout(() => setOpen(true), edgeDelay);
      } else if (timer) {
        clearTimeout(timer);
        timer = null;
      }
    };
    window.addEventListener("mousemove", onMove);
    return () => {
      window.removeEventListener("mousemove", onMove);
      if (timer) clearTimeout(timer);
    };
  }, [open, edgeTrigger, edgeDelay]);

  return <SidebarContext.Provider value={value}>{children}</SidebarContext.Provider>;
}

/* ------------------------------------------------------------------ */
/*  Item + Section                                                     */
/* ------------------------------------------------------------------ */

const PreviewContext = createContext<{ setPreview: (src: string | null) => void }>({
  setPreview: () => {},
});

interface ItemProps {
  href: string;
  label: string;
  isActive?: boolean;
  /** Optional video src to preview beside the cursor on hover [Optional] */
  preview?: string;
  className?: string;
  onClick?: () => void;
  enterDelay?: number;
}

/** Scroll an active row toward the viewport center once, on activation. */
function useScrollIntoView(active: boolean) {
  const ref = useRef<HTMLDivElement>(null);
  const done = useRef(false);
  useEffect(() => {
    if (!active || done.current || !ref.current) return;
    done.current = true;
    const el = ref.current;
    const id = requestAnimationFrame(() => {
      const viewport = el.closest("[data-scroll-viewport]");
      if (!(viewport instanceof HTMLElement)) return;
      const vr = viewport.getBoundingClientRect();
      const er = el.getBoundingClientRect();
      const delta = er.top - vr.top - vr.height / 2 + er.height / 2;
      if (Math.abs(delta) > 40) viewport.scrollBy({ top: delta, behavior: "smooth" });
    });
    return () => cancelAnimationFrame(id);
  }, [active]);
  useEffect(() => {
    if (!active) done.current = false;
  }, [active]);
  return ref;
}

export const EdgeDockSidebarItem = memo(export function EdgeDockSidebarItem({
  href,
  label,
  isActive = false,
  preview,
  className,
  onClick,
  enterDelay = 0,
}: ItemProps) {
  const { setPreview } = useContext(PreviewContext);
  const [hovered, setHovered] = useState(false);
  const scrollRef = useScrollIntoView(isActive);
  // State, not a ref: the entrance-vs-interaction delay is render output,
  // and reading a mutable ref during render tears under concurrent React.
  const [hasEntered, setHasEntered] = useState(false);
  useEffect(() => {
    setHasEntered(true);
  }, []);
  const delay = hasEntered ? 0 : enterDelay;
  const lineSpring = { type: "spring" as const, stiffness: 600, damping: 30 };

  return (
    <div ref={scrollRef} className="relative">
      {isActive && (
        <motion.span
          layoutId="sb002-active-bar"
          className="pointer-events-none absolute top-1/2 z-40 h-[1.8px] -translate-y-1/2 rounded-full bg-[#F4574D]"
          animate={{ width: 30 }}
          transition={{ type: "spring", stiffness: 500, damping: 30 }}
        />
      )}
      <motion.span
        className="pointer-events-none absolute left-0 top-1/2 h-px -translate-y-1/2 bg-zinc-400 dark:bg-zinc-500"
        initial={{ width: 0 }}
        animate={{ width: isActive ? 0 : hovered ? 26 : 18 }}
        transition={{ ...lineSpring, delay }}
      />
      <motion.span
        className="pointer-events-none absolute left-0 top-1/4 h-px bg-zinc-300 dark:bg-zinc-600"
        initial={{ width: 0 }}
        animate={{ width: 13 }}
        transition={{ duration: 0.2, delay: hasEntered ? 0 : enterDelay + 0.04, ease: "easeOut" }}
      />
      <motion.a
        href={href}
        onClick={(e) => {
          e.preventDefault();
          onClick?.();
        }}
        onMouseEnter={() => {
          setHovered(true);
          if (preview) setPreview(preview);
        }}
        onMouseLeave={() => {
          setHovered(false);
          setPreview(null);
        }}
        className={cn("relative flex select-none items-center py-1 pl-8 pr-3", className)}
        initial={{ opacity: 0, x: -18 }}
        animate={{ opacity: isActive || hovered ? 1 : 0.55, x: isActive ? 8 : hovered ? 6 : 0 }}
        transition={{
          opacity: { duration: 0.22, delay, ease: "easeOut" },
          x: { type: "spring", stiffness: 700, damping: 30, delay },
        }}
        style={{ transformOrigin: "left center" }}
      >
        <span className="truncate text-base text-zinc-900 dark:text-zinc-100">{label}</span>
      </motion.a>
    </div>
  );
});

interface SectionProps {
  label: string;
  count?: number;
  children: ReactNode;
  className?: string;
  enterDelay?: number;
}

/** A labelled group of items, with an optional count. */
export export function EdgeDockSidebarSection({ label, count, children, className, enterDelay = 0 }: SectionProps) {
  return (
    <div className={cn("mb-2 flex flex-col gap-0", className)}>
      <motion.span
        className="flex items-baseline gap-2 px-0 py-4 text-base font-medium text-zinc-400 dark:text-zinc-500"
        initial={{ opacity: 0, x: -12 }}
        animate={{ opacity: 1, x: 0 }}
        transition={{
          opacity: { duration: 0.2, delay: enterDelay, ease: "easeOut" },
          x: { type: "spring", stiffness: 600, damping: 30, delay: enterDelay },
        }}
      >
        {label}
        {count !== undefined && (
          <span className="text-xs font-normal tabular-nums text-zinc-300 dark:text-zinc-600">
            {count}
          </span>
        )}
      </motion.span>
      {children}
    </div>
  );
}

/* ------------------------------------------------------------------ */
/*  EdgeDockSidebar — the drawer                                            */
/* ------------------------------------------------------------------ */

interface EdgeDockSidebarProps {
  open: boolean;
  onClose: () => void;
  children: ReactNode;
  className?: string;
  position?: "fixed" | "absolute";
}

/**
 * A left-edge drawer that springs in over the page: escape, an outside
 * pointer-down, or a scroll dismisses it. A single hovered item drives a
 * video preview panel that follows the cursor. When positioned `absolute`
 * it anchors to the nearest positioned ancestor instead of the viewport.
 * @param {boolean} open - Whether the drawer is shown [Required]
 * @param {() => void} onClose - Dismiss handler [Required]
 */
export function EdgeDockSidebar({ open, onClose, children, className, position = "fixed" }: EdgeDockSidebarProps) {
  const panelRef = useRef<HTMLDivElement>(null);
  const anchorRef = useRef<HTMLElement | null>(null);
  const [preview, setPreview] = useState<string | null>(null);
  const px = useMotionValue(0);
  const py = useMotionValue(0);
  const springX = useSpring(px, { stiffness: 300, damping: 30 });
  const springY = useSpring(py, { stiffness: 300, damping: 30 });
  const previewCtx = useMemo(() => ({ setPreview }), []);

  useEffect(() => {
    if (position !== "absolute" || !panelRef.current) return;
    let el = panelRef.current.parentElement;
    while (el) {
      const pos = window.getComputedStyle(el).position;
      if (pos === "relative" || pos === "absolute" || pos === "sticky") {
        anchorRef.current = el;
        return;
      }
      el = el.parentElement;
    }
    anchorRef.current = document.documentElement;
  }, [position, open]);

  useEffect(() => {
    if (!open) return;
    const onKey = (e: KeyboardEvent) => {
      if (e.key === "Escape") onClose();
    };
    window.addEventListener("keydown", onKey);
    return () => window.removeEventListener("keydown", onKey);
  }, [open, onClose]);

  useEffect(() => {
    if (!open) return;
    const onScroll = () => onClose();
    const onDown = (e: PointerEvent) => {
      const t = e.target as HTMLElement;
      if (t.closest("[data-sidebar002-toggle]")) return;
      if (panelRef.current && !panelRef.current.contains(t)) onClose();
    };
    window.addEventListener("scroll", onScroll, { passive: true });
    document.addEventListener("pointerdown", onDown);
    return () => {
      window.removeEventListener("scroll", onScroll);
      document.removeEventListener("pointerdown", onDown);
    };
  }, [open, onClose]);

  useEffect(() => {
    if (!open) return;
    const onMove = (e: MouseEvent) => {
      if (position === "absolute" && anchorRef.current) {
        const r = anchorRef.current.getBoundingClientRect();
        px.set(e.clientX - r.left);
        py.set(e.clientY - r.top);
      } else {
        px.set(e.clientX);
        py.set(e.clientY);
      }
    };
    window.addEventListener("mousemove", onMove);
    return () => window.removeEventListener("mousemove", onMove);
  }, [open, position, px, py]);

  const anchor = position === "absolute" ? "absolute" : "fixed";

  return (
    <PreviewContext.Provider value={previewCtx}>
      <AnimatePresence>
        {open && (
          <>
            <motion.div
              ref={panelRef}
              className={cn(
                `${anchor} left-0 top-0 z-50 h-full w-72 border-r border-zinc-200 bg-white dark:border-zinc-800 dark:bg-zinc-950`,
                className,
              )}
              initial={{ x: "-100%" }}
              animate={{ x: "0%" }}
              exit={{ x: "-100%" }}
              transition={{ type: "spring", stiffness: 300, damping: 28 }}
            >
              <nav
                data-scroll-viewport
                className="[scrollbar-width:none] [&::-webkit-scrollbar]:hidden relative z-10 flex h-full flex-col gap-1 overflow-y-auto px-3 pb-32 pt-32"
              >
                {children}
              </nav>
            </motion.div>

            <AnimatePresence>
              {preview && (
                <motion.div
                  className={`${anchor} z-[150] overflow-hidden rounded-md shadow-2xl pointer-events-none`}
                  style={{ x: springX, y: springY, translateX: "20px", translateY: "-80%", width: 267, height: 167 }}
                  initial={{ opacity: 0, scale: 0.88 }}
                  animate={{ opacity: 1, scale: 1 }}
                  exit={{ opacity: 0, scale: 0.88 }}
                  transition={{ duration: 0.15 }}
                >
                  <video
                    key={preview}
                    src={preview}
                    autoPlay
                    loop
                    muted
                    playsInline
                    className="h-full w-full object-cover"
                  />
                </motion.div>
              )}
            </AnimatePresence>
          </>
        )}
      </AnimatePresence>
    </PreviewContext.Provider>
  );
}

Update the import paths to match your project setup.

Similar components

New

Docs Tree Sidebar

Peek Image Index

Max

Lens Dock

Unfold Panel Navbar

Install via CLI

Resource details

PublishedJuly 17, 2026
CategoryNavigation
ReactMotionTailwind CSS