Skip to content

Commit 97a411a

Browse files
committed
Add tab interaction
1 parent 3885978 commit 97a411a

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

playground/src/app.jsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ print("This is Code Hike")
1414
1515
`;
1616

17+
const defaultInput = {
18+
mdx: defaultCode,
19+
css: ".preview-container { border: 1px solid blue; }",
20+
config: {},
21+
};
22+
1723
function App() {
18-
const [code, setCode] = useState(defaultCode);
24+
const [input, setInput] = useState(defaultInput);
1925

2026
return (
2127
<div className="app">
@@ -29,8 +35,8 @@ function App() {
2935
</a>
3036
</header>
3137
<main>
32-
<Editor setCode={setCode} defaultCode={defaultCode} />
33-
<Preview code={code} />
38+
<Editor setInput={setInput} input={input} />
39+
<Preview input={input} />
3440
</main>
3541
</div>
3642
);

playground/src/editor.jsx

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
import MonacoEditor from "@monaco-editor/react";
22
import { useState } from "react";
33

4-
export function Editor({ setCode, defaultCode }) {
5-
function handleEditorChange(value, event) {
6-
setCode(value);
7-
}
4+
const tabs = {
5+
mdx: {
6+
lang: "markdown",
7+
code: (input) => input.mdx,
8+
},
9+
css: {
10+
lang: "css",
11+
code: (input) => input.css,
12+
},
13+
config: {
14+
lang: "json",
15+
code: (input) => JSON.stringify(input.config, null, 2),
16+
},
17+
};
818

19+
export function Editor({ setInput, input }) {
920
const [tab, setTab] = useState("mdx");
21+
22+
function handleEditorChange(code, event) {
23+
let value = code;
24+
if (tab === "config") {
25+
console.log({ code });
26+
value = JSON.parse(code);
27+
}
28+
setInput((prev) => ({ ...prev, [tab]: value }));
29+
}
30+
1031
return (
1132
<div className="editor-side">
1233
<nav>
@@ -35,9 +56,10 @@ export function Editor({ setCode, defaultCode }) {
3556
<MonacoEditor
3657
className="editor"
3758
onChange={handleEditorChange}
38-
defaultLanguage="markdown"
3959
theme="vs-dark"
40-
defaultValue={defaultCode}
60+
path={tab}
61+
defaultLanguage={tabs[tab].lang}
62+
defaultValue={tabs[tab].code(input)}
4163
options={{
4264
// https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.IEditorConstructionOptions.html
4365
minimap: {

playground/src/index.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ main {
7777
.editor-side nav {
7878
background-color: #111;
7979
color: #fafafa;
80-
height: 2rem;
80+
height: 2.2rem;
8181
min-height: 2rem;
8282
border-bottom: 1px solid #444;
8383
display: flex;
@@ -94,6 +94,9 @@ main {
9494
box-sizing: border-box;
9595
padding-top: 3px;
9696
padding-bottom: 3px;
97+
font-size: 0.95em;
98+
display: inline-flex;
99+
align-items: center;
97100
}
98101

99102
.editor-tab[data-active="true"] {

playground/src/preview.jsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import theme from "shiki/themes/material-darker.json";
1010
export function Preview(props) {
1111
return (
1212
<div className="preview">
13-
<ErrorBoundary resetKeys={[props.code]} FallbackComponent={ErrorFallback}>
13+
<ErrorBoundary
14+
resetKeys={[props.input.mdx, props.input.css, props.input.config]}
15+
FallbackComponent={ErrorFallback}
16+
>
1417
<InnerPreview {...props} />
1518
</ErrorBoundary>
1619
</div>
@@ -25,13 +28,15 @@ function ErrorFallback({ error }) {
2528
);
2629
}
2730

28-
function InnerPreview({ code }) {
31+
function InnerPreview({ input }) {
2932
const [Content, setContent] = useState(undefined);
3033
const [error, setError] = useState(undefined);
3134
useEffect(() => {
32-
compile(code, {
35+
compile(input.mdx, {
3336
outputFormat: "function-body",
34-
remarkPlugins: [[remarkCodeHike, { autoImport: false, theme }]],
37+
remarkPlugins: [
38+
[remarkCodeHike, { ...input.config, autoImport: false, theme }],
39+
],
3540
})
3641
.then((c) => {
3742
return run(String(c), runtime);
@@ -44,7 +49,7 @@ function InnerPreview({ code }) {
4449
setError(e.message);
4550
console.error({ e });
4651
});
47-
}, [code]);
52+
}, [input.mdx, input.css, input.config]);
4853
// console.log(error);
4954
return (
5055
<>

0 commit comments

Comments
 (0)