MCPcopy Create free account
hub / github.com/elastic/eui / useEuiTextDiff

Function useEuiTextDiff

packages/eui/src/components/text_diff/text_diff.tsx:50–96  ·  view source on GitHub ↗
({
  className,
  insertComponent = 'ins',
  deleteComponent = 'del',
  sameComponent,
  beforeText = '',
  afterText = '',
  timeout = 0.1,
  ...rest
}: EuiTextDiffProps)

Source from the content-addressed store, hash-verified

48 HTMLAttributes<HTMLElement>;
49
50export const useEuiTextDiff = ({
51 className,
52 insertComponent = 'ins',
53 deleteComponent = 'del',
54 sameComponent,
55 beforeText = '',
56 afterText = '',
57 timeout = 0.1,
58 ...rest
59}: EuiTextDiffProps) => {
60 const textDiff = useMemo(() => {
61 const diff = new Diff({ timeout }); // options may be passed to constructor
62
63 return diff.main(beforeText, afterText);
64 }, [beforeText, afterText, timeout]); // produces diff array
65
66 const styles = useEuiMemoizedStyles(euiTextDiffStyles);
67
68 const classes = classNames('euiTextDiff', className);
69
70 const rendereredHtml = useMemo(() => {
71 const html = [];
72 if (textDiff)
73 for (let i = 0; i < textDiff.length; i++) {
74 let Element;
75 const el = textDiff[i];
76 if (el[0] === 1) Element = insertComponent;
77 else if (el[0] === -1) Element = deleteComponent;
78 else if (sameComponent) Element = sameComponent;
79 if (Element) html.push(<Element key={i}>{el[1]}</Element>);
80 else html.push(el[1]);
81 }
82
83 return html;
84 }, [textDiff, deleteComponent, insertComponent, sameComponent]); // produces diff array
85
86 // specifically defining the return type here as the
87 // inferred type is not correct: array vs tuple
88 const textDiffResult: [JSX.Element, typeof textDiff] = [
89 <span css={styles.euiTextDiff} className={classes} {...rest}>
90 {rendereredHtml}
91 </span>,
92 textDiff,
93 ];
94
95 return textDiffResult;
96};

Callers 2

ElementFunction · 0.90
ComponentUseEuiTextDiffFunction · 0.90

Calls 1

useEuiMemoizedStylesFunction · 0.90

Tested by 1

ElementFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…