New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

render-diff-react

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

render-diff-react

A React component for rendering Git diffs with syntax highlighting

latest
Source
npmnpm
Version
1.1.0
Version published
Weekly downloads
0
-100%
Maintainers
0
Weekly downloads
 
Created
Source

Render Diff React

npm version license downloads bundle size

A beautiful and customizable React component for rendering Git diffs with syntax highlighting

Display Git diffs with beautiful syntax highlighting, extensive customization options, and zero external dependencies for diff functionality


FeaturesInstallationUsageAPI ReferenceExamplesTechnical DetailsLicense

Features

  • 🎨 Beautiful UI: GitHub-style diff rendering with clean, modern design
  • 🔧 Highly Customizable: Extensive theming and styling options
  • 📝 Syntax Highlighting: Built-in syntax highlighting for code changes
  • 📱 Responsive: Works great on desktop and mobile devices
  • 🎯 TypeScript: Full TypeScript support with comprehensive type definitions
  • Ultra Lightweight: Zero external dependencies for diff functionality and icons, optimized performance
  • 🎨 Theme Support: Customizable colors and styling
  • 🔧 Flexible: Show/hide various elements and customize behavior

Installation

npm install render-diff-react
# or
yarn add render-diff-react
# or
pnpm add render-diff-react

CSS Styles

The package includes CSS styles for syntax highlighting. You have several options to include them:

Option 1: Import CSS file

/* Import the styles in your CSS file */
@import "render-diff-react/src/styles.css";

Option 2: Import in JavaScript/TypeScript

// Import in your JavaScript/TypeScript file
import "render-diff-react/src/styles.css";

Option 3: Programmatic injection

import { injectSyntaxHighlightingStyles } from "render-diff-react";

// Call this function once in your app
injectSyntaxHighlightingStyles();

Option 4: Manual CSS injection

import { syntaxHighlightingCSS } from "render-diff-react";

// Inject the CSS manually
const style = document.createElement("style");
style.textContent = syntaxHighlightingCSS;
document.head.appendChild(style);

Quick Start

import { DiffViewer } from "render-diff-react";

function App() {
  const gitDiff = `
diff --git a/src/App.tsx b/src/App.tsx
index 1234567..abcdefg 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -1,3 +1,4 @@
 import React from 'react';
+import { useState } from 'react';
 
 function App() {
-  return <div>Hello World</div>;
+  const [count, setCount] = useState(0);
+  return <div>Hello World {count}</div>;
 }
`;

  return (
    <div>
      <h1>My Git Diff</h1>
      <DiffViewer patch={gitDiff} />
    </div>
  );
}

Props

Required Props

PropTypeDescription
patchstringThe Git diff patch string to render

Optional Props

Display Options

PropTypeDefaultDescription
showLineNumbersbooleantrueWhether to show line numbers
showFileHeaderbooleantrueWhether to show the file header
showHunkHeadersbooleantrueWhether to show the hunk headers
enableSyntaxHighlightingbooleantrueWhether to enable syntax highlighting
syntaxHighlightLanguagestring"typescript"The language to use for syntax highlighting

Styling Props

PropTypeDescription
classNamestringCustom CSS classes for the main container
fileClassNamestringCustom CSS classes for the file container
fileHeaderClassNamestringCustom CSS classes for the file header
fileNameClassNamestringCustom CSS classes for the file name
additionsClassNamestringCustom CSS classes for the additions count
deletionsClassNamestringCustom CSS classes for the deletions count
splitViewClassNamestringCustom CSS classes for the split view container
codeLineClassNamestringCustom CSS classes for the code lines
lineNumberClassNamestringCustom CSS classes for the line numbers
hunkHeaderClassNamestringCustom CSS classes for the hunk header
hunkContentClassNamestringCustom CSS classes for the hunk content
addedLineClassNamestringCustom CSS classes for added lines
deletedLineClassNamestringCustom CSS classes for deleted lines
contextLineClassNamestringCustom CSS classes for context lines
prefixClassNamestringCustom CSS classes for the prefix (+/-/space)
codeContentClassNamestringCustom CSS classes for the code content
highlightAddedClassNamestringCustom CSS classes for highlighted additions
highlightDeletedClassNamestringCustom CSS classes for highlighted deletions
tokenClassNamestringCustom CSS classes for syntax highlighted tokens
tokenTextClassNamestringCustom CSS classes for text tokens

Theme Props

PropTypeDescription
theme.backgroundColorstringBackground color for the main container
theme.borderColorstringBorder color for file containers
theme.fileHeaderBackgroundColorstringBackground color for file headers
theme.fileNameColorstringText color for file names
theme.additionsColorstringText color for additions count
theme.deletionsColorstringText color for deletions count
theme.splitViewBackgroundColorstringBackground color for the split view
theme.lineNumberColorstringText color for line numbers
theme.codeColorstringText color for code content
theme.addedLineBackgroundColorstringBackground color for added lines
theme.deletedLineBackgroundColorstringBackground color for deleted lines
theme.hunkHeaderBackgroundColorstringBackground color for hunk headers
theme.hunkHeaderColorstringText color for hunk headers
theme.highlightAddedBackgroundColorstringBackground color for highlighted additions
theme.highlightDeletedBackgroundColorstringBackground color for highlighted deletions

Styling

The component comes with built-in styles that work out of the box. For syntax highlighting to work properly, you need to import the CSS styles:

/* Import the syntax highlighting styles */
@import "render-diff-react/src/styles.css";

The component uses a dark theme by default, similar to GitHub's diff view. You can customize all colors and styles using the theme prop and various className props.

Live Demo

🚀 Try the interactive demo

The demo showcases all features with live examples and comprehensive documentation.

Examples

Basic Usage

import { DiffViewer } from "render-diff-react";

function BasicExample() {
  const diff = `diff --git a/file.js b/file.js
index 1234567..abcdefg 100644
--- a/file.js
+++ b/file.js
@@ -1,3 +1,4 @@
 function hello() {
-  console.log("Hello");
+  console.log("Hello World");
+  return true;
 }
`;

  return <DiffViewer patch={diff} />;
}

Custom Styling

import { DiffViewer } from "render-diff-react";

function CustomStylingExample() {
  return (
    <DiffViewer
      patch={diff}
      className="my-custom-diff"
      fileClassName="border-2 border-blue-500"
      theme={{
        backgroundColor: "#1a1a1a",
        borderColor: "#404040",
        fileNameColor: "#ffffff",
        additionsColor: "#4ade80",
        deletionsColor: "#f87171",
      }}
    />
  );
}

Disable Features

import { DiffViewer } from "render-diff-react";

function MinimalExample() {
  return (
    <DiffViewer
      patch={diff}
      showLineNumbers={false}
      showFileHeader={false}
      showHunkHeaders={false}
      enableSyntaxHighlighting={false}
    />
  );
}

Custom Syntax Highlighting

import { DiffViewer } from "render-diff-react";

function PythonExample() {
  return <DiffViewer patch={pythonDiff} syntaxHighlightLanguage="python" />;
}

Supported Languages

The component supports syntax highlighting for all languages supported by refractor, including:

  • JavaScript/TypeScript
  • Python
  • Java
  • C/C++
  • Go
  • Rust
  • PHP
  • Ruby
  • And many more...

Browser Support

  • Chrome >= 60
  • Firefox >= 60
  • Safari >= 12
  • Edge >= 79

Development

# Install dependencies
npm install

# Start development server
npm run dev

# Build for production
npm run build

# Run tests
npm test

Demo Development

The interactive demo is located in examples/vite-demo/:

cd examples/vite-demo
npm install
npm run dev

Visit http://localhost:3000 to see the demo in action.

Contributing

  • Fork the repository
  • Create your feature branch (git checkout -b feature/amazing-feature)
  • Commit your changes (git commit -m 'Add some amazing feature')
  • Push to the branch (git push origin feature/amazing-feature)
  • Open a Pull Request

License

MIT © Aashish Singhal

Technical Details

Custom Diff Implementation

This package includes a custom word-level diff implementation that eliminates the need for external diff libraries. The implementation:

  • Splits text by words and spaces for accurate diffing
  • Provides the same API as the original diff package
  • Reduces bundle size by ~50KB
  • Eliminates external dependencies for diff functionality

Dependencies

  • refractor: For syntax highlighting
  • react: Peer dependency
  • react-dom: Peer dependency

Custom Icons

The package includes custom SVG icons instead of external icon libraries:

  • FileTextIcon: For file headers
  • ChevronsUpDownIcon: For hunk headers

Acknowledgments

  • Built with React
  • Syntax highlighting powered by refractor
  • Custom SVG icons for minimal dependencies
  • Custom diff implementation for optimal performance

Keywords

react

FAQs

Package last updated on 25 Jul 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts