Start a project

// Cards & layout

020 / 112

A before/after reveal on one handle, by drag, touch or arrow keys.

Loading…

The code

All of it, in one file: the component above, restyle included. Nothing imported but React.

"use client";
import { useRef, useState } from "react";
/** CompareSlider — a before/after reveal on one handle: pointer drag, touch, and arrow keys. Swap the two layers for images. React only. */
export default function CompareSlider() {
  const [p, setP] = useState(50);
  const box = useRef<HTMLDivElement>(null);
  const at = (x: number) => { const b = box.current!.getBoundingClientRect(); setP(Math.max(0, Math.min(100, ((x - b.left) / b.width) * 100))); };
  return (
    <div ref={box} className="cs" onPointerDown={(e) => { e.currentTarget.setPointerCapture(e.pointerId); at(e.clientX); }} onPointerMove={(e) => e.buttons && at(e.clientX)}>
      <div className="cs-a">After</div>
      <div className="cs-b" style={{ clipPath: `inset(0 ${100 - p}% 0 0)` }}>Before</div>
      <button className="cs-h" role="slider" aria-label="Compare" aria-valuenow={Math.round(p)} aria-valuemin={0} aria-valuemax={100} style={{ left: `${p}%` }}
        onKeyDown={(e) => { if (e.key === "ArrowLeft") setP((v) => Math.max(0, v - 5)); if (e.key === "ArrowRight") setP((v) => Math.min(100, v + 5)); }}>↔</button>
      <style dangerouslySetInnerHTML={{ __html: `.cs{position:relative;width:min(420px,100%);aspect-ratio:16/10;border-radius:18px;overflow:hidden;touch-action:none;cursor:ew-resize;user-select:none}.cs-a,.cs-b{position:absolute;inset:0;display:grid;place-items:center;font:500 44px var(--f-display,system-ui);font-stretch:87.5%;text-transform:uppercase;letter-spacing:-.04em}.cs-a{background:linear-gradient(135deg,#ea4f16,#f6af60);color:#000}.cs-b{background:linear-gradient(135deg,#111,#2a2a2a);color:#fff}.cs-h{position:absolute;top:50%;width:42px;height:42px;margin:-21px 0 0 -21px;border-radius:50%;background:#fff;color:#000;display:grid;place-items:center;box-shadow:0 4px 18px rgba(0,0,0,.4)}` }} />
    </div>
  );
}

18 lines · 1.8K · CompareSlider.tsx

Theme tokens it reads

Set these on :root to re-skin it. Each has a fallback, so it also works with none of them defined.

VariableFallbackWhat it does
--f-displaysystem-uiDisplay face for big type (a condensed variable font reads best).

Or install it

Published to a shadcn-style registry, so one command fetches the file straight into your project.

npx shadcn@latest add https://your-domain.com/r/compare-slider.json

Placeholder domain — set NEXT_PUBLIC_SITE_URL to your deployed origin and the command updates.

// Next up