Skip to content

CodeDiff

The CodeDiff component renders a syntax-highlighted diff between two code strings. It supports inline (unified) and side-by-side layouts, optional line numbers, and context trimming.

Basic Usage (Inline)

vue
<template>
  <CodeDiff
    :old-code="oldCode"
    :new-code="newCode"
    language="js"
  />
</template>

<script setup>
import { CodeDiff } from 'vuetty';

const oldCode = `function sum(a, b) {
  return a + b;
}
`;

const newCode = `function sum(a, b) {
  return a + b + 1;
}
`;
</script>

Side-by-Side Mode

vue
<template>
  <CodeDiff
    :old-code="oldCode"
    :new-code="newCode"
    mode="side-by-side"
    :width="80"
  />
</template>

<script setup>
import { CodeDiff } from 'vuetty';

const oldCode = `const user = {
  name: 'Sam',
  active: true
};
`;

const newCode = `const user = {
  name: 'Sam',
  active: false
};
`;
</script>

Context and Full Diff

vue
<template>
  <Col :gap="1">
    <!-- Show 1 line of context around changes -->
    <CodeDiff
      :old-code="oldCode"
      :new-code="newCode"
      :context="1"
    />

    <!-- Show all lines, ignoring context -->
    <CodeDiff
      :old-code="oldCode"
      :new-code="newCode"
      :show-all="true"
      :show-line-numbers="false"
    />
  </Col>
</template>

<script setup>
import { Col, CodeDiff } from 'vuetty';

const oldCode = `line 1
line 2
line 3
line 4
line 5
`;

const newCode = `line 1
line 2
line three
line 4
line 5
`;
</script>

Props

NameTypeDefaultDescription
oldCodestring''Original code to compare
newCodestring''Updated code to compare
mode'inline' | 'side-by-side''inline'Diff layout
languagestring'text'Syntax highlighting language
showLineNumbersbooleantrueShow line numbers
contextnumber3Lines of context around changes (0 shows only changed lines)
showAllbooleanfalseShow all lines and ignore context
addedColorstringnullText color for added lines (theme fallback)
removedColorstringnullText color for removed lines (theme fallback)
unchangedColorstringnullText color for unchanged lines (theme fallback)
addedBgstringnullBackground color for added lines (theme fallback)
removedBgstringnullBackground color for removed lines (theme fallback)
lineNumberColorstringnullLine number color (theme fallback)
codeBgstringnullCode background color (theme fallback)
borderbooleantrueRender a border around the diff
paddingnumber1Inner padding

Layout Props (Box Props)

NameTypeDefaultDescription
flexnumber | stringnullFlex shorthand when inside a flex container
flexGrownumbernullFlex grow factor
flexShrinknumbernullFlex shrink factor
flexBasisnumber | stringnullFlex basis
alignSelfstringnullSelf alignment: 'auto', 'flex-start', 'flex-end', 'center', 'stretch', 'baseline'
widthnumber | stringnullWidth (chars or %)
heightnumber | stringnullHeight (rows)
minWidthnumbernullMinimum width
maxWidthnumbernullMaximum width
minHeightnumbernullMinimum height
maxHeightnumbernullMaximum height
paddingnumbernullPadding
paddingLeftnumbernullLeft padding
paddingRightnumbernullRight padding
paddingTopnumbernullTop padding
paddingBottomnumbernullBottom padding
marginnumbernullMargin
marginLeftnumbernullLeft margin
marginRightnumbernullRight margin
marginTopnumbernullTop margin
marginBottomnumbernullBottom margin

Theming Notes

  • Theme overrides can be provided via theme.components.codeDiff for colors and border.
  • If a color prop is null, CodeDiff falls back to theme defaults (for example theme.success and theme.danger).

Released under the MIT License.