DiffViewer.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { css } from '@emotion/css';
  2. import React from 'react';
  3. import ReactDiffViewer, { ReactDiffViewerProps, DiffMethod } from 'react-diff-viewer';
  4. import tinycolor from 'tinycolor2';
  5. import { useTheme } from '@grafana/ui';
  6. export const DiffViewer: React.FC<ReactDiffViewerProps> = ({ oldValue, newValue }) => {
  7. const theme = useTheme();
  8. const styles = {
  9. variables: {
  10. // the light theme supplied by ReactDiffViewer is very similar to Grafana
  11. // the dark theme needs some tweaks.
  12. dark: {
  13. diffViewerBackground: theme.colors.dashboardBg,
  14. diffViewerColor: theme.colors.text,
  15. addedBackground: tinycolor(theme.palette.greenShade).setAlpha(0.3).toString(),
  16. addedColor: 'white',
  17. removedBackground: tinycolor(theme.palette.redShade).setAlpha(0.3).toString(),
  18. removedColor: 'white',
  19. wordAddedBackground: tinycolor(theme.palette.greenBase).setAlpha(0.4).toString(),
  20. wordRemovedBackground: tinycolor(theme.palette.redBase).setAlpha(0.4).toString(),
  21. addedGutterBackground: tinycolor(theme.palette.greenShade).setAlpha(0.2).toString(),
  22. removedGutterBackground: tinycolor(theme.palette.redShade).setAlpha(0.2).toString(),
  23. gutterBackground: theme.colors.bg1,
  24. gutterBackgroundDark: theme.colors.bg1,
  25. highlightBackground: tinycolor(theme.colors.bgBlue1).setAlpha(0.4).toString(),
  26. highlightGutterBackground: tinycolor(theme.colors.bgBlue2).setAlpha(0.2).toString(),
  27. codeFoldGutterBackground: theme.colors.bg2,
  28. codeFoldBackground: theme.colors.bg2,
  29. emptyLineBackground: theme.colors.bg2,
  30. gutterColor: theme.colors.textFaint,
  31. addedGutterColor: theme.colors.text,
  32. removedGutterColor: theme.colors.text,
  33. codeFoldContentColor: theme.colors.textFaint,
  34. diffViewerTitleBackground: theme.colors.bg2,
  35. diffViewerTitleColor: theme.colors.textFaint,
  36. diffViewerTitleBorderColor: theme.colors.border3,
  37. },
  38. },
  39. codeFold: {
  40. fontSize: theme.typography.size.sm,
  41. },
  42. gutter: `
  43. pre {
  44. color: ${tinycolor(theme.colors.textFaint).setAlpha(1).toString()};
  45. opacity: 0.61;
  46. }
  47. `,
  48. };
  49. return (
  50. <div
  51. className={css`
  52. font-size: ${theme.typography.size.sm};
  53. // prevent global styles interfering with diff viewer
  54. pre {
  55. all: revert;
  56. }
  57. `}
  58. >
  59. <ReactDiffViewer
  60. styles={styles}
  61. oldValue={oldValue}
  62. newValue={newValue}
  63. splitView={false}
  64. compareMethod={DiffMethod.CSS}
  65. useDarkTheme={theme.isDark}
  66. />
  67. </div>
  68. );
  69. };