Skip to content

Commit

Permalink
[CSS-in-JS] Add provider to generated codesandbox examples (#5253)
Browse files Browse the repository at this point in the history
* add provider to generated codesandbox examples

* indentation

* euithemeprovider -> euiprovider

* legacy theme
  • Loading branch information
thompsongl authored Oct 14, 2021
1 parent f2bc587 commit f8d8675
Showing 1 changed file with 54 additions and 12 deletions.
66 changes: 54 additions & 12 deletions src-docs/src/components/codesandbox/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ export const CodeSandboxLinkComponent = ({
break;
}

const isLegacyTheme = context.theme.includes(LEGACY_NAME_KEY);

const providerPropsObject = {};
// Only add configuration if it isn't the default
if (context.theme.includes('dark')) {
providerPropsObject.colorMode = 'dark';
}
// Can't spread an object inside of a string literal
const providerProps = Object.keys(providerPropsObject)
.map((prop) => {
const value = providerPropsObject[prop];
return value !== null ? `${prop}="${value}"` : `${prop}={${value}}`;
})
.join(' ');

// Renders the new Demo component generically into the code sandbox page
const exampleClose = `ReactDOM.render(
${
isLegacyTheme
? '<Demo />'
: `<EuiProvider ${providerProps}>
<Demo />
</EuiProvider>`
},
document.getElementById('root')
);`;

let indexContent;

if (!content) {
Expand All @@ -74,28 +101,48 @@ import '${cssFile}';
import React from 'react';
import {
EuiButton,
${
isLegacyTheme
? 'EuiButton,'
: `EuiButton,
EuiProvider,`
}
} from '@elastic/eui';
const Demo = () => (<EuiButton>Hello world!</EuiButton>);
ReactDOM.render(
<Demo />,
document.getElementById('root')
);
${exampleClose}
`;
} else {
/** This cleans the Demo JS example for Code Sanbox.
- Replaces relative imports with pure @elastic/eui ones
- Adds provider import, if necessary
- Changes the JS example from a default export to a component const named Demo
**/
const exampleCleaned = cleanEuiImports(content)
let exampleCleaned = cleanEuiImports(content)
.replace('export default', 'const Demo =')
.replace(
/(from )'(..\/)+display_toggles(\/?';)/,
"from './display_toggles';"
);

if (!isLegacyTheme && !exampleCleaned.includes('EuiProvider')) {
if (exampleCleaned.includes(" } from '@elastic/eui';")) {
// Single line import statement
exampleCleaned = exampleCleaned.replace(
" } from '@elastic/eui';",
", EuiProvider } from '@elastic/eui';"
);
} else {
// Multi line import statement
exampleCleaned = exampleCleaned.replace(
"} from '@elastic/eui';",
` EuiProvider,
} from '@elastic/eui';`
);
}
}

// If the code example still has local doc imports after the above cleaning it's
// too complicated for code sandbox so we don't provide a link
const hasLocalImports = /(from )'((.|..)\/).*?';/.test(exampleCleaned);
Expand All @@ -104,11 +151,6 @@ ReactDOM.render(
return null;
}

// Renders the new Demo component generically into the code sandbox page
const exampleClose = `ReactDOM.render(
<Demo />,
document.getElementById('root')
);`;
// The Code Sanbbox demo needs to import CSS at the top of the document. CS has trouble
// with our dynamic imports so we need to warn the user for now
const exampleStart = `import ReactDOM from 'react-dom';
Expand All @@ -118,7 +160,7 @@ import '${cssFile}';`;
const cleanedContent = `${exampleStart}
${exampleCleaned}
${exampleClose}
`;
`;
indexContent = cleanedContent.replace(
/(from )'.+display_toggles';/,
"from './display_toggles';"
Expand Down

0 comments on commit f8d8675

Please sign in to comment.