CodeBlock.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { makeStyles, Theme } from '@material-ui/core';
  2. import { useTranslation } from 'react-i18next';
  3. import CopyButton from '../advancedSearch/CopyButton';
  4. import SyntaxHighlighter from 'react-syntax-highlighter';
  5. import { docco } from 'react-syntax-highlighter/dist/esm/styles/hljs';
  6. import { FC } from 'react';
  7. import { CodeBlockProps } from './Types';
  8. const getStyles = makeStyles((theme: Theme) => ({
  9. wrapper: {
  10. position: 'relative',
  11. padding: theme.spacing(3),
  12. borderRadius: 8,
  13. backgroundColor: '#fff',
  14. color: '#454545',
  15. },
  16. block: {
  17. margin: 0,
  18. },
  19. copy: {
  20. position: 'absolute',
  21. top: theme.spacing(2),
  22. right: theme.spacing(2),
  23. },
  24. }));
  25. const CodeStyle = {
  26. backgroundColor: '#fff',
  27. padding: 0,
  28. margin: 0,
  29. marginRight: 32,
  30. fontSize: 14,
  31. };
  32. const CodeBlock: FC<CodeBlockProps> = ({
  33. code,
  34. language,
  35. wrapperClass = '',
  36. }) => {
  37. const classes = getStyles();
  38. const { t: commonTrans } = useTranslation();
  39. const copyTrans = commonTrans('copy');
  40. return (
  41. <div className={`${classes.wrapper} ${wrapperClass}`}>
  42. <CopyButton
  43. className={classes.copy}
  44. label={copyTrans.label}
  45. value={code}
  46. />
  47. <SyntaxHighlighter
  48. language={language}
  49. style={docco}
  50. customStyle={CodeStyle}
  51. >
  52. {code}
  53. </SyntaxHighlighter>
  54. </div>
  55. );
  56. };
  57. export default CodeBlock;