Skip to content

Commit da6a06e

Browse files
authored
Revert "[beta] upgrade sandpack and refactor sandpack.css (#4843)" (#4959)
This reverts commit 537e0a3.
1 parent 59c68a7 commit da6a06e

File tree

15 files changed

+397
-548
lines changed

15 files changed

+397
-548
lines changed

beta/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"check-all": "npm-run-all prettier lint:fix tsc"
2323
},
2424
"dependencies": {
25-
"@codesandbox/sandpack-react": "1.3.3",
25+
"@codesandbox/sandpack-react": "v0.19.8-experimental.7",
2626
"@docsearch/css": "3.0.0-alpha.41",
2727
"@docsearch/react": "3.0.0-alpha.41",
2828
"@headlessui/react": "^1.3.0",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
diff --git a/node_modules/@codesandbox/sandpack-react/dist/esm/index.js b/node_modules/@codesandbox/sandpack-react/dist/esm/index.js
2+
index ced9bd3..7e5e366 100644
3+
--- a/node_modules/@codesandbox/sandpack-react/dist/esm/index.js
4+
+++ b/node_modules/@codesandbox/sandpack-react/dist/esm/index.js
5+
@@ -566,17 +566,16 @@ var REACT_TEMPLATE = {
6+
},
7+
"/index.js": {
8+
code: `import React, { StrictMode } from "react";
9+
-import ReactDOM from "react-dom";
10+
+import { createRoot } from "react-dom/client";
11+
import "./styles.css";
12+
13+
import App from "./App";
14+
15+
-const rootElement = document.getElementById("root");
16+
-ReactDOM.render(
17+
+const root = createRoot(document.getElementById("root"));
18+
+root.render(
19+
<StrictMode>
20+
<App />
21+
- </StrictMode>,
22+
- rootElement
23+
+ </StrictMode>
24+
);`
25+
},
26+
"/styles.css": {
27+
@@ -611,8 +610,8 @@ h1 {
28+
}
29+
},
30+
dependencies: {
31+
- react: "^17.0.0",
32+
- "react-dom": "^17.0.0",
33+
+ react: "^18.0.0",
34+
+ "react-dom": "^18.0.0",
35+
"react-scripts": "^4.0.0"
36+
},
37+
entry: "/index.js",

beta/src/components/MDX/CodeBlock/CodeBlock.tsx

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import cn from 'classnames';
66
import {
77
SandpackCodeViewer,
88
SandpackProvider,
9+
SandpackThemeProvider,
910
} from '@codesandbox/sandpack-react';
1011
import rangeParser from 'parse-numeric-range';
1112
import {CustomTheme} from '../Sandpack/Themes';
@@ -67,29 +68,27 @@ const CodeBlock = function CodeBlock({
6768
const decorators = getDecoratedLineInfo();
6869
return (
6970
<div
71+
translate="no"
7072
className={cn(
71-
'sandpack sandpack--codeblock',
7273
'rounded-lg h-full w-full overflow-x-auto flex items-center bg-wash dark:bg-gray-95 shadow-lg',
7374
!noMargin && 'my-8'
7475
)}>
7576
<SandpackProvider
76-
files={{
77-
[filename]: {
78-
code: children.trimEnd(),
79-
},
80-
}}
8177
customSetup={{
8278
entry: filename,
83-
}}
84-
options={{
85-
initMode: 'immediate',
86-
}}
87-
theme={CustomTheme}>
88-
<SandpackCodeViewer
89-
key={children.trimEnd()}
90-
showLineNumbers={false}
91-
decorators={decorators}
92-
/>
79+
files: {
80+
[filename]: {
81+
code: children.trimEnd(),
82+
},
83+
},
84+
}}>
85+
<SandpackThemeProvider theme={CustomTheme}>
86+
<SandpackCodeViewer
87+
key={children.trimEnd()}
88+
showLineNumbers={false}
89+
decorators={decorators}
90+
/>
91+
</SandpackThemeProvider>
9392
</SandpackProvider>
9493
</div>
9594
);

beta/src/components/MDX/Sandpack/CustomPreset.tsx

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@ import {
88
useSandpack,
99
useActiveCode,
1010
SandpackCodeEditor,
11-
// SandpackReactDevTools,
12-
SandpackLayout,
11+
SandpackThemeProvider,
12+
SandpackReactDevTools,
1313
} from '@codesandbox/sandpack-react';
1414
import scrollIntoView from 'scroll-into-view-if-needed';
1515
import cn from 'classnames';
1616

1717
import {IconChevron} from 'components/Icon/IconChevron';
1818
import {NavigationBar} from './NavigationBar';
1919
import {Preview} from './Preview';
20-
20+
import {CustomTheme} from './Themes';
2121
import {useSandpackLint} from './useSandpackLint';
2222

23+
// Workaround for https://github.com/reactjs/reactjs.org/issues/4686#issuecomment-1137402613.
24+
const emptyArray: Array<any> = [];
25+
2326
export function CustomPreset({
2427
isSingleFile,
2528
showDevTools,
@@ -38,11 +41,11 @@ export function CustomPreset({
3841
const {code} = useActiveCode();
3942
const [isExpanded, setIsExpanded] = React.useState(false);
4043

41-
const {activeFile} = sandpack;
42-
if (!lineCountRef.current[activeFile]) {
43-
lineCountRef.current[activeFile] = code.split('\n').length;
44+
const {activePath} = sandpack;
45+
if (!lineCountRef.current[activePath]) {
46+
lineCountRef.current[activePath] = code.split('\n').length;
4447
}
45-
const lineCount = lineCountRef.current[activeFile];
48+
const lineCount = lineCountRef.current[activePath];
4649
const isExpandable = lineCount > 16 || isExpanded;
4750

4851
return (
@@ -51,57 +54,59 @@ export function CustomPreset({
5154
className="shadow-lg dark:shadow-lg-dark rounded-lg"
5255
ref={containerRef}>
5356
<NavigationBar showDownload={isSingleFile} />
54-
55-
<SandpackLayout
56-
className={cn(
57-
showDevTools && devToolsLoaded && 'sp-layout-devtools',
58-
!isExpandable && 'rounded-b-lg overflow-hidden',
59-
isExpanded && 'sp-layout-expanded'
60-
)}>
61-
<SandpackCodeEditor
62-
showLineNumbers
63-
showInlineErrors
64-
showTabs={false}
65-
showRunButton={false}
66-
extensions={lintExtensions}
67-
/>
68-
<Preview
69-
className="order-last xl:order-2"
70-
isExpanded={isExpanded}
71-
lintErrors={lintErrors}
72-
/>
73-
{isExpandable && (
74-
<button
75-
translate="yes"
76-
className="sandpack-expand flex text-base justify-between dark:border-card-dark bg-wash dark:bg-card-dark items-center z-10 p-1 w-full order-2 xl:order-last border-b-1 relative top-0"
77-
onClick={() => {
78-
const nextIsExpanded = !isExpanded;
79-
flushSync(() => {
80-
setIsExpanded(nextIsExpanded);
81-
});
82-
if (!nextIsExpanded && containerRef.current !== null) {
83-
scrollIntoView(containerRef.current, {
84-
scrollMode: 'if-needed',
85-
block: 'nearest',
86-
inline: 'nearest',
57+
<SandpackThemeProvider theme={CustomTheme}>
58+
<div
59+
ref={sandpack.lazyAnchorRef}
60+
className={cn(
61+
'sp-layout sp-custom-layout',
62+
showDevTools && devToolsLoaded && 'sp-layout-devtools',
63+
isExpanded && 'sp-layout-expanded'
64+
)}>
65+
<SandpackCodeEditor
66+
showLineNumbers
67+
showInlineErrors
68+
showTabs={false}
69+
showRunButton={false}
70+
extensions={lintExtensions}
71+
extensionsKeymap={emptyArray}
72+
/>
73+
<Preview
74+
className="order-last xl:order-2"
75+
isExpanded={isExpanded}
76+
lintErrors={lintErrors}
77+
/>
78+
{isExpandable && (
79+
<button
80+
translate="yes"
81+
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"
82+
onClick={() => {
83+
const nextIsExpanded = !isExpanded;
84+
flushSync(() => {
85+
setIsExpanded(nextIsExpanded);
8786
});
88-
}
89-
}}>
90-
<span className="flex p-2 focus:outline-none text-primary dark:text-primary-dark">
91-
<IconChevron
92-
className="inline mr-1.5 text-xl"
93-
displayDirection={isExpanded ? 'up' : 'down'}
94-
/>
95-
{isExpanded ? 'Show less' : 'Show more'}
96-
</span>
97-
</button>
98-
)}
99-
</SandpackLayout>
87+
if (!nextIsExpanded && containerRef.current !== null) {
88+
scrollIntoView(containerRef.current, {
89+
scrollMode: 'if-needed',
90+
block: 'nearest',
91+
inline: 'nearest',
92+
});
93+
}
94+
}}>
95+
<span className="flex p-2 focus:outline-none text-primary dark:text-primary-dark">
96+
<IconChevron
97+
className="inline mr-1.5 text-xl"
98+
displayDirection={isExpanded ? 'up' : 'down'}
99+
/>
100+
{isExpanded ? 'Show less' : 'Show more'}
101+
</span>
102+
</button>
103+
)}
104+
</div>
100105

101-
{/* {showDevTools && (
102-
// @ts-ignore TODO(@danilowoz): support devtools
103-
<SandpackReactDevTools onLoadModule={onDevToolsLoad} />
104-
)} */}
106+
{showDevTools && (
107+
<SandpackReactDevTools onLoadModule={onDevToolsLoad} />
108+
)}
109+
</SandpackThemeProvider>
105110
</div>
106111
</>
107112
);

beta/src/components/MDX/Sandpack/FilesDropdown.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,31 @@ const getFileName = (filePath: string): string => {
1515

1616
export function FilesDropdown() {
1717
const {sandpack} = useSandpack();
18-
const {visibleFiles, setActiveFile, activeFile} = sandpack;
18+
const {openPaths, setActiveFile, activePath} = sandpack;
1919
return (
20-
<Listbox value={activeFile} onChange={setActiveFile}>
20+
<Listbox value={activePath} onChange={setActiveFile}>
2121
<Listbox.Button>
2222
{({open}) => (
2323
<span
2424
className={cn(
2525
'h-full py-2 px-1 mt-px -mb-px flex border-b-2 text-link dark:text-link-dark border-link dark:border-link-dark items-center text-md leading-tight truncate'
2626
)}
2727
style={{maxWidth: '160px'}}>
28-
{getFileName(activeFile)}
28+
{getFileName(activePath)}
2929
<span className="ml-2">
3030
<IconChevron displayDirection={open ? 'up' : 'down'} />
3131
</span>
3232
</span>
3333
)}
3434
</Listbox.Button>
3535
<Listbox.Options className="absolute mt-0.5 bg-card dark:bg-card-dark px-2 left-0 right-0 mx-0 rounded-b-lg border-1 border-border dark:border-border-dark rounded-sm shadow-md">
36-
{visibleFiles.map((filePath: string) => (
36+
{openPaths.map((filePath: string) => (
3737
<Listbox.Option
3838
key={filePath}
3939
value={filePath}
4040
className={cn(
4141
'text-md mx-2 my-4 cursor-pointer',
42-
filePath === activeFile && 'text-link dark:text-link-dark'
42+
filePath === activePath && 'text-link dark:text-link-dark'
4343
)}>
4444
{getFileName(filePath)}
4545
</Listbox.Option>

beta/src/components/MDX/Sandpack/NavigationBar.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {FilesDropdown} from './FilesDropdown';
1616
export function NavigationBar({showDownload}: {showDownload: boolean}) {
1717
const {sandpack} = useSandpack();
1818
const [dropdownActive, setDropdownActive] = React.useState(false);
19-
const {visibleFiles, clients} = sandpack;
19+
const {openPaths, clients} = sandpack;
2020
const clientId = Object.keys(clients)[0];
2121
const {refresh} = useSandpackNavigation(clientId);
2222

@@ -31,23 +31,23 @@ export function NavigationBar({showDownload}: {showDownload: boolean}) {
3131
}, [dropdownActive]);
3232

3333
React.useEffect(() => {
34-
if (visibleFiles.length > 1) {
34+
if (openPaths.length > 1) {
3535
resizeHandler();
3636
window.addEventListener('resize', resizeHandler);
3737
return () => {
3838
window.removeEventListener('resize', resizeHandler);
3939
};
4040
}
4141
return;
42-
}, [visibleFiles.length, resizeHandler]);
42+
}, [openPaths.length, resizeHandler]);
4343

4444
const handleReset = () => {
4545
sandpack.resetAllFiles();
4646
refresh();
4747
};
4848

4949
return (
50-
<div className="bg-wash dark:bg-card-dark flex justify-between items-center relative z-10 border-b border-border dark:border-border-dark rounded-t-lg text-lg">
50+
<div className="bg-wash dark:bg-card-dark flex justify-between items-center relative z-10 border-b border-border dark:border-border-dark rounded-t-lg rounded-b-none">
5151
<div className="px-4 lg:px-6">
5252
{dropdownActive ? <FilesDropdown /> : <FileTabs />}
5353
</div>

beta/src/components/MDX/Sandpack/Preview.tsx

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,12 @@
44

55
/* eslint-disable react-hooks/exhaustive-deps */
66
import * as React from 'react';
7-
import {
8-
useSandpack,
9-
LoadingOverlay,
10-
SandpackStack,
11-
} from '@codesandbox/sandpack-react';
7+
import {useSandpack, LoadingOverlay} from '@codesandbox/sandpack-react';
128
import cn from 'classnames';
139
import {Error} from './Error';
1410
import {SandpackConsole} from './Console';
1511
import type {LintDiagnostic} from './useSandpackLint';
1612

17-
/**
18-
* TODO: can we use React.useId?
19-
*/
2013
const generateRandomId = (): string =>
2114
Math.floor(Math.random() * 10000).toString();
2215

@@ -156,16 +149,16 @@ export function Preview({
156149
// The best way to test it is to actually go through some challenges.
157150

158151
return (
159-
<SandpackStack
160-
className={className}
152+
<div
153+
className={cn('sp-stack', className)}
161154
style={{
162155
// TODO: clean up this mess.
163156
...customStyle,
164157
...overrideStyle,
165158
}}>
166159
<div
167160
className={cn(
168-
'p-0 sm:p-2 md:p-4 lg:p-8 bg-card dark:bg-wash-dark h-full relative md:rounded-b-lg lg:rounded-b-none',
161+
'p-0 sm:p-2 md:p-4 lg:p-8 md:bg-card md:dark:bg-wash-dark h-full relative md:rounded-b-lg lg:rounded-b-none',
169162
// Allow content to be scrolled if it's too high to fit.
170163
// Note we don't want this in the expanded state
171164
// because it breaks position: sticky (and isn't needed anyway).
@@ -179,7 +172,7 @@ export function Preview({
179172
: isExpanded
180173
? 'sticky'
181174
: undefined,
182-
top: isExpanded ? '4rem' : undefined,
175+
top: isExpanded ? '2rem' : undefined,
183176
}}>
184177
<iframe
185178
ref={iframeRef}
@@ -213,12 +206,11 @@ export function Preview({
213206
</div>
214207
)}
215208
<LoadingOverlay
216-
showOpenInCodeSandbox
217209
clientId={clientId.current}
218210
loading={!isReady && iframeComputedHeight === null}
219211
/>
220212
</div>
221213
{!error && <SandpackConsole />}
222-
</SandpackStack>
214+
</div>
223215
);
224216
}

0 commit comments

Comments
 (0)