Skip to content

Repository files navigation

react-string-diff

Highlight the difference between two strings in a React component

NPMMINMINZIP

Install

yarn add react-string-diff
# or
npm i react-string-diff

Props

PropTypeDefaultDescription
oldValuestring*Old value as string.
newValuestring*New value as string.
componentelementTypedivThe component used for the root node. Either a string to use a HTML element or a component
classNamestringCss class for the root node
methodDiffMethodDiffMethod.CharsJsDiff text diff method used for diffing strings. Check out supported methods.
stylesDiffStylesdefaultStylesOverriding styles.

Usage

importReactfrom'react';importStringDifffrom'react-string-diff';constoldValue='Lorem ipsum dolor sit amet';constnewValue='Loremi psum sit amet';constDiffViewer=()=>{return(<StringDiffoldValue={oldValue}newValue={newValue}/>)}

Methods

Different styles of text block diffing are possible by using the enums corresponding to variou JsDiff methods (learn more). The supported methods are as follows.

enumDiffMethod{Chars='diffChars',Words='diffWords',WordsWithSpace='diffWordsWithSpace',Lines='diffLines',TrimmedLines='diffTrimmedLines',Sentences='diffSentences',CSS='diffCss'}

Overriding Styles

React String diff uses plan CSS object for styling. Override styles object to apply a custom styles variables

typeDiffStyles={added: React.CSSProperties;removed: React.CSSProperties;default: React.CSSProperties;};constdefaultStyle: DiffStyles={added: {backgroundColor: 'lightgreen'},removed: {backgroundColor: 'salmon'},default: {}};

Local Development

yarn install
yarn start
yarn start:example

Check package.json for more build scripts.

License

MIT

About

Highlight the difference between two strings in a React component

Topics

Resources

Stars

8 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages