本文へ移動
日本語
「画像比較スライダー」は Pro に含まれます。解除するとプレビューが表示されます。

仕組み

The after layer sizes the component; the before layer sits on top and is clipped on the right with clip-path. The divider and the clip both read one CSS variable, so dragging changes a single value and triggers no layout. Pointer Events with pointer capture keep the drag alive outside the component. On touch, a drag starts only after 6px of sideways movement, so vertical swipes still scroll the page. Labels are small ink-glass pills and the handle is a white knob with a soft glass ring. The demo is a photo-restore card: the comparison sits between a title row and the file with an ink Save button.

向いている場面

  • Showing the effect of photo restoration, enhancement or filters.
  • Comparing an old and a new design of a page.
  • Before and after results for renovation, repair or cleaning.

向かない場面

  • The two images differ in framing: the divider will not line up, so show them side by side.
  • More than two versions: use tabs or a grid.
  • Pixel-level analysis: use a difference overlay.

使い方

import { ImageCompare } from './ImageCompare.jsx'

export function RestorePreview({ rawUrl, fixedUrl }) {
  return (
    <ImageCompare
      style={{ width: 480, aspectRatio: '16 / 10' }}
      before={<img src={rawUrl} alt="Original photo" />}
      after={<img src={fixedUrl} alt="Restored photo" />}
      beforeLabel="Before"
      afterLabel="After"
      aria-label="Comparison position"
    />
  )
}

プロパティ

プロパティ型既定値説明
beforeReactNode—Content shown left of the divider (the original): image, SVG, video or any node.
afterReactNode—Content shown right of the divider (the result). It sizes the component in the document flow.
valuenumber—Controlled position from 0 to 100, as a percentage from the left edge.
defaultValuenumber50Initial position when uncontrolled.
onChange(value: number) => void—Called when the position changes, continuously while dragging.
beforeLabelReactNode'Before'Label in the top-left corner.
afterLabelReactNode'After'Label in the top-right corner.
showLabelsbooleantrueWhether to show the labels.
stepnumber1Percentage moved per arrow key press; multiplied by 10 with Shift.
hintbooleantrueThe first time the slider is half in view, the divider glides 12% toward the larger side and back to show it can be dragged. Only the picture moves; value and onChange are untouched, and any press or key cancels it.
aria-labelstring'Comparison position'Accessible name of the handle (the slider).
...restHTMLAttributes<HTMLDivElement>—Other attributes go to the root element; use style for width and aspect-ratio.

アクセシビリティ

  • The handle is a role="slider" with aria-valuenow, aria-valuemin and aria-valuemax; aria-valuetext reads like “Before 50%, After 50%”.
  • Arrow keys move 1% (10% with Shift), PageUp and PageDown 10%, Home and End go to the ends.
  • Pressing anywhere on the component focuses the handle so arrow keys can fine-tune; a focus ring is shown.
  • The handle has a 48px touch target, and vertical swipes on the image still scroll the page.
  • With reduced motion there is no first-view hint, and clicks jump straight to the position without gliding.

モーション

First view: the divider glides 12% out and back (720ms each way, ease-in-out) to hint that it can be dragged. Dragging follows the pointer with no transition. Click, Home and End glide over 360ms ease-out; the handle scales to 1.06 on hover and 1.1 while dragging (160ms).

プロジェクトに追加

Pro メンバーはプロンプトをコピーし、コーディングエージェントが無料コンポーネントと同じ手順でソースを導入します。

ImageCompare.jsximage-compare.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

データ表示
「コード差分」は Pro に含まれます。解除するとプレビューが表示されます。
「フリップカウントダウン」は Pro に含まれます。解除するとプレビューが表示されます。