-
Notifications
You must be signed in to change notification settings - Fork 7.7k
/
Copy pathCustomPreset.tsx
132 lines (121 loc) · 3.97 KB
/
CustomPreset.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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*/
import React from 'react';
// @ts-ignore
import {flushSync} from 'react-dom';
import {
useSandpack,
useActiveCode,
SandpackCodeEditor,
SandpackThemeProvider,
SandpackReactDevTools,
} from '@codesandbox/sandpack-react';
import scrollIntoView from 'scroll-into-view-if-needed';
import {useTimer} from 'use-timer';
import cn from 'classnames';
import {IconChevron} from 'components/Icon/IconChevron';
import {NavigationBar} from './NavigationBar';
import {Preview} from './Preview';
import {CustomTheme} from './Themes';
import {ReturnValue} from 'use-timer/lib/types';
/**
* Temp component: testing purposes only
*/
const Timer: React.FC<{timer: ReturnValue}> = ({timer}) => {
const {listen} = useSandpack();
React.useEffect(() => {
const unsub = listen((message) => {
if (message.type === 'start') {
timer.start();
} else if (message.type === 'done') {
timer.pause();
}
});
return () => unsub();
}, []);
return <>{timer.time}ms</>;
};
export function CustomPreset({
isSingleFile,
showDevTools,
onDevToolsLoad,
devToolsLoaded,
}: {
isSingleFile: boolean;
showDevTools: boolean;
devToolsLoaded: boolean;
onDevToolsLoad: () => void;
}) {
const timer = useTimer({interval: 1});
const lineCountRef = React.useRef<{[key: string]: number}>({});
const containerRef = React.useRef<HTMLDivElement>(null);
const {sandpack} = useSandpack();
const {code} = useActiveCode();
const [isExpanded, setIsExpanded] = React.useState(false);
const {activePath} = sandpack;
if (!lineCountRef.current[activePath]) {
lineCountRef.current[activePath] = code.split('\n').length;
}
const lineCount = lineCountRef.current[activePath];
const isExpandable = lineCount > 16 || isExpanded;
return (
<>
Time to load: <Timer timer={timer} />
<div
className="shadow-lg dark:shadow-lg-dark rounded-lg"
ref={containerRef}>
<NavigationBar showDownload={isSingleFile} onReset={timer.reset} />
<SandpackThemeProvider theme={CustomTheme}>
<div
ref={sandpack.lazyAnchorRef}
className={cn(
'sp-layout sp-custom-layout',
showDevTools && devToolsLoaded && 'sp-layout-devtools',
isExpanded && 'sp-layout-expanded'
)}>
<SandpackCodeEditor
showLineNumbers
showInlineErrors
showTabs={false}
showRunButton={false}
/>
<Preview
className="order-last xl:order-2"
isExpanded={isExpanded}
/>
{isExpandable && (
<button
translate="yes"
className="flex text-base justify-between dark:border-card-dark bg-wash dark:bg-card-dark items-center z-10 rounded-t-none p-1 w-full order-2 xl:order-last border-b-1 relative top-0"
onClick={() => {
const nextIsExpanded = !isExpanded;
flushSync(() => {
setIsExpanded(nextIsExpanded);
});
if (!nextIsExpanded && containerRef.current !== null) {
scrollIntoView(containerRef.current, {
scrollMode: 'if-needed',
block: 'nearest',
inline: 'nearest',
});
}
}}>
<span className="flex p-2 focus:outline-none text-primary dark:text-primary-dark">
<IconChevron
className="inline mr-1.5 text-xl"
displayDirection={isExpanded ? 'up' : 'down'}
/>
{isExpanded ? 'Show less' : 'Show more'}
</span>
</button>
)}
</div>
{showDevTools && (
<SandpackReactDevTools onLoadModule={onDevToolsLoad} />
)}
</SandpackThemeProvider>
</div>
</>
);
}