Skip to content

Sandpack: upgrade dependencies and adds ReactDevtools #4161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion beta/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
Expand Down
6 changes: 5 additions & 1 deletion beta/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"check-all": "npm-run-all prettier lint:fix tsc"
},
"dependencies": {
"@codesandbox/sandpack-react": "^0.1.20",
"@codesandbox/sandpack-react": "^0.10.2",
"@docsearch/css": "3.0.0-alpha.41",
"@docsearch/react": "3.0.0-alpha.41",
"@headlessui/react": "^1.3.0",
Expand Down Expand Up @@ -89,6 +89,10 @@
"unist-util-visit": "^2.0.3",
"webpack-bundle-analyzer": "^4.5.0"
},
"resolutions": {
"@codemirror/gutter": "0.19.4",
"@codemirror/view": "0.19.14"
},
"engines": {
"node": ">=12.x"
},
Expand Down
7 changes: 1 addition & 6 deletions beta/src/components/MDX/APIAnatomy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const colors = [

export function APIAnatomy({children}: APIAnatomyProps) {
const [activeStep, setActiveStep] = React.useState<number | null>(null);
const ref = React.useRef<HTMLDivElement>();

const {steps, code} = React.Children.toArray(children).reduce(
(acc: AnatomyContent, child) => {
Expand All @@ -60,11 +59,7 @@ export function APIAnatomy({children}: APIAnatomyProps) {
break;
case 'pre':
acc.code = (
<CodeBlock
ref={ref}
{...child.props.children.props}
noMargin={true}
/>
<CodeBlock {...child.props.children.props} noMargin={true} />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed ref as this hasn't been used anywhere

);
break;
}
Expand Down
33 changes: 14 additions & 19 deletions beta/src/components/MDX/CodeBlock/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* Copyright (c) Facebook, Inc. and its affiliates.
*/

import * as React from 'react';
import cn from 'classnames';
import {
ClasserProvider,
Expand All @@ -21,22 +20,19 @@ interface InlineHiglight {
endColumn: number;
}

const CodeBlock = React.forwardRef(function CodeBlock(
{
children,
className = 'language-js',
metastring,
noMargin,
noMarkers,
}: {
children: string;
className?: string;
metastring: string;
noMargin?: boolean;
noMarkers?: boolean;
},
ref?: React.Ref<HTMLDivElement>
) {
const CodeBlock = function CodeBlock({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only removed forwardRef

children,
className = 'language-js',
metastring,
noMargin,
noMarkers,
}: {
children: string;
className?: string;
metastring: string;
noMargin?: boolean;
noMarkers?: boolean;
}) {
const getDecoratedLineInfo = () => {
if (!metastring) {
return [];
Expand Down Expand Up @@ -95,7 +91,6 @@ const CodeBlock = React.forwardRef(function CodeBlock(
'sp-cm': styles.codeViewer,
}}>
<SandpackCodeViewer
ref={ref}
showLineNumbers={false}
decorators={decorators}
/>
Expand All @@ -104,7 +99,7 @@ const CodeBlock = React.forwardRef(function CodeBlock(
</SandpackProvider>
</div>
);
});
};

export default CodeBlock;

Expand Down
4 changes: 4 additions & 0 deletions beta/src/components/MDX/Sandpack/CustomPreset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
useActiveCode,
SandpackCodeEditor,
SandpackThemeProvider,
SandpackReactDevTools,
} from '@codesandbox/sandpack-react';
import scrollIntoView from 'scroll-into-view-if-needed';

Expand Down Expand Up @@ -77,6 +78,7 @@ export function CustomPreset({
maxHeight: isExpanded ? '' : 406,
}}
/>

{isExpandable && (
<button
translate="yes"
Expand Down Expand Up @@ -104,6 +106,8 @@ export function CustomPreset({
</button>
)}
</div>

<SandpackReactDevTools />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll want to make this conditional. (Show DevTools for some sandboxes, but not every one.)

diff --git a/beta/src/components/MDX/Sandpack/CustomPreset.tsx b/beta/src/components/MDX/Sandpack/CustomPreset.tsx
index 7050dcb6..acf0cb77 100644
--- a/beta/src/components/MDX/Sandpack/CustomPreset.tsx
+++ b/beta/src/components/MDX/Sandpack/CustomPreset.tsx
@@ -22,9 +22,11 @@ import {CustomTheme} from './Themes';
 export function CustomPreset({
   isSingleFile,
   onReset,
+  showDevTools,
 }: {
   isSingleFile: boolean;
   onReset: () => void;
+  showDevTools: boolean;
 }) {
   const lineCountRef = React.useRef<{[key: string]: number}>({});
   const containerRef = React.useRef<HTMLDivElement>(null);
@@ -107,7 +109,7 @@ export function CustomPreset({
             )}
           </div>
 
-          <SandpackReactDevTools />
+          {showDevTools && <SandpackReactDevTools />}
         </SandpackThemeProvider>
       </div>
     </>
diff --git a/beta/src/components/MDX/Sandpack/index.tsx b/beta/src/components/MDX/Sandpack/index.tsx
index 3cfa4ebd..374a6d51 100644
--- a/beta/src/components/MDX/Sandpack/index.tsx
+++ b/beta/src/components/MDX/Sandpack/index.tsx
@@ -15,6 +15,7 @@ type SandpackProps = {
   children: React.ReactChildren;
   autorun?: boolean;
   setup?: SandpackSetup;
+  showDevTools?: boolean;
 };
 
 const sandboxStyle = `
@@ -64,7 +65,7 @@ ul {
 `.trim();
 
 function Sandpack(props: SandpackProps) {
-  let {children, setup, autorun = true} = props;
+  let {children, setup, autorun = true, showDevTools = false} = props;
   let [resetKey, setResetKey] = React.useState(0);
   let codeSnippets = React.Children.toArray(children) as React.ReactElement[];
   let isSingleFile = true;
@@ -144,6 +145,7 @@ function Sandpack(props: SandpackProps) {
           onReset={() => {
             setResetKey((k) => k + 1);
           }}
+          showDevTools={showDevTools}
         />
       </SandpackProvider>
     </div>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to just push this small change myself but I don't have permissions to push to your fork :D

</SandpackThemeProvider>
</div>
</>
Expand Down
16 changes: 4 additions & 12 deletions beta/src/components/MDX/Sandpack/OpenInCodeSandboxButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,21 @@
*/

import * as React from 'react';
import {useCodeSandboxLink} from '@codesandbox/sandpack-react';
import {UnstyledOpenInCodeSandboxButton} from '@codesandbox/sandpack-react';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor Sandpack break change that fixed a bug for long sandboxes

import cn from 'classnames';
import {IconNewPage} from '../../Icon/IconNewPage';

export const OpenInCodeSandboxButton = ({className}: {className?: string}) => {
const url = useCodeSandboxLink();

return (
<a
<UnstyledOpenInCodeSandboxButton
className={cn(
'text-sm text-primary dark:text-primary-dark inline-flex items-center hover:text-link duration-100 ease-in transition mx-1',
className
)}
href={url}
rel="noreferrer noopener"
target="_blank"
title="Open in CodeSandbox">
<span className="hidden md:inline">
<IconNewPage className="inline mb-0.5 text-base" /> Fork
</span>
<span className="inline md:hidden">
<span className="inline">
<IconNewPage className="inline mb-0.5 text-base" /> Fork
</span>
</a>
</UnstyledOpenInCodeSandboxButton>
);
};
Loading