Device frames

CSS-only frames for presenting screenshots or live content: a browser window (three dots, back and forward, an address bar, optional tabs), a phone with side buttons and a pill cutout, a tablet, a laptop with its base, and a watch with crown and band. Each frame scales from one width value and keeps its proportions, and children render inside the screen with overflow hidden. Frames are drawn from your theme colours by default, with optional graphite, silver or sand finishes, light or dark chrome, three shadow depths and a diagonal screen reflection. A crop option cuts the device across the screen to show only the top or bottom for tight hero layouts. No images are needed, and frames are presentational so the content keeps its own semantics.

MarketingComponentTailwind v4

Source

import { useEffect, useRef, useState, type CSSProperties, type ReactNode } from "react";
import { ChevronLeft, ChevronRight, Lock, RotateCw } from "lucide-react";
import { cn } from "@/lib/utils";

export type Device = "browser" | "phone" | "tablet" | "laptop" | "watch";

export type DeviceFrameProps = {
  device: Device;
  /** Total width (number = px, or any CSS length such as "100%"); the frame keeps its proportions. */
  width?: number | string;
  maxWidth?: number | string;
  finish?: "graphite" | "silver" | "sand";
  /** Cut the device at the screen and show only its upper or lower part. */
  crop?: "top" | "bottom";
  url?: string;
  tabs?: string[];
  theme?: "auto" | "light" | "dark";
  shadow?: "none" | "soft" | "lifted";
  glare?: boolean;
  children?: ReactNode;
  className?: string;
};

const DEFAULT_WIDTH: Record<Device, number> = { browser: 640, phone: 280, tablet: 420, laptop: 640, watch: 180 };

/* Chrome colours. "auto" follows the theme tokens; light and dark pin the chrome regardless of the page. */
const THEMES = {
  auto: { body: "var(--card)", edge: "var(--border)", ink: "var(--muted-foreground)", pill: "var(--muted)", dot: "color-mix(in oklab, var(--foreground) 16%, transparent)" },
  light: { body: "oklch(0.985 0 0)", edge: "oklch(0.89 0 0)", ink: "oklch(0.5 0 0)", pill: "oklch(0.94 0 0)", dot: "oklch(0.85 0 0)" },
  dark: { body: "oklch(0.23 0 0)", edge: "oklch(0.33 0 0)", ink: "oklch(0.72 0 0)", pill: "oklch(0.3 0 0)", dot: "oklch(0.4 0 0)" },
} as const;

const FINISHES = {
  graphite: "linear-gradient(145deg, oklch(0.38 0.005 260), oklch(0.22 0.005 260) 50%, oklch(0.32 0.005 260))",
  silver: "linear-gradient(145deg, oklch(0.96 0.003 250), oklch(0.84 0.006 250) 50%, oklch(0.93 0.003 250))",
  sand: "linear-gradient(145deg, oklch(0.94 0.025 80), oklch(0.82 0.04 75) 50%, oklch(0.91 0.03 80))",
} as const;

const SHADOWS = {
  none: "none",
  soft: "0 1px 2px rgb(0 0 0 / 0.06), 0 10px 28px -10px rgb(0 0 0 / 0.18)",
  lifted: "0 2px 4px rgb(0 0 0 / 0.06), 0 44px 70px -28px rgb(0 0 0 / 0.45)",
} as const;

const BEZEL = "oklch(0.16 0 0)";

const css = (value: number | string) => (typeof value === "number" ? `${value}px` : value);

function Glare() {
  return (
    <div
      aria-hidden="true"
      className="pointer-events-none absolute inset-0 z-20"
      style={{ background: "linear-gradient(118deg, rgb(255 255 255 / 0.16), rgb(255 255 255 / 0.04) 38%, transparent 38.2%)" }}
    />
  );
}

function Screen({ children, glare, className, style }: { children?: ReactNode; glare?: boolean; className?: string; style?: CSSProperties }) {
  return (
    <div className={cn("relative isolate overflow-hidden bg-background text-foreground", className)} style={style}>
      {children}
      {glare && <Glare />}
    </div>
  );
}

const body = { background: "var(--df-fill)", border: "1px solid var(--df-edge)" } as CSSProperties;

function Browser({ url, tabs, glare, children }: Pick<DeviceFrameProps, "url" | "tabs" | "glare" | "children">) {
  return (
    <div className="overflow-hidden rounded-xl" style={{ ...body, boxShadow: "var(--df-shadow)" }}>
      {tabs && tabs.length > 0 && (
        <div className="flex items-end gap-1 px-3 pt-2" style={{ color: "var(--df-ink)" }}>
          <span className="mr-2 flex gap-1.5 self-center pb-1">
            {[0, 1, 2].map((dot) => <span key={dot} className="size-2.5 rounded-full" style={{ background: "var(--df-dot)" }} />)}
          </span>
          {tabs.map((tab, index) => (
            <span
              key={tab + index}
              className={cn("max-w-[10rem] truncate rounded-t-lg px-3 py-1.5 text-[11px]", index === 0 && "font-medium")}
              style={index === 0 ? { background: "var(--df-pill)", color: "var(--df-ink)" } : undefined}
            >
              {tab}
            </span>
          ))}
        </div>
      )}
      <div className="flex h-10 items-center gap-2 px-3" style={{ color: "var(--df-ink)", background: tabs?.length ? "var(--df-pill)" : undefined }}>
        {!tabs?.length && (
          <span className="flex gap-1.5">
            {[0, 1, 2].map((dot) => <span key={dot} className="size-2.5 rounded-full" style={{ background: "var(--df-dot)" }} />)}
          </span>
        )}
        <span className="ml-1 hidden items-center gap-0.5 min-[420px]:flex">
          <ChevronLeft aria-hidden="true" className="size-4" />
          <ChevronRight aria-hidden="true" className="size-4 opacity-50" />
        </span>
        <span
          className="mx-auto flex h-6 min-w-0 max-w-[60%] flex-1 items-center justify-center gap-1.5 rounded-md px-3 text-[11px]"
          style={{ background: tabs?.length ? "var(--df-fill)" : "var(--df-pill)" }}
        >
          <Lock aria-hidden="true" className="size-3 shrink-0" />
          <span className="truncate">{url ?? "example.app"}</span>
        </span>
        <RotateCw aria-hidden="true" className="hidden size-3.5 min-[420px]:block" />
      </div>
      <Screen glare={glare} className="aspect-[16/10] border-t" style={{ borderColor: "var(--df-edge)" }}>
        {children}
      </Screen>
    </div>
  );
}

function SideButton({ side, top, height }: { side: "left" | "right"; top: string; height: string }) {
  return (
    <span
      aria-hidden="true"
      className="absolute w-[1.3cqw] rounded-sm"
      style={{ ...body, top, height, [side]: "-0.9cqw" }}
    />
  );
}

function Phone({ glare, children }: Pick<DeviceFrameProps, "glare" | "children">) {
  return (
    <div className="relative px-[0.9cqw]">
      <SideButton side="left" top="19%" height="5%" />
      <SideButton side="left" top="27%" height="9%" />
      <SideButton side="left" top="38%" height="9%" />
      <SideButton side="right" top="29%" height="14%" />
      <div className="relative aspect-[9/19.3] rounded-[15cqw] p-[1.8cqw]" style={{ ...body, boxShadow: "var(--df-shadow)" }}>
        <div className="size-full rounded-[13.4cqw] p-[2.6cqw]" style={{ background: BEZEL }}>
          <Screen glare={glare} className="size-full rounded-[11cqw]">
            <span aria-hidden="true" className="absolute left-1/2 top-[2.4cqw] z-30 h-[7.5cqw] w-[28cqw] -translate-x-1/2 rounded-full" style={{ background: BEZEL }} />
            {children}
          </Screen>
        </div>
      </div>
    </div>
  );
}

function Tablet({ glare, children }: Pick<DeviceFrameProps, "glare" | "children">) {
  return (
    <div className="relative aspect-[3/4.1] rounded-[6cqw] p-[1.3cqw]" style={{ ...body, boxShadow: "var(--df-shadow)" }}>
      <div className="relative size-full rounded-[4.8cqw] p-[3.4cqw]" style={{ background: BEZEL }}>
        <span aria-hidden="true" className="absolute left-1/2 top-[1.3cqw] size-[1cqw] -translate-x-1/2 rounded-full bg-white/15" />
        <Screen glare={glare} className="size-full rounded-[2cqw]">{children}</Screen>
      </div>
    </div>
  );
}

function Laptop({ glare, children }: Pick<DeviceFrameProps, "glare" | "children">) {
  return (
    <div>
      <div className="mx-auto w-[82%] rounded-t-[2.6cqw] p-[0.7cqw] pb-0" style={{ ...body, borderBottom: 0, boxShadow: "var(--df-shadow)" }}>
        <div className="relative rounded-t-[2cqw] px-[1.4cqw] pb-[1.6cqw] pt-[2cqw]" style={{ background: BEZEL }}>
          <span aria-hidden="true" className="absolute left-1/2 top-[0.7cqw] size-[0.6cqw] -translate-x-1/2 rounded-full bg-white/20" />
          <Screen glare={glare} className="aspect-[16/10] rounded-[0.4cqw]">{children}</Screen>
        </div>
      </div>
      <div className="relative h-[2.4cqw] rounded-b-[1.8cqw] rounded-t-[0.3cqw]" style={{ ...body, boxShadow: "var(--df-shadow)" }}>
        <span aria-hidden="true" className="absolute left-1/2 top-0 h-[0.8cqw] w-[14cqw] -translate-x-1/2 rounded-b-[0.8cqw]" style={{ background: "var(--df-edge)" }} />
        <span aria-hidden="true" className="absolute inset-x-[1%] bottom-0 h-px" style={{ background: "var(--df-edge)" }} />
      </div>
    </div>
  );
}

function Watch({ glare, children }: Pick<DeviceFrameProps, "glare" | "children">) {
  const band = { background: "color-mix(in oklab, var(--df-edge) 70%, var(--df-body))", border: "1px solid var(--df-edge)" } as CSSProperties;
  return (
    <div className="flex flex-col items-center pr-[3cqw]">
      <div className="h-[22cqw] w-[60%] rounded-t-[10cqw]" style={{ ...band, borderBottom: 0 }} />
      <div className="relative w-[88%]">
        <span aria-hidden="true" className="absolute -right-[3.2cqw] top-[24%] h-[16%] w-[4cqw] rounded-[1.2cqw]" style={body} />
        <span aria-hidden="true" className="absolute -right-[1.6cqw] top-[52%] h-[16%] w-[2cqw] rounded-[0.8cqw]" style={body} />
        <div className="relative aspect-[1/1.18] rounded-[26cqw] p-[2.4cqw]" style={{ ...body, boxShadow: "var(--df-shadow)" }}>
          <div className="size-full rounded-[23cqw] p-[4cqw]" style={{ background: BEZEL }}>
            <Screen glare={glare} className="size-full rounded-[17cqw]">{children}</Screen>
          </div>
        </div>
      </div>
      <div className="h-[22cqw] w-[60%] rounded-b-[10cqw]" style={{ ...band, borderTop: 0 }} />
    </div>
  );
}

export function DeviceFrame({
  device,
  width,
  maxWidth = "100%",
  finish,
  crop,
  url,
  tabs,
  theme = "auto",
  shadow = "soft",
  glare = false,
  children,
  className,
}: DeviceFrameProps) {
  const frameRef = useRef<HTMLDivElement>(null);
  const [height, setHeight] = useState<number | null>(null);

  useEffect(() => {
    const frame = frameRef.current;
    if (!crop || !frame) return;
    const measure = () => setHeight(frame.offsetHeight);
    measure();
    const observer = new ResizeObserver(measure);
    observer.observe(frame);
    return () => observer.disconnect();
  }, [crop]);

  const colors = THEMES[theme];
  const vars = {
    "--df-body": colors.body,
    "--df-fill": finish ? FINISHES[finish] : colors.body,
    "--df-edge": finish === "graphite" ? "oklch(0.42 0.005 260)" : finish ? "color-mix(in oklab, black 14%, transparent)" : colors.edge,
    "--df-ink": colors.ink,
    "--df-pill": colors.pill,
    "--df-dot": colors.dot,
    "--df-shadow": SHADOWS[shadow],
    width: css(width ?? DEFAULT_WIDTH[device]),
    maxWidth: css(maxWidth),
    containerType: "inline-size",
  } as CSSProperties;

  const frame = (() => {
    switch (device) {
      case "browser": return <Browser url={url} tabs={tabs} glare={glare}>{children}</Browser>;
      case "phone": return <Phone glare={glare}>{children}</Phone>;
      case "tablet": return <Tablet glare={glare}>{children}</Tablet>;
      case "laptop": return <Laptop glare={glare}>{children}</Laptop>;
      case "watch": return <Watch glare={glare}>{children}</Watch>;
    }
  })();

  // Cropping keeps a little more than half so the cut lands inside the screen; a short fade softens the edge.
  const cropHeight = crop && height ? Math.round(height * 0.62) : undefined;
  const fade = crop === "top" ? "linear-gradient(to bottom, #000 86%, transparent)" : "linear-gradient(to top, #000 86%, transparent)";

  return (
    <div role="presentation" className={cn("relative shrink-0", className)} style={vars}>
      <div
        className={cn(crop && "relative overflow-hidden", crop && "px-[2cqw]")}
        style={crop ? { height: cropHeight, marginInline: "-2cqw", maskImage: fade, WebkitMaskImage: fade } : undefined}
      >
        <div ref={frameRef} className={cn(crop === "bottom" && cropHeight && "absolute inset-x-[2cqw] bottom-0")}>
          {frame}
        </div>
      </div>
    </div>
  );
}