the obvious build gives width and height a spring each, and it looks perfect, which is the trap. a spring is linear, so two axes released together from rest hold the same normalised progress however far apart their targets are.
interrupting the morph
it comes apart on interruption, and for this component interruption is not an edge case: a call arrives while the timer is still expanding. each axis carries its own velocity into the new leg, those velocities are not in the ratio of the new distances, and the island travels through shapes that belong to no state.
one scalar fixes it by construction, and an interruption moves the line rather than starting a second race.
content and the camera
the content is a handover rather than a crossfade, because two labels at half opacity stacked on each other is not a transition, it is mush. the layout follows the hardware too: a compact state keeps the camera's width empty in the middle and puts its content either side, the clock and battery slide outward as the island widens so it never sits on them, and the camera fades out while the island is busy and comes back only once it has settled.
take it with you
three steps, and the stage above runs in your own project.
install
nothing to add. it runs on react alone, 18 or newer.
copy
11 files, kept together in one folder, dynamic-island/. the styles travel with them.
"use client";import { useEffect, useRef, useState, type ReactNode } from "react";import { ISLAND } from "./geometry";import { useReducedMotion } from "./motion";import { beginMorph, boxAt, contentPhase, morphDone, stepMorph, type Box, type MorphState } from "./morph";import "./dynamic-island.css";export type IslandState =
| "idle" | "ring" | "record" | "battery" | "screen" | "call" | "timer" | "music";const IDLE: Box = { w: ISLAND.w, h: ISLAND.h, r: ISLAND.h / 2 };const EXPANDED_W = 344;const compact = (ear: number): Box => ({ ...IDLE, w: IDLE.w + 2 * ear });export const STATES: Record<IslandState, Box> = { idle: IDLE, ring: compact(58), record: compact(50), battery: compact(76), screen: { w: EXPANDED_W, h: 84, r: 40 }, call: { w: EXPANDED_W, h: 84, r: 40 }, timer: { w: EXPANDED_W, h: 84, r: 40 }, music: { w: EXPANDED_W, h: 168, r: 46 },
};export type BarMode = "full" | "compact" | "none";export const BAR: Record<IslandState, BarMode> = { idle: "full", ring: "compact", record: "compact", battery: "compact", screen: "none", call: "none", timer: "none", music: "none",
};const LEG_CEILING_MS = 1500;export default function DynamicIsland({ state, content, onTap, stiffness = 300, damping = 27, handover = 0.55,
}: { state: IslandState; // what the island shows in each state content: Record<IslandState, ReactNode>; onTap: () => void; // spring that carries the shape: stiffer is quicker, more damping settles with less wobble stiffness?: number; damping?: number; // 0 to 1: how far into the morph the old content hands over to the new handover?: number;
}) { const reduced = useReducedMotion(); const shell = useRef<HTMLButtonElement>(null); const leaving = useRef<HTMLDivElement>(null); const arriving = useRef<HTMLDivElement>(null); const cfg = useRef({ stiffness, damping, handover }); cfg.current = { stiffness, damping, handover }; const [pair, setPair] = useState<{ from: IslandState; to: IslandState }>({ from: state, to: state,
}); if (pair.to !== state) setPair({ from: pair.to, to: state }); const morph = useRef<MorphState>(beginMorph(STATES[state], STATES[state])); const frame = useRef(0); const last = useRef(0); useEffect(() => { const el = shell.current; const host = el?.parentElement; if (!el || !host) return; const write = (box: Box, t: number) => { host.style.setProperty("--iw", String(box.w)); host.style.setProperty("--ih", String(box.h)); host.style.setProperty("--ir", String(box.r)); const p = contentPhase(t, cfg.current.handover); for (const [node, fade] of [
[leaving.current, p.leaving],
[arriving.current, p.arriving],
] as const) { if (!node) continue; node.style.opacity = String(fade.opacity); node.style.transform = `scale(${fade.scale})`; node.style.filter = fade.blur > 0.01 ? `blur(${fade.blur}px)` : "none"; node.style.visibility = fade.opacity === 0 ? "hidden" : "visible";
}
}; const target = STATES[state]; if (reduced) { morph.current = beginMorph(target, target); write(target, 1); return;
} let alive = true; const at = boxAt(morph.current); morph.current = beginMorph(at, target, morph.current.t.v, morph.current.to); const finish = () => { morph.current = beginMorph(target, target); write(target, 1);
}; write(at, 0); const guard = window.setTimeout(() => { if (!alive) return; alive = false; cancelAnimationFrame(frame.current); finish();
}, LEG_CEILING_MS); last.current = performance.now(); const tick = (now: number) => { if (!alive) return; const dt = now - last.current; last.current = now; const { stiffness: k, damping: c } = cfg.current; morph.current = stepMorph(morph.current, dt, { stiffness: k, damping: c }); write(boxAt(morph.current), morph.current.t.x); if (morphDone(morph.current)) { window.clearTimeout(guard); return finish();
} frame.current = requestAnimationFrame(tick);
}; frame.current = requestAnimationFrame(tick); return () => { alive = false; window.clearTimeout(guard); cancelAnimationFrame(frame.current);
};
}, [state, reduced]); return (
<button type="button" ref={shell} className="island" onClick={onTap} aria-label={state === "idle" ? "expand the island" : `${state}, collapse the island`}
>
<div className="island-layer" ref={leaving} aria-hidden="true">
{content[pair.from]}
</div>
<div className="island-layer" ref={arriving}>
{content[pair.to]}
</div>
</button>
);
}
use
the stage above, exactly as it runs here. the comments mark what to change.