Skip to content

Commit 815bba7

Browse files
committed
docs: add useCodeExecution docs and update README link
1 parent 1de3379 commit 815bba7

6 files changed

Lines changed: 235 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
A React component library for rendering code with **live preview** and syntax highlighting.
88

9-
![React Code View](https://user-images.githubusercontent.com/1203827/178659124-f4a8658f-1087-4c55-b89b-04dcfc5568cb.gif)
9+
[Docs](https://react-code-view-rsuite.vercel.app/)
1010

1111
## ✨ Features
1212

docs/components/Sidebar.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const Sidebar: React.FC = () => {
2424
{ path: '/components/code-editor', label: 'CodeEditor' },
2525
{ path: '/components/renderer', label: 'Renderer' },
2626
{ path: '/components/markdown-renderer', label: 'MarkdownRenderer' },
27+
{ path: '/components/use-code-execution', label: 'useCodeExecution (hook)' },
2728
]
2829
},
2930
{
@@ -43,18 +44,19 @@ export const Sidebar: React.FC = () => {
4344
{ path: '/examples/typescript', label: 'TypeScript' },
4445
{ path: '/examples/theme', label: 'Theme Switcher' },
4546
{ path: '/examples/components', label: 'Custom Components' },
47+
{ path: '/examples/use-code-execution', label: 'useCodeExecution' },
4648
]
4749
}
4850
];
4951

5052
return (
5153
<aside className="docs-sidebar">
5254
<div className="sidebar-content">
53-
{sections.map((section) => (
55+
{sections.map(section => (
5456
<div key={section.title} className="sidebar-section">
5557
<h3 className="sidebar-title">{section.title}</h3>
5658
<ul className="sidebar-list">
57-
{section.items.map((item) => (
59+
{section.items.map(item => (
5860
<li key={item.path}>
5961
<Link
6062
to={item.path}

docs/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ import { InstallationPage } from './pages/InstallationPage';
1515
import { QuickStartPage } from './pages/QuickStartPage';
1616
import { ComponentsPage } from './pages/ComponentsPage';
1717
import { BuildToolsPage } from './pages/BuildToolsPage';
18+
import { UseCodeExecutionPage } from './pages/UseCodeExecutionPage';
1819

1920
// Examples
2021
import { CounterExample } from './pages/examples/CounterExample';
2122
import { TodoExample } from './pages/examples/TodoExample';
2223
import { TypeScriptExample } from './pages/examples/TypeScriptExample';
2324
import { ThemeExample } from './pages/examples/ThemeExample';
2425
import { ComponentsExample } from './pages/examples/ComponentsExample';
26+
import { UseCodeExecutionExample } from './pages/examples/UseCodeExecutionExample';
2527

2628
// Pre-initialize Shiki for faster first render
2729
initHighlighter();
@@ -61,12 +63,14 @@ const App: React.FC = () => {
6163
<Route path="/installation" element={<InstallationPage theme={theme} />} />
6264
<Route path="/quick-start" element={<QuickStartPage theme={theme} />} />
6365
<Route path="/components/:component" element={<ComponentsPage theme={theme} />} />
66+
<Route path="/components/use-code-execution" element={<UseCodeExecutionPage theme={theme} />} />
6467
<Route path="/build-tools/:tool" element={<BuildToolsPage theme={theme} />} />
6568
<Route path="/examples/counter" element={<CounterExample theme={theme} />} />
6669
<Route path="/examples/todo" element={<TodoExample theme={theme} />} />
6770
<Route path="/examples/typescript" element={<TypeScriptExample theme={theme} />} />
6871
<Route path="/examples/theme" element={<ThemeExample theme={theme} />} />
6972
<Route path="/examples/components" element={<ComponentsExample theme={theme} />} />
73+
<Route path="/examples/use-code-execution" element={<UseCodeExecutionExample theme={theme} />} />
7074
</Routes>
7175
</main>
7276
</div>

docs/pages/QuickStartPage.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ export default function Demo() {
7979
<p>See props for CodeView & friends</p>
8080
</Link>
8181
</div>
82+
83+
<h3>Need bundler setup?</h3>
84+
<p>
85+
We provide ready-to-copy configs for Vite, Webpack, Rollup, Esbuild, and Rspack in the
86+
<Link to="/build-tools/vite"> Build Tools</Link> section.
87+
</p>
8288
</Section>
8389
</div>
8490
);
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import React from 'react';
2+
import { Section } from '../components/Section';
3+
import { CodeBlock } from '../components/CodeBlock';
4+
5+
interface UseCodeExecutionPageProps {
6+
theme: string;
7+
}
8+
9+
export const UseCodeExecutionPage: React.FC<UseCodeExecutionPageProps> = ({ theme }) => {
10+
const isDark = theme === 'rcv-theme-dark';
11+
const codeTheme = isDark ? 'github-dark' : 'github-light';
12+
13+
const minimalExample = `import React from 'react';
14+
import { useCodeExecution } from '@react-code-view/react';
15+
16+
const initialCode = \`const Hello = () => <button onClick={() => alert('hi!')}>Click</button>;
17+
render(<Hello />);\`;
18+
19+
export function Demo() {
20+
const { element, error } = useCodeExecution(initialCode, {
21+
dependencies: { React },
22+
});
23+
24+
if (error) return <pre>{error.message}</pre>;
25+
return <>{element}</>;
26+
}
27+
`;
28+
29+
const advancedExample = `import React from 'react';
30+
import { useCodeExecution } from '@react-code-view/react';
31+
32+
const initialCode = \`import { useState } from 'react';
33+
34+
const Counter = () => {
35+
const [count, setCount] = useState(0);
36+
return (
37+
<div>
38+
<p>Count: {count}</p>
39+
<button onClick={() => setCount(count + 1)}>Increment</button>
40+
</div>
41+
);
42+
};
43+
44+
render(<Counter />);\`;
45+
46+
export function Demo() {
47+
const { element, error, execute, updateCode } = useCodeExecution(initialCode, {
48+
dependencies: { React },
49+
onError: (err) => console.error('Execution failed', err),
50+
transformOptions: { transforms: ['typescript', 'jsx'] },
51+
});
52+
53+
// Manually re-run after editing the string
54+
const handleRun = () => execute(initialCode);
55+
56+
if (error) return <pre>{error.message}</pre>;
57+
return (
58+
<div>
59+
<button onClick={handleRun}>Run</button>
60+
<div style={{ marginTop: 12 }}>{element}</div>
61+
</div>
62+
);
63+
}
64+
`;
65+
66+
const optionsTable = [
67+
{ name: 'dependencies', type: 'Record<string, unknown>', desc: 'Globals injected to execution sandbox' },
68+
{ name: 'transformOptions', type: 'Sucrase Options', desc: 'Customize JSX/TS transforms' },
69+
{ name: 'beforeCompile', type: '(code: string) => string', desc: 'Transform code before compile' },
70+
{ name: 'afterCompile', type: '(code: string) => string', desc: 'Transform code after compile' },
71+
{ name: 'onError', type: '(error: Error) => void', desc: 'Callback on execution failure' },
72+
];
73+
74+
return (
75+
<div className="page-content">
76+
<Section id="use-code-execution" title="useCodeExecution (hook)">
77+
<p className="section-intro">
78+
Lower-level hook used by CodeView to execute code strings, inject dependencies, and return rendered HTML.
79+
</p>
80+
81+
<h3>When to use</h3>
82+
<ul>
83+
<li>Custom rendering pipeline without the full CodeView UI.</li>
84+
<li>Server-rendered or pre-rendered demos where you handle layout yourself.</li>
85+
<li>Embedding execution output into existing docs components.</li>
86+
</ul>
87+
88+
<h3>Minimal example</h3>
89+
<CodeBlock language="tsx" theme={codeTheme} code={minimalExample} />
90+
91+
<h3>With dependencies and error handling</h3>
92+
<CodeBlock language="tsx" theme={codeTheme} code={advancedExample} />
93+
94+
<h3>Options</h3>
95+
<div className="api-table-wrapper">
96+
<table className="api-table">
97+
<thead>
98+
<tr>
99+
<th>Option</th>
100+
<th>Type</th>
101+
<th>Description</th>
102+
</tr>
103+
</thead>
104+
<tbody>
105+
{optionsTable.map(row => (
106+
<tr key={row.name}>
107+
<td><code>{row.name}</code></td>
108+
<td><code>{row.type}</code></td>
109+
<td>{row.desc}</td>
110+
</tr>
111+
))}
112+
</tbody>
113+
</table>
114+
</div>
115+
116+
<h3>Return value</h3>
117+
<ul>
118+
<li><code>element</code>: ReactNode — captured React output</li>
119+
<li><code>error</code>: Error | null — execution error, if any</li>
120+
<li><code>code</code>: string — current code string</li>
121+
<li><code>setCode / updateCode</code>: setter to change code (re-runs)</li>
122+
<li><code>execute</code>: function to manually run a code string</li>
123+
</ul>
124+
</Section>
125+
</div>
126+
);
127+
};
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import React from 'react';
2+
import { Section } from '../../components/Section';
3+
import { CodeBlock } from '../../components/CodeBlock';
4+
import { useCodeExecution } from '@react-code-view/react';
5+
6+
interface UseCodeExecutionExampleProps {
7+
theme: string;
8+
}
9+
10+
export const UseCodeExecutionExample: React.FC<UseCodeExecutionExampleProps> = ({ theme }) => {
11+
const isDark = theme === 'rcv-theme-dark';
12+
const codeTheme = isDark ? 'github-dark' : 'github-light';
13+
14+
const code = `const Badge = ({ children }) => (
15+
<span style={{
16+
padding: '6px 10px',
17+
borderRadius: 999,
18+
background: '#eef2ff',
19+
color: '#4338ca',
20+
fontWeight: 600
21+
}}>
22+
{children}
23+
</span>
24+
);
25+
26+
const Demo = () => (
27+
<div style={{ display: 'flex', gap: 12, alignItems: 'center' }}>
28+
<Badge>useCodeExecution</Badge>
29+
<Badge>renders</Badge>
30+
<Badge>custom</Badge>
31+
<Badge>UI</Badge>
32+
</div>
33+
);
34+
35+
render(<Demo />);`;
36+
37+
const { element, error } = useCodeExecution(code, {
38+
dependencies: { React },
39+
transformOptions: { transforms: ['jsx'] }
40+
});
41+
42+
const exampleComponentSource = `import React from 'react';
43+
import { useCodeExecution } from '@react-code-view/react';
44+
45+
const code = \`${code}\`;
46+
47+
export function UseCodeExecutionExample() {
48+
const { element, error, code: currentCode, updateCode, execute } = useCodeExecution(code, {
49+
dependencies: { React },
50+
transformOptions: { transforms: ['jsx'] }
51+
});
52+
53+
return (
54+
<div>
55+
<div>{error ? error.message : element}</div>
56+
<button onClick={() => execute(currentCode)}>Run</button>
57+
<button onClick={() => updateCode(code)}>Reset code</button>
58+
</div>
59+
);
60+
}
61+
`;
62+
63+
return (
64+
<div className="page-content">
65+
<Section id="use-code-execution-example" title="useCodeExecution Example">
66+
<p className="section-intro">
67+
When you want to execute code strings without the full CodeView UI, use the low-level hook
68+
<code> useCodeExecution</code>.
69+
</p>
70+
71+
<div className="example-demo" style={{ gap: 16 }}>
72+
<div className="preview-panel">
73+
{error ? (
74+
<pre className="error">{error.message}</pre>
75+
) : (
76+
<div>{element}</div>
77+
)}
78+
</div>
79+
</div>
80+
81+
82+
<h4>Full component source</h4>
83+
<CodeBlock language="tsx" theme={codeTheme} code={exampleComponentSource} />
84+
85+
<ul>
86+
<li>Inject dependencies via <code>dependencies</code> (here we pass React).</li>
87+
<li>Pick a Shiki theme with <code>shikiTheme</code> to match your site.</li>
88+
<li>Use <code>error</code> to render fallback UI when execution fails.</li>
89+
</ul>
90+
</Section>
91+
</div>
92+
);
93+
};

0 commit comments

Comments
 (0)