diff --git a/.dumi/pages/index/components/Theme/index.tsx b/.dumi/pages/index/components/Theme/index.tsx index 3e02c25293cf..c50ef1f321c3 100644 --- a/.dumi/pages/index/components/Theme/index.tsx +++ b/.dumi/pages/index/components/Theme/index.tsx @@ -336,7 +336,7 @@ export default function Theme() { ...ThemeDefault, themeType, ...ThemesInfo[themeType], - } as any; + }; setThemeData(mergedData); form.setFieldsValue(mergedData); @@ -517,13 +517,13 @@ export default function Theme() { const posStyle: React.CSSProperties = { position: 'absolute', }; - const leftTopImageStyle = { + const leftTopImageStyle: React.CSSProperties = { left: '50%', transform: 'translate3d(-900px, 0, 0)', top: -100, height: 500, }; - const rightBottomImageStyle = { + const rightBottomImageStyle: React.CSSProperties = { right: '50%', transform: 'translate3d(750px, 0, 0)', bottom: -100, diff --git a/.dumi/rehypeAntd.ts b/.dumi/rehypeAntd.ts index fa4733585f09..4c6354bb8474 100644 --- a/.dumi/rehypeAntd.ts +++ b/.dumi/rehypeAntd.ts @@ -8,7 +8,7 @@ function rehypeAntd(): UnifiedTransformer { return (tree, vFile) => { const { filename } = vFile.data.frontmatter as any; - unistUtilVisit.visit(tree, 'element', (node) => { + unistUtilVisit.visit(tree, 'element', (node, i, parent) => { if (node.tagName === 'DumiDemoGrid') { // replace DumiDemoGrid to DemoWrapper, to implement demo toolbar node.tagName = 'DemoWrapper'; @@ -66,6 +66,20 @@ function rehypeAntd(): UnifiedTransformer { node.tagName = 'LocaleLink'; } else if (node.type === 'element' && node.tagName === 'video') { node.tagName = 'VideoPlayer'; + } else if (node.tagName === 'SourceCode') { + const { lang } = node.properties; + if (lang === 'sandpack') { + parent!.children.splice(i!, 1, { + type: 'element', + tagName: 'Sandpack', + children: [ + { + type: 'text', + value: (node.children[0] as any).value, + }, + ], + }); + } } }); }; diff --git a/.dumi/theme/builtins/Sandpack/Sandpack.ts b/.dumi/theme/builtins/Sandpack/Sandpack.ts new file mode 100644 index 000000000000..8e8b767f0fc1 --- /dev/null +++ b/.dumi/theme/builtins/Sandpack/Sandpack.ts @@ -0,0 +1,3 @@ +import { Sandpack } from '@codesandbox/sandpack-react'; + +export default Sandpack; diff --git a/.dumi/theme/builtins/Sandpack/index.tsx b/.dumi/theme/builtins/Sandpack/index.tsx new file mode 100644 index 000000000000..037e5ccafb64 --- /dev/null +++ b/.dumi/theme/builtins/Sandpack/index.tsx @@ -0,0 +1,91 @@ +import type { ReactNode } from 'react'; +import React, { Suspense } from 'react'; +import { useSearchParams, useServerInsertedHTML } from 'dumi'; +import { createStyles } from 'antd-style'; +import { getSandpackCssText } from '@codesandbox/sandpack-react'; +import { Skeleton } from 'antd'; + +const OriginSandpack = React.lazy(() => import('./Sandpack')); + +const setup = { + dependencies: { + react: '^18.0.0', + 'react-dom': '^18.0.0', + antd: '^5.0.0', + }, + devDependencies: { + '@types/react': '^18.0.0', + '@types/react-dom': '^18.0.0', + typescript: '^5', + }, + entry: 'index.tsx', +}; + +const options = { + activeFile: 'app.tsx' as never, + visibleFiles: ['index.tsx', 'app.tsx', 'package.json'] as any, + showLineNumbers: true, + editorHeight: '500px', +}; + +const indexContent = `import React from 'react'; +import { createRoot } from 'react-dom/client'; +import App from './app'; + +const root = createRoot(document.getElementById("root")); +root.render(); +`; + +const useStyle = createStyles(({ token, css }) => ({ + fallback: css` + width: 100%; + > * { + width: 100% !important; + border-radius: 8px; + } + `, + placeholder: css` + color: ${token.colorTextDescription}; + font-size: 16px; + `, +})); + +const SandpackFallback = () => { + const { styles } = useStyle(); + + return ( +
+ + Loading Demo... + +
+ ); +}; + +const Sandpack = ({ children }: { children: ReactNode }) => { + const [searchParams] = useSearchParams(); + + useServerInsertedHTML(() => ( +