Skip to content

Commit 8a37d8d

Browse files
authored
docs: improve CodeSandbox integration (#4010)
1 parent 5532548 commit 8a37d8d

File tree

4 files changed

+67
-20
lines changed

4 files changed

+67
-20
lines changed

docs/src/components/ComponentDoc/ComponentControls/ComponentControlsCodeSandbox.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ const appTemplate = `import React from "react";
1010
import ReactDOM from "react-dom";
1111
import { Container, Header, List } from "semantic-ui-react";
1212
13+
import pkg from 'semantic-ui-react/package.json'
1314
import Example from "./example";
1415
1516
const App = ({ children }) => (
1617
<Container style={{ margin: 20 }}>
17-
<Header as="h3">This example is powered by Semantic UI React 😊</Header>
18+
<Header as="h3">This example is powered by Semantic UI React {pkg.version} 😊</Header>
1819
<List bulleted>
1920
<List.Item
2021
as="a"
@@ -48,6 +49,22 @@ ReactDOM.render(
4849
);
4950
`
5051

52+
function CodeSandboxIcon() {
53+
return (
54+
<i aria-hidden className='grey icon'>
55+
<svg
56+
xmlns='http://www.w3.org/2000/svg'
57+
fill='currentColor'
58+
height='100%'
59+
viewBox='0 0 24 24'
60+
width='100%'
61+
>
62+
<path d='M2 6l10.455-6L22.91 6 23 17.95 12.455 24 2 18V6zm2.088 2.481v4.757l3.345 1.86v3.516l3.972 2.296v-8.272L4.088 8.481zm16.739 0l-7.317 4.157v8.272l3.972-2.296V15.1l3.345-1.861V8.48zM5.134 6.601l7.303 4.144 7.32-4.18-3.871-2.197-3.41 1.945-3.43-1.968L5.133 6.6z' />
63+
</svg>
64+
</i>
65+
)
66+
}
67+
5168
class ComponentControlsCodeSandbox extends React.Component {
5269
state = {
5370
exampleCode: '',
@@ -91,7 +108,7 @@ class ComponentControlsCodeSandbox extends React.Component {
91108
example={enhanceExampleCode(exampleCode)}
92109
providedFiles={{
93110
'index.js': { content: appTemplate },
94-
'package.json': createPackageJson(),
111+
'package.json': createPackageJson(exampleCode),
95112
}}
96113
skipRedirect
97114
template='create-react-app'
@@ -103,10 +120,7 @@ class ComponentControlsCodeSandbox extends React.Component {
103120
<Menu.Item
104121
as='a'
105122
content={loading ? 'Exporting...' : 'CodeSandbox'}
106-
icon={{
107-
loading,
108-
name: loading ? 'spinner' : 'connectdevelop',
109-
}}
123+
icon={loading ? { loading, name: 'spinner' } : () => <CodeSandboxIcon />}
110124
title='Export to CodeSandbox'
111125
/>
112126
)

docs/src/components/ComponentDoc/ComponentControls/createPackageJson.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,31 @@ import { externals } from 'docs/src/components/ComponentDoc/ExampleEditor/render
33

44
const name = 'semantic-ui-example'
55
const description = 'An exported example from Semantic UI React, https://react.semantic-ui.com/'
6-
const dependencies = {
7-
..._.mapValues(externals, () => 'latest'),
8-
// required to enable all features due old templates in https://github.com/codesandbox/codesandbox-importers
9-
'react-scripts': 'latest',
6+
7+
function createDependencies(code) {
8+
// Will include only required packages intentionally like "react" or required by a current example
9+
const filteredPackages = _.pickBy(
10+
externals,
11+
(declaration, importName) =>
12+
declaration.required || new RegExp(`from ['|"]${importName}['|"]`).exec(code),
13+
)
14+
15+
return {
16+
..._.mapValues(filteredPackages, (pkg) => pkg.version),
17+
// required to enable all features due old templates in https://github.com/codesandbox/codesandbox-importers
18+
// https://github.com/microsoft/fluent-ui-react/issues/1519
19+
'react-scripts': 'latest',
20+
}
1021
}
1122

12-
const createPackageJson = () => ({
23+
const createPackageJson = (code) => ({
1324
content: JSON.stringify(
1425
{
1526
name,
1627
version: '1.0.0',
1728
description,
1829
main: 'index.js',
19-
dependencies,
30+
dependencies: createDependencies(code),
2031
},
2132
null,
2233
2,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const imagesRegex = /\/images\//gm
1+
const imagesRegex = /'\/images\//gm
22

33
const enhanceExampleCode = (code) => {
44
// To have absolute paths on CodeSandbox for images
5-
return code.replace(imagesRegex, 'https://react.semantic-ui.com/images/')
5+
return code.replace(imagesRegex, "'https://react.semantic-ui.com/images/")
66
}
77

88
export default enhanceExampleCode

docs/src/components/ComponentDoc/ExampleEditor/renderConfig.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import ReactDOM from 'react-dom'
44
import PropTypes from 'prop-types'
55
import * as SUIR from 'semantic-ui-react'
66

7+
import pkg from '../../../../../package.json'
8+
79
const isIE11 =
810
typeof window !== 'undefined' && !!window.MSInputMethodContext && !!document.documentMode
911

@@ -17,16 +19,36 @@ export const babelConfig = {
1719
}
1820

1921
export const externals = {
20-
faker,
22+
faker: {
23+
module: faker,
24+
required: false,
25+
version: pkg.devDependencies.faker,
26+
},
2127
lodash: require('lodash'),
22-
'prop-types': PropTypes,
23-
react: React,
24-
'react-dom': ReactDOM,
25-
'semantic-ui-react': SUIR,
28+
'prop-types': {
29+
module: PropTypes,
30+
required: false,
31+
version: pkg.dependencies['prop-types'],
32+
},
33+
react: {
34+
module: React,
35+
version: pkg.peerDependencies.react,
36+
required: true,
37+
},
38+
'react-dom': {
39+
module: ReactDOM,
40+
version: pkg.peerDependencies['react-dom'],
41+
required: true,
42+
},
43+
'semantic-ui-react': {
44+
module: SUIR,
45+
version: pkg.version,
46+
required: true,
47+
},
2648
}
2749

2850
export const resolver = (importPath, { displayName }) => {
29-
if (externals[importPath]) return externals[importPath]
51+
if (externals[importPath]) return externals[importPath].module
3052

3153
throw new Error(
3254
[

0 commit comments

Comments
 (0)