-
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
Conversation
Small observation. Previously in the sandpack |
Wowowow thank you! 🙇🏼 This is great! Let me pick it up and take a look. |
This is something I can take a look at! |
@@ -104,6 +106,8 @@ export function CustomPreset({ | |||
</button> | |||
)} | |||
</div> | |||
|
|||
<SandpackReactDevTools /> |
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 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>
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.
Tried to just push this small change myself but I don't have permissions to push to your fork :D
Note to self: Navigating the Components tree with arrow keys on a page with multiple Sandpacks feels slow. (It takes several up/down arrows to change the selected component.) Clicking to change the selected component is fast though. |
I think we'll have to solve this by passing through some params to DevTools to make sure it gets wired up to only its sibling sandbox. Going to dig a little deeper this afternoon and come up with a proposal. This will likely require a small change to the |
Status update: I need to update I got a good start on this and will try to wrap it up on Monday! |
Let's not merge until we've addressed this. (Would be good to chat with @lebo about the design) |
No. It's off by default (after my commit).
Don't know, although DevTools works well on touch devices.
Agreed. No hurry to merge this. Need to solve the multi-DevTools-per-page issue first too. |
I would put this component behind an editor option (like a tick or something) because once the user has learned to use devtool, it should be available to use all the time. However, I would keep it hidden by default and only turn it on for specific sandboxes (those related to the devtool, for example).
I'll address it. It seems something is no longer working on the Codemirror side. |
Once facebook/react#22949 lands, we should be able to publish a new DevTools release and then update this PR to fix this error: |
The But we'll need to update the Code Sandbox client (backend) and Sandpack (frontend) to share a wall as shown in the README here: https://github.com/facebook/react/blob/main/packages/react-devtools-inline/README.md#advanced-integration-with-custom-wall |
I still see an Inspect button on every sandbox which, when clicked, refreshes the sandbox. |
@gaearon just removed it too, let me know if anything else is missing. |
At this point, I defer to @gaearon to give the final stamp to land this. (Unless you want me to make that call, Dan.) I can't recall any blocking issues on the DevTools side in terms of features that we need to add before we can start making use of this in the new docs (@danilowoz– remind me or link me if I'm forgetting anything). |
@@ -121,29 +124,17 @@ function Sandpack(props: SandpackProps) { | |||
hidden: true, | |||
}; | |||
|
|||
let key = String(resetKey); |
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.
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 comment
The 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 files
shouldn't be enough to trigger a remount on the Sandpack component? At least, in the Sandpack implementation, we're taking this into account, but maybe I'm missing some specific case.
I brought back the key
logic anyway, but I added a new conditional not to remount it on production mode, let me know if it makes sense.
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.
but in my local tests, both situations worked fine (development and production mode)
Yeah this looks like it didn't work out of the box in the beginning of 2021, but has since been fixed!
I'm not sure why bot comment doesn't work. But this seems to have increased the page weight a bit (+7 KB minified). Is this because of the DevTools feature specifically, or was it a general sandbox code increase? https://github.com/reactjs/reactjs.org/runs/4839615866?check_suite_focus=true#step:10:187 I'd like, if possible, to hold the line since we're already not doing great on code size. |
@gaearon I tracked down and only the devtools dependency has increased the bundle in ~4.8k, the rest might be the CSS added in this PR and other minor changes in the Sandpack codebase. Although I agree that we need to keep in mind the final page weight, I'd like to highlight that the latest Sandpack version introduced many bug fixes and performance improvements (some of which we've already spoken about before). So given the tradeoff, I believe it's still worth merging and addressing the devtool dependency issue in a different PR (lazily loading it might help, as I'm doing here #4112). |
onReset={() => { | ||
setResetKey((k) => k + 1); | ||
/** |
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.
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?
This PR introduces a flash in the loading sequence: flash.movFor a moment, the entire code disappears: There's also a weird double border in the middle. Compare it to the loading sequence on the live site: smooth.movLet's make sure that we test this when doing future upgrades because we really don't want to regress like this. |
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.
need to fix a regression in the initial loading sequence
|
@bvaughn sorry you weren't able to upgrade Sandpack in #4159, so I created this PR to address those and a minor break change in the CodeSandbox fork button. So, this PR adds:
Unrecognized extension value in extension set ([object Object])
, I think it's due to some inconsistency in the dependencies from codemirror;Blockers