Skip to content

<git-diff>

Render unified diff text as an accessible, themeable file-by-file view.

Unified diff

A unified diff describes file-level changes with file paths, hunks, removed lines, and added lines. <git-diff> parses that text and presents the changes as an accessible, file-by-file view:

html
<git-diff>
  <script type="text/plain">
    --- a/message.txt
    +++ b/message.txt
    @@ -1 +1 @@
    -Hello, world!
    +Hello, Git Elements!
  </script>
</git-diff>

Input methods

Every input resolves to unified diff text. Choose the form that fits where the patch already lives. Unlike <git-log> and <git-show>, <git-diff> does not load directly from a repository host.

URL

Use src for a browser-accessible patch file:

html
<git-diff src="./changes.patch"></git-diff>

JavaScript

Use patch when the diff is already in memory:

js
const diff = document.querySelector("git-diff");
diff.patch = rawDiffText;

Assigning patch cancels any pending src request so a late response cannot replace explicitly supplied content.

Inline text

A non-executable text/plain script keeps declarative patches beside their markup. Common HTML indentation is removed.

html
<git-diff>
  <script type="text/plain">
    --- a/message.txt
    +++ b/message.txt
    @@ -1 +1 @@
    -Hello, world!
    +Hello, Git Elements!
  </script>
</git-diff>

Playground

Edit the patch below. The patch property updates the rendered output immediately:

Rendered output
View playground source
html
<div class="playground">
  <label for="patch-input">Unified diff</label>
  <textarea id="patch-input" spellcheck="false"></textarea>

  <git-diff
    id="patch-output"
    line-numbers
  ></git-diff>
</div>
js
import { GitDiffElement } from "@erkde/git-elements/components/git-diff";

customElements.define("git-diff", GitDiffElement);

const input = document.querySelector("#patch-input");
const output = document.querySelector("#patch-output");

function render() {
  output.patch = input.value;
}

input.addEventListener("input", render);
render();

Supported changes

The parser supports multi-file patches, additions, deletions, renames, copies, mode changes, binary file indicators, and Git-quoted paths.

Pass output generated with git diff --no-color for consistent rendering.

Line numbers

Old and new line-number gutters are hidden by default. Enable them with the line-numbers boolean attribute or lineNumbers property:

html
<git-diff
  src="./changes.patch"
  line-numbers
></git-diff>

Change summaries

Stat

Add stat for a visual, per-file summary with a totals line:

html
<git-diff
  src="./changes.patch"
  stat
></git-diff>
Loading sample patch…The sample patch is unavailable.

Each bar shows additions and deletions, scaled against the file with the most changed lines. File paths sit above the bars and wrap on narrow screens. The view includes compact labels for new, deleted, renamed, copied, and mode-changed files. Binary files show “Binary” without a bar because the patch does not provide their byte counts. If several summary attributes are present, stat takes precedence.

Numstat

Add numstat to show additions and deletions for each changed file in place of the patch:

html
<git-diff
  src="./changes.patch"
  numstat
></git-diff>
Loading sample patch…The sample patch is unavailable.

Binary files show - for both counts. Metadata-only changes show 0 and 0. File paths wrap to fit narrow screens. If both numstat and shortstat are present, numstat is shown.

Short stat

Add shortstat to show a single line with changed-file, insertion, and deletion totals in place of the patch:

html
<git-diff
  src="./changes.patch"
  shortstat
></git-diff>
Loading sample patch…The sample patch is unavailable.

The summary is calculated from the supplied patch. Binary and metadata-only files count as changed files but add no text-line counts. A partial patch produces a summary of only the supplied changes.

API

Attributes

NameTypeDescription
srcstringBrowser-accessible URL of a unified diff to fetch and render.
shortstatbooleanShow file, insertion, and deletion totals instead of the patch.
numstatbooleanShow additions and deletions per file instead of the patch.
statbooleanShow a per-file change graph and totals instead of the patch.
line-numbersbooleanShow old and new line-number gutters.
theme"light" | "dark"Override the operating-system color preference.

Properties

NameTypeDescription
srcstringURL of the unified diff loaded by the element.
lineNumbersbooleanWhether old and new line-number gutters are shown.
shortStatbooleanWhether file, insertion, and deletion totals replace the patch.
numStatbooleanWhether per-file addition and deletion counts replace the patch.
statbooleanWhether per-file change graphs and totals replace the patch.
patchstringRaw unified diff text currently rendered by the element.
parsedDiffsParsedDiff[]Parsed files, hunks, line numbers, and metadata for the current patch. Read-only.

Slots

NameDescription
loadingContent shown when a src request exceeds the loading delay.
errorContent shown when a src request fails.

Events

NameTypeDescription
loadEventFired after a src patch has loaded and rendered.
errorEventFired when a src patch cannot be loaded.

CSS custom properties

NameDefaultDescription
--git-diff-bg#f5f5f4Component background color.
--git-diff-text-color#292524Primary text color.
--git-diff-border-color#d6d3d1Separator and gutter border color.
--git-diff-muted-color#78716cMuted metadata and line-number color.
--git-diff-add-text#167044Added-line text color.
--git-diff-del-text#b4232cDeleted-line text color.
--git-diff-hunk-text#006d8fHunk-header text color.
--git-diff-meta-text#806000File metadata and status color.
--git-diff-hover-bgrgba(41, 37, 36, 0.045)Line hover background.
--git-diff-font-familyui-monospace, monospaceComponent font family.
--git-diff-font-size12pxComponent font size.
--git-diff-line-height20pxDiff line height.
--git-diff-loading-delay150msDelay before slotted loading content appears.

CSS parts

NameDescription
containerContainer for the summary or rendered file diffs.
shortstatFile, insertion, and deletion totals.
numstatTable of additions and deletions per file.
numstat-rowA file row in the numstat table.
numstat-additionsA file's addition count.
numstat-deletionsA file's deletion count.
numstat-pathA file path in the numstat table.
statPer-file change graphs and totals.
stat-fileA file row in the stat view.
stat-pathA file path in the stat view.
stat-annotationA file status or mode change in the stat view.
stat-graphA visual graph of a file's changed lines.
stat-additionsAdded-line segment of a stat graph.
stat-deletionsDeleted-line segment of a stat graph.
loadingLoading-state container.
errorError-state container.
fileA rendered file diff.
file-modifiedA modified file diff.
file-addedAn added file diff.
file-deletedA deleted file diff.
file-renamedA renamed file diff.
file-copiedA copied file diff.
file-headerHeader containing a file path and optional status.
file-pathDisplayed file path.
statusFile status label.
status-addedAdded-file status label.
status-deletedDeleted-file status label.
status-renamedRenamed-file status label.
status-copiedCopied-file status label.
file-metaFile mode, similarity, or binary metadata.
tableTable containing diff hunks and lines.
rowA diff row.
row-contextAn unchanged context row.
row-additionAn added line row.
row-deletionA deleted line row.
hunk-headerA hunk header row.
line-numAn old or new line-number cell.
old-line-numAn old line-number cell.
new-line-numA new line-number cell.
contentA diff-content cell.

Exports and registration

NameKindModule
<git-diff>Custom element registration@erkde/git-elements
GitDiffElementClass@erkde/git-elements/components/git-diff

See loading and errors and styling for the shared component conventions.

Released under the Apache-2.0 License.