Skip to content

Commit

Permalink
[material-ui] Stabilize CssVarsProvider and extendTheme (mui#42246)
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp authored and bharatkashyap committed Jun 26, 2024
1 parent a3f7645 commit 0aff8a8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
24 changes: 23 additions & 1 deletion docs/src/modules/sandbox/CodeSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,29 @@ function openSandbox({ files, codeVariant, initialFile }: any) {

function createReactApp(demoData: DemoData) {
const ext = getFileExtension(demoData.codeVariant);
const { title, githubLocation: description } = demoData;
const { title, relativeModules = [], githubLocation: description } = demoData;

if (relativeModules) {
relativeModules.forEach(({ module, raw: content }) => {
// remove exports from relative module
content = content.replace(/export( )*(default)*( )*\w+;|export default|export/gm, '');
// replace import statement with relative module content
// the module might be imported with or without extension, so we need to cover all cases
// E.g.: /import .* from '(.\/top100Films.js|.\/top100Films)';/
const extensions = ['', '.js', '.jsx', '.ts', '.tsx', '.css', '.json'];
const patterns = extensions
.map((ex) => {
if (module.endsWith(ex)) {
return module.replace(ex, '');
}
return '';
})
.filter(Boolean)
.join('|');
const importPattern = new RegExp(`import .* from '(${patterns})';`);
demoData.raw = demoData.raw.replace(importPattern, content);
});
}

const files: Record<string, object> = {
'public/index.html': {
Expand Down
24 changes: 23 additions & 1 deletion docs/src/modules/sandbox/StackBlitz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,29 @@ import { DemoData } from 'docs/src/modules/sandbox/types';

function createReactApp(demoData: DemoData) {
const ext = getFileExtension(demoData.codeVariant);
const { title, githubLocation: description } = demoData;
const { title, relativeModules = [], githubLocation: description } = demoData;

if (relativeModules) {
relativeModules.forEach(({ module, raw: content }) => {
// remove exports from relative module
content = content.replace(/export( )*(default)*( )*\w+;|export default|export/gm, '');
// replace import statement with relative module content
// the module might be imported with or without extension, so we need to cover all cases
// E.g.: /import .* from '(.\/top100Films.js|.\/top100Films)';/
const extensions = ['', '.js', '.jsx', '.ts', '.tsx', '.css', '.json'];
const patterns = extensions
.map((ex) => {
if (module.endsWith(ex)) {
return module.replace(ex, '');
}
return '';
})
.filter(Boolean)
.join('|');
const importPattern = new RegExp(`import .* from '(${patterns})';`);
demoData.raw = demoData.raw.replace(importPattern, content);
});
}

const files: Record<string, string> = {
'index.html': CRA.getHtml(demoData),
Expand Down
6 changes: 6 additions & 0 deletions docs/src/modules/sandbox/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import type { MuiProductId } from 'docs/src/modules/utils/getProductInfoFromUrl'

export type CodeStyling = 'Tailwind' | 'MUI System';
export type CodeVariant = 'TS' | 'JS';

type RelativeModule = {
module: string;
raw: string;
};
export interface DemoData {
title: string;
language: string;
Expand All @@ -10,4 +15,5 @@ export interface DemoData {
githubLocation: string;
productId?: Exclude<MuiProductId, 'null'>;
codeStyling: CodeStyling;
relativeModules?: RelativeModule[];
}

0 comments on commit 0aff8a8

Please sign in to comment.