@@ -130,7 +130,7 @@ import React from 'react';
130130import Alert from ' ../alert' ;
131131import ' ../style' ;
132132
133- export default () => < Alert kind= ' warning' >< / Alert> ;
133+ export default () => < Alert kind= " warning" >< / Alert> ;
134134```
135135
136136** components/alert/index.mdx**
@@ -170,15 +170,16 @@ export default () => <Alert kind='warning'></Alert>;
170170安装依赖:
171171
172172```
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
174174```
175175
176176- [ react-use] ( https://github.com/streamich/react-use ) - 2020 年了,当然要用` hooks `
177- - [ antd] ( https://ant.design/ ) - 使用一些辅助组件
178177- [ react-simple-code-editor] ( https://github.com/satya164/react-simple-code-editor ) - 代码展示区域
179178- [ prismjs] ( https://github.com/PrismJS/prism ) - 代码高亮
180179- [ raw-loader] ( https://github.com/webpack-contrib/raw-loader ) - 将源码转成字符串
181180- [ react-copy-to-clipboard] ( https://github.com/nkbt/react-copy-to-clipboard ) - 让用户爸爸们能够 copy demo 代码
181+ - react-tooltip/react-feather 辅助组件
182+ - styled-components 方便在文档示例中让用户看到样式,也用作文档组件的样式处理
182183
183184> 这些依赖都是服务于文档站点应用,和组件库自身毫无关联。
184185
@@ -192,7 +193,7 @@ yarn add react-use antd react-simple-code-editor prismjs react-copy-to-clipboard
192193
193194```
194195├── happy-box
195- │ ├── index.less
196+ │ ├── style.ts
196197│ └── index.tsx
197198└── index.ts
198199```
@@ -203,63 +204,65 @@ yarn add react-use antd react-simple-code-editor prismjs react-copy-to-clipboard
203204import React from ' react' ;
204205import Editor from ' react-simple-code-editor' ;
205206import 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' ;
208211import { highlight , languages } from ' prismjs/components/prism-core' ;
212+ import { StyledContainer , StyledIconWrapper } from ' ./style' ;
209213
210214import ' prismjs/components/prism-clike' ;
211215import ' prismjs/components/prism-javascript' ;
212216import ' prismjs/components/prism-markup' ;
213- import ' ./index.less' ;
214217
215218require (' prismjs/components/prism-jsx' );
216219
217- const { Text } = Typography;
218220interface Props {
219221 code: string;
220222 title?: React .ReactNode ;
221223 desc?: React .ReactNode ;
222224}
223225
224- const HappyBox: React .FC <Props > = ({ code, title, desc, children }) => {
226+ export const HappyBox: React .FC <Props > = ({ code, title, desc, children }) => {
225227 const [isEditVisible , toggleEditVisible ] = useToggle (false );
226228
227229 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>
234235 < / 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>
245248 < / div>
246249 < / section>
247250 {renderEditor ()}
248- < / div>
251+ < ReactTooltip / >
252+ < / StyledContainer>
249253 );
250254
251- /* 代码展示区域 */
252255 function renderEditor () {
253256 if (! isEditVisible) return null ;
254257 return (
255- < div className= ' container_editor_area' >
258+ < div className= " container_editor_area" >
256259 < Editor
257260 readOnly
258261 value= {code}
259262 onValueChange= {() => {}}
260263 highlight= {code => highlight (code, languages .jsx )}
261264 padding= {10 }
262- className= ' container__editor'
265+ className= " container__editor"
263266 style= {{
264267 fontFamily: ' "Fira code", "Fira Mono", monospace' ,
265268 fontSize: 14 ,
@@ -275,8 +278,7 @@ export default HappyBox;
275278
276279### 相关配置变更
277280
278- - 增加 ` alias ` 别名,源码展示相对路径不够友好;
279- - antd 按需引入,即使是站点应用。
281+ - 增加 ` alias ` 别名,源码展示相对路径不够友好,让用户直接拷贝才够省心
280282
281283新建` gatsby-node.js ` ,写入以下内容以开启` alias ` :
282284
@@ -289,59 +291,67 @@ exports.onCreateWebpackConfig = args => {
289291 modules: [path .resolve (__dirname , ' ../src' ), ' node_modules' ],
290292 alias: {
291293 ' happy-ui/lib' : path .resolve (__dirname , ' ../components/' ),
294+ ' happy-ui/esm' : path .resolve (__dirname , ' ../components/' ),
295+ ' happy-ui' : path .resolve (__dirname , ' ../components/' ),
292296 },
293297 },
294298 });
295299};
296300```
297301
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 ` 时包含其中:
323303
324304** tsconfig.json**
325305
326306``` diff
327307{
328308 "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+ + },
330315 "target": "esnext",
331316 "module": "commonjs",
332317 "jsx": "react",
333318 "declaration": true,
334- "outDir ": "types ",
319+ "declarationDir ": "lib ",
335320 "strict": true,
336321 "moduleResolution": "node",
337322 "allowSyntheticDefaultImports": true,
338- "esModuleInterop": true
323+ "esModuleInterop": true,
324+ "resolveJsonModule": true
339325 },
340- "include": ["components"],
341- + "exclude": ["components/**/demo"]
326+ "include": ["components", "global.d.ts"],
327+ - "exclude": ["node_modules"]
328+ + "exclude": ["node_modules", "**/demo/**"]
342329}
330+
343331```
344332
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+
345355### 改造相关文件
346356
347357** components/alert/demo/1-demo-basic.tsx**
0 commit comments