-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils.ts
More file actions
240 lines (225 loc) · 6.29 KB
/
utils.ts
File metadata and controls
240 lines (225 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* eslint-disable no-new-func */
import { CodeSandboxTemplate } from "storybook-addon-preview";
import outdent from "outdent";
type CreateControlsOptions = {
unions?: string[];
ignore?: string[];
allow?: string[];
};
export const createControls = (options?: CreateControlsOptions) => {
try {
const ignoredControls = (options?.ignore || []).reduce((cur, key) => {
return {
...cur,
[key]: { table: { disable: true } },
};
}, {});
const allowedControls = (options?.allow || []).reduce((cur, key) => {
return {
...cur,
[key]: { table: { disable: true } },
};
}, {});
return { ...ignoredControls, ...allowedControls };
} catch (error) {
console.log(error);
}
};
interface FilesProp {
[index: string]: string;
}
interface Props {
js?: { template: string; files?: FilesProp };
ts?: { template: string; files?: FilesProp };
jsUtils?: string;
tsUtils?: string;
css?: string;
deps?: string[];
}
export function createPreviewTabs(props: Props) {
const { js, ts, jsUtils, tsUtils, css, deps: extraDeps = [] } = props;
const tabs = [];
if (js) {
tabs.push({
tab: "JSX",
template: js.template,
language: "jsx",
copy: true,
codesandbox: REACTJS_CUSTOM_CODESANDBOX([...extraDeps], {
"src/components/index.js": js.template,
...(js.files && js.files),
}),
});
}
if (jsUtils) {
tabs.push({
tab: "UtilsJSX",
template: jsUtils,
language: "jsx",
copy: true,
});
}
if (ts) {
tabs.push({
tab: "TSX",
template: ts.template,
language: "tsx",
copy: true,
codesandbox: REACT_CUSTOM_CODESANDBOX([...extraDeps], {
"src/components/index.tsx": ts.template,
...(ts.files && ts.files),
}),
});
}
if (tsUtils) {
tabs.push({
tab: "UtilsTSX",
template: tsUtils,
language: "tsx",
copy: true,
});
}
if (css) {
tabs.push({
tab: "CSS",
template: css,
language: "css",
copy: true,
});
}
return tabs;
}
export const REACTJS_CUSTOM_CODESANDBOX: CodeSandboxTemplate = (
userDependencies = [],
files = {},
) => {
return {
template: "create-react-app",
files: {
"public/index.html": `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<title>React App</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
</body>
</html>
`,
"src/index.js": `
import App from "./App";
import { createRoot } from 'react-dom/client';
const rootElement = document.getElementById('root');
const root = createRoot(rootElement);
root.render(<App />);
`,
"src/App.js": outdent`
import React from "react";
import Component from "./components";
export default function App() {
return <Component />
}
`,
...files,
},
dependencies: {
"@adaptui/react": "latest",
react: "18.0.0",
"react-dom": "18.0.0",
"react-scripts": "latest",
"@internationalized/date": "^3.0.0-rc.0",
},
scripts: {
start: "react-scripts start",
build: "react-scripts build",
test: "react-scripts test --env=jsdom",
eject: "react-scripts eject",
},
main: "src/index.js",
userDependencies,
};
};
export const REACT_CUSTOM_CODESANDBOX: CodeSandboxTemplate = (
userDependencies = [],
files = {},
) => {
return {
template: "create-react-app-typescript",
files: {
"public/index.html": `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<title>React App</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
</body>
</html>
`,
"src/index.tsx": `
import * as ReactDOM from "react-dom";
import * as React from "react";
import App from "./App";
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
`,
"src/App.tsx": `import React from \"react\";\n import Component from \"./components\";\n\nexport default function App() {\n return (\n <div>\n <main >\n <Component />\n </main>\n </div>\n );\n}\n`,
...files,
},
dependencies: {
"@adaptui/react": "latest",
react: "17.0.2",
"react-dom": "17.0.2",
next: "12.0.7",
},
devDependencies: {
"@types/node": "17.0.5",
"@types/react": "17.0.38",
"@types/react-dom": "17.0.11",
typescript: "4.5.4",
},
scripts: {
dev: "next dev",
build: "next build",
start: "next start",
lint: "next lint",
},
main: "src/index.ts",
userDependencies,
};
};
export const CreateAppTemplate = (props: object | undefined) => {
return outdent`
import React from "react";
import Component from "./components";
export default function App() {
return <Component ${props && `{...${JSON.stringify(props)}}`} />
}
`;
};