Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

CodeBlock.js

125 lines3.9 KB

1import React, { useState } from 'react';
2import { LiveProvider, LiveEditor, LiveError, LivePreview } from 'react-live';
3import { mdx } from '@mdx-js/react';
4import { TWButton } from './TWButton';
5import { useClipboard } from './useClipboard';
6import Component from '@reactions/component';
7export const liveEditorStyle = {
8 fontSize: 14,
9 overflowX: 'auto',
10 color: '#f8f8f2',
11 fontFamily: 'SFMono-Regular,Consolas,Liberation Mono,Menlo,monospace',
12 height: '100%',
13 background: '#161e2e'
14};
15export const liveErrorStyle = {
16 fontFamily: 'SFMono-Regular,Consolas,Liberation Mono,Menlo,monospace',
17 fontSize: 12,
18 padding: '1em',
19 overflowX: 'auto',
20 color: 'white',
21 backgroundColor: 'red'
22};
23​
24const LiveCodePreview = props => <div className="font-sans text-gray-900 rounded-md p-4 overflow-x-scroll">
25 <LivePreview {...props} />
26 </div>;
27​
28const EditableNotice = props => {
29 return <div className="absolute right-0 top-0 p-3 text-gray-400 text-xs font-sans uppercase">
30 Editable Example
31 </div>;
32};
33​
34const CodeBlock = ({
35 className,
36 live = false,
37 noInline = false,
38 collapsed = false,
39 isManual,
40 render,
41 children,
42 ...props
43}) => {
44 const initialCode = React.useRef(children.trim());
45 const [editorCode, setEditorCode] = useState(children.trim());
46 const language = className && className.replace(/language-/, '');
47 const [hasCopied, onCopy] = useClipboard(editorCode);
48 const [isCollapsed, setCollapse] = React.useState(collapsed);
49 const liveProviderProps = {
50 theme: {
51 plain: {},
52 styles: []
53 },
54 language,
55 code: editorCode,
56 transformCode: code => `<>${code}</>`,
57 scope: {
58 mdx,
59 Component
60 },
61 noInline,
62 ...props
63 };
64​
65 const handleCodeChange = newCode => setEditorCode(newCode ? newCode.trim() : '');
66​
67 if (language === 'jsx' && live === true) {
68 return <LiveProvider {...liveProviderProps}>
69 <div className="border relative rounded shadow-sm">
70 <LiveCodePreview />
71 <EditableNotice />
72 {isCollapsed ? <div className="border-t">
73 <button className="text-gray-600 py-2 bg-gray-100 font-semibold w-full text-sm hover:bg-gray-200 transition duration-100 ease-in-out" onClick={() => setCollapse(false)}>
74 Show Code
75 </button>
76 </div> : <>
77 <div className="relative">
78 <LiveEditor onChange={handleCodeChange} style={liveEditorStyle} className="outline-none " />
79 <div className="absolute right-0 top-0 p-2">
80 <TWButton size="xs" className="font-sans mr-2" onClick={onCopy}>
81 {hasCopied ? 'Copied' : 'Copy'}
82 </TWButton>
83 <TWButton size="xs" className="font-sans" onClick={() => setEditorCode(initialCode.current)}>
84 Reset
85 </TWButton>
86 </div>
87 </div>
88​
89 <LiveError style={liveErrorStyle} />
90 <div className="border-t">
91 <button className="text-gray-600 py-2 bg-gray-100 font-semibold w-full text-sm hover:bg-gray-200 transition duration-100 ease-in-out" onClick={() => setCollapse(true)}>
92 Hide Code
93 </button>
94 </div>
95 </>}
96 </div>
97 </LiveProvider>;
98 }
99​
100 if (render) {
101 return <div style={{
102 marginTop: '40px'
103 }}>
104 <LiveProvider {...liveProviderProps}>
105 <LiveCodePreview className="font-sans" />
106 </LiveProvider>
107 </div>;
108 }
109​
110 return <LiveProvider disabled {...liveProviderProps}>
111 <div className="relative">
112 <LiveEditor style={liveEditorStyle} className="rounded shadow-sm" />
113 <div className="absolute right-0 top-0 p-2">
114 <TWButton size="xs" className="font-sans" onClick={onCopy}>
115 {hasCopied ? 'Copied' : 'Copy'}
116 </TWButton>
117 </div>
118 </div>
119 </LiveProvider>;
120};
121​
122CodeBlock.defaultProps = {
123 mountStylesheet: false
124};
125export default CodeBlock;