Skip to content

Commit e70ef58

Browse files
Live code editor for UI docs (#2351)
* Docausaurus code live editor test * Docusaurus sandpack test * Fix setup * Delete files * Fixes * Complete work * Fix config * Fix config --------- Co-authored-by: bosiraphael <[email protected]>
1 parent 1ed4965 commit e70ef58

18 files changed

+4361
-1720
lines changed

docs/docs/contributor/frontend/ui-components/forms/_category_.json renamed to docs/docs/contributor/frontend/ui-components/display/_category_.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"label": "Forms",
3-
"position": 2,
2+
"label": "Display",
3+
"position": 0,
44
"collapsible": true,
55
"collapsed": true,
66
"customProps": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Checkmark
3+
sidebar_position: 1
4+
---
5+
6+
import { SandpackEditor} from '@site/src/ui/SandpackEditor'
7+
8+
<SandpackEditor
9+
componentPath='@/ui/Checkmark/components/Checkmark'
10+
componentCode={`import { Checkmark } from "@/ui/Checkmark/components/Checkmark";
11+
12+
export const MyComponent = () => {
13+
return <Checkmark />;
14+
}
15+
`}
16+
/>

docs/docs/contributor/frontend/ui-components/forms/button.mdx

-15
This file was deleted.

docs/docs/contributor/frontend/ui-components/ui-components.mdx

-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,3 @@
22
title: Overview
33
sidebar_position: 0
44
---
5-
import DocCardList from '@theme/DocCardList';
6-
7-
<DocCardList />

docs/docusaurus.config.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// @ts-check
33
// Note: type annotations allow type checking and IDEs autocompletion
44

5-
const lightCodeTheme = require('prism-react-renderer/themes/github');
6-
const darkCodeTheme = require('prism-react-renderer/themes/dracula');
5+
import { themes } from 'prism-react-renderer';
6+
7+
const lightCodeTheme = themes.github;
8+
const darkCodeTheme = themes.dracula;
79

810
/** @type {import('@docusaurus/types').Config} */
911
const config = {

docs/package.json

+11-9
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,24 @@
1515
"typecheck": "tsc"
1616
},
1717
"dependencies": {
18-
"@docusaurus/core": "^2.4.1",
19-
"@docusaurus/preset-classic": "^2.4.1",
20-
"@mdx-js/react": "^1.6.22",
18+
"@codesandbox/sandpack-react": "^2.9.0",
19+
"@docusaurus/core": "^3.0.0",
20+
"@docusaurus/preset-classic": "^3.0.0",
21+
"@mdx-js/react": "^3.0.0",
2122
"clsx": "^1.2.1",
2223
"graphiql": "^2.4.7",
2324
"graphql": "^16.6.0",
2425
"iframe-resizer-react": "^1.1.0",
25-
"prism-react-renderer": "^1.3.5",
26-
"react": "^17.0.2",
27-
"react-dom": "^17.0.2",
26+
"prism-react-renderer": "^2.1.0",
27+
"react": "^18.2.0",
28+
"react-dom": "^18.2.0",
2829
"react-icons": "^4.9.0"
2930
},
3031
"devDependencies": {
31-
"@docusaurus/module-type-aliases": "^2.4.1",
32-
"@tsconfig/docusaurus": "^1.0.5",
33-
"typescript": "^4.7.4"
32+
"@docusaurus/module-type-aliases": "^3.0.0",
33+
"@docusaurus/tsconfig": "3.0.0",
34+
"raw-loader": "^4.0.2",
35+
"typescript": "~5.2.2"
3436
},
3537
"overrides": {
3638
"trim": "^0.0.3",

docs/src/css/custom.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,4 @@ a.table-of-contents__link:hover{
289289

290290
.hidden {
291291
display: none !important;
292-
}
292+
}

docs/src/ui/SandpackEditor.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { SandpackProvider, SandpackLayout, SandpackCodeEditor, SandpackPreview } from "@codesandbox/sandpack-react";
2+
import uiModule from "!!raw-loader!@site/src/ui/generated/index.js";
3+
4+
export const SandpackEditor = ({ componentPath, componentCode}) => {
5+
return (
6+
<SandpackProvider
7+
files={{
8+
"/index.js": {
9+
hidden: true,
10+
code: `
11+
import React, { StrictMode } from "react";
12+
import { createRoot } from "react-dom/client";
13+
14+
import App from "./App";
15+
16+
const root = createRoot(document.getElementById("root"));
17+
root.render(
18+
<StrictMode>
19+
<App />
20+
</StrictMode>
21+
);`,
22+
},
23+
"/App.tsx": {
24+
hidden: true,
25+
code: `import { ThemeProvider } from "${componentPath}";
26+
import { MyComponent } from "./MyComponent.tsx";
27+
28+
export default function App() {
29+
return (<ThemeProvider>
30+
<MyComponent />
31+
</ThemeProvider>);
32+
};`,
33+
},
34+
"/MyComponent.tsx": {
35+
code: componentCode,
36+
},
37+
[`/node_modules/${componentPath}/package.json`]: {
38+
hidden: true,
39+
code: JSON.stringify({
40+
name: componentPath,
41+
main: "./index.js",
42+
}),
43+
},
44+
[`/node_modules/${componentPath}/index.js`]: {
45+
hidden: true,
46+
code: uiModule,
47+
},
48+
}}
49+
customSetup={{
50+
entry: "/index.js",
51+
dependencies: {
52+
react: "latest",
53+
"react-dom": "latest",
54+
"react-scripts": "^5.0.0",
55+
},
56+
}}
57+
>
58+
<SandpackLayout>
59+
<SandpackCodeEditor
60+
style={{ minWidth: "100%", height: "auto" }}
61+
/>
62+
<SandpackPreview
63+
style={{ minWidth: "100%", height: "auto" }}
64+
/>
65+
</SandpackLayout>
66+
</SandpackProvider>
67+
);
68+
};
25.4 KB
Loading

0 commit comments

Comments
 (0)