Skip to content

Commit

Permalink
Sandpack 2.0: upgrade playgrounds (reactjs#5917)
Browse files Browse the repository at this point in the history
* Update 17 files

* remove: legacy react devtool

* Update 3 files

* fix(editor): allow horizontal scroll

* Update SandpackRoot.tsx
  • Loading branch information
danilowoz committed Sep 12, 2023
1 parent 5219d73 commit 43ada1f
Show file tree
Hide file tree
Showing 21 changed files with 248 additions and 815 deletions.
1 change: 0 additions & 1 deletion .env.development
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
SANDPACK_BARE_COMPONENTS=true
3 changes: 1 addition & 2 deletions .env.production
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
NEXT_PUBLIC_GA_TRACKING_ID = 'UA-41298772-4'
SANDPACK_BARE_COMPONENTS=true
NEXT_PUBLIC_GA_TRACKING_ID = 'UA-41298772-4'
8 changes: 1 addition & 7 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ const nextConfig = {
scrollRestoration: true,
legacyBrowsers: false,
},
env: {
SANDPACK_BARE_COMPONENTS: process.env.SANDPACK_BARE_COMPONENTS,
},
env: {},
webpack: (config, {dev, isServer, ...options}) => {
if (process.env.ANALYZE) {
const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer');
Expand All @@ -35,10 +33,6 @@ const nextConfig = {

const {IgnorePlugin, NormalModuleReplacementPlugin} = require('webpack');
config.plugins.push(
new NormalModuleReplacementPlugin(
/^@stitches\/core$/,
require.resolve('./src/utils/emptyShim.js')
),
new NormalModuleReplacementPlugin(
/^raf$/,
require.resolve('./src/utils/rafShim.js')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"check-all": "npm-run-all prettier lint:fix tsc"
},
"dependencies": {
"@codesandbox/sandpack-react": "1.15.5",
"@codesandbox/sandpack-react": "2.6.0",
"@docsearch/css": "3.0.0-alpha.41",
"@docsearch/react": "3.0.0-alpha.41",
"@headlessui/react": "^1.7.0",
Expand Down
30 changes: 0 additions & 30 deletions patches/@codemirror+lang-javascript+0.19.6.patch

This file was deleted.

62 changes: 0 additions & 62 deletions patches/@codesandbox+sandpack-react+1.15.5.patch

This file was deleted.

345 changes: 0 additions & 345 deletions patches/@lezer+javascript+0.15.2.patch

This file was deleted.

7 changes: 5 additions & 2 deletions src/components/MDX/CodeBlock/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
*/

import cn from 'classnames';
import {highlightTree, HighlightStyle, tags} from '@codemirror/highlight';
import {HighlightStyle} from '@codemirror/language';
import {highlightTree} from '@lezer/highlight';
import {javascript} from '@codemirror/lang-javascript';
import {html} from '@codemirror/lang-html';
import {css} from '@codemirror/lang-css';
import rangeParser from 'parse-numeric-range';
import {tags} from '@lezer/highlight';

import {CustomTheme} from '../Sandpack/Themes';

interface InlineHighlight {
Expand Down Expand Up @@ -52,7 +55,7 @@ const CodeBlock = function CodeBlock({
let tokenStarts = new Map();
let tokenEnds = new Map();
const highlightTheme = getSyntaxHighlight(CustomTheme);
highlightTree(tree, highlightTheme.match, (from, to, className) => {
highlightTree(tree, highlightTheme, (from, to, className) => {
tokenStarts.set(from, className);
tokenEnds.set(to, className);
});
Expand Down
5 changes: 4 additions & 1 deletion src/components/MDX/Sandpack/Console.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import cn from 'classnames';
import {useState, useRef, useEffect} from 'react';
import {IconChevron} from 'components/Icon/IconChevron';

import {SandpackCodeViewer, useSandpack} from '@codesandbox/sandpack-react';
import {
SandpackCodeViewer,
useSandpack,
} from '@codesandbox/sandpack-react/unstyled';
import type {SandpackMessageConsoleMethods} from '@codesandbox/sandpack-client';

const getType = (
Expand Down
23 changes: 1 addition & 22 deletions src/components/MDX/Sandpack/CustomPreset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import {
useSandpack,
useActiveCode,
SandpackCodeEditor,
// SandpackReactDevTools,
SandpackLayout,
} from '@codesandbox/sandpack-react';
} from '@codesandbox/sandpack-react/unstyled';
import cn from 'classnames';

import {IconChevron} from 'components/Icon/IconChevron';
Expand All @@ -19,14 +18,8 @@ import {Preview} from './Preview';
import {useSandpackLint} from './useSandpackLint';

export const CustomPreset = memo(function CustomPreset({
showDevTools,
onDevToolsLoad,
devToolsLoaded,
providedFiles,
}: {
showDevTools: boolean;
devToolsLoaded: boolean;
onDevToolsLoad: () => void;
providedFiles: Array<string>;
}) {
const {lintErrors, lintExtensions} = useSandpackLint();
Expand All @@ -41,9 +34,6 @@ export const CustomPreset = memo(function CustomPreset({
const isExpandable = lineCount > 16;
return (
<SandboxShell
showDevTools={showDevTools}
onDevToolsLoad={onDevToolsLoad}
devToolsLoaded={devToolsLoaded}
providedFiles={providedFiles}
lintErrors={lintErrors}
lintExtensions={lintExtensions}
Expand All @@ -53,16 +43,11 @@ export const CustomPreset = memo(function CustomPreset({
});

const SandboxShell = memo(function SandboxShell({
showDevTools,
devToolsLoaded,
providedFiles,
lintErrors,
lintExtensions,
isExpandable,
}: {
showDevTools: boolean;
devToolsLoaded: boolean;
onDevToolsLoad: () => void;
providedFiles: Array<string>;
lintErrors: Array<any>;
lintExtensions: Array<any>;
Expand All @@ -81,7 +66,6 @@ const SandboxShell = memo(function SandboxShell({
<NavigationBar providedFiles={providedFiles} />
<SandpackLayout
className={cn(
showDevTools && devToolsLoaded && 'sp-layout-devtools',
!(isExpandable || isExpanded) && 'rounded-b-lg overflow-hidden',
isExpanded && 'sp-layout-expanded'
)}>
Expand Down Expand Up @@ -123,11 +107,6 @@ const SandboxShell = memo(function SandboxShell({
</button>
)}
</SandpackLayout>

{/* {showDevTools && (
// @ts-ignore TODO(@danilowoz): support devtools
<SandpackReactDevTools onLoadModule={onDevToolsLoad} />
)} */}
</div>
</>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/MDX/Sandpack/DownloadButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import {useSyncExternalStore} from 'react';
import {useSandpack} from '@codesandbox/sandpack-react';
import {useSandpack} from '@codesandbox/sandpack-react/unstyled';
import {IconDownload} from '../../Icon/IconDownload';
export interface DownloadButtonProps {}

Expand Down
2 changes: 1 addition & 1 deletion src/components/MDX/Sandpack/LoadingOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
LoadingOverlayState,
OpenInCodeSandboxButton,
useSandpack,
} from '@codesandbox/sandpack-react';
} from '@codesandbox/sandpack-react/unstyled';
import {useEffect} from 'react';

const FADE_ANIMATION_DURATION = 200;
Expand Down
2 changes: 1 addition & 1 deletion src/components/MDX/Sandpack/NavigationBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
FileTabs,
useSandpack,
useSandpackNavigation,
} from '@codesandbox/sandpack-react';
} from '@codesandbox/sandpack-react/unstyled';
import {OpenInCodeSandboxButton} from './OpenInCodeSandboxButton';
import {ResetButton} from './ResetButton';
import {DownloadButton} from './DownloadButton';
Expand Down
2 changes: 1 addition & 1 deletion src/components/MDX/Sandpack/OpenInCodeSandboxButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Facebook, Inc. and its affiliates.
*/

import {UnstyledOpenInCodeSandboxButton} from '@codesandbox/sandpack-react';
import {UnstyledOpenInCodeSandboxButton} from '@codesandbox/sandpack-react/unstyled';
import {IconNewPage} from '../../Icon/IconNewPage';

export const OpenInCodeSandboxButton = () => {
Expand Down
17 changes: 2 additions & 15 deletions src/components/MDX/Sandpack/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/* eslint-disable react-hooks/exhaustive-deps */
import {useRef, useState, useEffect, useMemo, useId} from 'react';
import {useSandpack, SandpackStack} from '@codesandbox/sandpack-react';
import {useSandpack, SandpackStack} from '@codesandbox/sandpack-react/unstyled';
import cn from 'classnames';
import {ErrorMessage} from './ErrorMessage';
import {SandpackConsole} from './Console';
Expand Down Expand Up @@ -42,14 +42,7 @@ export function Preview({
null
);

let {
error: rawError,
registerBundler,
unregisterBundler,
errorScreenRegisteredRef,
openInCSBRegisteredRef,
loadingScreenRegisteredRef,
} = sandpack;
let {error: rawError, registerBundler, unregisterBundler} = sandpack;

if (
rawError &&
Expand Down Expand Up @@ -88,12 +81,6 @@ export function Preview({
const clientId = useId();
const iframeRef = useRef<HTMLIFrameElement | null>(null);

// SandpackPreview immediately registers the custom screens/components so the bundler does not render any of them
// TODO: why are we doing this during render?
openInCSBRegisteredRef.current = true;
errorScreenRegisteredRef.current = true;
loadingScreenRegisteredRef.current = true;

const sandpackIdle = sandpack.status === 'idle';

useEffect(function createBundler() {
Expand Down
15 changes: 4 additions & 11 deletions src/components/MDX/Sandpack/SandpackRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Copyright (c) Facebook, Inc. and its affiliates.
*/

import {Children, useState} from 'react';
import {Children} from 'react';
import * as React from 'react';
import {SandpackProvider} from '@codesandbox/sandpack-react';
import {SandpackProvider} from '@codesandbox/sandpack-react/unstyled';
import {SandpackLogLevel} from '@codesandbox/sandpack-client';
import {CustomPreset} from './CustomPreset';
import {createFileMap} from './createFileMap';
Expand All @@ -13,7 +13,6 @@ import {CustomTheme} from './Themes';
type SandpackProps = {
children: React.ReactNode;
autorun?: boolean;
showDevTools?: boolean;
};

const sandboxStyle = `
Expand Down Expand Up @@ -67,8 +66,7 @@ ul {
`.trim();

function SandpackRoot(props: SandpackProps) {
let {children, autorun = true, showDevTools = false} = props;
const [devToolsLoaded, setDevToolsLoaded] = useState(false);
let {children, autorun = true} = props;
const codeSnippets = Children.toArray(children) as React.ReactElement[];
const files = createFileMap(codeSnippets);

Expand All @@ -90,12 +88,7 @@ function SandpackRoot(props: SandpackProps) {
bundlerURL: 'https://1e4ad8f7.sandpack-bundler-4bw.pages.dev',
logLevel: SandpackLogLevel.None,
}}>
<CustomPreset
showDevTools={showDevTools}
onDevToolsLoad={() => setDevToolsLoaded(true)}
devToolsLoaded={devToolsLoaded}
providedFiles={Object.keys(files)}
/>
<CustomPreset providedFiles={Object.keys(files)} />
</SandpackProvider>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/MDX/Sandpack/createFileMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Facebook, Inc. and its affiliates.
*/

import type {SandpackFile} from '@codesandbox/sandpack-react';
import type {SandpackFile} from '@codesandbox/sandpack-react/unstyled';

export const createFileMap = (codeSnippets: any) => {
return codeSnippets.reduce(
Expand Down
2 changes: 1 addition & 1 deletion src/content/community/conferences.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ October 5 - 7, 2023. In-person in Goa, India (hybrid event) + Oct 3 2023 - remot

[Website](https://www.reactindia.io) - [Twitter](https://twitter.com/react_india) - [Facebook](https://www.facebook.com/ReactJSIndia) - [Youtube](https://www.youtube.com/channel/UCaFbHCBkPvVv1bWs_jwYt3w)

### React Brussels 2023 {/*react-brussels-2023*/}
### React Brussels 2023 {/*react-brussels-2023*/}
October 13th 2023. In-person in Brussels, Belgium + Remote (hybrid)

[Website](https://www.react.brussels/) - [Twitter](https://twitter.com/BrusselsReact)
Expand Down
19 changes: 1 addition & 18 deletions src/styles/sandpack.css
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ html.dark .sp-wrapper {
background: var(--sp-colors-surface1);
}

.sandpack .sp-code-editor .cm-editor,
.sandpack .sp-code-editor .cm-editor .cm-gutters {
.sandpack .sp-code-editor .cm-editor {
background-color: transparent;
}

Expand Down Expand Up @@ -262,8 +261,6 @@ html.dark .sp-wrapper {
}

.sp-code-editor .sp-cm .cm-scroller {
overflow-x: hidden;
overflow-y: auto;
padding-top: 18px;
}

Expand Down Expand Up @@ -548,20 +545,6 @@ html.dark .sandpack--playground .sp-overlay {
@apply text-secondary;
}

/**
* Integrations: React devtools inline
*/
.sandpack .sp-devtools {
border-bottom-left-radius: 0.5rem;
border-bottom-right-radius: 0.5rem;
overflow: hidden;
}

.sandpack .sp-wrapper .sp-layout-devtools {
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}

/**
* Overwrite inline sty
*/
Expand Down
6 changes: 0 additions & 6 deletions src/utils/emptyShim.js

This file was deleted.

Loading

0 comments on commit 43ada1f

Please sign in to comment.