Skip to content

Magnetic Dock

macOS-style dock for React: icons swell with a gaussian falloff, labels float up, launches bounce and running apps get a dot.

  • Cursors & Interactions
  • Productivity
  • React only · 0 deps
  • 1 file
Full screen

Browser support · All evergreen browsers. Magnification is pointer-only; keyboard focus magnifies too. Tooltip-on-focus uses :has().

Install. One command, or copy the file.

$npx shadcn@latest add https://freedesignmd.com/r/magnetic-dock.json
  • Byte-identical to the preview
  • No Tailwind or config needed
  • SSR-safe · keyboard accessible
  • Respects reduced motion & transparency
components/interactions/magnetic-dock.tsx
1/**
2 * MagneticDock, a macOS-style dock. Icons grow with a gaussian falloff
3 * around the cursor, the bar widens with them, labels float above on hover
4 * or focus, launching an app bounces it, and running apps get a dot.
5 *
6 * Props
7 * - apps: { id, label, icon: ReactNode, running?: boolean, badge?: number }[]
8 * - onLaunch?: (id) => void
9 * - size?: number Base icon size in px. Default 52 (44 under 640px).
10 * - magnify?: number Peak scale. Default 1.75.
11 * - spread?: number Gaussian sigma in icon widths. Default 1.25.
12 * - separatorAfter?: number Draw a divider after this index.
13 * CSS vars: --fd-dock-bg, --fd-dock-border, --fd-dock-tip-bg, --fd-dock-tip-fg.
14 *
15 * Accessibility: a real toolbar of <button>s with roving tabindex. ← → Home
16 * End move focus (focus also magnifies), Enter / Space launch. Badges and
17 * running state are in each button's accessible name.
18 * Motion: per-icon spring on scale (380 / 30); layout reflows via width so
19 * neighbours make room. Reduced motion or touch: no magnification, bounce
20 * becomes a short opacity blink.
21 * Browser support: all evergreen browsers (backdrop-filter optional).
22 */
23import { useEffect, useId, useRef, useState } from "react";
24import type { KeyboardEvent, ReactNode } from "react";
25
26export type DockApp = { id: string; label: string; icon: ReactNode; running?: boolean; badge?: number };
27
28export interface MagneticDockProps {
29 apps: DockApp[];
30 onLaunch?: (id: string) => void;
31 size?: number;
32 magnify?: number;
33 spread?: number;
34 separatorAfter?: number;
35 className?: string;
36}
37
38export function MagneticDock({ apps, onLaunch, size = 52, magnify = 1.75, spread = 1.25, separatorAfter, className }: MagneticDockProps) {
39 const bar = useRef<HTMLDivElement>(null);
40 const items = useRef<(HTMLLIElement | null)[]>([]);
41 const btns = useRef<(HTMLButtonElement | null)[]>([]);
42 const [focus, setFocus] = useState(0);
43 const [bouncing, setBouncing] = useState<string | null>(null);
44 const focusX = useRef<number | null>(null);
45
46 useEffect(() => {
47 const el = bar.current;
48 if (!el) return;
49 const reduced = window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
50 const fine = window.matchMedia?.("(hover: hover) and (pointer: fine)").matches;
51 if (reduced || !fine) return;
52 const n = apps.length;
53 const sc = new Float32Array(n).fill(1);
54 const vs = new Float32Array(n);
55 const tg = new Float32Array(n).fill(1);
56 const centers = new Float32Array(n);
57 let base = size;
58 let mx: number | null = null;
59 let raf = 0;
60 let last = 0;
61
62 // centres measured from the un-magnified layout (offsetLeft at rest is stable enough;
63 // we compute from index so magnification never feeds back into the input)
64 const measure = () => {
65 base = el.clientWidth < 1 ? size : parseFloat(getComputedStyle(el).getPropertyValue("--fd-dock-size")) || size;
66 };
67 const restCenters = () => {
68 if (!items.current[0]) return;
69 const r0 = el.getBoundingClientRect();
70 // rest layout: icons packed with gap; reconstruct from the bar's centre so it is symmetric
71 const gap = parseFloat(getComputedStyle(el.firstElementChild as Element).columnGap || "0") || 0;
72 let extra = 0;
73 const ws: number[] = [];
74 for (let i = 0; i < n; i++) {
75 ws.push(base);
76 if (separatorAfter !== undefined && i === separatorAfter) extra += 17;
77 }
78 const total = ws.reduce((a, b) => a + b, 0) + gap * (n - 1) + extra;
79 let x = r0.left + r0.width / 2 - total / 2;
80 for (let i = 0; i < n; i++) {
81 centers[i] = x + base / 2;
82 x += base + gap + (separatorAfter !== undefined && i === separatorAfter ? 17 : 0);
83 }
84 };
85
86 const tick = (t: number) => {
87 raf = 0;
88 const dt = Math.min(0.032, (t - last) / 1000 || 0.016);
89 last = t;
90 const src = mx ?? focusX.current;
91 if (src !== null) restCenters();
92 const sig = spread * base;
93 let moving = false;
94 for (let i = 0; i < n; i++) {
95 if (src === null) tg[i] = 1;
96 else {
97 const d = src - centers[i];
98 tg[i] = 1 + (magnify - 1) * Math.exp(-(d * d) / (2 * sig * sig));
99 }
100 const h = dt / 2;
101 for (let k = 0; k < 2; k++) {
102 vs[i] += (-520 * (sc[i] - tg[i]) - 36 * vs[i]) * h;
103 sc[i] += vs[i] * h;
104 }
105 if (Math.abs(sc[i] - tg[i]) > 0.001 || Math.abs(vs[i]) > 0.01) moving = true;
106 }
107 for (let i = 0; i < n; i++) {
108 const it = items.current[i];
109 if (it) it.style.setProperty("--s", sc[i].toFixed(4));
110 }
111 if (moving || src !== null) raf = requestAnimationFrame(tick);
112 };
113 const kick = () => {
114 if (!raf) {
115 last = performance.now();
116 raf = requestAnimationFrame(tick);
117 }
118 };
119 const onMove = (e: PointerEvent) => {
120 mx = e.clientX;
121 kick();
122 };
123 const onLeave = () => {
124 mx = null;
125 kick();
126 };
127 measure();
128 el.addEventListener("pointermove", onMove);
129 el.addEventListener("pointerleave", onLeave);
130 const onFocusMove = () => kick();
131 el.addEventListener("fd-dock-focus", onFocusMove);
132 window.addEventListener("resize", measure);
133 return () => {
134 cancelAnimationFrame(raf);
135 el.removeEventListener("pointermove", onMove);
136 el.removeEventListener("pointerleave", onLeave);
137 el.removeEventListener("fd-dock-focus", onFocusMove);
138 window.removeEventListener("resize", measure);
139 };
140 }, [apps.length, size, magnify, spread, separatorAfter]);
141
142 const launch = (id: string) => {
143 setBouncing(null);
144 requestAnimationFrame(() => setBouncing(id));
145 onLaunch?.(id);
146 };
147
148 const onKey = (e: KeyboardEvent<HTMLDivElement>) => {
149 const n = apps.length;
150 let next = focus;
151 if (e.key === "ArrowRight") next = (focus + 1) % n;
152 else if (e.key === "ArrowLeft") next = (focus - 1 + n) % n;
153 else if (e.key === "Home") next = 0;
154 else if (e.key === "End") next = n - 1;
155 else return;
156 e.preventDefault();
157 setFocus(next);
158 btns.current[next]?.focus();
159 };
160
161 const onFocusBtn = (i: number) => {
162 setFocus(i);
163 const b = btns.current[i];
164 if (b && b.matches(":focus-visible")) {
165 const r = b.getBoundingClientRect();
166 focusX.current = r.left + r.width / 2;
167 bar.current?.dispatchEvent(new Event("fd-dock-focus"));
168 }
169 };
170 const onBlurBtn = () => {
171 focusX.current = null;
172 bar.current?.dispatchEvent(new Event("fd-dock-focus"));
173 };
174
175 return (
176 <div className={`fd-dock${className ? " " + className : ""}`} style={{ ["--fd-dock-size" as string]: `${size}px`, ["--fd-dock-mag" as string]: magnify }}>
177 <style>{css}</style>
178 <div ref={bar} className="fd-dock-bar" role="toolbar" aria-label="Dock" onKeyDown={onKey}>
179 <ul className="fd-dock-list">
180 {apps.map((a, i) => (
181 <li
182 key={a.id}
183 ref={(n) => { items.current[i] = n; }}
184 className="fd-dock-item"
185 data-sep={separatorAfter === i ? "" : undefined}
186 data-bounce={bouncing === a.id ? "" : undefined}
187 onAnimationEnd={() => setBouncing((b) => (b === a.id ? null : b))}
188 >
189 <button
190 ref={(n) => { btns.current[i] = n; }}
191 type="button"
192 className="fd-dock-btn"
193 tabIndex={i === focus ? 0 : -1}
194 aria-label={`${a.label}${a.badge ? `, ${a.badge} new` : ""}${a.running ? ", running" : ""}`}
195 onClick={() => launch(a.id)}
196 onFocus={() => onFocusBtn(i)}
197 onBlur={onBlurBtn}
198 >
199 <span className="fd-dock-icon" aria-hidden="true">{a.icon}</span>
200 {a.badge ? <span className="fd-dock-badge" aria-hidden="true">{a.badge > 99 ? "99+" : a.badge}</span> : null}
201 </button>
202 <span className="fd-dock-tip" aria-hidden="true">{a.label}</span>
203 <span className="fd-dock-dot" data-on={a.running ? "" : undefined} aria-hidden="true" />
204 </li>
205 ))}
206 </ul>
207 </div>
208 </div>
209 );
210}
211
212const css = `
213.fd-dock,.fd-dock *,.fd-dock *::before,.fd-dock *::after{box-sizing:border-box}
214.fd-dock{--fd-dock-bg:rgba(246,246,248,.42);--fd-dock-border:rgba(255,255,255,.45);--fd-dock-tip-bg:rgba(28,28,34,.86);--fd-dock-tip-fg:#F5F5F7;
215 --fd-dock-pad:8px;display:flex;justify-content:center;margin:0;padding:0;font:500 13px/1 ui-sans-serif,-apple-system,BlinkMacSystemFont,"SF Pro Text","Inter","Segoe UI",sans-serif}
216.fd-dock-bar{position:relative;display:flex;align-items:flex-end;margin:0;padding:var(--fd-dock-pad) 10px;border-radius:calc(var(--fd-dock-size) * .44 + var(--fd-dock-pad));
217 background:var(--fd-dock-bg);-webkit-backdrop-filter:blur(28px) saturate(1.8);backdrop-filter:blur(28px) saturate(1.8);
218 box-shadow:inset 0 0 0 1px var(--fd-dock-border),inset 0 1px 0 rgba(255,255,255,.5),0 20px 50px -18px rgba(10,10,20,.5),0 2px 8px rgba(10,10,20,.12)}
219.fd-dock-list{display:flex;align-items:flex-end;gap:6px;margin:0;padding:0;list-style:none}
220.fd-dock-item{--s:1;position:relative;display:flex;flex-direction:column;align-items:center;margin:0;padding:0;list-style:none;
221 width:calc(var(--fd-dock-size) * var(--s));height:var(--fd-dock-size);will-change:width}
222.fd-dock-item[data-sep]{margin-right:17px}
223.fd-dock-item[data-sep]::after{content:"";position:absolute;right:-12px;bottom:4px;width:1px;height:calc(var(--fd-dock-size) - 8px);background:rgba(20,20,30,.18)}
224.fd-dock-btn{appearance:none;-webkit-appearance:none;position:absolute;left:50%;bottom:0;display:block;width:var(--fd-dock-size);height:var(--fd-dock-size);margin:0;padding:0;border:0;background:none;
225 cursor:pointer;transform-origin:50% 100%;transform:translateX(-50%) scale(var(--s));border-radius:calc(var(--fd-dock-size) * .23);-webkit-tap-highlight-color:transparent}
226.fd-dock-btn:focus-visible{outline:2px solid rgba(10,132,255,.85);outline-offset:3px}
227.fd-dock-icon{display:block;width:100%;height:100%;margin:0;padding:0;border-radius:inherit;overflow:hidden;
228 box-shadow:0 1px 1px rgba(0,0,0,.12),0 4px 10px -4px rgba(0,0,0,.35);transition:filter 120ms}
229.fd-dock-icon svg{display:block;width:100%;height:100%}
230.fd-dock-btn:active .fd-dock-icon{filter:brightness(.78)}
231.fd-dock-badge{position:absolute;top:-4px;right:-6px;min-width:20px;height:20px;margin:0;padding:0 6px;border-radius:9999px;background:#FF3B30;color:#fff;
232 font:600 12px/20px ui-sans-serif,-apple-system,"SF Pro Text",sans-serif;text-align:center;box-shadow:0 1px 2px rgba(0,0,0,.25)}
233.fd-dock-tip{position:absolute;left:50%;bottom:calc(var(--fd-dock-size) * var(--s) + 14px);margin:0;padding:6px 10px;border-radius:8px;background:var(--fd-dock-tip-bg);color:var(--fd-dock-tip-fg);
234 font:500 13px/1 ui-sans-serif,-apple-system,"SF Pro Text","Inter",sans-serif;letter-spacing:-.005em;white-space:nowrap;pointer-events:none;
235 opacity:0;transform:translate(-50%,4px);transition:opacity 120ms cubic-bezier(.22,1,.36,1),transform 240ms cubic-bezier(.22,1,.36,1);
236 -webkit-backdrop-filter:blur(12px);backdrop-filter:blur(12px)}
237.fd-dock-tip::after{content:"";position:absolute;left:50%;bottom:-4px;width:8px;height:8px;margin-left:-4px;background:inherit;transform:rotate(45deg);border-radius:1px}
238.fd-dock-item:hover .fd-dock-tip,.fd-dock-item:has(.fd-dock-btn:focus-visible) .fd-dock-tip{opacity:1;transform:translate(-50%,0)}
239.fd-dock-dot{position:absolute;left:50%;bottom:-7px;width:4px;height:4px;margin-left:-2px;border-radius:50%;background:rgba(20,20,30,.72);opacity:0;transition:opacity 240ms}
240.fd-dock-dot[data-on]{opacity:1}
241.fd-dock-item[data-bounce] .fd-dock-btn{animation:fd-dock-bounce 1150ms cubic-bezier(.3,0,.5,1)}
242@keyframes fd-dock-bounce{
243 0%{translate:0 0}18%{translate:0 -34%;animation-timing-function:cubic-bezier(.5,0,.7,1)}36%{translate:0 0;animation-timing-function:cubic-bezier(.3,0,.5,1)}
244 52%{translate:0 -20%;animation-timing-function:cubic-bezier(.5,0,.7,1)}68%{translate:0 0;animation-timing-function:cubic-bezier(.3,0,.5,1)}82%{translate:0 -7%}100%{translate:0 0}}
245@media (max-width:640px){.fd-dock{--fd-dock-size:40px!important}.fd-dock-list{gap:4px}.fd-dock-bar{padding:6px 8px}}
246@media (prefers-reduced-motion:reduce){.fd-dock-item[data-bounce] .fd-dock-btn{animation:fd-dock-blink 240ms linear}.fd-dock-tip{transition:none}}
247@keyframes fd-dock-blink{50%{opacity:.5}}
248@media (prefers-reduced-transparency:reduce){.fd-dock{--fd-dock-bg:#ECECF0}}
249@media (prefers-contrast:more){.fd-dock{--fd-dock-bg:#ECECF0;--fd-dock-border:#1C1C22}}
250`;
251
252/* ───────────────────────── original app icons ───────────────────────── */
253
254const Icon = ({ bg, children }: { bg: [string, string]; children: ReactNode }) => {
255 const id = `fd-dock${useId().replace(/[^a-zA-Z0-9]/g, "")}`;
256 return (
257 <svg aria-hidden="true" focusable="false" viewBox="0 0 64 64">
258 <defs>
259 <linearGradient id={id} x1="0" y1="0" x2="0" y2="1"><stop offset="0" stopColor={bg[0]} /><stop offset="1" stopColor={bg[1]} /></linearGradient>
260 </defs>
261 <rect width="64" height="64" fill={`url(#${id})`} />
262 {children}
263 <rect width="64" height="32" fill="#fff" opacity=".06" />
264 </svg>
265 );
266};
267
268const ICONS: Record<string, ReactNode> = {
269 finder: (
270 <Icon bg={["#7CC4FF", "#2F7BF6"]}>
271 <path d="M32 0v64" stroke="#1D4FB8" strokeWidth="0" />
272 <rect x="32" width="32" height="64" fill="#EAF4FF" />
273 <circle cx="22" cy="26" r="2.6" fill="#0B2A66" /><circle cx="42" cy="26" r="2.6" fill="#0B2A66" />
274 <path d="M32 12c-3 8-4 18-1 28h-5" fill="none" stroke="#0B2A66" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" />
275 <path d="M18 44c8 6 20 6 28 0" fill="none" stroke="#0B2A66" strokeWidth="2.4" strokeLinecap="round" />
276 </Icon>
277 ),
278 mail: (
279 <Icon bg={["#6FD3FF", "#1C8BF0"]}>
280 <rect x="12" y="19" width="40" height="27" rx="4" fill="#fff" />
281 <path d="M13 21l19 14 19-14" fill="none" stroke="#1C8BF0" strokeWidth="2.6" strokeLinejoin="round" />
282 </Icon>
283 ),
284 calendar: (
285 <Icon bg={["#FFFFFF", "#ECECEF"]}>
286 <rect width="64" height="18" fill="#FF3B30" />
287 <text x="32" y="13" textAnchor="middle" fill="#fff" style={{ font: '700 9px ui-sans-serif,-apple-system,"SF Pro Text",sans-serif', letterSpacing: ".06em" }}>NOV</text>
288 <text x="32" y="50" textAnchor="middle" fill="#1C1C22" style={{ font: '300 30px "SF Pro Display",ui-sans-serif,-apple-system,sans-serif', letterSpacing: "-.04em" }}>14</text>
289 </Icon>
290 ),
291 notes: (
292 <Icon bg={["#FFF3B0", "#FFE16A"]}>
293 <rect width="64" height="15" fill="#F5C518" />
294 {[26, 34, 42, 50].map((y) => <rect key={y} x="11" y={y} width={y === 50 ? 26 : 42} height="2" rx="1" fill="#B79A3A" opacity=".55" />)}
295 </Icon>
296 ),
297 chat: (
298 <Icon bg={["#5BF08A", "#16B84B"]}>
299 <path d="M32 14c11 0 20 7 20 16s-9 16-20 16c-2.4 0-4.6-.3-6.6-.9L16 49l2.6-7.4C14.5 38.6 12 34.5 12 30c0-9 9-16 20-16z" fill="#fff" />
300 </Icon>
301 ),
302 music: (
303 <Icon bg={["#FF7A9A", "#F5294F"]}>
304 <path d="M26 44V20l18-4v24" fill="none" stroke="#fff" strokeWidth="3.2" strokeLinecap="round" strokeLinejoin="round" />
305 <ellipse cx="22" cy="44" rx="5" ry="4" fill="#fff" /><ellipse cx="40" cy="40" rx="5" ry="4" fill="#fff" />
306 </Icon>
307 ),
308 settings: (
309 <Icon bg={["#A9AAB4", "#6C6D78"]}>
310 <g transform="translate(32 32)">
311 {Array.from({ length: 12 }, (_, k) => <rect key={k} x="-2.6" y="-21" width="5.2" height="8" rx="1.4" fill="#E8E8ED" transform={`rotate(${k * 30})`} />)}
312 <circle r="14" fill="#E8E8ED" /><circle r="6.5" fill="#6C6D78" />
313 </g>
314 </Icon>
315 ),
316};
317
318/* ───────────────────────────── demo ───────────────────────────── */
319
320const APPS: DockApp[] = [
321 { id: "finder", label: "Files", icon: ICONS.finder, running: true },
322 { id: "mail", label: "Mail", icon: ICONS.mail, running: true, badge: 3 },
323 { id: "calendar", label: "Calendar", icon: ICONS.calendar },
324 { id: "notes", label: "Notes", icon: ICONS.notes },
325 { id: "chat", label: "Messages", icon: ICONS.chat },
326 { id: "music", label: "Music", icon: ICONS.music },
327 { id: "settings", label: "Settings", icon: ICONS.settings },
328];
329
330export function Demo() {
331 const [apps, setApps] = useState(APPS);
332 const launch = (id: string) => setApps((as) => as.map((a) => (a.id === id ? { ...a, running: true, badge: undefined } : a)));
333 return (
334 <div className="fd-dockd">
335 <style>{demoCss}</style>
336 <MagneticDock apps={apps} onLaunch={launch} />
337 </div>
338 );
339}
340
341const demoCss = `
342.fd-dockd,.fd-dockd *,.fd-dockd *::before,.fd-dockd *::after{box-sizing:border-box}
343.fd-dockd{display:flex;align-items:center;justify-content:center;min-height:100vh;width:100%;margin:0;padding:32px 12px;overflow:hidden;background:#ececec;color:#1d1d1f;font:400 15px/1.4 ui-sans-serif,-apple-system,BlinkMacSystemFont,"SF Pro Text","Inter","Segoe UI",Roboto,sans-serif;letter-spacing:normal}
344`;

More Cursors & Interactions. Same file contract.

Image Trail, interactions preview: Image trail cursor effect: cards spawn along the cursor path, pop in with a tilt and fall away

Image Trail

Image trail cursor effect: cards spawn along the cursor path, pop in with a tilt and fall away. A gallery you move through.

Custom Cursor, interactions preview: Custom cursor for React: a dot and a spring-lagged ring that morph into labels like View, Drag or Play and…

Custom Cursor

Custom cursor for React: a dot and a spring-lagged ring that morph into labels like View, Drag or Play and snap to buttons.

Fisheye Grid, interactions preview: Fisheye grid: an infinite two-axis tile wall you drag with momentum, with a lens that swells tiles around the…

Fisheye Grid

Fisheye grid: an infinite two-axis tile wall you drag with momentum, with a lens that swells tiles around the cursor.

Questions about Magnetic Dock

Run npx shadcn@latest add https://freedesignmd.com/r/magnetic-dock.json, or open the page and copy the file into your project. It has zero dependencies besides React. What you copy is the exact file that renders the live demo.

macOS-style dock for React: icons swell with a gaussian falloff, labels float up, launches bounce and running apps get a dot. It is a free, MIT-licensed React cursors & interactions component from the freedesignmd library.

Yes. It is free and MIT-licensed, use, modify and ship it in personal and commercial projects without attribution.

All evergreen browsers. Magnification is pointer-only; keyboard focus magnifies too. Tooltip-on-focus uses :has().

Yes. It is a plain React component with its own styles, so it runs in Next.js, Vite, Remix and Lovable projects, and it needs no Tailwind setup. It has zero dependencies besides React.