前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >从零开始构建自己的react组件库

从零开始构建自己的react组件库

作者头像
lilugirl
发布2020-11-26 11:38:09
5420
发布2020-11-26 11:38:09
举报
文章被收录于专栏:前端导学前端导学

前面已经讲过一些使用react创建组件库的方式,这次玩真的,从0开始。

创建组件项目

代码语言:javascript
复制
create-react-app react-ts-ui-demo --typescript

样式系统文件的结构设计

安装node-sass

代码语言:javascript
复制
npm i node-sass -S

定义全局样式 src/styles/_variables.scss

代码语言:javascript
复制
$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

代码语言:javascript
复制
// 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

代码语言:javascript
复制
//config
@import "variables";

//layout
@import "reboot";

在src/index.tsx中引入 样式入口文件

import './styles/index.scss'

安装一个css的小工具 classnames

代码语言:javascript
复制
npm i classnames -S
npm i @types/classnames -S

从最简单组件做起,创建一个button组件

在app.tsx中测试使用button组件

具体源码查看 https://github.com/lilugirl/react-ts-ui-demo

接下来将为组件添加测试

在根目录下创建jest.test.js文件

代码语言:javascript
复制
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'})
})

执行命令

代码语言:javascript
复制
npx jest jest-test.js --watch
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档