Compare
A before/after slider. Two images share one box, and a handle wipes between them. Drag it, press anywhere, or use the arrow keys.
Compare stacks two layers in one box: before shows from the start edge up to a line, and after past it. Dragging the line, or pressing anywhere on the image, moves it. Use it for a restoration, an edit, a redesign: any two images that line up.
import { Compare } from '@42-central/ui-react/compare';
import { Image } from '@42-central/ui-react/image';
<Compare
ratio="3/2"
before={<Image src={faded} alt="A faded sepia print of the peaks, before restoration" />}
after={<Image src={restored} alt="The same peaks after restoration, in full color" />}
/>Playground
Component
Dir
Presets
Props
Code
<Component
defaultValue={50}
step={5}
ratio="3/2"
/>Full prop browser
Controlled
value and onChange hold the position in your state, from 0 to 100: how much of before shows, which is the line's position from the start edge. onChange fires on every change, while dragging too. Without value, the slider keeps its own, starting at defaultValue (50).
25% black and white
Code
const [value, setValue] = useState(25);
<Compare value={value} onChange={setValue} ratio="16/9" before={…} after={…} />
<Text>{Math.round(value)}% black and white</Text>
{[0, 25, 50, 75, 100].map((preset) => (
<Button key={preset} onClick={() => setValue(preset)}>{preset}%</Button>
))}A drag moves the line continuously, so value can be fractional (33.4). Round it for display.
Keyboard step
The arrow keys move the line by step percent, 5 by default. A keyboard step eases into place over a short transition; a drag never does, so the line stays under the pointer. Home and End jump to the ends, and Page Up and Page Down take the browser's larger step.
Code
<Compare step={10} defaultValue={20} before={…} after={…} />Ratio and radius
ratio fixes the box's shape, and both images fill it, cropped with object-fit: cover. Without a ratio, the box takes the height of before, so give that image a size or a ratio of its own. radius rounds the corners on Image's scale, xs by default; none squares them.
Code
<Compare ratio="1/1" radius="lg" before={…} after={…} />
<Compare ratio="4/3" radius="none" before={…} after={…} />The box owns the corners: it clips both layers to its radius and squares the images inside, so their own radius doesn't show. Leave them at their default.
Room for the grip
The grip stays centred on the line at every value, so at 0% and 100% it hangs half past the image's edge, about 1.4rem with its focus ring. Compare doesn't clip it. In a full-bleed layout, where the image runs to the edge of the screen, give its container that much inline padding, or the overhang is cut off by the page (or scrolls it sideways) at the ends.
In a figure
A caption goes in a Figure, as for any media. Give the figure a width: it hugs its media, and the comparison is fluid.
Code
<Figure className="w-full max-w-120" caption="…">
<Compare ratio="3/2" before={…} after={…} />
</Figure>Right-to-left
The direction comes from the locale, as for the kit's other interactive parts. Under an RTL locale, before shows from the right edge, the line moves toward the left as the value grows, and ← moves it that way. The value text uses the locale's digits.
Code
<IntlProvider locale="ar-EG">
<div dir="rtl" lang="ar">
<Compare
ratio="16/9"
defaultValue={30}
translations={{
slider: 'مقارنة قبل وبعد',
valueText: (percent) => `${percent} بالأبيض والأسود`,
}}
before={<Image src={grayscale} alt="بحيرة جبلية عند الفجر، بالأبيض والأسود" />}
after={<Image src={color} alt="البحيرة نفسها بالألوان" />}
/>
</div>
</IntlProvider>Compare stamps the locale's dir on its root, so the wipe, the native slider and the arrow keys always agree. <html dir="rtl"> alone leaves it left to right: see Text direction.
API
Prop
Type
Every other <div> attribute is passed to the root. No asChild: the root renders both layers, the slider and the handle.
Accessibility
-
A native slider. The control is an
<input type="range">laid over the whole image at zero opacity. Screen readers announce it as a slider, named bytranslations.slider, witharia-valuetextfromtranslations.valueText("30% before"). -
Keyboard.
Key Action Tab Focuses the slider. The ring goes around the grip. → ↑ Shows more of before, bystep. ← under RTL.← ↓ Shows more of after, bystep. → under RTL.Home / End All after/ allbefore.Page Up / Page Down The browser's larger step. -
Pointer and touch. Press anywhere on the image to move the line there, then drag. On touch, a horizontal drag moves the line and a vertical one still scrolls the page. The whole image is the hit area.
-
Visible focus. The kit's focus ring goes around the grip, which stays centred on the line. At the ends the grip and its ring hang half past the image's edge; nothing in Compare clips them, so the ring is never cut. Leave room for the grip in full-bleed layouts.
-
Name the images. Each image keeps its own
alt, so say which side is which in it: "The fresco before restoration", "The fresco after restoration". The slider's name says what is being compared; the alts say what each layer shows. -
Reduced motion. A keyboard step eases over 150ms; under
prefers-reduced-motionit jumps. A drag never animates. -
Forced colors. The line and the grip switch to system colors in Windows High Contrast, and the grip gets the system focus outline.
-
Right-to-left. Everything is placed with logical properties, and the direction comes from the locale. See Right-to-left.
Internationalization
translations holds the two strings the slider announces. Both are optional; a partial override keeps the other English default.
| Key | Default | |
|---|---|---|
slider | "Before and after comparison" | The slider's accessible name. |
valueText | (percent) => `${percent} before` | Its announced value. percent is already formatted for the locale: "30%", "30 %" in French, "٣٠٪" in Arabic. |
<Compare
translations={{
slider: t('compare.label'), // "Avant et après restauration"
valueText: (percent) => t('compare.value', { percent }), // "30 % avant"
}}
before={<Image src={original} alt={t('fresco.before')} />}
after={<Image src={restored} alt={t('fresco.after')} />}
/>The percentage is formatted with Intl.NumberFormat in the locale from IntlProvider. The images' alt text is your content: translate it with the rest of the page.
Changelog
Carousel
A row of slides that scrolls a page at a time, with buttons, page dots and an optional autoplay. Built on native scroll snap, so touch, trackpads and right-to-left work as the browser does.
ImageCropper
Pick the part of an image to keep. Drag and resize the selection, zoom, rotate and flip the image, then export the crop as a Blob or as coordinates for your server.