本文へ移動
日本語
「コード差分」は Pro に含まれます。解除するとプレビューが表示されます。

仕組み

The diff is computed in the component by longest common subsequence, then removed and added lines in the same block are paired by position. Within a pair, the common prefix and suffix are left plain and the middle gets a stronger tint, which is what people actually scan for. Lines are 22px monospace and wrap instead of scrolling, so the card never overflows its column; in split view each row is one four-column grid, so both sides stay level when a line wraps, and the missing side of an unpaired line is hatched. Unchanged runs longer than the context fold into a full-width button; rows it reveals fade in. Below minSplitWidth a ResizeObserver switches split to unified.

向いている場面

  • Reviewing a change to code, configuration or any text with lines.
  • Showing what an automated fix or a template upgrade will change before people accept it.

向かない場面

  • Prose where sentences move around: compare paragraphs or use tracked changes.
  • Files of tens of thousands of lines: diff on the server and pass the hunks.

使い方

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

export function FileChange({ file }) {
  return (
    <CodeDiff
      fileName={file.path}
      before={file.previousText}
      after={file.text}
      layout="split"
      context={3}
    />
  )
}

プロパティ

プロパティ型既定値説明
before / afterstring—The two versions. Lines are compared exactly by longest common subsequence; fine for files up to a few thousand lines.
fileNameReactNode—Shows a header with the name and +/− counts with five proportion cells.
layout'split' | 'unified''split'Side by side or one column. Split falls back to unified when the component is narrower than minSplitWidth.
minSplitWidthnumber520Width in px under which split shows as unified.
contextnumber3Unchanged lines kept next to each change; longer runs fold into a button that reveals them.
wordDiffbooleantrueMarks the changed middle of each removed/added pair, after their common prefix and suffix.
labels{ added, removed, expand(n), stats(add, del), before, after }—Every built-in string, including what screen readers hear before each changed line.

アクセシビリティ

  • Removed lines are del elements and added lines ins elements, each starting with visually hidden “Removed:” or “Added:”, so the change is never told by colour alone; the + and − signs are decorative.
  • Line numbers cannot be selected, so copying code takes only the code.
  • Each fold is a real button that says how many lines it hides; Tab reaches it and Enter or Space opens it.
  • The +/− summary has an accessible name with both counts.
  • Reduced motion removes the fade of revealed rows.

モーション

Rows revealed by a fold fade in over 260ms.

プロジェクトに追加

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

CodeDiff.jsxcode-diff.csstokens.cssREADME.md

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

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