Compare

Slant / Media & motion

A before and after with a seam you drag, or move with the keys.

Demo

Before / after

Usage

@tuttn/slant
import { Compare } from '@tuttn/slant'

// Uncontrolled: the seam starts at defaultValue (50 when left out). Each side
// fills the frame; size your images to it.
<Compare aspectRatio="3 / 2"
  before={<img src="/draft.png" alt="The draft, in flat greys" className="shot" />}
  after={<img src="/render.png" alt="The render, in colour" className="shot" />} />
// .shot { display: block; width: 100%; height: 100%; object-fit: cover; }

// Controlled, with captions of your own: they name the corner chips and the
// seam's value text ("60% Draft, 40% Render").
const [seam, setSeam] = useState(60)
<Compare value={seam} onValueChange={setSeam} beforeLabel="Draft" afterLabel="Render"
  label="Draft and render" aspectRatio="16 / 9" before={draft} after={render} />

// Stacked: before on top, the seam moved with Up and Down. No captions, and a
// bigger step for the arrows (Page Up and Page Down move ten of them).
<Compare orientation="vertical" captions={false} step={5} before={draft} after={render} />

// Frozen: the seam stays put and leaves the tab order.
<Compare disabled defaultValue={30} before={draft} after={render} />

Props

PropTypeDefaultDescription
beforeReactNode—The before side: an image, or any element that fills the frame.
afterReactNode—The after side, drawn under the before side and revealed past the seam.
beforeLabelstringmessages.beforeThe before side's caption, also read in the seam's value text.
afterLabelstringmessages.afterThe after side's caption, also read in the seam's value text.
valuenumber—How much of the frame shows the before side, 0–100, from the inline start (or the top when vertical). Pass it with onValueChange to control the seam.
defaultValuenumber50Where the seam starts, when uncontrolled.
onValueChange(value: number) => void—Called with the new position as the seam is dragged or moved with the keys.
labelstringmessages.compareThe seam's accessible name.
stepnumber1How far one arrow key moves the seam, in percent. Page Up and Page Down move ten steps.
orientation'horizontal' | 'vertical''horizontal'Horizontal puts before at the inline start and moves the seam across; vertical puts before on top.
captionsbooleantrueShows the before and after captions in the corners.
aspectRatiostring—The frame's aspect ratio, as CSS `aspect-ratio` ("16 / 9"). Leave it out and the after side sets the height.
disabledbooleanfalseFreezes the seam: it leaves the tab order and ignores the pointer.
…restHTMLAttributes<HTMLDivElement>—className, style, other attributes and the ref go on the frame's root div, which sets --sl-compare-value (the seam's position, in percent) and aspect-ratio in its style. Your own pointer and mouse handlers run first; one that calls preventDefault on pointerdown keeps the seam from moving.

Keyboard

← / →moves the seam a step the way the arrow points, under right-to-left too (horizontal)
↑ / ↓grows or shrinks the before side a step (horizontal), or moves the seam up or down (vertical)
Page Up / Page Downmoves ten steps, the way ↑ and ↓ do
Home / Endshows all of the after side, or all of the before side

Accessibility

  • The seam's grip is a focusable slider (aria-valuemin 0, aria-valuemax 100, aria-orientation), named by label ("Before and after" by default). Its aria-valuetext says how much of each side shows: "60% before, 40% after", or with your captions "60% Draft, 40% Render".
  • Both sides stay in the accessibility tree, before then after, so give each image its own alt. The corner captions are hidden from assistive technology, since the value text names both sides.
  • A press anywhere on the frame moves the seam there and puts focus on the grip, so the keys carry on from where the pointer left it. A press on the grip drags it from where it is.
  • Disabled, the grip leaves the tab order with aria-disabled, and neither the keys nor the pointer move it.

The after side fills the frame and, without aspectRatio, sets its height; the before side lies over it, clipped at the seam with clip-path. Horizontally the before side keeps the inline start, so under <Slant dir="rtl"> it sits on the right and the seam measures from there; the pictures themselves never mirror. Each caption lives in its own side, so the seam covers it as it covers the side. Both sides are clipped to the frame's radius, but the frame clips nothing else, so the grip and its focus ring can overhang an edge.

The position is a CSS variable set inline, so the server's first frame matches the browser's, and nothing animates: the seam follows the pointer and the keys directly. On a touch screen a vertical swipe still scrolls the page (touch-action: pan-y, pan-x when vertical), and a sideways one moves the seam.

Full source