1- import type {
2- SandpackOptions ,
3- SandpackSetup ,
4- } from "@codesandbox/sandpack-react" ;
51import {
62 SandpackCodeEditor ,
73 SandpackConsole ,
84 SandpackLayout ,
95 SandpackPreview ,
106 SandpackProvider ,
7+ useErrorMessage ,
118} from "@codesandbox/sandpack-react" ;
129import { useAtomValue } from "jotai" ;
13- import { Fragment } from "react" ;
1410
15- import { CodeEditor } from "./CodeEditor/index.tsx" ;
1611import { dependenciesAtom , lastEditorValueAtom } from "./state/index.ts" ;
1712import { styles } from "./styles.ts" ;
1813
@@ -30,17 +25,6 @@ type TryItNowProps = {
3025 * Starting value of the editor
3126 */
3227 defaultValue ?: string ;
33- /**
34- * Props for the container that wraps the editor and console output.
35- */
36- containerProps ?: React . HTMLAttributes < HTMLDivElement > ;
37- /**
38- * Render only the Sandpack provider and components to use in a
39- * custom container.
40- */
41- disableContainer ?: boolean ;
42- sandpackOptions ?: Partial < SandpackOptions > ;
43- sandpackSetupOptions ?: Partial < SandpackSetup > ;
4428 /**
4529 * Experimental: When enabled, the editor will automatically
4630 * scan for external dependencies from npm as the user adds them
@@ -49,30 +33,40 @@ type TryItNowProps = {
4933 _enableUnsafeAutoImport ?: boolean ;
5034} ;
5135
36+ const TryItNowContents = ( ) => {
37+ const error = useErrorMessage ( ) ;
38+ return (
39+ < SandpackLayout >
40+ < SandpackCodeEditor />
41+ { ! error && (
42+ < SandpackConsole
43+ resetOnPreviewRestart
44+ showSetupProgress
45+ showRestartButton
46+ />
47+ ) }
48+ < SandpackPreview style = { error ? undefined : styles . preview } >
49+ { error ? < pre > { error } </ pre > : null }
50+ </ SandpackPreview >
51+ </ SandpackLayout >
52+ ) ;
53+ } ;
54+
5255export const TryItNow = ( {
5356 externalDependencies,
5457 defaultValue = "" ,
5558 _enableUnsafeAutoImport,
56- containerProps,
57- disableContainer,
58- sandpackOptions = { } ,
59- sandpackSetupOptions = { } ,
6059} : TryItNowProps ) => {
6160 const autoImportDependencies = useAtomValue ( dependenciesAtom ) ;
6261 const previousCodeAtomValue = useAtomValue ( lastEditorValueAtom ) ;
63- const OuterContainer = disableContainer ? Fragment : "div" ;
6462
6563 return (
66- < OuterContainer
67- style = { { ...styles . container , ...containerProps ?. style } }
68- { ...containerProps }
69- >
64+ < div style = { { ...styles . container } } >
7065 < SandpackProvider
7166 options = { {
7267 autoReload : false ,
7368 autorun : false ,
7469 activeFile : "index.tsx" ,
75- ...sandpackOptions ,
7670 } }
7771 template = "vanilla-ts"
7872 files = { {
@@ -90,20 +84,11 @@ export const TryItNow = ({
9084 ? { ...autoImportDependencies , ...externalDependencies }
9185 : externalDependencies ,
9286 entry : "index.tsx" ,
93- ...sandpackSetupOptions ,
9487 } }
9588 theme = "dark"
9689 >
97- < SandpackLayout >
98- < SandpackPreview style = { styles . preview } />
99- { _enableUnsafeAutoImport ? < CodeEditor /> : < SandpackCodeEditor /> }
100- < SandpackConsole
101- resetOnPreviewRestart
102- showSetupProgress
103- showRestartButton
104- />
105- </ SandpackLayout >
90+ < TryItNowContents />
10691 </ SandpackProvider >
107- </ OuterContainer >
92+ </ div >
10893 ) ;
10994} ;
0 commit comments