src/components/atoms/CodeBox/index.tsx   A
last analyzed

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 129
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 1
eloc 81
mnd 1
bc 1
fnc 0
dl 0
loc 129
ccs 17
cts 17
cp 1
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1 1
import "./styles.css";
2
3 1
import React from "react";
4
5
import { CodeBoxComponent } from "./types";
6
7 1
import { buildBoxComponent } from "../../../utils";
8 1
import { parseCode } from "./parser";
9
10 1
import Button from "../Button";
11
12
/**
13
 * A smart code box. Display code text as a compiler, with properly UI.
14
 * Optionally, can highlight code text, with a selectable environment
15
 *
16
 * @since 1.0.0
17
 *
18
 * @param {string} value code to display
19
 * @param {boolean} enhanced enable/disable advanced mode, to access extra features, like the integrated copy button and text highlight
20
 * @param environment environment for text highlight feature, default to "terminal" (only enabled into enhanced mode)
21
 * @param {string} label `common modular-ui prop` - component top label
22
 * @param {string} className `common modular-ui prop` - custom className (to better customize it)
23
 * @param {boolean} unstyled `common modular-ui prop` - Style/unstyle component (to better customize it)
24
 * @param {string} id `common modular-ui prop` - `data-id` parameter (for testing purpose, to easily find the component into the DOM)
25
 * @param {boolean} dark `common modular-ui prop` - Enable/disable dark mode
26
 * @param {boolean} hide `common modular-ui prop` - Hide/show component
27
 * @param {boolean} shadow `common modular-ui prop` - Enable/disable shadow behind component (to better customize it)
28
 *
29
 * @example <caption>Example CodeBox usage</caption>
30
 * import { render } from "react-dom";
31
 * import { CodeBox } from '@cianciarusocataldo/modular-ui';
32
 *
33
 * render(<CodeBox value="node version" />, document.getElementById("root"));
34
 *
35
 * @see https://cianciarusocataldo.github.io/modular-ui/components/atoms/CodeBox
36
 *
37
 * @author Cataldo Cianciaruso <https://github.com/CianciarusoCataldo>
38
 *
39
 * @copyright 2022 Cataldo Cianciaruso
40
 */
41 1
const CodeBox: CodeBoxComponent = ({
42 3
  value,
43 3
  enhanced,
44 3
  environment,
45 3
  label,
46 3
  ...commonProps
47
}) =>
48 3
  buildBoxComponent<string>({
49
    defaultValue: "",
50
    value,
51
    label,
52
    callBack: (code, _) => {
53 3
      const selectedLanguage = environment || "terminal";
54
55 3
      return {
56
        name: "modular-codebox",
57
        Component: (
58
          <div>
59
            {enhanced && (
60
              <div key="key_icon" className="copy-icon">
61
                <Button
62
                  unstyled
63 1
                  onClick={() => navigator.clipboard.writeText(code)}
64
                >
65
                  <svg
66
                    xmlns="http://www.w3.org/2000/svg"
67
                    viewBox="0 0 60 60"
68
                    width="25px"
69
                    height="25px"
70
                    version="1.0"
71
                  >
72
                    <rect
73
                      strokeLinejoin="round"
74
                      fillRule="evenodd"
75
                      stroke="#4c4c4c"
76
                      strokeWidth="3.125"
77
                      fill="#ccc"
78
                      rx="2.4745"
79
                      height="36.513"
80
                      width="34.732"
81
                      y="4.5767"
82
                      x="5.0385"
83
                    />
84
                    <rect
85
                      strokeLinejoin="round"
86
                      fillRule="evenodd"
87
                      stroke="#4c4c4c"
88
                      strokeWidth="3.125"
89
                      fill="#b3b3b3"
90
                      rx="2.4745"
91
                      height="36.513"
92
                      width="34.732"
93
                      y="20.161"
94
                      x="20.178"
95
                    />
96
                    <path
97
                      strokeLinejoin="round"
98
                      fillRule="evenodd"
99
                      stroke="#333"
100
                      strokeLinecap="round"
101
                      strokeWidth="3.125"
102
                      fill="#7f7f7f"
103
                      d="M10.971 22.475C19.445 41.064 33.797 36.69 33.934 36.69v-6.15l8.269 9.459-8.338 8.788v-6.219s-19.887 4.647-22.894-20.093z"
104
                    />
105
                  </svg>
106
                </Button>
107
              </div>
108
            )}
109
            <code key="code" className="code">
110
              {enhanced
111 12
                ? parseCode(code, selectedLanguage).map((part, index) => (
112
                    <span
113
                      key={`code-block_${selectedLanguage}_${index}`}
114
                      style={{
115
                        color: part.color,
116
                      }}
117
                    >{`${part.code}`}</span>
118
                  ))
119
                : code}
120
            </code>
121
          </div>
122
        ),
123
        commonProps,
124
      };
125
    },
126
  });
127
128
export default CodeBox;
129