{"id":13727394,"url":"https://github.com/praneshr/react-diff-viewer","last_synced_at":"2025-05-14T01:06:44.059Z","repository":{"id":33046782,"uuid":"150538002","full_name":"praneshr/react-diff-viewer","owner":"praneshr","description":"A simple and beautiful text diff viewer component made with Diff and React.","archived":false,"fork":false,"pushed_at":"2024-03-28T15:46:01.000Z","size":3640,"stargazers_count":1534,"open_issues_count":84,"forks_count":299,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-05-12T16:13:30.919Z","etag":null,"topics":["code-compare","diff","diff-viewer","reactjs"],"latest_commit_sha":null,"homepage":"https://praneshravi.in/react-diff-viewer/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/praneshr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":"https://www.buymeacoffee.com/cAHgxoB"}},"created_at":"2018-09-27T06:17:36.000Z","updated_at":"2025-05-11T06:34:19.000Z","dependencies_parsed_at":"2023-01-14T23:11:09.383Z","dependency_job_id":"4a165e44-6f2f-4a2a-94bd-0482a783a83a","html_url":"https://github.com/praneshr/react-diff-viewer","commit_stats":{"total_commits":176,"total_committers":13,"mean_commits":"13.538461538461538","dds":"0.20454545454545459","last_synced_commit":"5572d1c121ea095fe913a862b9e89e46a45f2599"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/praneshr%2Freact-diff-viewer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/praneshr%2Freact-diff-viewer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/praneshr%2Freact-diff-viewer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/praneshr%2Freact-diff-viewer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/praneshr","download_url":"https://codeload.github.com/praneshr/react-diff-viewer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253997908,"owners_count":21996963,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-compare","diff","diff-viewer","reactjs"],"created_at":"2024-08-03T01:03:53.475Z","updated_at":"2025-05-14T01:06:44.000Z","avatar_url":"https://github.com/praneshr.png","language":"TypeScript","readme":"\n\u003cp align=\"center\"\u003e\n  \u003cimg src='https://i.ibb.co/DKrGhVQ/Frame-1-1.png' width=\"100%\" alt='React Diff Viewer' /\u003e\n\u003c/p\u003e\n\u003cbr/\u003e\n\n[![Build Status](https://travis-ci.com/praneshr/react-diff-viewer.svg?branch=master)](https://travis-ci.com/praneshr/react-diff-viewer)\n[![npm version](https://badge.fury.io/js/react-diff-viewer.svg)](https://badge.fury.io/js/react-diff-viewer)\n[![GitHub license](https://img.shields.io/github/license/praneshr/react-diff-viewer.svg)](https://github.com/praneshr/react-diff-viewer/blob/master/LICENSE)\n\nA simple and beautiful text diff viewer component made with [Diff](https://github.com/kpdecker/jsdiff) and [React](https://reactjs.org).\n\nInspired from Github diff viewer, it includes features like split view, inline view, word diff, line highlight and more. It is highly customizable and it supports almost all languages.\n\nCheck [here](https://github.com/praneshr/react-diff-viewer/tree/v2.0) for v2.0\n\n## Install\n\n```bash\nyarn add react-diff-viewer\n\n# or\n\nnpm i react-diff-viewer\n```\n\n## Usage\n\n```javascript\nimport React, { PureComponent } from 'react';\nimport ReactDiffViewer from 'react-diff-viewer';\n\nconst oldCode = `\nconst a = 10\nconst b = 10\nconst c = () =\u003e console.log('foo')\n\nif(a \u003e 10) {\n  console.log('bar')\n}\n\nconsole.log('done')\n`;\nconst newCode = `\nconst a = 10\nconst boo = 10\n\nif(a === 10) {\n  console.log('bar')\n}\n`;\n\nclass Diff extends PureComponent {\n  render = () =\u003e {\n    return (\n      \u003cReactDiffViewer oldValue={oldCode} newValue={newCode} splitView={true} /\u003e\n    );\n  };\n}\n```\n\n## Props\n\n| Prop                      | Type            | Default                        | Description                                                                                                                                                                                                                                                                                                                                                                                                      |\n| ------------------------- | --------------- | ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| oldValue                  | `string`        | `''`                           | Old value as string.                                                                                                                                                                                                                                                                                                                                                                                             |\n| newValue                  | `string`        | `''`                           | New value as string.                                                                                                                                                                                                                                                                                                                                                                                             |\n| splitView                 | `boolean`       | `true`                         | Switch between `unified` and `split` view.                                                                                                                                                                                                                                                                                                                                                                       |\n| disableWordDiff           | `boolean`       | `false`                        | Show and hide word diff in a diff line.                                                                                                                                                                                                                                                                                                                                                                          |\n| compareMethod             | `DiffMethod`    | `DiffMethod.CHARS`             | JsDiff text diff method used for diffing strings. Check out the [guide](https://github.com/praneshr/react-diff-viewer/tree/v3.0.0#text-block-diff-comparison) to use different methods.                                                                                                                                                                                                                          |\n| hideLineNumbers           | `boolean`       | `false`                        | Show and hide line numbers.                                                                                                                                                                                                                                                                                                                                                                                      |\n| renderContent             | `function`      | `undefined`                    | Render Prop API to render code in the diff viewer. Helpful for [syntax highlighting](#syntax-highlighting)                                                                                                                                                                                                                                                                                                       |\n| onLineNumberClick         | `function`      | `undefined`                    | Event handler for line number click. `(lineId: string) =\u003e void`                                                                                                                                                                                                                                                                                                                                                  |\n| highlightLines            | `array[string]` | `[]`                           | List of lines to be highlighted. Works together with `onLineNumberClick`. Line number are prefixed with `L` and `R` for the left and right section of the diff viewer, respectively. For example, `L-20` means 20th line in the left pane. To highlight a range of line numbers, pass the prefixed line number as an array. For example, `[L-2, L-3, L-4, L-5]` will highlight the lines `2-5` in the left pane. |\n| showDiffOnly              | `boolean`       | `true`                         | Shows only the diffed lines and folds the unchanged lines                                                                                                                                                                                                                                                                                                                                                        |\n| extraLinesSurroundingDiff | `number`        | `3`                            | Number of extra unchanged lines surrounding the diff. Works along with `showDiffOnly`.                                                                                                                                                                                                                                                                                                                           |\n| codeFoldMessageRenderer   | `function`      | `Expand {number} of lines ...` | Render Prop API to render code fold message.                                                                                                                                                                                                                                                                                                                                                                     |\n| styles                    | `object`        | `{}`                           | To override style variables and styles. Learn more about [overriding styles](#overriding-styles)                                                                                                                                                                                                                                                                                                                 |\n| useDarkTheme              | `boolean`       | `true`                         | To enable/disable dark theme.                                                                                                                                                                                                                                                                                                                                                                                    |\n| leftTitle                 | `string`        | `undefined`                    | Column title for left section of the diff in split view. This will be used as the only title in inline view.                                                                                                                                                                                                                                                                                                     |\n| rightTitle                | `string`        | `undefined`                    | Column title for right section of the diff in split view. This will be ignored in inline view.                                                                                                                                                                                                                                                                                                                   |\n| linesOffset               | `number`        | `0`                            | Number to start count code lines from.                                                                                                                                                                                                                                                                                                                                                                           |\n\n## Instance Methods\n\n`resetCodeBlocks()` - Resets the expanded code blocks to it's initial state. Return `true` on successful reset and `false` during unsuccessful reset.\n\n## Syntax Highlighting\n\nSyntax highlighting is a bit tricky when combined with diff. Here, React Diff Viewer provides a simple render prop API to handle syntax highlighting. Use `renderContent(content: string) =\u003e JSX.Element` and your favorite syntax highlighting library to achieve this.\n\nAn example using [Prism JS](https://prismjs.com)\n\n```html\n// Load Prism CSS\n\u003clink\n  href=\"https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/prism.min.css\"\n/\u003e\n\n// Load Prism JS\n\u003cscript src=\"https://cdnjs.cloudflare.com/ajax/libs/prism/1.15.0/prism.min.js\"\u003e\u003c/script\u003e\n```\n\n```javascript\nimport React, { PureComponent } from 'react';\nimport ReactDiffViewer from 'react-diff-viewer';\n\nconst oldCode = `\nconst a = 10\nconst b = 10\nconst c = () =\u003e console.log('foo')\n\nif(a \u003e 10) {\n  console.log('bar')\n}\n\nconsole.log('done')\n`;\nconst newCode = `\nconst a = 10\nconst boo = 10\n\nif(a === 10) {\n  console.log('bar')\n}\n`;\n\nclass Diff extends PureComponent {\n  highlightSyntax = str =\u003e (\n    \u003cpre\n      style={{ display: 'inline' }}\n      dangerouslySetInnerHTML={{\n        __html: Prism.highlight(str, Prism.languages.javascript),\n      }}\n    /\u003e\n  );\n\n  render = () =\u003e {\n    return (\n      \u003cReactDiffViewer\n        oldValue={oldCode}\n        newValue={newCode}\n        splitView={true}\n        renderContent={this.highlightSyntax}\n      /\u003e\n    );\n  };\n}\n```\n\n## Text block diff comparison\n\nDifferent styles of text block diffing are possible by using the enums corresponding to variou JsDiff methods ([learn more](https://github.com/kpdecker/jsdiff/tree/v4.0.1#api)). The supported methods are as follows.\n\n```javascript\nenum DiffMethod {\n  CHARS = 'diffChars',\n  WORDS = 'diffWords',\n  WORDS_WITH_SPACE = 'diffWordsWithSpace',\n  LINES = 'diffLines',\n  TRIMMED_LINES = 'diffTrimmedLines',\n  SENTENCES = 'diffSentences',\n  CSS = 'diffCss',\n}\n```\n\n```javascript\nimport React, { PureComponent } from 'react';\nimport ReactDiffViewer, { DiffMethod } from 'react-diff-viewer';\n\nconst oldCode = `\n{\n  \"name\": \"Original name\",\n  \"description\": null\n}\n`;\nconst newCode = `\n{\n  \"name\": \"My updated name\",\n  \"description\": \"Brand new description\",\n  \"status\": \"running\"\n}\n`;\n\nclass Diff extends PureComponent {\n  render = () =\u003e {\n    return (\n      \u003cReactDiffViewer\n        oldValue={oldCode}\n        newValue={newCode}\n        compareMethod={DiffMethod.WORDS}\n        splitView={true}\n      /\u003e\n    );\n  };\n}\n```\n\n## Overriding Styles\n\nReact Diff Viewer uses [emotion](https://emotion.sh/) for styling. It also offers a simple way to override styles and style variables. You can supply different variables for both light and dark themes. Styles will be common for both themes.\n\nBelow are the default style variables and style object keys.\n\n```javascript\n\n// Default variables and style keys\n\nconst defaultStyles = {\n  variables: {\n    light: {\n      diffViewerBackground: '#fff',\n      diffViewerColor: '#212529',\n      addedBackground: '#e6ffed',\n      addedColor: '#24292e',\n      removedBackground: '#ffeef0',\n      removedColor: '#24292e',\n      wordAddedBackground: '#acf2bd',\n      wordRemovedBackground: '#fdb8c0',\n      addedGutterBackground: '#cdffd8',\n      removedGutterBackground: '#ffdce0',\n      gutterBackground: '#f7f7f7',\n      gutterBackgroundDark: '#f3f1f1',\n      highlightBackground: '#fffbdd',\n      highlightGutterBackground: '#fff5b1',\n      codeFoldGutterBackground: '#dbedff',\n      codeFoldBackground: '#f1f8ff',\n      emptyLineBackground: '#fafbfc',\n      gutterColor: '#212529',\n      addedGutterColor: '#212529',\n      removedGutterColor: '#212529',\n      codeFoldContentColor: '#212529',\n      diffViewerTitleBackground: '#fafbfc',\n      diffViewerTitleColor: '#212529',\n      diffViewerTitleBorderColor: '#eee',\n    },\n    dark: {\n      diffViewerBackground: '#2e303c',\n      diffViewerColor: '#FFF',\n      addedBackground: '#044B53',\n      addedColor: 'white',\n      removedBackground: '#632F34',\n      removedColor: 'white',\n      wordAddedBackground: '#055d67',\n      wordRemovedBackground: '#7d383f',\n      addedGutterBackground: '#034148',\n      removedGutterBackground: '#632b30',\n      gutterBackground: '#2c2f3a',\n      gutterBackgroundDark: '#262933',\n      highlightBackground: '#2a3967',\n      highlightGutterBackground: '#2d4077',\n      codeFoldGutterBackground: '#21232b',\n      codeFoldBackground: '#262831',\n      emptyLineBackground: '#363946',\n      gutterColor: '#464c67',\n      addedGutterColor: '#8c8c8c',\n      removedGutterColor: '#8c8c8c',\n      codeFoldContentColor: '#555a7b',\n      diffViewerTitleBackground: '#2f323e',\n      diffViewerTitleColor: '#555a7b',\n      diffViewerTitleBorderColor: '#353846',\n    }\n  },\n  diffContainer?: {}, // style object\n  diffRemoved?: {}, // style object\n  diffAdded?: {}, // style object\n  marker?: {}, // style object\n  emptyGutter?: {}, // style object\n  highlightedLine?: {}, // style object\n  lineNumber?: {}, // style object\n  highlightedGutter?: {}, // style object\n  contentText?: {}, // style object\n  gutter?: {}, // style object\n  line?: {}, // style object\n  wordDiff?: {}, // style object\n  wordAdded?: {}, // style object\n  wordRemoved?: {}, // style object\n  codeFoldGutter?: {}, // style object\n  codeFold?: {}, // style object\n  emptyLine?: {}, // style object\n  content?: {}, // style object\n  titleBlock?: {}, // style object\n  splitView?: {}, // style object\n}\n```\n\nTo override any style, just pass the new style object to the `styles` prop. New style will be computed using `Object.assign(default, override)`.\n\nFor keys other than `variables`, the value can either be an object or string interpolation.\n\n```javascript\nimport React, { PureComponent } from 'react';\nimport ReactDiffViewer from 'react-diff-viewer';\n\nconst oldCode = `\nconst a = 10\nconst b = 10\nconst c = () =\u003e console.log('foo')\n\nif(a \u003e 10) {\n  console.log('bar')\n}\n\nconsole.log('done')\n`;\nconst newCode = `\nconst a = 10\nconst boo = 10\n\nif(a === 10) {\n  console.log('bar')\n}\n`;\n\nclass Diff extends PureComponent {\n  highlightSyntax = str =\u003e (\n    \u003cspan\n      style={{ display: 'inline' }}\n      dangerouslySetInnerHTML={{\n        __html: Prism.highlight(str, Prism.languages.javascript),\n      }}\n    /\u003e\n  );\n\n  render = () =\u003e {\n    const newStyles = {\n      variables: {\n        dark: {\n          highlightBackground: '#fefed5',\n          highlightGutterBackground: '#ffcd3c',\n        },\n      },\n      line: {\n        padding: '10px 2px',\n        '\u0026:hover': {\n          background: '#a26ea1',\n        },\n      },\n    };\n\n    return (\n      \u003cReactDiffViewer\n        styles={newStyles}\n        oldValue={oldCode}\n        newValue={newCode}\n        splitView={true}\n        renderContent={this.highlightSyntax}\n      /\u003e\n    );\n  };\n}\n```\n\n## Local Development\n\n```bash\nyarn install\nyarn build # or use yarn build:watch\nyarn start:examples\n```\n\nCheck package.json for more build scripts.\n\n## License\n\nMIT\n","funding_links":["https://www.buymeacoffee.com/cAHgxoB"],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpraneshr%2Freact-diff-viewer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpraneshr%2Freact-diff-viewer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpraneshr%2Freact-diff-viewer/lists"}