Data displayBeforeAfter
Searchastronomic.dev# BeforeAfter
To show a job's before and after photos: drag or use the arrow keys to compare. With no script, the two photos sit side by side.
Interactive: inline script.
## Usage
```astro
import BeforeAfter from "@astronomic/ui-pro/components/BeforeAfter.astro";
```
## Examples
### Drag to compare
A kitchen remodel by Sixpenny Builders. Drag across the photo, or Tab to it and use the arrow keys (Home and End reach the ends). "All before" and "All after" show one photo in full, gliding there where motion is allowed.
```astro
---
import BeforeAfter from "@astronomic/ui-pro/components/BeforeAfter.astro";
import { construction } from "../_data/groundcrew.ts";
const job = construction.beforeAfter;
const photos = job.photos!;
const lower = (text: string) => text.charAt(0).toLowerCase() + text.slice(1);
---
<!-- Drag across the photo, or Tab to it and use the arrow keys; the two buttons show one photo in full. -->
<BeforeAfter
class="mx-auto w-full max-w-3xl"
title={job.title}
before={{ ...photos.before, alt: `Before: ${lower(photos.before.alt)}` }}
after={{ ...photos.after, alt: `After: ${lower(photos.after.alt)}` }}
imageSizes="(min-width: 48rem) 48rem, 100vw"
/>
```
### Beside the job's story
Tilth & Stone's front yard next to its words, as on a service page. focalPoint keeps the house in view as the stage crops both photos.
```astro
---
import BeforeAfter from "@astronomic/ui-pro/components/BeforeAfter.astro";
import { landscaping } from "../_data/groundcrew.ts";
const job = landscaping.beforeAfter;
const photos = job.photos!;
const lower = (text: string) => text.charAt(0).toLowerCase() + text.slice(1);
---
<!-- A job beside its story, as on a service page: the words on one side, the comparison on the other. -->
<div class="mx-auto grid w-full max-w-5xl items-center gap-group md:grid-cols-[minmax(0,2fr)_minmax(0,3fr)]">
<div class="grid gap-item">
<h2 class="m-0 text-subhead text-balance">{job.title}</h2>
<p class="m-0 text-body text-pretty text-muted">{job.before} {job.after}</p>
<p class="m-0 text-small text-muted">{job.town}</p>
</div>
<BeforeAfter
title={job.title}
before={{ ...photos.before, alt: `Before: ${lower(photos.before.alt)}` }}
after={{ ...photos.after, alt: `After: ${lower(photos.after.alt)}` }}
focalPoint="50% 60%"
/>
</div>
```
### Starting at 80
defaultValue={80} starts the divider with most of the before photo showing, and the value text reads "20% after shown". ratio="16/9" gives a wide stage. Drag toward the end and the after tag fades once its side is too narrow for it.
```astro
---
import BeforeAfter from "@astronomic/ui-pro/components/BeforeAfter.astro";
import { construction } from "../_data/groundcrew.ts";
const job = construction.beforeAfter;
const photos = job.photos!;
const lower = (text: string) => text.charAt(0).toLowerCase() + text.slice(1);
---
<!-- defaultValue={80}: the divider starts with most of the before photo showing, and the value text reads "20% after shown". -->
<BeforeAfter
class="mx-auto w-full max-w-3xl"
title="Kitchen and mudroom"
before={{ ...photos.before, alt: `Before: ${lower(photos.before.alt)}` }}
after={{ ...photos.after, alt: `After: ${lower(photos.after.alt)}` }}
defaultValue={80}
ratio="16/9"
/>
```
### In a narrow column
In a 20rem sidebar with a square stage, the two buttons wrap under the caption. With no script the two photos stack, each with its tag.
```astro
---
import BeforeAfter from "@astronomic/ui-pro/components/BeforeAfter.astro";
import { landscaping } from "../_data/groundcrew.ts";
const job = landscaping.beforeAfter;
const photos = job.photos!;
const lower = (text: string) => text.charAt(0).toLowerCase() + text.slice(1);
---
<!-- In a sidebar: the stage keeps its shape, and the buttons wrap under the caption. With no script the photos stack. -->
<div class="mx-auto w-full max-w-xs">
<BeforeAfter
title="Front yard, Centennial"
before={{ ...photos.before, alt: `Before: ${lower(photos.before.alt)}` }}
after={{ ...photos.after, alt: `After: ${lower(photos.after.alt)}` }}
ratio="1/1"
imageSizes="20rem"
/>
</div>
```
## Props
### BeforeAfter
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `before` | `{ src: ImageSource; alt: string; width?: number; height?: number }` | None | Required. The photo before the job: a URL or Astro's imported image, with alt text that says what it shows ("Before: the lawn bare and patchy"). An empty alt throws. |
| `after` | `{ src: ImageSource; alt: string; width?: number; height?: number }` | None | Required. The photo after the job, the same way. |
| `title` | `string` | None | Required. What is compared. It shows in the caption and names the range: "Compare before and after: {title}". |
| `defaultValue` | `number` | `50` | Where the divider starts, 0 to 100: the share of the before photo shown. |
| `ratio` | `MediaRatio` | `"4/3"` | The stage's shape, width/height. Both photos fill it and are cropped. |
| `focalPoint` | `FocalPoint` | None | The point of both photos to keep in view when the stage crops them, as Media's focalPoint. |
| `imageComponent` | `ImageComponent` | None | Your image component (Astro's <Image>, or a wrapper for your CMS), as Media's imageComponent. |
| `imageSizes` | `string` | None | How wide the photos show, as the sizes attribute, for your image component's srcset. |
| `beforeText` | `string` | `"Before"` | The before photo's tag. |
| `afterText` | `string` | `"After"` | The after photo's tag. |
| `allBeforeText` | `string` | `"All before"` | The button that shows only the before photo. |
| `allAfterText` | `string` | `"All after"` | The button that shows only the after photo. |
| `hintText` | `string` | `"Drag or use the arrow keys."` | The hint after the title in the caption, shown once the script runs. |
| `valueText` | `string` | `"{after}% after shown"` | The range's value text for a screen reader, {after} filled with the share of the after photo shown. |
| `stageClass` | `string` | None | For recipes: more classes on the stage that holds the photos. |
| `barClass` | `string` | None | For recipes: more classes on the bar under the stage: the figure's <figcaption>, holding the caption text and the buttons. |
| `captionClass` | `string` | None | For recipes: more classes on the caption text (the title and the hint) inside the bar. |
| `class` | `string` | None | More classes on the figure, merged with cn(), so yours win on conflicts. |
Any other attribute goes on the <figure>.
## Accessibility
| Key | Action |
| --- | --- |
| Tab | Moves to the comparison, then to "All before" and "All after" |
| Left Arrow or Right Arrow | Moves the divider one step (Down and Up do the same) |
| Page Up or Page Down | Moves the divider a larger step |
| Home or End | Shows all after (Home) or all before (End) |
| Enter or Space | On a button: shows that photo in full |
- With no script, two photos sit side by side, each with its tag and its own alt text, and no control shows: the range, the handle and the buttons are hidden until the script runs.
- The bar under the stage is the figure's <figcaption>, and the figure is named by the title alone.
- The comparison is a native range named "Compare before and after: {title}", with a value text such as "20% after shown". The arrow keys move it a step, Home and End reach the ends, and a vertical swipe on the stage still scrolls the page.
- "All before" and "All after" are toggle buttons (aria-pressed) in a group named "Show one photo", each 44px tall. The drawn handle is hidden from assistive tech; the knob shows the on-photo focus ring when the range has keyboard focus.
- A tag fades out when its side is too narrow to hold it, so it is never cut in half. The before tag sits on the scrim (solid under reduced transparency), the after tag on the accent with the accent's own text color.
- No filter, blend or tint is ever drawn over a photo. A drag never animates; the buttons glide only where motion is allowed.
BeforeAfter
ProTo show a job's before and after photos: drag or use the arrow keys to compare. With no script, the two photos sit side by side.
- File
- BeforeAfter.astro
- Group
- Data display
- Kind
- Interactive: inline script
- Examples
- 4
Examples
Drag the orange handle to set the width. Frames get real breakpoints.
- Static
- Interactive
- Framed
Drag to compare
A kitchen remodel by Sixpenny Builders. Drag across the photo, or Tab to it and use the arrow keys (Home and End reach the ends). "All before" and "All after" show one photo in full, gliding there where motion is allowed.
This preview did not load.
examples/beforeafter/main.astro
---
import BeforeAfter from "@astronomic/ui-pro/components/BeforeAfter.astro";
import { construction } from "../_data/groundcrew.ts";
const job = construction.beforeAfter;
const photos = job.photos!;
const lower = (text: string) => text.charAt(0).toLowerCase() + text.slice(1);
---
<!-- Drag across the photo, or Tab to it and use the arrow keys; the two buttons show one photo in full. -->
<BeforeAfter
class="mx-auto w-full max-w-3xl"
title={job.title}
before={{ ...photos.before, alt: `Before: ${lower(photos.before.alt)}` }}
after={{ ...photos.after, alt: `After: ${lower(photos.after.alt)}` }}
imageSizes="(min-width: 48rem) 48rem, 100vw"
/>Beside the job's story
Tilth & Stone's front yard next to its words, as on a service page. focalPoint keeps the house in view as the stage crops both photos.
This preview did not load.
examples/beforeafter/landscaping.astro
---
import BeforeAfter from "@astronomic/ui-pro/components/BeforeAfter.astro";
import { landscaping } from "../_data/groundcrew.ts";
const job = landscaping.beforeAfter;
const photos = job.photos!;
const lower = (text: string) => text.charAt(0).toLowerCase() + text.slice(1);
---
<!-- A job beside its story, as on a service page: the words on one side, the comparison on the other. -->
<div class="mx-auto grid w-full max-w-5xl items-center gap-group md:grid-cols-[minmax(0,2fr)_minmax(0,3fr)]">
<div class="grid gap-item">
<h2 class="m-0 text-subhead text-balance">{job.title}</h2>
<p class="m-0 text-body text-pretty text-muted">{job.before} {job.after}</p>
<p class="m-0 text-small text-muted">{job.town}</p>
</div>
<BeforeAfter
title={job.title}
before={{ ...photos.before, alt: `Before: ${lower(photos.before.alt)}` }}
after={{ ...photos.after, alt: `After: ${lower(photos.after.alt)}` }}
focalPoint="50% 60%"
/>
</div>Starting at 80
defaultValue={80} starts the divider with most of the before photo showing, and the value text reads "20% after shown". ratio="16/9" gives a wide stage. Drag toward the end and the after tag fades once its side is too narrow for it.
This preview did not load.
examples/beforeafter/default-80.astro
---
import BeforeAfter from "@astronomic/ui-pro/components/BeforeAfter.astro";
import { construction } from "../_data/groundcrew.ts";
const job = construction.beforeAfter;
const photos = job.photos!;
const lower = (text: string) => text.charAt(0).toLowerCase() + text.slice(1);
---
<!-- defaultValue={80}: the divider starts with most of the before photo showing, and the value text reads "20% after shown". -->
<BeforeAfter
class="mx-auto w-full max-w-3xl"
title="Kitchen and mudroom"
before={{ ...photos.before, alt: `Before: ${lower(photos.before.alt)}` }}
after={{ ...photos.after, alt: `After: ${lower(photos.after.alt)}` }}
defaultValue={80}
ratio="16/9"
/>In a narrow column
In a 20rem sidebar with a square stage, the two buttons wrap under the caption. With no script the two photos stack, each with its tag.
This preview did not load.
examples/beforeafter/narrow.astro
---
import BeforeAfter from "@astronomic/ui-pro/components/BeforeAfter.astro";
import { landscaping } from "../_data/groundcrew.ts";
const job = landscaping.beforeAfter;
const photos = job.photos!;
const lower = (text: string) => text.charAt(0).toLowerCase() + text.slice(1);
---
<!-- In a sidebar: the stage keeps its shape, and the buttons wrap under the caption. With no script the photos stack. -->
<div class="mx-auto w-full max-w-xs">
<BeforeAfter
title="Front yard, Centennial"
before={{ ...photos.before, alt: `Before: ${lower(photos.before.alt)}` }}
after={{ ...photos.after, alt: `After: ${lower(photos.after.alt)}` }}
ratio="1/1"
imageSizes="20rem"
/>
</div>