# Image Compare

Compare a before & an after under one divider, dragged across or moved with the arrow keys.

```tsx title="components/ui/image-compare.tsx"
"use client";

import { IconArrowsHorizontal } from "@tabler/icons-react";
import { useEffect, useRef, useState, type CSSProperties } from "react";
import { ReactCompareSlider, ReactCompareSliderImage } from "react-compare-slider";
import { merge } from "yummacss/merge";
import { FOCUS_WITHIN, RADIUS, type Shape, useShape } from "./style";

export interface ImageCompareProps {
  /** The picture on the left, before the change. */
  before: string;
  /** The picture on the right, after it. */
  after: string;
  /** What both pictures show, for screen readers. */
  alt: string;
  /** The label over the left side. `""` drops it. */
  beforeLabel?: string;
  /** The label over the right side. `""` drops it. */
  afterLabel?: string;
  /** Where the divider starts, in percent from the left. */
  defaultPosition?: number;
  /** The frame's width over its height, such as `16 / 10`. */
  aspectRatio?: string;
  /** Corner radius of the frame. Inherits the nearest `Shape`. */
  shape?: Shape;
  /**
   * The focus outline. `true` draws it, `false` removes it, and a string of
   * Yumma CSS utilities restyles it on every focusable part of the component,
   * which is more than `className` reaches. Removing it outright and putting
   * nothing back fails WCAG 2.4.7.
   */
  focus?: boolean | string;
  /**
   * Extra classes. `merge` folds them in last, so one here replaces the
   * component's own class for the same utility.
   */
  className?: string;
}

/**
 * Two pictures under one divider, dragged across or moved with the arrow keys,
 * to show a before and an after.
 */
export default function ImageCompare({
  before,
  after,
  alt,
  beforeLabel = "Before",
  afterLabel = "After",
  defaultPosition = 50,
  aspectRatio = "16 / 10",
  shape: shapeProp,
  className,
  focus = true,
}: ImageCompareProps) {
  const shape = useShape(shapeProp);
  const outline = focus ? merge(FOCUS_WITHIN, focus === true ? "" : focus) : "";
  const root = useRef<HTMLDivElement>(null);
  // the window the slider is drawn in, which is not the page's inside a frame
  const [view, setView] = useState<Window | undefined>();
  useEffect(() => setView(root.current?.ownerDocument.defaultView ?? undefined), []);
  const label = "p:a zi:10 t:3 px:2 py:1 br:9999 bg:black/50 c:white fs:xs pe:none";

  return (
    <div
      ref={root}
      className={merge("p:r w:100% ar:var(--ratio) o:h bw:1 bc:rim", outline, RADIUS.popup[shape], className)}
      style={{ "--ratio": aspectRatio } as CSSProperties}
    >
      <ReactCompareSlider
        defaultPosition={defaultPosition}
        browsingContext={view}
        className="w:100% h:100%"
        itemOne={<ReactCompareSliderImage src={before} alt={`${alt}, before`} />}
        itemTwo={<ReactCompareSliderImage src={after} alt={`${alt}, after`} />}
        handle={
          // the line and the knob take the pointer: on a touch screen only the handle drags
          <div className="p:r d:f jc:c h:100% w:0">
            <span className="p:a t:0 b:0 w:10 pe:auto c:cr" />
            <span className="p:a t:0 b:0 w:calc(2px) bg:white bs-o:pop pe:none" />
            <span className="p:a t:50% tty:-half d:f ai:c jc:c w:9 h:9 br:9999 bg:white c:black bs-o:pop pe:auto c:cr">
              <IconArrowsHorizontal aria-hidden className="w:4 h:4" />
            </span>
          </div>
        }
      />
      {beforeLabel && <span className={merge(label, "l:3")}>{beforeLabel}</span>}
      {afterLabel && <span className={merge(label, "r:3")}>{afterLabel}</span>}
    </div>
  );
}
```

Two pictures under one divider, dragged across or moved with the arrow keys, to show a before and an after.

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `before` | `string` | - | The picture on the left, before the change. |
| `after` | `string` | - | The picture on the right, after it. |
| `alt` | `string` | - | What both pictures show, for screen readers. |
| `beforeLabel` | `string` | `"Before"` | The label over the left side. `""` drops it. |
| `afterLabel` | `string` | `"After"` | The label over the right side. `""` drops it. |
| `defaultPosition` | `number` | `50` | Where the divider starts, in percent from the left. |
| `aspectRatio` | `string` | `"16 / 10"` | The frame's width over its height, such as `16 / 10`. |
| `shape` | `"none"` \| `"rounded"` \| `"pill"` | `"rounded"` | Corner radius of the frame. Inherits the nearest `Shape`. |
| `className` | `string` | - | Extra classes. `merge` folds them in last, so one here replaces the component's own class for the same utility. |
| `focus` | `boolean \| string` | `true` | The focus outline. `true` draws it, `false` removes it, and a string of Yumma CSS utilities restyles it on every focusable part of the component, which is more than `className` reaches. Removing it outright and putting nothing back fails WCAG 2.4.7. |