Skip to content

Commit 7a3e2dc

Browse files
authored
Try it now error UI (#21)
* Added error reporting for try it now * Cleanup * lint fix
1 parent 00107f8 commit 7a3e2dc

File tree

2 files changed

+36
-45
lines changed

2 files changed

+36
-45
lines changed
Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
import type {
2-
SandpackOptions,
3-
SandpackSetup,
4-
} from "@codesandbox/sandpack-react";
51
import {
62
SandpackCodeEditor,
73
SandpackConsole,
84
SandpackLayout,
95
SandpackPreview,
106
SandpackProvider,
7+
useErrorMessage,
118
} from "@codesandbox/sandpack-react";
129
import { useAtomValue } from "jotai";
13-
import { Fragment } from "react";
1410

1511
import { CodeEditor } from "./CodeEditor/index.tsx";
1612
import { 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+
5260
export 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
};

packages/docs-md/src/generator/mdx/renderer.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class Site {
4343
recursive: true,
4444
withFileTypes: true,
4545
})
46-
.filter((f) => f.isFile())
46+
.filter((f) => f.isFile() && f.name !== "tsconfig.json")
4747
.map((f) => join(f.parentPath, f.name).replace(ASSET_PATH + "/", ""));
4848
for (const assetFile of assetFileList) {
4949
this.#pages.set(
@@ -213,13 +213,14 @@ sidebarTitle: ${this.escapeText(sidebarLabel)}
213213
title: string;
214214
embedName: string;
215215
}) {
216-
this.#includeSidebar = true;
217-
this.insertComponentImport("SideBarCta", "SideBar/index.tsx");
218-
this.insertComponentImport("SideBar", "SideBar/index.tsx");
216+
// If this is a circular import, skip processing sidebar
219217
if (!this.#insertEmbedImport(embedName)) {
220-
console.error(`Direct circular import detected, skipping sidebar link`);
218+
// TODO: add debug logging
221219
return;
222220
}
221+
this.#includeSidebar = true;
222+
this.insertComponentImport("SideBarCta", "SideBar/index.tsx");
223+
this.insertComponentImport("SideBar", "SideBar/index.tsx");
223224
this.#lines.push(
224225
`<p>
225226
<SideBarCta cta="${`View ${this.escapeText(title, { mdxOnly: true })}`}" title="${this.escapeText(title)}">

0 commit comments

Comments
 (0)