Open History to see the last commit
1import * as React from 'react';
2import Highlight, { defaultProps } from 'prism-react-renderer';
3import { LiveProvider, LiveEditor, LiveError, LivePreview } from 'react-live'; // Original: https://raw.githubusercontent.com/PrismJS/prism-themes/master/themes/prism-ghcolors.css
4β
5/*:: import type { PrismTheme } from '../src/types' */
6β
7const theme
8/*: PrismTheme */
9= {
10 plain: {
11 color: '#293742',
12 borderRadius: 12,
13 fontFamily: `SFMono-Regular,Consolas,Liberation Mono,Menlo,Courier,monospace`,
14 fontSize: 14,
15 lineHeight: '1.5'
16 },
17 styles: [{
18 types: ['comment', 'prolog', 'doctype', 'cdata'],
19 style: {
20 color: '#A7B6C2',
21 fontStyle: 'italic'
22 }
23 }, {
24 types: ['namespace'],
25 style: {
26 opacity: 0.7
27 }
28 }, {
29 types: ['string', 'attr-value'],
30 style: {
31 color: '#DB2C6F'
32 }
33 }, {
34 types: ['punctuation', 'operator'],
35 style: {
36 color: '#394B59'
37 }
38 }, {
39 types: ['entity', 'url', 'symbol', 'number', 'boolean', 'variable', 'constant', 'property', 'regex', 'inserted'],
40 style: {
41 color: '#36acaa'
42 }
43 }, {
44 types: ['atrule', 'keyword', 'attr-name', 'selector'],
45 style: {
46 color: '#00B3A4'
47 }
48 }, {
49 types: ['function', 'deleted', 'tag'],
50 style: {
51 color: '#DB2C6F'
52 }
53 }, {
54 types: ['function-variable'],
55 style: {
56 color: '#634DBF'
57 }
58 }, {
59 types: ['tag', 'selector', 'keyword'],
60 style: {
61 color: '#1a56db'
62 }
63 }]
64};
65β
66const Code = ({
67 children,
68 codeString,
69 className = 'language-js',
70 ...props
71}) => {
72 const language = className.replace(/language-/, '');
73 const [key, setKey] = React.useState(`${Math.random() * 7}`);
74β
75 if (props['live']) {
76 return <div>
77 <LiveProvider key={key} code={children.trim()} theme={theme} noInline>
78 <div className="flex items-center justify-between">
79 <div>Live JSX Editor</div>
80 <div>
81 <button onClick={() => setKey(k => `${k * Math.random()}`)}>
82 Reset
83 </button>
84 </div>
85 </div>
86 <div style={{
87 paddingRight: 10,
88 paddingLeft: 10
89 }}>
90 <LiveEditor />
91 </div>
92 <div>
93 <div>
94 <div>Live Preview</div>
95 </div>
96 <div>
97 <LiveError style={{
98 fontSize: 13,
99 background: '#FED7D7',
100 color: '#9B2C2C',
101 lineHeight: '1.5',
102 margin: -4,
103 padding: 4
104 }} />
105 <LivePreview />
106 </div>
107 </div>
108 </LiveProvider>
109 </div>;
110 } else {
111 return <Highlight {...defaultProps} code={children.trim()} language={language} theme={theme}>
112 {({
113 className,
114 style,
115 tokens,
116 getLineProps,
117 getTokenProps
118 }) => <pre className={className + ' bg-gray-50 pb-4 pt-4 pr-4 overflow-scroll'} style={{ ...style,
119 border: '1px solid #eee',
120 fontSize: 13,
121 lineHeight: '1.5'
122 }}>
123 {tokens.map((line, i) => <div key={i} {...getLineProps({
124 line,
125 key: i
126 })}>
127 {tokens.length > 1 ? <span aria-hidden="true" className="select-none text-gray-300 text-right w-5 inline-block mx-2">
128 {i + 1}
129 </span> : <span className="mx-2 w-5" />}{' '}
130 {line.map((token, key) => <span key={key} {...getTokenProps({
131 token,
132 key
133 })} />)}
134 </div>)}
135 </pre>}
136 </Highlight>;
137 }
138};
139β
140export default Code;