前面已经讲过一些使用react创建组件库的方式,这次玩真的,从0开始。
创建组件项目
create-react-app react-ts-ui-demo --typescript
样式系统文件的结构设计
安装node-sass
npm i node-sass -S
定义全局样式 src/styles/_variables.scss
$white: #fff !default;
$gray-100:#f8f9fa !default;
$gray-200:#e9ecef !default;
$gray-300:#dee2e6 !default;
$gray-400:#ced4da !default;
$gray-500:#adb4db !default;
$gray-600:#6c757d !default;
$gray-700:#495057 !default;
$gray-800:#343a40 !default;
$gray-900:#212529 !default;
$black: #000 !default;
$blue:#0d6efd !default;
$indigo:#6610f2 !default;
$purple:#6f42c1 !default;
$pink:#d63384 !default;
$red:#dc3545 !default;
$orange:#fd7314 !default;
$yellow:#fadb14 !default;
$green:#52c41a !default;
$teal:#20c997 !default;
$cyan: #17a2b8 !default;
$primary: $blue !default;
$secondary:$gray-600 !default;
$success:$green !default;
$info: $cyan !default;
$warning: $yellow !default;
$danger: $red !default;
$light:$gray-100 !default;
$dark: $gray-800 !default;
$theme-colors:(
"primary":$primary,
"secondary":$secondary,
"success":$success,
"info":$info,
"warning":$warning,
"danger":$danger,
"light":$light,
"dark":$dark
);
$font-family-sans-serif:-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji" !default;
$font-family-monospace: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace !default;
$font-family-base: $font-family-sans-serif !default;
// 字体大小
$font-size-base: 1rem !default;
$font-size-lg:$font-size-base*1.25 !default;
$font-size-sm:$font-size-base*0.875 !default;
// 后续定义省略
整合normalize.css到 src/styles/_reboot.scss中
拷贝 https://github.com/necolas/normalize.css 中的 normalize.css到_reboot.scss中 整合之前定义的变量
src/styles/_reboot.scss
// stylelint-disable at-rule-no-vendor-prefix, declaration-no-important, selector-no-qualifying-type, property-no-vendor-prefix
// Reboot
//
// Normalization of HTML elements, manually forked from Normalize.css to remove
// styles targeting irrelevant browsers while applying new styles.
//
// Normalize is licensed MIT. https://github.com/necolas/normalize.css
// Document
//
// Change from `box-sizing: content-box` so that `width` is not affected by `padding` or `border`.
*,
*::before,
*::after {
box-sizing: border-box;
}
// Body
//
// 1. Remove the margin in all browsers.
// 2. As a best practice, apply a default `background-color`.
// 3. Prevent adjustments of font size after orientation changes in IE on Windows Phone and in iOS.
// 4. Change the default tap highlight to be completely transparent in iOS.
body {
margin: 0; // 1
font-family: $font-family-base;
font-size: $font-size-base;
font-weight: $font-weight-base;
line-height: $line-height-base;
color: $body-color;
text-align: $body-text-align;
background-color: $body-bg; // 2
-webkit-text-size-adjust: 100%; // 3
-webkit-tap-highlight-color: rgba($black, 0); // 4
}
// Future-proof rule: in browsers that support :focus-visible, suppress the focus outline
// on elements that programmatically receive focus but wouldn't normally show a visible
// focus outline. In general, this would mean that the outline is only applied if the
// interaction that led to the element receiving programmatic focus was a keyboard interaction,
// or the browser has somehow determined that the user is primarily a keyboard user and/or
// wants focus outlines to always be presented.
//
// See https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible
// and https://developer.paciellogroup.com/blog/2018/03/focus-visible-and-backwards-compatibility/
[tabindex="-1"]:focus:not(:focus-visible) {
outline: 0 !important;
}
// Content grouping
//
// 1. Reset Firefox's gray color
// 2. Set correct height and prevent the `size` attribute to make the `hr` look like an input field
// See https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_hr_size
hr {
margin: $hr-margin-y 0;
color: $hr-color; // 1
background-color: currentColor;
border: 0;
opacity: $hr-opacity;
}
hr:not([size]) {
height: $hr-height; // 2
}
// Typography
//
// 1. Remove top margins from headings
// By default, `<h1>`-`<h6>` all receive top and bottom margins. We nuke the top
// margin for easier control within type scales as it avoids margin collapsing.
%heading {
margin-top: 0; // 1
margin-bottom: $headings-margin-bottom;
font-family: $headings-font-family;
font-style: $headings-font-style;
font-weight: $headings-font-weight;
line-height: $headings-line-height;
color: $headings-color;
}
// 后续定义省略
创建样式入口文件 src/styles/index.scss
//config
@import "variables";
//layout
@import "reboot";
在src/index.tsx中引入 样式入口文件
import './styles/index.scss'
安装一个css的小工具 classnames
npm i classnames -S
npm i @types/classnames -S
从最简单组件做起,创建一个button组件
在app.tsx中测试使用button组件
具体源码查看 https://github.com/lilugirl/react-ts-ui-demo
接下来将为组件添加测试
在根目录下创建jest.test.js文件
test('test common matcher',()=>{
expect(2+2).toBe(4)
expect(2+2).not.toBe(5)
})
test('test to be true or false',()=>{
expect(1).toBeTruthy()
expect(0).toBeFalsy()
})
test('test nuimber',()=>{
expect(4).toBeGreaterThan(3)
expect(2).toBeLessThan(3)
})
test('test object',()=>{
expect({name:'liuyi'}).toEqual({name:'liuyi'})
})
执行命令
npx jest jest-test.js --watch