forked from vanilla-extract-css/vanilla-extract
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiledCode.tsx
More file actions
224 lines (215 loc) · 7.14 KB
/
CompiledCode.tsx
File metadata and controls
224 lines (215 loc) · 7.14 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { useState } from 'react';
import { assignInlineVars } from '@vanilla-extract/dynamic';
import { Box, Stack } from '../system';
import Text from '../Typography/Text';
import SyntaxHighlighter from './SyntaxHighlighter';
import * as styles from './CompiledCode.css';
import { vars } from '../themes.css';
interface File {
contents: string;
fileName?: string;
language?: string;
tokenized?: boolean;
}
export interface CompiledCodeProps {
code: Array<File>;
css?: Record<string, string>;
background?: {
lightMode?: keyof typeof vars.palette;
darkMode?: keyof typeof vars.palette;
};
}
export const CompiledCode = ({ code, css, background }: CompiledCodeProps) => {
const [activeFileName, setActiveFileName] = useState(code[0].fileName);
const [showCss, setShowCss] = useState(false);
const showOutputPanel = css && activeFileName?.endsWith('.css.ts');
const activeFile = code.filter(
({ fileName }) => fileName === activeFileName,
)[0];
return (
<Box
padding={{
mobile: 'large',
tablet: 'xlarge',
}}
paddingY="xlarge"
paddingBottom={css ? 'xxlarge' : undefined}
className={styles.root}
style={
background
? assignInlineVars({
[styles.darkModeBg]: vars.palette[background.darkMode!],
[styles.lightModeBg]: vars.palette[background.lightMode!],
})
: undefined
}
>
<Stack space={code.length > 1 ? 'xlarge' : 'large'}>
{activeFile.fileName ? (
<Box display="flex" alignItems="center">
{code.map(({ fileName }) => {
const isActiveFile = fileName === activeFileName;
return (
<Box
key={fileName}
component={code.length > 1 ? 'button' : 'span'}
cursor={code.length > 1 ? 'pointer' : undefined}
marginRight="large"
marginTop="-medium"
className={styles.fileNameFocus}
borderRadius="small"
onClick={
code.length > 1
? () => {
setActiveFileName(fileName);
setShowCss(false);
}
: undefined
}
>
<Box position="relative" display="flex" alignItems="center">
<Box
component="span"
position="absolute"
background={{
lightMode: 'green300',
darkMode: 'green400',
}}
borderRadius="full"
paddingLeft="xsmall"
paddingTop="large"
marginLeft="xsmall"
transition="slow"
className={
isActiveFile
? styles.fileIndicatorActive
: styles.fileIndicatorInactive
}
/>
<Box
component="span"
paddingLeft="large"
position="relative"
className={styles.boldLayoutShiftFix}
data-text={fileName}
>
<Box position="absolute" left={0} paddingLeft="large">
<Text
key={fileName}
size="small"
weight={isActiveFile ? 'strong' : undefined}
baseline={false}
>
<Box
component="span"
className={[
styles.fileName,
!isActiveFile
? styles.fileNameInactive
: undefined,
]}
>
{fileName}
</Box>
</Text>
</Box>
</Box>
</Box>
</Box>
);
})}
</Box>
) : null}
<Box padding="large" margin="-large" overflow="hidden">
<Box
display="flex"
className={showCss ? styles.showCssOnMobile : undefined}
>
<Box
width="full"
flexGrow={1}
flexShrink={0}
minWidth={0}
transition="slow"
className={styles.sourceContainer}
>
<SyntaxHighlighter
language={activeFile.language || 'tsx'}
tokenized={activeFile.tokenized}
>
{activeFile.contents}
</SyntaxHighlighter>
</Box>
{showOutputPanel ? (
<Box
id="outputContainer"
width="full"
flexShrink={0}
minWidth={0}
padding="large"
transition="slow"
className={styles.outputContainer}
>
<Stack space="large">
<Text weight="strong" size="small" color="secondary">
CSS
</Text>
{activeFileName && css[activeFileName] ? (
<Box className={styles.maxHeight}>
<SyntaxHighlighter language="css">
{css[activeFileName]}
</SyntaxHighlighter>
</Box>
) : (
<Text size="small" type="code">
No CSS created
</Text>
)}
</Stack>
</Box>
) : null}
</Box>
</Box>
</Stack>
{showOutputPanel ? (
<Box
display="flex"
justifyContent="flex-end"
paddingTop={{ mobile: 'medium' }}
className={styles.buttonContainer}
>
<Box
component="button"
background="gray600"
borderRadius="medium"
padding="medium"
cursor="pointer"
className={styles.button}
onClick={() => setShowCss(!showCss)}
position="relative"
transition="fast"
aria-expanded={showCss}
aria-controls="outputContainer"
>
<Box
component="span"
display="block"
background="pink600"
position="absolute"
inset={0}
pointerEvents="none"
borderRadius="medium"
opacity={showCss ? 0 : undefined}
transition="slow"
/>
<Box component="span" position="relative">
<Text size="small" color="code" weight="strong">
{showCss ? 'Close' : 'Show Output'}
</Text>
</Box>
</Box>
</Box>
) : null}
</Box>
);
};