-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Sandpack: upgrade dependencies and adds ReactDevtools #4161
Changes from 22 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,6 @@ const CodeBlock = React.forwardRef(function CodeBlock( | |
'sp-cm': styles.codeViewer, | ||
}}> | ||
<SandpackCodeViewer | ||
ref={ref} | ||
showLineNumbers={false} | ||
decorators={decorators} | ||
/> | ||
|
@@ -104,7 +99,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,21 @@ | |
*/ | ||
|
||
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" | ||
harish-sethuraman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
width=".8em" | ||
height=".8em" | ||
style={{top: 1}} | ||
danilowoz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/> | ||
<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,9 @@ 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); | ||
const [devToolsLoaded, setDevToolsLoaded] = React.useState(false); | ||
let codeSnippets = React.Children.toArray(children) as React.ReactElement[]; | ||
let isSingleFile = true; | ||
|
||
|
@@ -133,16 +135,25 @@ function Sandpack(props: SandpackProps) { | |
} | ||
|
||
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}> | ||
autorun={autorun} | ||
key={key}> | ||
<CustomPreset | ||
isSingleFile={isSingleFile} | ||
showDevTools={showDevTools} | ||
onDevToolsLoad={() => setDevToolsLoaded(true)} | ||
devToolsLoaded={devToolsLoaded} | ||
onReset={() => { | ||
setResetKey((k) => k + 1); | ||
/** | ||
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. Hmm actually this doesn't seem necessary anymore? If I remove this onReset and even the key generation logic, it still works in development and replaces the sandbox when MDX content is edited. So we can remove this after all? |
||
* Leverage Sandpack API to reset the changes | ||
* and refresh the preview component | ||
*/ | ||
if (process.env.NODE_ENV !== 'production') { | ||
setResetKey((k) => k + 1); | ||
} | ||
}} | ||
/> | ||
</SandpackProvider> | ||
|
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.