-
Notifications
You must be signed in to change notification settings - Fork 112
/
index.tsx
142 lines (133 loc) · 4.53 KB
/
index.tsx
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// https://github.com/FormidableLabs/react-live
import * as React from 'react';
import {LiveProvider, LiveEditor, LiveError, LivePreview as ReactLivePreview} from 'react-live';
import type {LiveProviderProps} from 'react-live';
import {Box} from '@twilio-paste/box';
import {Button} from '@twilio-paste/button';
import {Theme, useTheme} from '@twilio-paste/theme';
import {useUID} from '@twilio-paste/uid-library';
import {ShowIcon} from '@twilio-paste/icons/esm/ShowIcon';
import {HideIcon} from '@twilio-paste/icons/esm/HideIcon';
import {CodeblockTheme} from './theme';
import {CodeBlockOverlayShadow} from './CodeBlockOverlayShadow';
import {CopyButton} from '../../CopyButton';
import {ThemeSwitcher} from './ThemeSwitcher';
import {usePreviewThemeContext} from '../../../context/PreviewThemeContext';
interface LivePreviewProps {
children: string;
scope: {[key: string]: any};
language?: LiveProviderProps['language'];
disabled?: boolean;
noInline?: boolean;
showOverflow?: boolean;
}
const LivePreview: React.FC<React.PropsWithChildren<LivePreviewProps>> = ({
children,
language = 'jsx',
disabled = false,
noInline = false,
showOverflow = false,
scope,
}) => {
const [viewCode, setViewCode] = React.useState(false);
const id = useUID();
const pasteTheme = useTheme();
const {theme: previewTheme} = usePreviewThemeContext();
// Display different UI if codeblock is small
const isSmallCodeVariant = (children.match(/\n/g) || '').length + 1 <= 3;
const handleToggleCodeEditor = (): void => {
setViewCode(!viewCode);
};
const overflow = showOverflow ? 'visible' : 'auto';
return (
<Box marginBottom="space110" data-cy="live-preview">
<LiveProvider
code={children}
scope={scope}
language={language}
theme={CodeblockTheme}
noInline={noInline}
disabled={disabled}
>
<Box
borderColor="colorBorderWeak"
borderStyle="solid"
borderWidth="borderWidth20"
borderTopLeftRadius="borderRadius20"
borderTopRightRadius="borderRadius20"
>
<Theme.Provider theme={previewTheme}>
<Box
padding="space70"
backgroundColor="colorBackgroundBody"
borderTopLeftRadius="borderRadius20"
borderTopRightRadius="borderRadius20"
position="relative"
overflow={overflow}
>
<ReactLivePreview />
</Box>
</Theme.Provider>
<ThemeSwitcher />
</Box>
<Box
backgroundColor="colorBackgroundBodyInverse"
paddingY="space110"
paddingX="space70"
borderBottomLeftRadius="borderRadius20"
borderBottomRightRadius="borderRadius20"
borderColor="colorBorderWeak"
borderStyle="solid"
borderWidth="borderWidth20"
borderTopWidth="borderWidth0"
fontSize="fontSize20"
position="relative"
height={isSmallCodeVariant || viewCode ? 'auto' : '112px'}
overflow="hidden"
>
{isSmallCodeVariant ? null : (
<Box position="absolute" top="space40" right="space40" zIndex="zIndex10">
<Button
variant="secondary"
size="small"
onClick={handleToggleCodeEditor}
aria-label="View Code"
aria-expanded={viewCode}
aria-controls={id}
>
{viewCode ? (
<>
<HideIcon decorative />
<span>Hide code</span>
</>
) : (
<>
<ShowIcon decorative />
<span>View code</span>
</>
)}
</Button>
</Box>
)}
{/* corrects an inbuilt 10px margin style so we can accurately use token values */}
<LiveEditor
id={id}
style={{
margin: '-10px',
fontFamily: pasteTheme.fonts.fontFamilyCode,
backgroundColor: pasteTheme.backgroundColors.colorBackgroundBodyInverse,
}}
/>
{isSmallCodeVariant || viewCode ? null : <CodeBlockOverlayShadow />}
{isSmallCodeVariant || viewCode ? (
<Box position="absolute" bottom="space40" right="space40">
<CopyButton text={children} />
</Box>
) : null}
</Box>
<LiveError />
</LiveProvider>
</Box>
);
};
export {LivePreview};