Skip to content

Commit

Permalink
Sandpack: support react devtools (#6337)
Browse files Browse the repository at this point in the history
* draft

* csb-core: support reactDevTools option
  • Loading branch information
danilowoz authored Dec 8, 2021
1 parent 646c713 commit a1c4232
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
13 changes: 11 additions & 2 deletions packages/app/src/sandbox/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,12 @@ async function initializeManager(
{
hasFileResolver = false,
customNpmRegistries = [],
}: { hasFileResolver?: boolean; customNpmRegistries?: NpmRegistry[] } = {}
reactDevTools = false,
}: {
hasFileResolver?: boolean;
customNpmRegistries?: NpmRegistry[];
reactDevTools?: boolean;
} = {}
) {
const newManager = new Manager(
sandboxId,
Expand All @@ -323,6 +328,7 @@ async function initializeManager(
{
hasFileResolver,
versionIdentifier: SCRIPT_VERSION,
reactDevTools,
}
);

Expand Down Expand Up @@ -452,6 +458,7 @@ interface CompileOptions {
hasFileResolver?: boolean;
disableDependencyPreprocessing?: boolean;
clearConsoleDisabled?: boolean;
reactDevTools?: boolean;
}

async function compile(opts: CompileOptions) {
Expand All @@ -471,6 +478,7 @@ async function compile(opts: CompileOptions) {
hasFileResolver = false,
disableDependencyPreprocessing = false,
clearConsoleDisabled = false,
reactDevTools = false,
} = opts;

if (firstLoad) {
Expand Down Expand Up @@ -538,6 +546,7 @@ async function compile(opts: CompileOptions) {
(await initializeManager(sandboxId, template, modules, configurations, {
hasFileResolver,
customNpmRegistries,
reactDevTools,
}));

let dependencies: NPMDependencies = getDependencies(
Expand Down Expand Up @@ -605,7 +614,7 @@ async function compile(opts: CompileOptions) {
template,
modules,
configurations,
{ hasFileResolver }
{ hasFileResolver, reactDevTools }
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export async function reactPreset(pkg: PackageJSON) {
}
},
preEvaluate: async manager => {
if (manager.isFirstLoad && !process.env.SANDPACK) {
if (manager.isFirstLoad && manager.reactDevTools) {
await initializeReactDevTools();
}

Expand Down
10 changes: 8 additions & 2 deletions packages/common/src/components/Preview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ class BasePreview extends React.PureComponent<Props, State> {
previewSecret: sandbox.previewSecret,
showScreen,
clearConsoleDisabled: !settings.clearConsoleEnabled,
reactDevTools: true,
});
}
}
Expand Down Expand Up @@ -639,9 +640,14 @@ class BasePreview extends React.PureComponent<Props, State> {
...style,
zIndex: 1,
backgroundColor: 'white',
userSelect: this.props.isResponsivePreviewResizing ? "none" : "initial",
userSelect: this.props.isResponsivePreviewResizing
? 'none'
: 'initial',
pointerEvents:
dragging || inactive || this.props.isResizing || this.props.isResponsivePreviewResizing
dragging ||
inactive ||
this.props.isResizing ||
this.props.isResponsivePreviewResizing
? 'none'
: 'initial',
}}
Expand Down
4 changes: 4 additions & 0 deletions packages/sandpack-core/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ type TManagerOptions = {
*/
hasFileResolver: boolean;
versionIdentifier: string;
reactDevTools: boolean;
};

function triggerFileWatch(path: string, type: 'rename' | 'change') {
Expand Down Expand Up @@ -164,6 +165,8 @@ export default class Manager implements IEvaluator {
};
};

reactDevTools: boolean;

envVariables: { [envName: string]: string } = {};
preset: Preset;
modules: ModuleObject;
Expand Down Expand Up @@ -226,6 +229,7 @@ export default class Manager implements IEvaluator {
this.version = options.versionIdentifier;
this.esmodules = new Map();
this.resolverCache = new Map();
this.reactDevTools = options.reactDevTools;

/**
* Contribute the file fetcher, which needs the manager to resolve the files
Expand Down

0 comments on commit a1c4232

Please sign in to comment.