From 537e0a354d50bb1167de0f5c4b1865a067257f44 Mon Sep 17 00:00:00 2001 From: Danilo Woznica Date: Wed, 31 Aug 2022 23:35:37 +0100 Subject: [PATCH] [beta] upgrade sandpack and refactor sandpack.css (#4843) * upgrade sandpack and address breaking changes * remove unnecessaries fixes * use Sandpack components instead reimplementing them * refactor: sandpack.css * fixes * fix: sticky and syntax-highlight Co-authored-by: dan --- beta/package.json | 2 +- ...sandpack-react+0.19.8-experimental.4.patch | 37 -- .../components/MDX/CodeBlock/CodeBlock.tsx | 31 +- .../components/MDX/Sandpack/CustomPreset.tsx | 117 ++--- .../components/MDX/Sandpack/FilesDropdown.tsx | 10 +- .../components/MDX/Sandpack/NavigationBar.tsx | 8 +- beta/src/components/MDX/Sandpack/Preview.tsx | 20 +- .../components/MDX/Sandpack/SandpackRoot.tsx | 20 +- beta/src/components/MDX/Sandpack/Themes.tsx | 51 +- beta/src/components/MDX/Sandpack/index.tsx | 2 +- beta/src/pages/_app.tsx | 1 - beta/src/pages/_document.tsx | 2 + beta/src/pages/learn/index.md | 2 +- beta/src/styles/sandpack.css | 493 ++++++++++-------- beta/yarn.lock | 149 +++++- 15 files changed, 548 insertions(+), 397 deletions(-) delete mode 100644 beta/patches/@codesandbox+sandpack-react+0.19.8-experimental.4.patch diff --git a/beta/package.json b/beta/package.json index f6996879..cca603d6 100644 --- a/beta/package.json +++ b/beta/package.json @@ -22,7 +22,7 @@ "check-all": "npm-run-all prettier lint:fix tsc" }, "dependencies": { - "@codesandbox/sandpack-react": "v0.19.8-experimental.7", + "@codesandbox/sandpack-react": "1.3.3", "@docsearch/css": "3.0.0-alpha.41", "@docsearch/react": "3.0.0-alpha.41", "@headlessui/react": "^1.3.0", diff --git a/beta/patches/@codesandbox+sandpack-react+0.19.8-experimental.4.patch b/beta/patches/@codesandbox+sandpack-react+0.19.8-experimental.4.patch deleted file mode 100644 index 2298cbd1..00000000 --- a/beta/patches/@codesandbox+sandpack-react+0.19.8-experimental.4.patch +++ /dev/null @@ -1,37 +0,0 @@ -diff --git a/node_modules/@codesandbox/sandpack-react/dist/esm/index.js b/node_modules/@codesandbox/sandpack-react/dist/esm/index.js -index ced9bd3..7e5e366 100644 ---- a/node_modules/@codesandbox/sandpack-react/dist/esm/index.js -+++ b/node_modules/@codesandbox/sandpack-react/dist/esm/index.js -@@ -566,17 +566,16 @@ var REACT_TEMPLATE = { - }, - "/index.js": { - code: `import React, { StrictMode } from "react"; --import ReactDOM from "react-dom"; -+import { createRoot } from "react-dom/client"; - import "./styles.css"; - - import App from "./App"; - --const rootElement = document.getElementById("root"); --ReactDOM.render( -+const root = createRoot(document.getElementById("root")); -+root.render( - - -- , -- rootElement -+ - );` - }, - "/styles.css": { -@@ -611,8 +610,8 @@ h1 { - } - }, - dependencies: { -- react: "^17.0.0", -- "react-dom": "^17.0.0", -+ react: "^18.0.0", -+ "react-dom": "^18.0.0", - "react-scripts": "^4.0.0" - }, - entry: "/index.js", diff --git a/beta/src/components/MDX/CodeBlock/CodeBlock.tsx b/beta/src/components/MDX/CodeBlock/CodeBlock.tsx index 965bc816..24696e0a 100644 --- a/beta/src/components/MDX/CodeBlock/CodeBlock.tsx +++ b/beta/src/components/MDX/CodeBlock/CodeBlock.tsx @@ -6,7 +6,6 @@ import cn from 'classnames'; import { SandpackCodeViewer, SandpackProvider, - SandpackThemeProvider, } from '@codesandbox/sandpack-react'; import rangeParser from 'parse-numeric-range'; import {CustomTheme} from '../Sandpack/Themes'; @@ -68,27 +67,29 @@ const CodeBlock = function CodeBlock({ const decorators = getDecoratedLineInfo(); return (
- - - + }} + options={{ + initMode: 'immediate', + }} + theme={CustomTheme}> +
); diff --git a/beta/src/components/MDX/Sandpack/CustomPreset.tsx b/beta/src/components/MDX/Sandpack/CustomPreset.tsx index bb0f1c67..e5d44671 100644 --- a/beta/src/components/MDX/Sandpack/CustomPreset.tsx +++ b/beta/src/components/MDX/Sandpack/CustomPreset.tsx @@ -8,8 +8,8 @@ import { useSandpack, useActiveCode, SandpackCodeEditor, - SandpackThemeProvider, - SandpackReactDevTools, + // SandpackReactDevTools, + SandpackLayout, } from '@codesandbox/sandpack-react'; import scrollIntoView from 'scroll-into-view-if-needed'; import cn from 'classnames'; @@ -17,11 +17,8 @@ import cn from 'classnames'; import {IconChevron} from 'components/Icon/IconChevron'; import {NavigationBar} from './NavigationBar'; import {Preview} from './Preview'; -import {CustomTheme} from './Themes'; -import {useSandpackLint} from './useSandpackLint'; -// Workaround for https://github.com/reactjs/reactjs.org/issues/4686#issuecomment-1137402613. -const emptyArray: Array = []; +import {useSandpackLint} from './useSandpackLint'; export function CustomPreset({ isSingleFile, @@ -41,11 +38,11 @@ export function CustomPreset({ const {code} = useActiveCode(); const [isExpanded, setIsExpanded] = React.useState(false); - const {activePath} = sandpack; - if (!lineCountRef.current[activePath]) { - lineCountRef.current[activePath] = code.split('\n').length; + const {activeFile} = sandpack; + if (!lineCountRef.current[activeFile]) { + lineCountRef.current[activeFile] = code.split('\n').length; } - const lineCount = lineCountRef.current[activePath]; + const lineCount = lineCountRef.current[activeFile]; const isExpandable = lineCount > 16 || isExpanded; return ( @@ -54,59 +51,57 @@ export function CustomPreset({ className="shadow-lg dark:shadow-lg-dark rounded-lg" ref={containerRef}> - -
- - - {isExpandable && ( - - )} -
- {showDevTools && ( - + + + + {isExpandable && ( + )} -
+ + + {/* {showDevTools && ( + // @ts-ignore TODO(@danilowoz): support devtools + + )} */} ); diff --git a/beta/src/components/MDX/Sandpack/FilesDropdown.tsx b/beta/src/components/MDX/Sandpack/FilesDropdown.tsx index 68d068ad..2c47de71 100644 --- a/beta/src/components/MDX/Sandpack/FilesDropdown.tsx +++ b/beta/src/components/MDX/Sandpack/FilesDropdown.tsx @@ -15,9 +15,9 @@ const getFileName = (filePath: string): string => { export function FilesDropdown() { const {sandpack} = useSandpack(); - const {openPaths, setActiveFile, activePath} = sandpack; + const {visibleFiles, setActiveFile, activeFile} = sandpack; return ( - + {({open}) => ( - {getFileName(activePath)} + {getFileName(activeFile)} @@ -33,13 +33,13 @@ export function FilesDropdown() { )} - {openPaths.map((filePath: string) => ( + {visibleFiles.map((filePath: string) => ( {getFileName(filePath)} diff --git a/beta/src/components/MDX/Sandpack/NavigationBar.tsx b/beta/src/components/MDX/Sandpack/NavigationBar.tsx index 6114ef67..0d3eb921 100644 --- a/beta/src/components/MDX/Sandpack/NavigationBar.tsx +++ b/beta/src/components/MDX/Sandpack/NavigationBar.tsx @@ -16,7 +16,7 @@ import {FilesDropdown} from './FilesDropdown'; export function NavigationBar({showDownload}: {showDownload: boolean}) { const {sandpack} = useSandpack(); const [dropdownActive, setDropdownActive] = React.useState(false); - const {openPaths, clients} = sandpack; + const {visibleFiles, clients} = sandpack; const clientId = Object.keys(clients)[0]; const {refresh} = useSandpackNavigation(clientId); @@ -31,7 +31,7 @@ export function NavigationBar({showDownload}: {showDownload: boolean}) { }, [dropdownActive]); React.useEffect(() => { - if (openPaths.length > 1) { + if (visibleFiles.length > 1) { resizeHandler(); window.addEventListener('resize', resizeHandler); return () => { @@ -39,7 +39,7 @@ export function NavigationBar({showDownload}: {showDownload: boolean}) { }; } return; - }, [openPaths.length, resizeHandler]); + }, [visibleFiles.length, resizeHandler]); const handleReset = () => { sandpack.resetAllFiles(); @@ -47,7 +47,7 @@ export function NavigationBar({showDownload}: {showDownload: boolean}) { }; return ( -
+
{dropdownActive ? : }
diff --git a/beta/src/components/MDX/Sandpack/Preview.tsx b/beta/src/components/MDX/Sandpack/Preview.tsx index cf1a595b..8df69e8e 100644 --- a/beta/src/components/MDX/Sandpack/Preview.tsx +++ b/beta/src/components/MDX/Sandpack/Preview.tsx @@ -4,12 +4,19 @@ /* eslint-disable react-hooks/exhaustive-deps */ import * as React from 'react'; -import {useSandpack, LoadingOverlay} from '@codesandbox/sandpack-react'; +import { + useSandpack, + LoadingOverlay, + SandpackStack, +} from '@codesandbox/sandpack-react'; import cn from 'classnames'; import {Error} from './Error'; import {SandpackConsole} from './Console'; import type {LintDiagnostic} from './useSandpackLint'; +/** + * TODO: can we use React.useId? + */ const generateRandomId = (): string => Math.floor(Math.random() * 10000).toString(); @@ -149,8 +156,8 @@ export function Preview({ // The best way to test it is to actually go through some challenges. return ( -