device frame.
iPhone and iPad frames drawn entirely from one CSS length, so they resize as real layout rather than as a scaled picture.
live component, try it
what to expect.
how it works.
Three labs here stand a phone in the stage, and the frame under them is 390x844 with every dimension a count of one length, --k. The tempting shortcut is to draw it once at native size and scale it, and the reason not to is not sharpness, since a static transform rasterises at its own scale and stays crisp. It is that a transform makes a coordinate space and not a layout box: the frame keeps reserving its full width, getBoundingClientRect stops agreeing with offsetWidth, and every gesture inside comes out wrong by exactly the scale factor with nothing anywhere reporting an error. The corners are the other half. The screen's radius is the shell's less the rim and the bezel, or the arcs stop being concentric and the gap around the corner opens and closes. The iPad is the same idea at 1000x710 with its own --k, and switching between them is the part to watch: the status bar and island blur out before the shell moves, width, height and every corner then share one spring curve, and the wallpaper is laid out in fractions of the screen, so it reshapes with the glass rather than being redrawn.
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 the files
5 files, kept together in one folder,
phone-frame/. The styles travel with them."use client"; import type { CSSProperties } from "react"; const BANDS = 5; export default function Bands() { return ( <div className="pf-art" aria-hidden="true"> {Array.from({ length: BANDS }, (_, i) => ( <span key={i} data-side={i % 2 === 0 ? "left" : "right"} style={{ "--i": i } as CSSProperties} /> ))} </div> ); }Use it
The stage above, exactly as it runs here. The comments mark what to change.
"use client"; import { useEffect, useRef, useState } from "react"; import { DEVICES } from "./phone-frame/geometry"; import Bands from "./phone-frame/Bands"; import PhoneFrame from "./phone-frame/PhoneFrame"; import { useReducedMotion } from "./phone-frame/motion"; const { phone, tablet } = DEVICES; const OUT_MS = 110; const ARRIVE_MS = 300; const MORPH_MS = 620; type Phase = "rest" | "out" | "morph"; export default function PhoneFrameExample({ width = 320, bezel = 8, radius = 62, }: { // px the phone is drawn at, its bezel and its corner; the ipad scales from these width?: number; bezel?: number; radius?: number; }) { const [ipad, setIpad] = useState(false); const reduced = useReducedMotion(); const [shown, setShown] = useState(ipad); const [phase, setPhase] = useState<Phase>("rest"); const [morphing, setMorphing] = useState(false); const current = useRef(ipad); useEffect(() => { if (ipad === current.current) return; current.current = ipad; if (reduced) { setShown(ipad); setPhase("rest"); setMorphing(false); return; } setPhase("out"); const timers = [ window.setTimeout(() => { setShown(ipad); setPhase("morph"); setMorphing(true); }, OUT_MS), window.setTimeout(() => setPhase("rest"), OUT_MS + ARRIVE_MS), window.setTimeout(() => setMorphing(false), OUT_MS + MORPH_MS), ]; return () => timers.forEach((t) => window.clearTimeout(t)); }, [ipad, reduced]); return ( <div className="pf-lab" data-phase={phase} data-morphing={morphing || undefined} role="button" tabIndex={0} aria-label={ipad ? "switch to the iPhone" : "switch to the iPad"} aria-pressed={ipad} onClick={() => setIpad((on) => !on)} onKeyDown={(e) => { if (e.key !== "Enter" && e.key !== " ") return; e.preventDefault(); setIpad((on) => !on); }} > <PhoneFrame kind={shown ? "tablet" : "phone"} width={shown ? tablet.width : width} fill={shown ? width / phone.width : 1} bezel={shown ? (bezel * tablet.bezel) / phone.bezel : bezel} radius={shown ? (radius * tablet.radius) / phone.radius : radius} wallpaper={<Bands />} /> </div> ); }