web 的三要素 html, css, js 在前端组件化的过程中,比如 reactvue 等组件化框架的运用,使 html 的弱化与 js 的强化 成为了一种趋势,而在这个过程中,其实还有另一种趋势也在慢慢形成:css 的弱化与 js 的强化

之前有写过一篇 CSS 模块化,但对 css in js 这种理念没有过多讲解,所以这次深入一下。

css in js 理念,即是摒弃原有的用 .css 文件书写样式,而把样式写进 js 里面,这样就可以做到一个组件对应一个文件、一个文件便是一个组件。

1. 支持的第三方库

  1. styled-components: 仅支持 react
  2. radium: 仅支持 react
  3. emotion
  4. aphrodite
  5. polished
  6. jss
  7. glamorous: 仅支持 react
  8. styled-jsx: 仅支持 react
  9. glamor: 仅支持 react
  10. styletron: 仅支持 react

更多第三方库可以参考 css-in-js

2. 书写方式

一般 css in js 的写法有两种:

  1. 使用 es6 的模板字符串
  2. 使用 js 对象 {}

2.1 使用 es6 的模板字符串

styled-componentsemotionstyled-jsx 都是采用的这种写法。

比如 styled-components:

 import React from 'react';
import styled from 'styled-components'; // 创建一个使用 <h1> 标签的 <Title> React 组件
const Title = styled.h1`
font-size: .5em;
text-align: center;
color: palevioletred;
`; // 创建一个使用 <section> 标签的 <Wrapper> React 组件
const Wrapper = styled.section`
padding: 4em;
background: papayawhip;
`; // 就像正常的 React 组件一样,只不过他们都自带样式
<Wrapper>
<Title>Hello World, this is my first styled component!</Title>
</Wrapper>

比如 emotion:

 import { css } from 'emotion';

 const app = document.getElementById('root');
const myStyle = css`
color: rebeccapurple;
`;
app.classList.add(myStyle);

2.2 使用 js 对象 {}

radiumaphroditepolishedjssglamorousglamorstyletron 都是采用的这种写法。

比如 radium:

 import Radium from 'radium';
import React from 'react';
import color from 'color'; var styles = {
base: {
color: '#fff',
':hover': {
background: color('#0074d9').lighten(0.2).hexString()
}
},
primary: {
background: '#0074D9'
},
warning: {
background: '#FF4136'
}
}; class Button extends React.Component {
render() {
return (
<button
style={[styles.base, styles[this.props.kind]]}>
{this.props.children}
</button>
);
}
} Button = Radium(Button); <Button kind="primary">Primary</Button>
<Button kind="warning">Warning</Button>

比如 aphrodite:

 import React, { Component } from 'react';
import { StyleSheet, css } from 'aphrodite'; const styles = StyleSheet.create({
red: {
backgroundColor: 'red'
},
blue: {
backgroundColor: 'blue'
},
hover: {
':hover': {
backgroundColor: 'red'
}
},
small: {
'@media (max-width: 600px)': {
backgroundColor: 'red',
}
}
}); class App extends Component {
render() {
return <div>
<span className={css(styles.red)}>
This is red.
</span>
<span className={css(styles.hover)}>
This turns red on hover.
</span>
<span className={css(styles.small)}>
This turns red when the browser is less than 600px width.
</span>
<span className={css(styles.red, styles.blue)}>
This is blue.
</span>
<span className={css(styles.blue, styles.small)}>
This is blue and turns red when the browser is less than 600px width.
</span>
</div>;
}
}

这种写法的好处是,不需要 es6 的语法,对属性可以更方便的操作。

3. 决定是否使用

如果你是喜欢把样式和组件分开书写,那么这种方式就可能不太适合你;如果你追求一个组件对应一个文件、一个文件便是一个组件,那就立马用上吧。

4. 后续

转载自 https://segmentfault.com/a/1190000016422897

css 的弱化与 js 的强化(转)的更多相关文章

  1. 前端进阶(12) - css 的弱化与 js 的强化

    css 的弱化与 js 的强化 web 的三要素 html, css, js 在前端组件化的过程中,比如 react.vue 等组件化框架的运用,使 html 的弱化与 js 的强化 成为了一种趋势, ...

  2. CSS样式表、JS脚本加载顺序与SpringMVC在URL路径中传参数与SpringMVC 拦截器

    CSS样式表和JS脚本加载顺序 Css样式表文件要在<head>中先加载,这样网页显示时可以第一次就渲染出正确的布局和样式,网页就不会闪烁,或跳变 JS脚本尽可能放在<body> ...

  3. switchable css dark theme in js & html custom element

    switchable css dark theme in js & html custom element dark theme / dark mode https://codepen.io/ ...

  4. 【CSS进阶】原生JS getComputedStyle等方法解析

    最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...

  5. [django]Django的css、image和js静态文件生产环境配置

    前言:在Django中HTML文件如果采用外联的方式引入css,js文件或者image图片,一般采用<link rel="stylesheet" href="../ ...

  6. animate.css配合wow.min.js实现各种页面滚动效果

    有的页面在向下滚动的时候,有些元素会产生细小的动画效果.虽然动画比较小,但却能吸引你的注意.比如刚刚发布的 iPhone 6 的页面(查看).如果你希望你的页面也更加有趣,那么你可以试试 WOW.js ...

  7. css字体转换程序(Node.js)

    我下载的是ttf文件,css导入的文件有多种格式:eot,woff,svg 在windows下,需要寻找相应的exe文件来处理或者node.js来处理: ttf2eot: https://github ...

  8. css动画结束后 js无法修改translated值 .

    由于项目的需要,俺要做一些页面的转场动画. 即将是移动端,肯定是首先css动画了. 结果确发现,css动画中,如果设置animation-fill-mode: both;在动画结束后无法个性trans ...

  9. 将鼠标移到文本弹出一些字幕CSS达到,不及格JS达到

    使用css实施内容流行,否js代码,下面的代码,可直接使用复制, 需要背景图到下面的地址下载,谢谢! 住址:http://download.csdn.net/detail/zurich1979/722 ...

随机推荐

  1. [转帖]Exadata X8发布

    Exadata X8发布 2019-04-09 10:45:32 dingdingfish 阅读数 193 文章标签: ExadataOracleX8 更多 分类专栏: Exadata   版权声明: ...

  2. 设计模式 AOP,OOP

    AOP.OOP在字面上虽然非常类似,但却是面向不同领域的两种设计思想. 简单说,AOP面向动词领域,OOP面向名词领域 AOP: (Aspect Oriented Programming) 面向切面编 ...

  3. python 之 面向对象(反射、__str__、__del__)

    7.10 反射 下述四个函数是专门用来操作类与对象属性的.通过字符串来操作类与对象的属性,这种操作称为反射 class People: country="China" def __ ...

  4. C基础 stack 设计

    前言 - stack 设计思路 先说说设计 stack 结构的原由. 以前我们再释放查找树的时候多数用递归的后续遍历去释放. 其内部隐含了运行时的函数栈, 有些语言中存在爆栈风险. 所以想运用显示栈来 ...

  5. go开发环境

    1.go 下载地址 https://studygolang.com/dl 根据操作系统 下载相应的安装包 2.设置环境变量 goroot gopath path 增加%goroot%\bin 3.开发 ...

  6. go包管理

    摘自: http://blueskykong.com/2019/02/18/go-dep-1/ https://www.cnblogs.com/apocelipes/p/10295096.html#v ...

  7. ubuntu Docker安装部署Rancher

    一.Rancher简介 Rancher是一个开源的企业级容器管理平台.通过Rancher,企业再也不必自己使用一系列的开源软件去从头搭建容器服务平台.Rancher提供了在生产环境中使用的管理Dock ...

  8. linux服务器同时运行两个或多个tomcat

    第一步:条件:配置好jdk,如果没有配置好请看:https://www.cnblogs.com/weibanggang/p/11460537.html 第二步:下载安装tomcat 下载tomcat到 ...

  9. Java 阿拉伯数字转换为中文大写数字

    Java 阿拉伯数字转换为中文大写数字 /** * <html> * <body> * <P> Copyright 1994 JsonInternational&l ...

  10. json.dumps()包装中文字符串

    开发环境 系统: ubuntu18.04 系统编码: $LANG = en_US.UTF-8 python解释器版本: Python 3.6.7 乱码现场 使用 json.dumps() 将 dict ...