summaryrefslogtreecommitdiff
path: root/extension/react-app/src/components/CodeBlock.tsx
blob: e033655425d565f10bf5681ec6822d290d1a1005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import hljs from "highlight.js";
import { useEffect } from "react";
import styled from "styled-components";
import { defaultBorderRadius, vscBackground } from ".";

import { Clipboard } from "@styled-icons/heroicons-outline";

const StyledPre = styled.pre`
  overflow-y: scroll;
  word-wrap: normal;
  border: 1px solid gray;
  border-radius: ${defaultBorderRadius};
  background-color: ${vscBackground};
`;

const StyledCode = styled.code`
  background-color: ${vscBackground};
`;

const StyledCopyButton = styled.button`
  float: right;
  border: none;
  background-color: ${vscBackground};
  cursor: pointer;
  padding: 0;
  margin: 4px;
  &:hover {
    color: #fff;
  }
`;

function CopyButton(props: { textToCopy: string }) {
  return (
    <>
      <StyledCopyButton
        onClick={() => {
          navigator.clipboard.writeText(props.textToCopy);
        }}
      >
        <Clipboard color="white" size="1.4em" />
      </StyledCopyButton>
    </>
  );
}

function CodeBlock(props: { language?: string; children: string }) {
  useEffect(() => {
    hljs.highlightAll();
  }, [props.children]);
  return (
    <StyledPre>
      <CopyButton textToCopy={props.children} />
      <StyledCode>{props.children}</StyledCode>
    </StyledPre>
  );
}

export default CodeBlock;