Dock
A macOS-style dock for React built with Motion and Tailwind CSS: icons magnify smoothly as the cursor approaches, labels fade in on hover and focus, and an active dot glides between apps. Pointer tracking runs on motion values and springs, so it never re-renders — and it works horizontally or vertically.
- macOS magnification
- No re-renders
- Keyboard magnify
- Vertical option
- Static on touch
- prefers-reduced-motion
"use client";
import * as React from "react";
import { Calendar, Camera, Compass, Folder, Mail, MessageCircle, Music, Settings, Trash2 } from "lucide-react";
import { Dock, DockItem, DockSeparator } from "@/components/dusk/dock/dock";
const apps = [
{ label: "Finder", icon: Folder },
{ label: "Browser", icon: Compass },
{ label: "Mail", icon: Mail },
{ label: "Messages", icon: MessageCircle },
{ label: "Calendar", icon: Calendar },
{ label: "Photos", icon: Camera },
{ label: "Music", icon: Music },
{ label: "Settings", icon: Settings },
];
export default function DockDemo() {
const [active, setActive] = React.useState("Browser");
return (
<div className="flex h-56 w-full items-end justify-center">
{/* Phones get a shorter dock: apps 5–8 hide below sm. */}
<Dock aria-label="Applications" className="max-sm:[&_li:nth-child(n+5):nth-child(-n+8)]:hidden">
{apps.map(({ label, icon: Icon }) => (
<DockItem key={label} label={label} active={active === label} onClick={() => setActive(label)}>
<Icon />
</DockItem>
))}
<DockSeparator />
<DockItem label="Trash">
<Trash2 />
</DockItem>
</Dock>
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/dock.json1. Install the dependencies.
npm install motion @radix-ui/react-slot2. Copy the source into
components/dusk/dock/dock.tsx.dock.tsx "use client"; import * as React from "react"; import { Slot } from "@radix-ui/react-slot"; import { AnimatePresence, LayoutGroup, motion, useMotionValue, useReducedMotion, useSpring, useTransform, type MotionValue, } from "motion/react"; import { cn } from "@/lib/utils"; type Position = "bottom" | "top" | "left" | "right"; type DockContextValue = { pointer: MotionValue<number>; position: Position; vertical: boolean; iconSize: number; magnification: number; distance: number; /** False on touch-only use and under reduced motion. */ magnify: boolean; }; const DockContext = React.createContext<DockContextValue | null>(null); function useDock() { const context = React.useContext(DockContext); if (!context) throw new Error("DockItem must be used inside <Dock>."); return context; } const sizeSpring = { mass: 0.1, stiffness: 170, damping: 14 }; export interface DockProps extends Omit<React.ComponentProps<"nav">, "children"> { children: React.ReactNode; /** The screen edge the dock sits on. Icons grow away from it; `left` / `right` make it vertical. */ position?: Position; /** Resting icon size, in px. */ iconSize?: number; /** Size of the icon right under the cursor, in px. */ magnification?: number; /** How far from the cursor (in px) icons start to grow. */ distance?: number; /** Turn magnification off (it's always off for touch and under reduced motion). */ disableMagnification?: boolean; } /** * A macOS-style dock. Icons swell as the cursor approaches — each one's size * comes from its distance to the pointer through motion values and springs, * so moving the mouse never re-renders React. Keyboard focus magnifies too, * labels fade in on hover and focus, and an active dot glides between items. * * The bar keeps its height: icons grow out of it, so nothing around it moves. * Static on touch screens and under reduced motion. Arrow keys move between items. */ export function Dock({ position = "bottom", iconSize = 44, magnification = 72, distance = 140, disableMagnification = false, className, style, children, "aria-label": ariaLabel = "Dock", ...props }: DockProps) { const layoutId = React.useId(); const reduceMotion = useReducedMotion() ?? false; const pointer = useMotionValue(Infinity); const vertical = position === "left" || position === "right"; const magnify = !disableMagnification && !reduceMotion; const context = React.useMemo( () => ({ pointer, position, vertical, iconSize, magnification, distance, magnify }), [pointer, position, vertical, iconSize, magnification, distance, magnify] ); const handleKeyDown = (event: React.KeyboardEvent<HTMLUListElement>) => { const prevKey = vertical ? "ArrowUp" : "ArrowLeft"; const nextKey = vertical ? "ArrowDown" : "ArrowRight"; if (![prevKey, nextKey, "Home", "End"].includes(event.key)) return; const targets = Array.from(event.currentTarget.querySelectorAll<HTMLElement>("[data-dock-target]")); const index = targets.indexOf(document.activeElement as HTMLElement); if (index === -1) return; event.preventDefault(); const next = event.key === "Home" ? 0 : event.key === "End" ? targets.length - 1 : (index + (event.key === nextKey ? 1 : -1) + targets.length) % targets.length; targets[next]?.focus(); }; const pad = 8; return ( <DockContext.Provider value={context}> <nav aria-label={ariaLabel} data-slot="dock" data-position={position} className={cn( "relative inline-flex rounded-2xl border border-white/10 bg-background/90 shadow-lg", "supports-[backdrop-filter]:bg-background/70 supports-[backdrop-filter]:backdrop-blur-md", className )} style={{ ...(vertical ? { width: iconSize + pad * 2 } : { height: iconSize + pad * 2 }), ...style }} {...props} > <LayoutGroup id={layoutId}> <ul className={cn( "flex gap-2", vertical ? "flex-col" : "flex-row", { bottom: "items-end", top: "items-start", left: "items-start", right: "items-end", }[position] )} style={{ padding: pad, ...(vertical ? { width: "100%" } : { height: "100%" }) }} onPointerMove={(event) => { if (!magnify || event.pointerType !== "mouse") return; pointer.set(vertical ? event.clientY : event.clientX); }} onPointerLeave={() => pointer.set(Infinity)} onKeyDown={handleKeyDown} > {children} </ul> </LayoutGroup> </nav> </DockContext.Provider> ); } const labelPlacement: Record<Position, string> = { bottom: "bottom-full left-1/2 mb-3 -translate-x-1/2", top: "top-full left-1/2 mt-3 -translate-x-1/2", left: "left-full top-1/2 ml-3 -translate-y-1/2", right: "right-full top-1/2 mr-3 -translate-y-1/2", }; const labelOffset: Record<Position, { x?: number; y?: number }> = { bottom: { y: 4 }, top: { y: -4 }, left: { x: -4 }, right: { x: 4 }, }; const dotPlacement: Record<Position, string> = { bottom: "-bottom-1.5 inset-x-0 mx-auto", top: "-top-1.5 inset-x-0 mx-auto", left: "-left-1.5 inset-y-0 my-auto", right: "-right-1.5 inset-y-0 my-auto", }; export interface DockItemProps extends Omit<React.ComponentProps<"button">, "children"> { /** Accessible name, shown as the hover / focus label. */ label: string; /** The icon. Sized to 45% of the item automatically. */ children: React.ReactNode; /** Render a link. */ href?: string; /** Show the active dot (and aria-current="page" on links). */ active?: boolean; /** Render your own element (e.g. Next.js `Link` wrapping the icon) instead of a button / anchor. */ asChild?: boolean; } /** An icon in the dock. Renders a `<button>`, an `<a>` with `href`, or your own element with `asChild`. */ export function DockItem({ label, href, active = false, asChild = false, className, children, onPointerEnter, onPointerLeave, onFocus, onBlur, ...props }: DockItemProps) { const { pointer, position, vertical, iconSize, magnification, distance, magnify } = useDock(); const ref = React.useRef<HTMLLIElement>(null); const [hovered, setHovered] = React.useState(false); const [focused, setFocused] = React.useState(false); const reduceMotion = useReducedMotion(); const offset = useTransform(pointer, (p) => { const el = ref.current; if (!Number.isFinite(p) || !el) return Infinity; const rect = el.getBoundingClientRect(); return p - (vertical ? rect.top + rect.height / 2 : rect.left + rect.width / 2); }); const target = useTransform(offset, [-distance, 0, distance], [iconSize, magnification, iconSize]); const size = useSpring(target, sizeSpring); const centre = () => { const rect = ref.current?.getBoundingClientRect(); if (!rect) return Infinity; return vertical ? rect.top + rect.height / 2 : rect.left + rect.width / 2; }; const Comp: React.ElementType = asChild ? Slot : href ? "a" : "button"; const showLabel = hovered || focused; return ( <motion.li ref={ref} data-slot="dock-item" className="relative shrink-0" style={{ width: magnify ? size : iconSize, height: magnify ? size : iconSize }} > <AnimatePresence> {showLabel && ( <motion.span aria-hidden initial={{ opacity: 0, scale: 0.96, ...(reduceMotion ? {} : labelOffset[position]) }} animate={{ opacity: 1, scale: 1, x: 0, y: 0 }} exit={{ opacity: 0, scale: 0.95, transition: { duration: 0.1 } }} transition={{ duration: 0.15, ease: "easeOut" }} className={cn( "pointer-events-none absolute z-10 rounded-md border border-white/10 bg-popover px-2 py-1 text-xs font-medium whitespace-nowrap text-popover-foreground", "shadow-md", labelPlacement[position] )} > {label} </motion.span> )} </AnimatePresence> <Comp data-dock-target aria-label={label} aria-current={active ? (Comp === "button" ? "true" : "page") : undefined} {...(Comp === "button" ? { type: "button" } : {})} {...(href && !asChild ? { href } : {})} className={cn( "flex size-full items-center justify-center rounded-[28%] border border-white/10 bg-white/[0.06] text-foreground outline-none", "transition-colors duration-150 hover:border-white/20 hover:bg-white/10", "focus-visible:ring-[3px] focus-visible:ring-ring/60", "[&_svg]:pointer-events-none [&_svg]:size-[45%] [&_svg]:shrink-0", active && "border-white/20 bg-white/10", className )} onPointerEnter={(event: React.PointerEvent<HTMLButtonElement>) => { onPointerEnter?.(event); if (event.pointerType === "mouse") setHovered(true); }} onPointerLeave={(event: React.PointerEvent<HTMLButtonElement>) => { onPointerLeave?.(event); setHovered(false); }} onFocus={(event: React.FocusEvent<HTMLButtonElement>) => { onFocus?.(event); if (!event.currentTarget.matches(":focus-visible")) return; setFocused(true); // Keyboard focus magnifies the item and its neighbours, as if the cursor were on it. if (magnify) pointer.set(centre()); }} onBlur={(event: React.FocusEvent<HTMLButtonElement>) => { onBlur?.(event); setFocused(false); const next = event.relatedTarget as HTMLElement | null; if (!next?.closest("[data-slot=dock]")) pointer.set(Infinity); }} {...props} > {children} </Comp> {active && ( <motion.span layoutId="dock-active-dot" aria-hidden transition={reduceMotion ? { duration: 0 } : { type: "spring", bounce: 0.1, duration: 0.3 }} className={cn( "pointer-events-none absolute size-1 rounded-full bg-primary", dotPlacement[position] )} /> )} </motion.li> ); } /** A thin divider between groups of items. */ export function DockSeparator({ className, ...props }: React.ComponentProps<"li">) { const { vertical } = useDock(); return ( <li aria-hidden data-slot="dock-separator" className={cn( "shrink-0 self-center bg-white/12", vertical ? "mx-1 h-px w-[calc(100%-0.5rem)]" : "my-1 h-[70%] w-px", className )} {...props} /> ); }
The dock doesn't position itself — wrap it in e.g. fixed inset-x-0 bottom-4 flex justify-center. Its bar keeps a fixed height; icons grow out of it, so nothing around it shifts.
Usage
import { Folder, Mail, Settings } from "lucide-react"
import { Dock, DockItem, DockSeparator } from "@/components/dusk/dock/dock"
export function AppDock() {
return (
<div className="fixed inset-x-0 bottom-4 flex justify-center">
<Dock aria-label="Applications">
<DockItem label="Finder" active>
<Folder />
</DockItem>
<DockItem label="Mail" href="/mail">
<Mail />
</DockItem>
<DockSeparator />
<DockItem label="Settings">
<Settings />
</DockItem>
</Dock>
</div>
)
}Examples
Vertical
position="left" (or right) stands the dock up: icons grow to the side, labels open the other way and Up / Down arrows move between items. Tune iconSize and magnification to fit a sidebar.
"use client";
import * as React from "react";
import { BarChart3, Bell, House, Inbox, Users } from "lucide-react";
import { Dock, DockItem } from "@/components/dusk/dock/dock";
const items = [
{ label: "Home", icon: House },
{ label: "Inbox", icon: Inbox },
{ label: "Analytics", icon: BarChart3 },
{ label: "Team", icon: Users },
{ label: "Notifications", icon: Bell },
];
export default function DockVerticalDemo() {
const [active, setActive] = React.useState("Home");
return (
<div className="flex w-full max-w-md items-center">
<Dock position="left" iconSize={40} magnification={64} aria-label="Sidebar">
{items.map(({ label, icon: Icon }) => (
<DockItem key={label} label={label} active={active === label} onClick={() => setActive(label)}>
<Icon />
</DockItem>
))}
</Dock>
</div>
);
}Links at the top
Give items an href to render links (the active one gets aria-current="page"), or use asChild with your router's Link. position="top" grows icons downward; a smaller distance makes the swell tighter.
import { AtSign, BookOpen, Code2, Rss } from "lucide-react";
import { Dock, DockItem, DockSeparator } from "@/components/dusk/dock/dock";
export default function DockLinksDemo() {
return (
<div className="flex h-40 w-full items-start justify-center pt-4">
<Dock position="top" iconSize={36} magnification={56} distance={110} aria-label="Social">
<DockItem label="Docs" href="#docs" active>
<BookOpen />
</DockItem>
<DockItem label="Source code" href="#source">
<Code2 />
</DockItem>
<DockSeparator />
<DockItem label="Blog" href="#blog">
<Rss />
</DockItem>
<DockItem label="Contact" href="#contact">
<AtSign />
</DockItem>
</Dock>
</div>
);
}API Reference
Dock renders a nav with a list; DockItem renders a button, a link (href) or your element (asChild) and accepts its props; DockSeparator draws a divider.
| Prop | Type | Default | Description |
|---|---|---|---|
| position | "bottom" | "top" | "left" | "right" | "bottom" | On Dock: the edge it sits on. Icons grow away from it; left / right are vertical. |
| iconSize | number | 44 | On Dock: resting icon size in px. |
| magnification | number | 72 | On Dock: size of the icon under the cursor in px. |
| distance | number | 140 | On Dock: how far from the cursor icons start to grow, in px. |
| disableMagnification | boolean | false | On Dock: keep icons static. |
| aria-label | string | "Dock" | On Dock: names the navigation landmark. |
| label | string | — | On DockItem: accessible name, shown as the hover / focus label. |
| active | boolean | false | On DockItem: shows the gliding dot and sets aria-current. |
| href / asChild | string / boolean | — | On DockItem: render a link or your own element. |
Accessibility
- The dock is a nav landmark containing a list; every item is a real button or link named by its label, so screen readers never depend on the icon.
- Tab reaches each item, and arrow keys (Left / Right, or Up / Down when vertical) plus Home / End move between them.
- Keyboard focus magnifies the focused icon and its neighbours and shows its label, just like hovering.
- Active items carry aria-current ("page" for links); the active dot and the floating label are decorative and aria-hidden.
- Magnification only follows a mouse — touch screens get a static, tappable dock — and it's off entirely under prefers-reduced-motion.