diff --git a/packages/document/main-doc/package.json b/packages/document/main-doc/package.json index 7166a0e3157f..2e6fd2fb68e0 100644 --- a/packages/document/main-doc/package.json +++ b/packages/document/main-doc/package.json @@ -27,6 +27,9 @@ "access": "public", "provenance": true }, + "dependencies": { + "@modern-js/sandpack-react": "workspace:*" + }, "peerDependencies": { "@modern-js/builder-doc": "workspace:^2.33.1" }, diff --git a/packages/document/main-doc/src/components/Sandpack/index.css b/packages/document/main-doc/src/components/Sandpack/index.css new file mode 100644 index 000000000000..e92db2526957 --- /dev/null +++ b/packages/document/main-doc/src/components/Sandpack/index.css @@ -0,0 +1,7 @@ +.light { + --sp-layout-height: 500px !important; +} + +.dark { + --sp-layout-height: 500px !important; +} diff --git a/packages/document/main-doc/src/components/Sandpack/index.tsx b/packages/document/main-doc/src/components/Sandpack/index.tsx new file mode 100644 index 000000000000..dfd89f0d41bd --- /dev/null +++ b/packages/document/main-doc/src/components/Sandpack/index.tsx @@ -0,0 +1,28 @@ +import ModernSandpack, { ModernSandpackProps } from '@modern-js/sandpack-react'; +import React, { PropsWithChildren } from 'react'; +import { useDark } from 'rspress/runtime'; + +import './index.css'; + +const Sandpack = (props: PropsWithChildren) => { + const dark = useDark(); + const { children, ...otherProps } = props; + const files: Record = {}; + React.Children.forEach(children, (child: any) => { + if (child) { + const { meta, children } = child.props.children.props; + const matches = meta.match(/title="(.*)"/); + if (matches.length > 1) { + files[matches[1]] = children; + } + } + }); + return ( + + ); +}; +export default Sandpack; diff --git a/packages/generator/generators/mwa-generator/src/index.ts b/packages/generator/generators/mwa-generator/src/index.ts index 86d1e6629638..30dbcb4de88c 100644 --- a/packages/generator/generators/mwa-generator/src/index.ts +++ b/packages/generator/generators/mwa-generator/src/index.ts @@ -145,7 +145,7 @@ export const handleTemplateFile = async ( isMonorepoSubProject, modernVersion, packageManager, - isTsProject: language === Language.TS, + isTs: language === Language.TS, }, ); diff --git a/packages/generator/generators/mwa-generator/templates/base-template/package.json.handlebars b/packages/generator/generators/mwa-generator/templates/base-template/package.json.handlebars index 818769afd632..9ddb1cc82026 100644 --- a/packages/generator/generators/mwa-generator/templates/base-template/package.json.handlebars +++ b/packages/generator/generators/mwa-generator/templates/base-template/package.json.handlebars @@ -41,7 +41,7 @@ "@modern-js/eslint-config": "{{modernVersion}}", "@modern-js/tsconfig":"{{modernVersion}}", "@modern-js-app/eslint-config": "{{modernVersion}}", - {{#if isTsProject}} + {{#if isTs}} "typescript": "~5.0.4", "@types/jest": "~29.2.4", "@types/node": "~16.11.7", diff --git a/packages/generator/sandpack-react/scripts/template.ts b/packages/generator/sandpack-react/scripts/template.ts index 4fcc20c01153..db8ed9ee9527 100644 --- a/packages/generator/sandpack-react/scripts/template.ts +++ b/packages/generator/sandpack-react/scripts/template.ts @@ -5,6 +5,18 @@ import { renderString } from '@modern-js/codesmith-api-handlebars'; import { getModernVersion } from '@modern-js/generator-utils'; import { Solution } from '@modern-js/generator-common'; +const IgnoreFiles = [ + '.nvmrc', + '.eslintrc.js.handlebars', + 'src/.eslintrc.js.handlebars', + '.prettierrc', + '.vscode/extensions.json', + '.vscode/settings.json', + '.husky/pre-commit', + 'README.md', + '.gitignore.handlebars', +]; + async function handleTemplate( templatePath: string, data: Record = {}, @@ -17,6 +29,9 @@ async function handleTemplate( const templateFiles = await recursive(templatePath); templateFiles.forEach(filePath => { const file = filePath.replace(`${templatePath}/`, ''); + if (IgnoreFiles.includes(file)) { + return; + } if (fs.statSync(filePath).isFile()) { if (file.endsWith('.handlebars')) { files[ diff --git a/packages/generator/sandpack-react/src/index.tsx b/packages/generator/sandpack-react/src/index.tsx index cd17b418315a..a1a74fa369b5 100644 --- a/packages/generator/sandpack-react/src/index.tsx +++ b/packages/generator/sandpack-react/src/index.tsx @@ -6,6 +6,7 @@ import { OpenInCodeSandboxButton, SandpackSetup, SandpackFiles, + SandpackThemeProp, } from '@codesandbox/sandpack-react'; import { ModernTemplates } from './templates'; @@ -13,9 +14,10 @@ export type ModernSandpackProps = { template: 'web-app' | 'npm-module'; customSetup?: SandpackSetup; files?: SandpackFiles; - removeFiles: string[]; + removeFiles?: string[]; options?: Record; initialCollapsedFolder?: string[]; + theme?: SandpackThemeProp; }; function fileterFiles(files: SandpackFiles, removeFiles: string[]) { @@ -39,6 +41,7 @@ export default function ModernSandpack(props: ModernSandpackProps) { removeFiles = [], options = {}, initialCollapsedFolder = [], + theme = 'light', } = props; const initFiles = ModernTemplates[template]; const files = { @@ -47,15 +50,11 @@ export default function ModernSandpack(props: ModernSandpackProps) { }; return ( @@ -69,7 +68,7 @@ export default function ModernSandpack(props: ModernSandpackProps) { ]} /> -
+