Comparison Lens.
Two aligned layers with a draggable divider. The divider is a native range input, so keyboard, touch, and screen readers work without extra code.
Add className="my-comparison-lens" to your component.
Make a change to create your CSS.
Installation
In a project with React and shadcn configured, run:
npx shadcn@latest add https://aduneer.github.io/opalframe/r/comparison-lens.jsonCopies the component and its stylesheet into components/ui. No additional runtime dependencies. Tailwind classes work through className.
For manual installation, copy both files from Source below. Keep the CSS import alongside the component.
Usage
import { ComparisonLens } from "@/components/ui/comparison-lens";
export default function Example() {
return (
<ComparisonLens
before={<div style={{ padding: 32 }}>Original layout</div>}
after={<div style={{ padding: 32 }}>Refined layout</div>}
beforeLabel="Original"
afterLabel="Refined"
defaultValue={58}
/>
);
}Customize
Colors and sizes are scoped CSS variables. Override them on your own class.
<ComparisonLens
before={<Original />}
after={<Refined />}
className="my-comparison"
style={{ aspectRatio: "4 / 3" }}
/>
.my-comparison { --lens-accent: #a6cfff; }Props
| Prop | Type | What it does |
|---|---|---|
before / after | ReactNode | Visual layers occupying the same coordinate system. |
beforeLabel / afterLabel | string | Visible labels and accessible comparison descriptions. |
value / defaultValue | number (0–100) | Percentage of the after layer revealed. |
onValueChange | (value: number) => void | Called when the range changes. |
className / style | string / CSSProperties | Root customization and aspect ratio. |
Accessibility & motion
The full canvas is a native range input: use arrows, Home, End, touch, or drag. Screen readers receive descriptive values. Layers are visual-only and must not contain interactive controls; provide an adjacent text explanation for meaningful differences. No animation is necessary.
Dependencies
React only. Styles live in a plain CSS file next to the component, and Tailwind utilities work through className.
Source
comparison-lens.tsx
"use client";
import { useId, useState, type ReactNode, type CSSProperties } from "react";
import "./comparison-lens.css";
export interface ComparisonLensProps {
before: ReactNode;
after: ReactNode;
beforeLabel?: string;
afterLabel?: string;
value?: number;
defaultValue?: number;
onValueChange?: (value: number) => void;
className?: string;
style?: CSSProperties;
}
/** Both layers share one coordinate system. The native range handles touch and keyboard input. */
export function ComparisonLens({
before,
after,
beforeLabel = "Before",
afterLabel = "After",
value,
defaultValue = 50,
onValueChange,
className = "",
style,
}: ComparisonLensProps) {
const uid = useId();
const [internalValue, setInternalValue] = useState(defaultValue);
const position = Math.min(100, Math.max(0, value ?? internalValue));
return (
<div
className={`is-lens ${className}`}
style={{ ...style, "--lens-position": `${position}%` } as CSSProperties}
>
<div className="is-lens-layer" aria-hidden="true">
{before}
</div>
<div className="is-lens-layer is-lens-after" aria-hidden="true">
{after}
</div>
<span className="is-lens-tag is-lens-before-tag" aria-hidden="true">
{beforeLabel}
</span>
<span className="is-lens-tag is-lens-after-tag" aria-hidden="true">
{afterLabel}
</span>
<div className="is-lens-divider" aria-hidden="true">
<span>
<svg viewBox="0 0 24 24" width="20" height="20" fill="none">
<path
d="m8 8-4 4 4 4m8-8 4 4-4 4"
stroke="currentColor"
strokeWidth="1.6"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</span>
</div>
<label className="is-lens-sr" htmlFor={uid}>
Compare {beforeLabel} and {afterLabel}
</label>
<input
id={uid}
className="is-lens-input"
type="range"
min={0}
max={100}
step={1}
value={position}
aria-valuetext={`${position}% ${afterLabel}, ${100 - position}% ${beforeLabel}`}
onChange={(event) => {
const next = Number(event.target.value);
if (value === undefined) setInternalValue(next);
onValueChange?.(next);
}}
/>
</div>
);
}
comparison-lens.css
.is-lens {
--lens-accent: var(--accent, #aacfff);
position: relative;
isolation: isolate;
aspect-ratio: 16 / 10;
overflow: hidden;
border-radius: 8px;
border: 1px solid var(--line, #343439);
background: #1b1b20;
}
.is-lens-layer {
isolation: isolate;
z-index: 0;
position: absolute;
inset: 0;
pointer-events: none;
}
.is-lens-after {
z-index: 1;
clip-path: inset(0 calc(100% - var(--lens-position)) 0 0);
}
.is-lens-tag {
position: absolute;
top: 18px;
padding: 6px 9px;
border-radius: 3px;
background: #15151a;
color: #f4f4f5;
font:
9px ui-monospace,
monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
z-index: 2;
pointer-events: none;
}
.is-lens-before-tag {
right: 18px;
}
.is-lens-after-tag {
left: 18px;
}
.is-lens-divider {
position: absolute;
top: 0;
bottom: 0;
left: var(--lens-position);
width: 1px;
background: var(--lens-accent);
z-index: 3;
pointer-events: none;
}
.is-lens-divider span {
position: absolute;
top: 50%;
left: 0;
transform: translate(-50%, -50%);
width: 40px;
height: 40px;
border-radius: 50%;
background: var(--lens-handle, #f7faff);
color: var(--lens-handle-ink, #304b68);
border: 1px solid #a5b8cf;
display: grid;
place-items: center;
box-shadow:
0 2px 8px #223f5a24,
inset 0 1px 0 #fff;
}
.is-lens-input {
position: absolute;
inset: 0;
margin: 0;
width: 100%;
height: 100%;
opacity: 0;
z-index: 4;
cursor: ew-resize;
touch-action: pan-y;
appearance: none;
background: transparent;
}
.is-lens-input::-webkit-slider-thumb {
appearance: none;
width: 0;
height: 100%;
}
.is-lens-input::-moz-range-thumb {
border: none;
width: 0;
height: 100%;
}
.is-lens:focus-within {
outline: 2px solid var(--lens-accent);
outline-offset: 4px;
}
.is-lens-sr {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}