第二章:React:从0到1组件库构建——css篇

一、css样式初始化定义

其实我一直就纠结到底要不要写这一章,对于css而言我感觉好多人都不屑于顾,但是我感觉并不是每个人都会"真正"的写css,本人也是特别讨厌写css,但是迫于压力,只能重新写起css。废话不多说先看下目录设计

react-ally-component
|-- src
    |-- components

    |-- styles
        |-- index.scss // 所有组件的css 以及 初始化的css都引入到当前文件中
        |-- _animation.scss // 关于动画的css
        |-- _mixin.scss  // scss混合宏的封装
                |-- _reset.scss  // 初始化的css
                |-- _variables.scss // scss的所有变量

二、_variables

我们先从_variables文件说起,我感觉所有不知如何设计组件库的同学有一部分原因都卡在初始样式这一关,因为你需要设计整个UI框架的色彩规范、布局规范、以及一些细节。至今我也不知如何下手,只能摸着石头过河

2-1、色彩设计

色彩设计你可以参考一些色彩网站,但是本章节咱们参考Ant Design的色彩设计(我们目前只考虑功能色)
所谓的功能色 : 比如成功、出错、失败、提醒、链接等

// 灰色
$white:    #fff    !default;
$gray-100: #f8f9fa !default;
$gray-200: #e9ecef !default;
$gray-300: #dee2e6 !default;
$gray-400: #ced4da !default;
$gray-500: #adb5bd !default;
$gray-600: #6c757d !default;
$gray-700: #495057 !default;
$gray-800: #343a40 !default;
$gray-900: #212529 !default;
$black:    #000    !default;

// 功能色
$blue:     #1890ff;
$green:    #52c41a;
$red:      #ff4d4f;
$cyan:     #faad14;


$primary:   $blue  !default;
$success:   $green !default;
$warning:   $cyan  !default;
$danger:    $red   !default;

// 功能色集合
$theme-colors: 
(
  "primary":    $primary,
  "success":    $success,
  "warning":    $warning,
  "danger":     $danger,
);

// 字体
$font-family: -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';
$font-family-base: $font-family !default;

// 字体大小
$font-size-base: 1rem;
$font-size-lg:   $font-size-base * 1.25 !default;
$font-size-sm:   $font-size-base * .875 !default;

// 行高
$line-height-base:            1.5 !default;
$line-height-lg:              2 !default;
$line-height-sm:              1.25 !default;

// 链接
$link-color:                              $primary !default;
$link-decoration:                         none !default;
$link-hover-color:                        darken($link-color, 15%) !default;
$link-hover-decoration:                   underline !default;

// body
$body-bg:                   $white !default;
$body-color:                $gray-900 !default;
$body-text-align:           null !default;

// 边框
$border-width:                1px !default;
$border-color:                $gray-300 !default;

$border-radius:               .25rem !default;
$border-radius-lg:            .3rem !default;
$border-radius-sm:            .2rem !default;

三、_reset.scss

reset为重置样式文件

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body,
ul,
li,
ol,
dl,
dd,
dt,
p,
h1,
h2,
h3,
h4,
h5,
h6,
form,
fieldset,
legend,
img {
    margin: 0;
    padding: 0;
}
img {
    display: block;
}

address,
caption,
cite,
code,
dfn,
th,
var {
    font-style: normal;
    font-weight: normal;
}

ul,
ol,
li {
    list-style: none;
}

body {
    margin: 0; 
    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; 
}

.border-1px::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  width: 200%;
  height: 200%;
  -webkit-transform: scale(0.5);
  transform: scale(0.5);
  -webkit-transform-origin: left top;
  transform-origin: left top;
  pointer-events: none;
}

a {
  color: $link-color;
  text-decoration: $link-decoration;

  &:hover {
    color: $link-hover-color;
    text-decoration: $link-hover-decoration;
  }
}

input,
button,
select,
optgroup,
textarea {
  margin: 0;
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
}

四、index.scss

index.scss为css入口文件,组件的样式以及通用样式在此引入

  // config
@import "variables";

//layout
@import "reset";

// button
@import "../components/Button/mixin";
@import "../components/Button/style";

五、Button组件为例

目录结构

react-ally-component
|-- src
    |-- components
                |-- Button
                      |-- conf.ts // ts类型文件
                        |-- Button.tsx // 组件编写文件
                        |-- Button.stories.tsx // story 文件
                        |-- _style.scss // 组件样式文件
                        |-- _mixin.scss // 组件混合宏文件
                        |-- index.ts // 组件导出文件
    |-- styles
        |-- index.scss // 所有组件的css 以及 初始化的css都引入到当前文件中
        |-- _animation.scss // 关于动画的css
        |-- _mixin.scss  // scss混合宏的封装
                |-- _reset.scss  // 初始化的css
                |-- _variables.scss // scss的所有变量

5-1、_style.scss

.btn{
    position: relative;
    display: inline-block;
    font-weight: $btn-font-weight;
    line-height: $btn-line-height;
    color:$body-color;
    white-space: nowrap;
    text-align: center;
    vertical-align: middle;
    background-image: none;
    border: $border-width solid transparent;
    box-shadow: $btn-box-shadow;
    cursor: pointer;
    transition: $btn-transition;
    @include button-size( $btn-padding-y,  $btn-padding-x,  $btn-font-size,  $border-radius);
    box-shadow: $btn-box-shadow;
    &.disabled,
    &[disabled] {
      cursor: not-allowed;
      opacity: $btn-disabled-opacity;
      box-shadow: none;
      > * {
        pointer-events: none;
      }
    }
}

.btn-lg {
    @include button-size($btn-padding-y-lg, $btn-padding-x-lg, $btn-font-size-lg, $btn-border-radius-lg);
  }
  .btn-sm {
    @include button-size($btn-padding-y-sm, $btn-padding-x-sm, $btn-font-size-sm, $btn-border-radius-sm);
  }
  
  .btn-primary {
    @include button-style($primary, $primary, $white)
  }
  .btn-danger {
    @include button-style($danger, $danger, $white)
  }
  
  .btn-default {
    @include button-style($white, $gray-400, $body-color, $white, $primary, $primary)
  }
  
  .btn-link {
    font-weight: $font-weight-normal;
    color: $btn-link-color;
    text-decoration: $link-decoration;
    box-shadow: none;
    &:hover {
      color: $btn-link-hover-color;
      text-decoration: $link-hover-decoration; 
    }
    &:focus,
    &.focus {
      text-decoration: $link-hover-decoration;
      box-shadow: none;
    }
    &:disabled,
    &.disabled {
      color: $btn-link-disabled-color;
      pointer-events: none;
    }
  }

5-2、_mixin.scss

@mixin button-size($padding-y, $padding-x, $font-size, $border-raduis) {
    padding: $padding-y $padding-x;
    font-size: $font-size;
    border-radius: $border-raduis;
}


@mixin button-style(
  $background,
  $border,
  $color,
  $hover-background: lighten($background, 7.5%),
  $hover-border: lighten($border, 10%),
  $hover-color: $color,
) {
  color: $color;
  background: $background;
  border-color: $border;
  &:hover {
    color: $hover-color;
    background: $hover-background;
    border-color: $hover-border;    
  }
  &:focus,
  &.focus {
    color: $hover-color;
    background: $hover-background;
    border-color: $hover-border;    
  }
  &:disabled,
  &.disabled {
    color: $color;
    background: $background;
    border-color: $border;    
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,539评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,594评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,871评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,963评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,984评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,763评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,468评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,850评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,002评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,144评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,823评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,483评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,026评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,150评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,415评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,092评论 2 355