内容简介:技术栈:React + Webpack + TypeScript + Docz先上脚手架目录结构
技术栈:React + Webpack + TypeScript + Docz
先上脚手架目录结构
docz example lib scripts src
注意:本脚手架只是基本配置,内部代码比较简洁,主要是起到一个学习引导作用。
组件开发过程中没什么技术难点,主要是样式处理。 全局 default.less 样式处理借鉴了 antd ,将常用的样式用变量存储,方便后续主题设置
@wui-prefix: wui; @primary-color: red; @info-color: #3bb4f2; @success-color: #5eb95e; @danger-color: #dd514c; @warning-color: #f37b1d; @white: #fff; @black: #000; @alert-info-bg-color: @info-color; @alert-success-bg-color: @success-color; @alert-warning-bg-color: @warning-color; @alert-danger-bg-color: @danger-color; 复制代码
mixins/alert.less 混合用函数注入变量
.var-alert(@background-color, @border-corlor, @text-color) {
border: 1px solid @border-corlor;
background-color: @background-color;
color: @text-color;
}
复制代码
然后组件 components/alert/index.less 中直接引入即可
@import '../style/default.less';
@import '../style/mixins/alert.less';
@alert-prefix-cls: ~'@{wui-prefix}-alert';
.@{alert-prefix-cls} {
position: relative;
padding: 8px 16px;
margin-bottom: 10px;
&-info {
.var-alert(@alert-info-bg-color, @alert-info-bg-color, @white);
}
&-success {
.var-alert(@alert-success-bg-color, @alert-success-bg-color, @white);
}
&-warning {
.var-alert(@alert-warning-bg-color, @alert-warning-bg-color, @white);
}
&-danger {
.var-alert(@alert-danger-bg-color, @alert-danger-bg-color, @white);
}
}
复制代码
构建过程中, less 文件无需转换为 css 文件,只需通过脚本遍历文件夹同步到对应目录下即可, build-less.js 代码:
const readFiles = async (filePath, name, callback) => {
const files = fs.readdirSync(filePath)
files.forEach(file => {
const filedir = path.join(filePath, file)
fs.stat(filedir, (error, stats) => {
if (error) {
return console.error('stats error:', error)
}
if (stats.isFile() && filedir.indexOf(name) > -1) {
callback && callback(filedir)
} else if (stats.isDirectory()) {
readFiles(filedir, name, callback)
}
})
})
}
const componentsPath = 'src/components'
readFiles(
path.join(__dirname, '../', componentsPath),
'.less',
(file, error) => {
if (error) {
return console.error('read files error:', error)
}
fs.outputFileSync(
file.replace(componentsPath, 'lib'),
fs.readFileSync(file)
)
}
)
复制代码
文档界面:
贴一段 Alert 组件简易代码
import React, { Component } from 'react'
import classNames from 'classnames'
import { getPrefix } from '../util/method'
import './index.less'
interface AlertProps {
type?: 'success' | 'error' | 'warn' | 'info'
message: React.ReactNode
className?: string
}
export default class Alert extends Component<AlertProps> {
render() {
const { type = 'info' } = this.props
const prefix = getPrefix('alert')
const className = classNames(prefix, `${prefix}-${type}`)
return <div className={className}>{this.props.message}</div>
}
}
复制代码
在本地开发及测试完组件后需要发布到仓库测试,不建议发布到npm官方仓库中,因为毕竟是半成品,可以使用 verdaccio 在本地搭建一个私有仓库进行测试
搭建私有参考可以参考 使用verdaccio搭建私有npm仓库
以上所述就是小编给大家介绍的《如何开发React UI组件库》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- Serverless 组件开发尝试:全局变量组件和单独部署组件
- UI组件库从1到N开发心得-组件篇
- PJBreedsViewController 组件开发总结
- PJPickerView 组件开发总结
- Vue组件开发
- Laravel 后台开发常用组件
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
A=B
Marko Petkovsek、Herbert S. Wilf、Doron Zeilberger / AK Peters, Ltd. / 1996-01 / USD 49.00
At some point, this book describes methods of solving the problem raised by Donald E. Knuth in the classical book "The Art of Computer Programming, Volume 1: Fundamental Algorithms". The main purpo......一起来看看 《A=B》 这本书的介绍吧!