Components
Navigation
Dock
A macOS-style dock for React. Icons grow smoothly as the pointer nears them. Bring your own SVG icons.
Plays on its own. Click to take over.
Install
Copies the source into components/ui/dock.tsx and installs what it needs.
$ pnpm dlx shadcn@latest add https://makeui.xyz/r/dock.jsonUsage
app/page.tsx
import { Dock } from "@/components/ui/dock"
export default function Example() {
return (
<Dock
items={[
{ label: "Home", icon: <HomeIcon />, active: true },
{ label: "Mail", icon: <MailIcon />, onClick: () => console.log("mail") },
]}
/>
)
}Source
Prefer to copy by hand? It's one file. Requires motion and the shadcn cn helper.
components/ui/dock.tsx
"use client"
import { useEffect, useRef, useState, type ReactNode } from "react"
import {
AnimatePresence,
motion,
useAnimationControls,
useMotionValue,
useReducedMotion,
useSpring,
useTransform,
type MotionValue,
} from "motion/react"
import { cn } from "@/lib/utils"
export interface DockItemData {
id: string
label: string
icon: ReactNode
onClick?: () => void
}
export interface DockMenuProps {
items: DockItemData[]
/** Id that shows the active dot. */
defaultActive?: string
/**
* Draw the icons as they are, with no tile behind them. Use this with the Mac icons or any
* full-color icon. Defaults to false, which puts line icons on a neutral tile.
*/
bare?: boolean
/** Resting icon size in pixels. */
size?: number
/** Icon size under the cursor. */
magnification?: number
/** How far the effect reaches, in pixels. */
distance?: number
/**
* Plays itself with a visible cursor: glides across the dock, then clicks an icon.
* Stops the moment a real pointer arrives and resumes when it leaves.
*/
autoPlay?: boolean
onSelect?: (item: DockItemData) => void
className?: string
}
/* ------------------------------------------------------------------ */
/* One icon */
/* ------------------------------------------------------------------ */
function DockItem({
item,
mouseX,
size,
magnification,
distance,
active,
bare,
tip,
pulse,
onPick,
}: {
item: DockItemData
mouseX: MotionValue<number>
size: number
magnification: number
distance: number
active: boolean
bare: boolean
/** Force the tooltip open (used by autoplay). */
tip: boolean
/** Changes every time autoplay "clicks" this icon. */
pulse: number
onPick: () => void
}) {
const ref = useRef<HTMLButtonElement>(null)
const [hover, setHover] = useState(false)
const icon = useAnimationControls()
const bounce = () =>
icon.start({
y: [0, -16, 0, -7, 0],
transition: { duration: 0.6, ease: "easeOut" },
})
useEffect(() => {
if (pulse > 0) bounce()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [pulse])
const away = useTransform(mouseX, (x) => {
const b = ref.current?.getBoundingClientRect()
return b ? x - b.x - b.width / 2 : Infinity
})
const raw = useTransform(away, [-distance, 0, distance], [size, magnification, size])
// Critically damped-ish: follows the cursor tightly, no jelly overshoot.
const width = useSpring(raw, { mass: 0.12, stiffness: 320, damping: 28 })
const showTip = hover || tip
return (
<div
className="relative flex flex-col items-center"
onMouseEnter={() => setHover(true)}
onMouseLeave={() => setHover(false)}
>
<AnimatePresence>
{showTip && (
<motion.span
initial={{ opacity: 0, y: 6, scale: 0.95 }}
animate={{ opacity: 1, y: 0, scale: 1 }}
exit={{ opacity: 0, y: 4, scale: 0.95 }}
transition={{ duration: 0.12 }}
className="pointer-events-none absolute bottom-full z-10 mb-3 whitespace-nowrap rounded-lg bg-neutral-900/95 px-2.5 py-1 text-xs font-medium text-white shadow-lg ring-1 ring-white/10 dark:bg-white/95 dark:text-neutral-900 dark:ring-black/10"
>
{item.label}
</motion.span>
)}
</AnimatePresence>
<motion.button
ref={ref}
type="button"
aria-label={item.label}
aria-current={active || undefined}
onClick={() => {
bounce()
onPick()
}}
whileTap={{ scale: 0.92 }}
style={{ width, height: width }}
className={cn(
"grid cursor-pointer place-items-center outline-none focus-visible:ring-2 focus-visible:ring-sky-500",
bare
? "rounded-[22%]"
: "rounded-2xl bg-white text-neutral-700 shadow-sm ring-1 ring-black/5 dark:bg-neutral-800 dark:text-neutral-200 dark:ring-white/10",
)}
>
{/* The inner span bounces on click, like launching an app. */}
<motion.span
animate={icon}
className={cn(
"grid size-full place-items-center",
bare ? "[&>svg]:size-full" : "[&>svg]:size-1/2",
)}
>
{item.icon}
</motion.span>
</motion.button>
<span
aria-hidden
className={cn(
"absolute -bottom-[7px] size-1 rounded-full bg-neutral-800 transition-opacity duration-200 dark:bg-white/90",
active ? "opacity-100" : "opacity-0",
)}
/>
</div>
)
}
/* ------------------------------------------------------------------ */
/* Fake cursor for autoplay */
/* ------------------------------------------------------------------ */
function Cursor({
x,
y,
opacity,
scale,
}: {
x: MotionValue<number>
y: MotionValue<number>
opacity: MotionValue<number>
scale: MotionValue<number>
}) {
return (
<motion.svg
aria-hidden
width="22"
height="22"
viewBox="0 0 24 24"
style={{ x, y, opacity, scale, transformOrigin: "4px 3px" }}
className="pointer-events-none absolute left-0 top-0 z-50 drop-shadow-[0_2px_3px_rgb(0_0_0/0.35)]"
>
<path
d="M4 2.5 L4 18.5 L8.2 14.6 L11 21 L13.6 19.9 L10.9 13.6 L16.8 13.4 Z"
fill="#fff"
stroke="#111"
strokeWidth="1.4"
strokeLinejoin="round"
/>
</motion.svg>
)
}
const lerp = (a: number, b: number, p: number) => a + (b - a) * p
const easeInOut = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2)
const easeOut = (t: number) => 1 - Math.pow(1 - t, 3)
/* ------------------------------------------------------------------ */
/* Dock */
/* ------------------------------------------------------------------ */
export default function DockMenu({
items,
defaultActive,
bare = false,
size = 44,
magnification = 72,
distance = 130,
autoPlay = false,
onSelect,
className,
}: DockMenuProps) {
const reduce = useReducedMotion()
const mouseX = useMotionValue(Infinity)
const [active, setActive] = useState(defaultActive)
// autoplay state
const rootRef = useRef<HTMLDivElement>(null)
const navRef = useRef<HTMLElement>(null)
const userRef = useRef(false)
const [autoTip, setAutoTip] = useState<string | null>(null)
const [pulse, setPulse] = useState({ id: "", n: 0 })
const cX = useMotionValue(0)
const cY = useMotionValue(0)
const cO = useMotionValue(0)
const cS = useMotionValue(1)
useEffect(() => {
if (!autoPlay || reduce) return
const root = rootRef.current
const nav = navRef.current
if (!root || !nav) return
const ids = items.map((i) => i.id)
if (!ids.length) return
const home = Math.max(0, ids.indexOf(defaultActive ?? ids[0]))
const goal = (home + 2) % ids.length // the icon it will click
// timeline, ms
const D = { enter: 800, sweep: 2800, aim: 1000, press: 320, hold: 1200, leave: 800, rest: 700 }
const e1 = D.enter
const e2 = e1 + D.sweep
const e3 = e2 + D.aim
const e4 = e3 + D.press
const e5 = e4 + D.hold
const e6 = e5 + D.leave
const e7 = e6 + D.rest
let raf = 0
let t0 = 0
let visible = true
let clicked = false
let tipId: string | null = null
const io = new IntersectionObserver(
([e]) => {
visible = e.isIntersecting
},
{ threshold: 0.2 },
)
io.observe(root)
const tick = (now: number) => {
raf = requestAnimationFrame(tick)
if (userRef.current || !visible) {
t0 = 0
return
}
if (!t0) {
t0 = now
clicked = false
setActive(ids[home])
}
const t = now - t0
if (t > e7) {
t0 = 0
return
}
const nr = nav.getBoundingClientRect()
const rr = root.getBoundingClientRect()
const k = rr.width / (root.offsetWidth || 1) // in case a parent scales us
const btns = Array.from(nav.querySelectorAll("button"))
const g = btns[goal]?.getBoundingClientRect()
const gx = g ? g.left + g.width / 2 : nr.left + nr.width / 2
const left = nr.left + nr.width * 0.07
const right = nr.right - nr.width * 0.07
const yIcon = nr.bottom - 26
const yBelow = nr.bottom + 46
let x = gx
let y = yIcon
let op = 1
let sc = 1
if (t < e1) {
const p = easeOut(t / D.enter)
x = lerp(left - 50, left, p)
y = lerp(yBelow, yIcon, p)
op = Math.min(1, p * 2.5)
} else if (t < e2) {
const p = easeInOut((t - e1) / D.sweep)
x = lerp(left, right, p)
y = yIcon + Math.sin(p * Math.PI * 3) * 3
} else if (t < e3) {
const p = easeInOut((t - e2) / D.aim)
x = lerp(right, gx, p)
} else if (t < e4) {
const q = (t - e3) / D.press
sc = q < 0.5 ? lerp(1, 0.8, q * 2) : lerp(0.8, 1, (q - 0.5) * 2)
if (!clicked && q >= 0.4) {
clicked = true
setActive(ids[goal])
setPulse((s) => ({ id: ids[goal], n: s.n + 1 }))
}
} else if (t < e5) {
// hold on the icon
} else if (t < e6) {
const p = easeInOut((t - e5) / D.leave)
x = gx + p * 36
y = lerp(yIcon, yBelow, p)
op = 1 - p
} else {
y = yBelow
op = 0
}
// the dock only reacts while the cursor is on it
const over = op > 0.35 && y < nr.bottom + 6
mouseX.set(over ? x : Infinity)
let nextTip: string | null = null
if (over && btns.length) {
let best = 0
let bd = Infinity
btns.forEach((b, i) => {
const r = b.getBoundingClientRect()
const d = Math.abs(x - (r.left + r.width / 2))
if (d < bd) {
bd = d
best = i
}
})
nextTip = ids[best]
}
if (nextTip !== tipId) {
tipId = nextTip
setAutoTip(nextTip)
}
cX.set((x - rr.left) / k - 3)
cY.set((y - rr.top) / k - 2)
cO.set(op)
cS.set(sc)
}
raf = requestAnimationFrame(tick)
return () => {
cancelAnimationFrame(raf)
io.disconnect()
mouseX.set(Infinity)
cO.set(0)
setAutoTip(null)
}
}, [autoPlay, reduce, items, defaultActive, mouseX, cX, cY, cO, cS])
const takeOver = () => {
userRef.current = true
cO.set(0)
setAutoTip(null)
}
return (
<div
ref={rootRef}
className={cn("relative flex w-full justify-center", className)}
// room above the dock for the magnified icons and the tooltip
style={{ paddingTop: magnification - size + 40 }}
>
<motion.nav
ref={navRef}
aria-label="Dock"
onPointerEnter={takeOver}
onPointerMove={(e) => {
if (e.pointerType !== "touch") mouseX.set(e.clientX)
}}
onPointerLeave={() => {
mouseX.set(Infinity)
userRef.current = false
}}
style={{ height: size + 22 }}
className={cn(
"flex items-end gap-2.5 rounded-[26px] border px-3 pb-2.5",
"border-black/10 bg-white/55 dark:border-white/15 dark:bg-white/[0.08]",
"backdrop-blur-2xl backdrop-saturate-150",
"shadow-[0_12px_32px_-10px_rgb(0_0_0/0.3),inset_0_1px_0_rgb(255_255_255/0.55)]",
"dark:shadow-[0_12px_32px_-10px_rgb(0_0_0/0.7),inset_0_1px_0_rgb(255_255_255/0.12)]",
)}
>
{items.map((item) => (
<DockItem
key={item.id}
item={item}
mouseX={mouseX}
size={size}
magnification={magnification}
distance={distance}
active={active === item.id}
bare={bare}
tip={autoTip === item.id}
pulse={pulse.id === item.id ? pulse.n : 0}
onPick={() => {
setActive(item.id)
item.onClick?.()
onSelect?.(item)
}}
/>
))}
</motion.nav>
{autoPlay && !reduce && <Cursor x={cX} y={cY} opacity={cO} scale={cS} />}
</div>
)
}