labs

device frame

2026-09-02
320 px
8 px
62 px

one unit, real layout

four labs on this page 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.

corners that stay concentric

the corners are the second part: the screen's corner 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.

changing the device

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.

the frame this site uses

it moved out of the labs and into src/components when /projects started standing seven recordings in it, so the frame on this page is the one the site ships rather than one written to be looked at.

take it with you

three steps, and the stage above runs in your own project.

  1. install

    nothing to add. it runs on react alone, 18 or newer.

  2. copy

    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>
      );
    }
    
  3. use

    the stage above, exactly as it runs here. the comments mark what to change.

    example.tsx
    "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,
      ipad = false,
    }: {
      // these four are the sliders and the switch above the stage
      width?: number;
      bezel?: number;
      radius?: number;
      ipad?: boolean;
    }) {
      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}>
          <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>
      );
    }