前面的话

  使用jsx语法可以实现HTML in JS,使用svgr可以实现svg in JS,使用styled-components可以实现CSS in JS。这样,使用react开发,就变成了使用JS开发,统一且方便。本文将详细介绍styled-components的用法

基本用法

【安装】

$ npm install styled-components

  使用非常简单,下面的代码片段展示了 React 项目中使用 styled-components,定义了 Wrapper 和 Button 两个组件,包含了 html 结构和对应的 css 样式,从而将样式和组件之间的 class 映射关系移除

import styled from 'styled-components';
const Wrapper = styled.section`
margin: auto;
width: 300px;
text-align: center;
`;
const Button = styled.button`
width: 100px;
color: white;
background: skyblue;
`;
render(
<Wrapper>
<Button>Hello World</Button>
</Wrapper>
);

组件样式

  如果要为组件设置样式,则需要使用小括号语法,而且需要在组件上设置className和children

const Link = ({ className, children }) => (
<a className={className}>
{children}
</a>
)
const StyledLink = styled(Link)`
color: palevioletred;
font-weight: bold;
`;
render(
<div>
<Link>Unstyled, boring Link</Link>
<br />
<StyledLink>Styled, exciting Link</StyledLink>
</div>
);

  

扩展样式

  使用扩展样式,可以基于已经存在的样式进行扩展

const Button = styled.button`
color: palevioletred;
font-size: 1em;
margin: 1em;
padding: .25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
`; const TomatoButton = Button.extend`
color: tomato;
border-color: tomato;
`; render(
<div>
<Button>Normal Button</Button>
<TomatoButton>Tomato Button</TomatoButton>
</div>
);

更换标签

  在某些情况下,可以在复用样式的基础上更换标签

const Button = styled.button`
display: inline-block;
color: palevioletred;
font-size: 1em;
margin: 1em;
padding: .25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
`;
const Link = Button.withComponent('a')
const TomatoLink = Link.extend`
color: tomato;
border-color: tomato;
`;
render(
<div>
<Button>Normal Button</Button>
<Link>Normal Link</Link>
<TomatoLink>Tomato Link</TomatoLink>
</div>
);

传递属性

  通过props可以从父组件向子组件传递属性

const GlassModal = ({ children, className, backgroundColor, padding }) => (
<Wrap backgroundColor={backgroundColor}>
<Main padding={padding} className={className}>
{children}
</Main>
</Wrap>
)
export default GlassModal const Wrap = styled.section`
background-color: ${props => props.backgroundColor || BLUE_DARK};
`
const Main = styled.main`
padding: ${props => props.padding || ''};
background-color: ${OPACITY_LIGHT};
`
const StyledGlassModal = styled(GlassModal)`
padding: 20px 10px;
text-align: center;
`

  或者,基于prop来定制主题

const Button = styled.button`
background: ${props => props.primary ? 'palevioletred' : 'white'};
color: ${props => props.primary ? 'white' : 'palevioletred'};
font-size: 1em;
margin: 1em;
padding: .25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
`; render(
<div>
<Button>Normal</Button>
<Button primary>Primary</Button>
</div>
);

attrs函数

  通过使用attrs函数,可以用来设置其他属性

const Input = styled.input.attrs({
type: 'password',
margin: props => props.size || '1em',
padding: props => props.size || '1em'
})`
color: palevioletred;
border-radius: 3px;
margin: ${props => props.margin};
padding: ${props => props.padding};
`;
render(
<div>
<Input placeholder="A small text input" size="1em" />
<Input placeholder="A bigger text input" size="2em" />
</div>
);

  或者引入第三方库的样式,如activeClassName

const Button = styled.button.attrs({
className: 'small',
})`
background: black;
color: white;
`;

  编译后的 html 结构如下:

<button class="sc-gPEVay small gYllyG">
Styled Components
</button>

动画

  styled-components 同样对 css 动画中的 @keyframe 做了很好的支持

import { keyframes } from 'styled-components';
const rotate360 = keyframes`
from {transform: rotate(0deg);}
to {transform: rotate(360deg);}
`;
const Rotate = styled.div`
display: inline-block;
animation: ${rotate360} 2s linear infinite; `;
render(
<Rotate>&lt;

使用styled-components实现CSS in JS的更多相关文章

  1. 运用 CSS in JS 实现模块化

    一.什么是 CSS in JS 上图来源:https://2019.stateofcss.com/technologies/ CSS in JS 是2014年推出的一种设计模式,它的核心思想是把 CS ...

  2. 9 CSS in JS Libraries You Should Know in 2018

    转自:https://blog.bitsrc.io/9-css-in-js-libraries-you-should-know-in-2018-25afb4025b9b 实际上  wix 的 styl ...

  3. styled components草根中文版文档

    1.styled components官网网址 https://www.styled-components.com/docs   以组件的形式来写样式. 1.1安装 yarn add styled-c ...

  4. WebKit HTML、CSS、JS

    开发者需要了解的WebKit https://www.infoq.cn/article/webkit-for-developers 开发者需要了解的 WebKit   彭超 2013 年 3 月 18 ...

  5. Vue项目中引入外部文件(css、js、less)

    例子中css文件采用bootstrap.css,js文件采用jQuery,less文件用less.less(自定义文件) 步骤一:安装webpack cnpm install webpack -g 步 ...

  6. vue components & `@import css` bug

    vue components @import css not support css3 @import https://github.com/vuejs/vue-loader/issues/138#i ...

  7. vue中html、css、js 分离

    在正常的创建和引用vue文件都是html.css.js三者在一起的,这样写起来虽然方便了,但是页面比较大或者代码比较多的情况下,即使使用组件有时代码也比较多,简单来说查找不变不利于编程,大的来说影像优 ...

  8. vue初始化、数据处理、组件传参、路由传参、全局定义CSS与JS、组件生命周期

    目录 项目初始化 组件数据局部化处理 子组件 父组件 路由逻辑跳转 案例 组件传参 父传子 子组件 父组件 子传父 子组件 父组件 组件的生命周期钩子 路由传参 第一种 配置:router/index ...

  9. 6.前台项目vue环境、创建、目录重构、CSS、JS配置

    目录 前台 vue环境 创建项目 重构项目目录 文件修订:目录中非配置文件的多余文件可以移除 App.vue router/index.js Home.vue 全局配置:全局样式.配置文件 globa ...

随机推荐

  1. KNN-笔记(1)

    1 - 背景 KNN:k近邻,表示基于k个最近的邻居的一种机器学习方法.该方法原理简单,构造方便.且是一个非参数化模型. KNN是一个"懒学习"方法,也就是其本身没有训练过程.只有 ...

  2. 【php增删改查实例】第二十二节 - 引入百度地图

    20.用户新增地址字段 在实际的开发中,经常会出现对数据表新增或者修改字段的事情,所以,当用户提出加字段的需求时,我们的页面以及后台程序都要进行相应的改动. 本节就以增加一个地址字段为例. 打开nav ...

  3. 微信小程序项目实战 - 菜谱大全

    1. 项目简介 最近研究小程序云开发,上线了一个有关菜品查询的小程序.包括搜索.分享转发.收藏.查看历史记录等功能.菜谱 API 来自聚合数据.云开发为开发者提供完整的云端支持,弱化后端和运维概念,无 ...

  4. 多模块后带来的问题解决方法 - OSGI原形(.NET)

    目前只做了基础的功能,比如: 各个模块单独的AppDomain容器 Activator激活 导出的服务检查 不过,虽说这样,但目前的这个版本已经能实现模块分离.互相依赖调用等功能了,对模块划分已经有很 ...

  5. Redis中单机数据库的实现

    1. 内存操作层 zmalloc 系接口 redis为了优化内存操作, 封装了一层内存操作接口. 默认情况下, 其底层实现就是最简朴的libc中的malloc系列接口. 如果有定制化需求, 可以通过配 ...

  6. 【小技巧】css文字两端对齐

    一.文字两端对齐方法:  text-align-last: justify; 二.举个丽子:  三.效果如下:  四.注意:  要使文字在容器中两端对齐,该容器需要是一个块级元素,要有自己的宽度.

  7. 200 ok 几种状态

    浏览器加载资源成功一般会有几种状态 200 ok   ----  从原始服务器请求成功 200 ok from cache    ---- 200 ok from disk cache  ---- 2 ...

  8. hibernate多对多 一对多 及简单入门 主键生成策略

    Hibernate简单使用 入门 通过hibernate的 一对多 多对多轻松看懂hibernate配置 (不使用注解) hibernate对jdbc访问数据库的代码进行轻量级封装,简化重复代码 减少 ...

  9. ORACLE 当字段中有数据如何修改字段类型

    创建视图的时候,因为表太多,里面一些字段类型不一样,PL/SQL报错,为‘表达式必须具有对应表达式相同的数据类型’,发现后,一个字段的类型为CLOB和VARCHAR2(4000)两种,将CLOB进行修 ...

  10. vue的三种传参方式

    <template> <div> <router-link :to="{'name':'x',params:{'type':'users'}}"> ...