Before & After Slider
ProOriginal-image comparisons with a draggable or hover reveal, labels, a vertical option and a multi-pair carousel.
Installation
pnpm dlx shadcn@latest add @viewport/before-and-after-slider
First time? Add the @viewport registry to components.json. Setup takes one minute.
Usage
The code behind the preview above comes with Truffle Pro.
Props
| Prop | Type | Default |
|---|---|---|
| pairs* | BeforeAfterPair[] | — |
| layout | "list" | "carousel"A stacked series or a one-at-a-time carousel. | "list" |
| direction | "horizontal" | "vertical"The reveal moves left-to-right or top-to-bottom. | "horizontal" |
| interaction | "dragclick" | "hover"`hover` follows a mouse without a press; touch and keyboard still work. | "dragclick" |
| defaultPosition | number | 50 |
| resetOnMouseOut | boolean | false |
| dividerColor | string | "#fff" |
| dividerSize | number | 6 |
| dividerArrows | boolean | true |
| imageRatio | numberWidth divided by height of each image pair. | 1 |
| maxWidth | number | 820 |
| heading | string | — |
| background | string | — |
| className | string | — |
Accessibility
- Each comparison exposes a named keyboard slider with orientation and position.
- Arrow keys move by 2%, Shift by 10%, Home and End reach both edges.
- Carousel changes fade instead of sliding under ordinary motion and swap instantly under reduced motion.