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
1511import { CodeEditor } from "./CodeEditor/index.tsx" ;
1612import { dependenciesAtom , lastEditorValueAtom } from "./state/index.ts" ;
@@ -30,17 +26,6 @@ type TryItNowProps = {
3026 * Starting value of the editor
3127 */
3228 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 > ;
4429 /**
4530 * Experimental: When enabled, the editor will automatically
4631 * scan for external dependencies from npm as the user adds them
@@ -49,34 +34,48 @@ type TryItNowProps = {
4934 _enableUnsafeAutoImport ?: boolean ;
5035} ;
5136
37+ const TryItNowContents = ( {
38+ _enableUnsafeAutoImport,
39+ } : {
40+ _enableUnsafeAutoImport ?: boolean ;
41+ } ) => {
42+ const error = useErrorMessage ( ) ;
43+ return (
44+ < SandpackLayout >
45+ { _enableUnsafeAutoImport ? < CodeEditor /> : < SandpackCodeEditor /> }
46+ { ! error && (
47+ < SandpackConsole
48+ resetOnPreviewRestart
49+ showSetupProgress
50+ showRestartButton
51+ />
52+ ) }
53+ < SandpackPreview style = { error ? undefined : styles . preview } >
54+ { error ? < pre > { error } </ pre > : null }
55+ </ SandpackPreview >
56+ </ SandpackLayout >
57+ ) ;
58+ } ;
59+
5260export const TryItNow = ( {
5361 externalDependencies,
5462 defaultValue = "" ,
5563 _enableUnsafeAutoImport,
56- containerProps,
57- disableContainer,
58- sandpackOptions = { } ,
59- sandpackSetupOptions = { } ,
6064} : TryItNowProps ) => {
6165 const autoImportDependencies = useAtomValue ( dependenciesAtom ) ;
6266 const previousCodeAtomValue = useAtomValue ( lastEditorValueAtom ) ;
63- const OuterContainer = disableContainer ? Fragment : "div" ;
6467
6568 return (
66- < OuterContainer
67- style = { { ...styles . container , ...containerProps ?. style } }
68- { ...containerProps }
69- >
69+ < div style = { { ...styles . container } } >
7070 < SandpackProvider
7171 options = { {
7272 autoReload : false ,
7373 autorun : false ,
74- activeFile : "index.tsx" ,
75- ...sandpackOptions ,
74+ activeFile : "index.ts" ,
7675 } }
7776 template = "vanilla-ts"
7877 files = { {
79- "index.tsx " : {
78+ "index.ts " : {
8079 code :
8180 _enableUnsafeAutoImport && previousCodeAtomValue
8281 ? previousCodeAtomValue
@@ -89,21 +88,12 @@ export const TryItNow = ({
8988 autoImportDependencies && _enableUnsafeAutoImport
9089 ? { ...autoImportDependencies , ...externalDependencies }
9190 : externalDependencies ,
92- entry : "index.tsx" ,
93- ...sandpackSetupOptions ,
91+ entry : "index.ts" ,
9492 } }
9593 theme = "dark"
9694 >
97- < SandpackLayout >
98- < SandpackPreview style = { styles . preview } />
99- { _enableUnsafeAutoImport ? < CodeEditor /> : < SandpackCodeEditor /> }
100- < SandpackConsole
101- resetOnPreviewRestart
102- showSetupProgress
103- showRestartButton
104- />
105- </ SandpackLayout >
95+ < TryItNowContents />
10696 </ SandpackProvider >
107- </ OuterContainer >
97+ </ div >
10898 ) ;
10999} ;
0 commit comments