@@ -130,7 +130,7 @@ import React from 'react';
130
130
import Alert from ' ../alert' ;
131
131
import ' ../style' ;
132
132
133
- export default () => < Alert kind= ' warning' >< / Alert> ;
133
+ export default () => < Alert kind= " warning" >< / Alert> ;
134
134
```
135
135
136
136
** components/alert/index.mdx**
@@ -170,15 +170,16 @@ export default () => <Alert kind='warning'></Alert>;
170
170
安装依赖:
171
171
172
172
```
173
- yarn add react-use antd react-simple-code-editor prismjs react-copy-to-clipboard raw-loader --dev
173
+ yarn add react-use react-tooltip react-feather react- simple-code-editor prismjs react-copy-to-clipboard raw-loader styled-components --dev
174
174
```
175
175
176
176
- [ react-use] ( https://github.com/streamich/react-use ) - 2020 年了,当然要用` hooks `
177
- - [ antd] ( https://ant.design/ ) - 使用一些辅助组件
178
177
- [ react-simple-code-editor] ( https://github.com/satya164/react-simple-code-editor ) - 代码展示区域
179
178
- [ prismjs] ( https://github.com/PrismJS/prism ) - 代码高亮
180
179
- [ raw-loader] ( https://github.com/webpack-contrib/raw-loader ) - 将源码转成字符串
181
180
- [ react-copy-to-clipboard] ( https://github.com/nkbt/react-copy-to-clipboard ) - 让用户爸爸们能够 copy demo 代码
181
+ - react-tooltip/react-feather 辅助组件
182
+ - styled-components 方便在文档示例中让用户看到样式,也用作文档组件的样式处理
182
183
183
184
> 这些依赖都是服务于文档站点应用,和组件库自身毫无关联。
184
185
@@ -192,7 +193,7 @@ yarn add react-use antd react-simple-code-editor prismjs react-copy-to-clipboard
192
193
193
194
```
194
195
├── happy-box
195
- │ ├── index.less
196
+ │ ├── style.ts
196
197
│ └── index.tsx
197
198
└── index.ts
198
199
```
@@ -203,63 +204,65 @@ yarn add react-use antd react-simple-code-editor prismjs react-copy-to-clipboard
203
204
import React from ' react' ;
204
205
import Editor from ' react-simple-code-editor' ;
205
206
import CopyToClipboard from ' react-copy-to-clipboard' ;
206
- import useToggle from ' react-use/esm/useToggle' ;
207
- import { Divider , Typography , Icon , Tooltip , message } from ' antd' ;
207
+ import { useToggle } from ' react-use' ;
208
+ import ReactTooltip from ' react-tooltip' ;
209
+ import IconCopy from ' react-feather/dist/icons/clipboard' ;
210
+ import IconCode from ' react-feather/dist/icons/code' ;
208
211
import { highlight , languages } from ' prismjs/components/prism-core' ;
212
+ import { StyledContainer , StyledIconWrapper } from ' ./style' ;
209
213
210
214
import ' prismjs/components/prism-clike' ;
211
215
import ' prismjs/components/prism-javascript' ;
212
216
import ' prismjs/components/prism-markup' ;
213
- import ' ./index.less' ;
214
217
215
218
require (' prismjs/components/prism-jsx' );
216
219
217
- const { Text } = Typography;
218
220
interface Props {
219
221
code: string;
220
222
title?: React .ReactNode ;
221
223
desc?: React .ReactNode ;
222
224
}
223
225
224
- const HappyBox: React .FC <Props > = ({ code, title, desc, children }) => {
226
+ export const HappyBox: React .FC <Props > = ({ code, title, desc, children }) => {
225
227
const [isEditVisible , toggleEditVisible ] = useToggle (false );
226
228
227
229
return (
228
- < div className= ' code-box' >
229
- < section className= ' code-box-demo' > {children}< / section>
230
- < section className= ' code-box-meta' >
231
- < Divider orientation= ' left' > {title || ' 示例' }< / Divider>
232
- < div className= ' code-box-description' >
233
- < Text > {desc || ' 暂无描述' }< / Text >
230
+ < StyledContainer>
231
+ < section className= " code-box-demo" > {children}< / section>
232
+ < section className= " code-box-meta" >
233
+ < div className= " text-divider" >
234
+ < span> {title || ' 示例' }< / span>
234
235
< / div>
235
- < Divider dashed>< / Divider>
236
- < div className= ' code-box-action' >
237
- < Tooltip placement= ' top' title= {' 复制代码' }>
238
- < CopyToClipboard text= {code} onCopy= {() => message .success (' 复制成功' )}>
239
- < Icon type= ' copy' / >
240
- < / CopyToClipboard>
241
- < / Tooltip>
242
- < Tooltip placement= ' top' title= {isEditVisible ? ' 收起代码' : ' 显示代码' }>
243
- < Icon type= ' code' onClick= {toggleEditVisible} / >
244
- < / Tooltip>
236
+ < div className= " code-box-description" >
237
+ < p> {desc || ' 暂无描述' }< / p>
238
+ < / div>
239
+ < div className= " divider" / >
240
+ < div className= " code-box-action" >
241
+ < CopyToClipboard text= {code} onCopy= {() => alert (' 复制成功' )}>
242
+ < IconCopy data- place= " top" data- tip= " 复制代码" / >
243
+ < / CopyToClipboard>
244
+
245
+ < StyledIconWrapper onClick= {toggleEditVisible}>
246
+ < IconCode data- place= " top" data- tip= {isEditVisible ? ' 收起代码' : ' 显示代码' } / >
247
+ < / StyledIconWrapper>
245
248
< / div>
246
249
< / section>
247
250
{renderEditor ()}
248
- < / div>
251
+ < ReactTooltip / >
252
+ < / StyledContainer>
249
253
);
250
254
251
- /* 代码展示区域 */
252
255
function renderEditor () {
253
256
if (! isEditVisible) return null ;
254
257
return (
255
- < div className= ' container_editor_area' >
258
+ < div className= " container_editor_area" >
256
259
< Editor
257
260
readOnly
258
261
value= {code}
259
262
onValueChange= {() => {}}
260
263
highlight= {code => highlight (code, languages .jsx )}
261
264
padding= {10 }
262
- className= ' container__editor'
265
+ className= " container__editor"
263
266
style= {{
264
267
fontFamily: ' "Fira code", "Fira Mono", monospace' ,
265
268
fontSize: 14 ,
@@ -275,8 +278,7 @@ export default HappyBox;
275
278
276
279
### 相关配置变更
277
280
278
- - 增加 ` alias ` 别名,源码展示相对路径不够友好;
279
- - antd 按需引入,即使是站点应用。
281
+ - 增加 ` alias ` 别名,源码展示相对路径不够友好,让用户直接拷贝才够省心
280
282
281
283
新建` gatsby-node.js ` ,写入以下内容以开启` alias ` :
282
284
@@ -289,59 +291,67 @@ exports.onCreateWebpackConfig = args => {
289
291
modules: [path .resolve (__dirname , ' ../src' ), ' node_modules' ],
290
292
alias: {
291
293
' happy-ui/lib' : path .resolve (__dirname , ' ../components/' ),
294
+ ' happy-ui/esm' : path .resolve (__dirname , ' ../components/' ),
295
+ ' happy-ui' : path .resolve (__dirname , ' ../components/' ),
292
296
},
293
297
},
294
298
});
295
299
};
296
300
```
297
301
298
- ` antd ` 按需引入,安装依赖,并配置` gatsby-config.js ` :
299
-
300
- ``` bash
301
- yarn add babel-plugin-import gatsby-plugin-import --dev
302
- ```
303
-
304
- ** gatsby-config.js**
305
-
306
- ``` js
307
- module .exports = {
308
- plugins: [
309
- ' gatsby-theme-docz' ,
310
- ' gatsby-plugin-less' ,
311
- {
312
- resolve: ' gatsby-plugin-import' ,
313
- options: {
314
- libraryName: ' antd' ,
315
- style: ' css' ,
316
- },
317
- },
318
- ],
319
- };
320
- ```
321
-
322
- ` tsconfig.json ` 忽略` demo ` ,避免组件库打包生成` types ` 时包含其中:
302
+ ` tsconfig.json ` 打包时需要忽略` demo ` ,避免组件库打包生成` types ` 时包含其中:
323
303
324
304
** tsconfig.json**
325
305
326
306
``` diff
327
307
{
328
308
"compilerOptions": {
329
- "allowJs": false,
309
+ "baseUrl": "./",
310
+ + "paths": {
311
+ + "happy-ui": ["components/index.ts"],
312
+ + "happy-ui/esm/*": ["components/*"],
313
+ + "happy-ui/lib/*": ["components/*"]
314
+ + },
330
315
"target": "esnext",
331
316
"module": "commonjs",
332
317
"jsx": "react",
333
318
"declaration": true,
334
- "outDir ": "types ",
319
+ "declarationDir ": "lib ",
335
320
"strict": true,
336
321
"moduleResolution": "node",
337
322
"allowSyntheticDefaultImports": true,
338
- "esModuleInterop": true
323
+ "esModuleInterop": true,
324
+ "resolveJsonModule": true
339
325
},
340
- "include": ["components"],
341
- + "exclude": ["components/**/demo"]
326
+ "include": ["components", "global.d.ts"],
327
+ - "exclude": ["node_modules"]
328
+ + "exclude": ["node_modules", "**/demo/**"]
342
329
}
330
+
343
331
```
344
332
333
+ 新的问题出现了,vscode 的 alias 提示还是依赖 tsconfig.json,忽略 demo 文件夹后,demo 内的文件模块类型找不到声明(paths 失效),所以不能将 demo 在 tsconfig.json 中移除:
334
+
335
+ ``` diff
336
+ {
337
+ - "exclude": ["node_modules", "**/demo/**"]
338
+ + "exclude": ["node_modules"]
339
+ }
340
+ ```
341
+
342
+ 新建一个 tsconfig.build.json 文件:
343
+
344
+ ** tsconfig.build.json**
345
+
346
+ ``` json
347
+ {
348
+ "extends" : " ./tsconfig.json" ,
349
+ "exclude" : [" **/demo/**" , " node_modules" ]
350
+ }
351
+ ```
352
+
353
+ 后续使用 tsc 生成类型声明文件指定` tsconfig.build.json ` 即可。
354
+
345
355
### 改造相关文件
346
356
347
357
** components/alert/demo/1-demo-basic.tsx**
0 commit comments