-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
Changes from all commits
059d8c7
c5ebd61
2d4e27b
5202a8f
50498f5
c53ec60
b5adb42
3679b10
1301a9d
1425f2c
5666721
77b5860
29d034e
b7f3766
8fbcaac
5a522d1
15f8c71
877b8b2
c29fa17
5b40436
bfe9ad9
a91d10e
a965508
2a1cf07
32ba5ff
0686663
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) => { | ||
|
@@ -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} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||
); | ||
break; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
* Copyright (c) Facebook, Inc. and its affiliates. | ||
*/ | ||
|
||
import * as React from 'react'; | ||
import cn from 'classnames'; | ||
import { | ||
ClasserProvider, | ||
|
@@ -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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only removed |
||
children, | ||
className = 'language-js', | ||
metastring, | ||
noMargin, | ||
noMarkers, | ||
}: { | ||
children: string; | ||
className?: string; | ||
metastring: string; | ||
noMargin?: boolean; | ||
noMarkers?: boolean; | ||
}) { | ||
const getDecoratedLineInfo = () => { | ||
if (!metastring) { | ||
return []; | ||
|
@@ -95,7 +91,7 @@ const CodeBlock = React.forwardRef(function CodeBlock( | |
'sp-cm': styles.codeViewer, | ||
}}> | ||
<SandpackCodeViewer | ||
ref={ref} | ||
key={children.trimEnd()} | ||
showLineNumbers={false} | ||
decorators={decorators} | ||
/> | ||
|
@@ -104,7 +100,7 @@ const CodeBlock = React.forwardRef(function CodeBlock( | |
</SandpackProvider> | ||
</div> | ||
); | ||
}); | ||
}; | ||
|
||
export default CodeBlock; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,29 +3,20 @@ | |
*/ | ||
|
||
import * as React from 'react'; | ||
import {useCodeSandboxLink} from '@codesandbox/sandpack-react'; | ||
import cn from 'classnames'; | ||
import {UnstyledOpenInCodeSandboxButton} from '@codesandbox/sandpack-react'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 {IconNewPage} from '../../Icon/IconNewPage'; | ||
|
||
export const OpenInCodeSandboxButton = ({className}: {className?: string}) => { | ||
const url = useCodeSandboxLink(); | ||
|
||
export const OpenInCodeSandboxButton = () => { | ||
return ( | ||
<a | ||
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" | ||
<UnstyledOpenInCodeSandboxButton | ||
className="text-sm text-primary dark:text-primary-dark inline-flex items-center hover:text-link duration-100 ease-in transition mx-1 ml-3 md:ml-1" | ||
title="Open in CodeSandbox"> | ||
<span className="hidden md:inline"> | ||
<IconNewPage className="inline mb-0.5 text-base" /> Fork | ||
</span> | ||
<span className="inline md:hidden"> | ||
<IconNewPage className="inline mb-0.5 text-base" /> Fork | ||
</span> | ||
</a> | ||
<IconNewPage | ||
className="inline mb-0.5 ml-1 mr-1 relative top-px" | ||
width=".8em" | ||
height=".8em" | ||
/> | ||
<span className="hidden md:block">Fork</span> | ||
</UnstyledOpenInCodeSandboxButton> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ type SandpackProps = { | |
children: React.ReactChildren; | ||
autorun?: boolean; | ||
setup?: SandpackSetup; | ||
showDevTools?: boolean; | ||
}; | ||
|
||
const sandboxStyle = ` | ||
|
@@ -64,8 +65,8 @@ ul { | |
`.trim(); | ||
|
||
function Sandpack(props: SandpackProps) { | ||
let {children, setup, autorun = true} = props; | ||
let [resetKey, setResetKey] = React.useState(0); | ||
let {children, setup, autorun = true, showDevTools = false} = props; | ||
const [devToolsLoaded, setDevToolsLoaded] = React.useState(false); | ||
let codeSnippets = React.Children.toArray(children) as React.ReactElement[]; | ||
let isSingleFile = true; | ||
|
||
|
@@ -121,29 +122,17 @@ function Sandpack(props: SandpackProps) { | |
hidden: true, | ||
}; | ||
|
||
let key = String(resetKey); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this removed? Is this not needed anymore? We use it in development so that editing sandbox source immediately forces it to refresh. This is important for authoring content. See the lines just below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry if I'm missing something, but in my local tests, both situations worked fine (development and production mode). The main reason I removed the whole reset logic is that this key was causing an unnecessary render on production mode, and Sandpack already provides an API to reset all changes. However, even in development mode, any changes on I brought back the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah this looks like it didn't work out of the box in the beginning of 2021, but has since been fixed! |
||
if (process.env.NODE_ENV !== 'production') { | ||
// Remount on any source change in development. | ||
key += | ||
'-' + | ||
JSON.stringify({ | ||
...props, | ||
children: files, | ||
}); | ||
} | ||
|
||
return ( | ||
<div className="my-8" translate="no"> | ||
<div className="sandpack-container my-8" translate="no"> | ||
<SandpackProvider | ||
key={key} | ||
template="react" | ||
customSetup={{...setup, files: files}} | ||
autorun={autorun}> | ||
<CustomPreset | ||
isSingleFile={isSingleFile} | ||
onReset={() => { | ||
setResetKey((k) => k + 1); | ||
}} | ||
showDevTools={showDevTools} | ||
onDevToolsLoad={() => setDevToolsLoaded(true)} | ||
devToolsLoaded={devToolsLoaded} | ||
/> | ||
</SandpackProvider> | ||
</div> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine 👍🏼 at least until the caching APIs become stable.