VisorVisor
ComponentsData Display

Before After Compare

A before/after capture pair in four modes (Side by Side, Swipe, Flip and a pixel Diff), with a shared crosshair, a synced 2.5× loupe, click-to-mark pins and phones drawn at their true size in a bezel. Copy it into your project and own it completely.

Desktop Pair

1280 × 800 @1x
Before
Meter Docs, before
After
Meter Docs, after

Phone Pair

A capture narrower than 600 CSS pixels renders as a phone: at its capture's CSS width, never scaled up, in a bezel whose screen corners are concentric with the frame.

390 × 844 @2x
Before
Meter Docs, Phone, before
After
Meter Docs, Phone, after

When to Use

Use BeforeAfterCompare for any review of a change between two captures of the same screen: an approval inbox, a visual regression report, a PR comment. Use FidelityMirror instead when the comparison is a design against its build, with live routes, a verdict and classed deltas.

Modes

ModeKeyWhat it does
Side by Side1Before and after next to each other, top-aligned when their heights differ. Stacks under 768px.
Swipe2After over before, clipped at a divider. The divider is a role="slider": drag it, or focus it and use the arrow keys (Shift for 10%, Home and End for the ends).
Flip3One image at a time. Click or press Space to swap them instantly; both stay mounted so nothing fades.
Diff4The after capture greyed, every changed pixel painted in --text-warning, changed regions boxed and a stat line (regions, share of pixels, height difference). The diff runs in the browser on a canvas, so the captures must be same-origin or served with CORS. The engine is lib/capture-diff.ts, shared with FidelityMirror's onDiff.

Z toggles the loupe. Keys act while focus is inside the component; clicking an image focuses it.

Crosshair and Loupe

Moving the pointer over either image draws a faint crosshair (white at 18%, difference blend) on both at the same spot. With the loupe on, each image shows a 2.5× lens of its own capture at that spot, so the two lenses compare the same pixels.

Marks

In Side by Side and Diff, a click drops a numbered pin at the same spot on both images. Clicking a pin removes it. Each mark carries its position as 0–1 of the image (x, y) and in the capture's CSS pixels (px), worked out from viewport.width. markLine(mark, label) writes the line a session receives: 1. Meter Docs at x 640, y 212.

Marks are uncontrolled by default (defaultMarks); pass marks and onMarksChange to keep them, for example as chips on a review note.

Installation

npx visor add before-after-compare

Usage

import { BeforeAfterCompare, markLine, type CompareMark } from '@/components/ui/before-after-compare/before-after-compare';

export function Review() {
  const [marks, setMarks] = React.useState<CompareMark[]>([]);
  const pair = {
    label: 'Settings',
    before: '/captures/settings-before.webp',
    after: '/captures/settings-after.webp',
    viewport: { width: 1440, height: 900, dpr: 2 },
  };
  return (
    <>
      <BeforeAfterCompare pair={pair} marks={marks} onMarksChange={setMarks} />
      <textarea defaultValue={marks.map((m) => markLine(m, pair.label)).join('\n')} />
    </>
  );
}

API Reference

PropTypeDefaultDescription
pair{ before: string; after: string; label: string; viewport: { width: number; height: number; dpr?: number } }—The two capture URLs, the pair's name (alt text and mark lines) and the viewport they were taken at. A width under 600 renders as a phone at that CSS width. Required.
mode'side' | 'swipe' | 'flip' | 'diff'—Controlled mode.
defaultMode'side' | 'swipe' | 'flip' | 'diff''side'Uncontrolled initial mode.
onModeChange(mode: CompareMode) => void—Fires when the mode changes, from the control or keys 1–4.
marks{ n: number; x: number; y: number; px: { x: number; y: number } }[]—Controlled marks. x and y are 0–1 of the image; px is in the capture's CSS pixels.
defaultMarksCompareMark[][]Uncontrolled initial marks.
onMarksChange(marks: CompareMark[]) => void—Fires when a click adds a mark or removes one.
defaultLoupebooleanfalseWhether the loupe starts on. Z toggles it.
...propsReact.ComponentProps<'section'>—Standard section attributes are forwarded to the root.