集成css代码在js中
CSS-IN-JS 是 WEB 项⽬中将 CSS 代码捆绑在 JavaScript 代码中的解决⽅案.这种⽅案旨在解决 CSS 的局限性, 例如缺乏动态功能, 作⽤域和可移植性.
Emotion 是⼀个旨在使⽤ JavaScript 编写 CSS 样式的库.
npm install @emotion/core @emotion/styled
通知 babel, 不再需要将 jsx 语法转换为 React.createElement ⽅法, ⽽是需要转换为 jsx ⽅法.
import React from 'react';
/** @jsx jsx */
import {jsx} from '@emotion/core'
function App() {
return
}
import React from 'react';
function App() {
return
}
props 对象中的 css 属性优先级⾼于组件内部的 css 属性. 在调⽤组件时可以在覆盖组件默认样式.
css.js
样式化组件就是⽤来构建⽤户界⾯的,是 emotion 库提供的另⼀种为元素添加样式的⽅式。
import react from 'react';
import styled from '@emotion/styled'
const Button = styled.button`
width: 100px;
height: 30px;
background: blue;
`
const Container = styled.div({
width: 1000,
background: pink,
margin: '0 auto',
})
function App() {
return
}
export default App;
import react from 'react';
import styled from '@emotion/styled'
const Button = styled.button`
width: 100px;
height: 30px;
background: ${props => props.bgColor || 'yellow'};
`
const Container = styled.div({
width: props.w || 1000,
background: pink,
margin: '0 auto',
})
function App() {
return
}
export default App;
根据 props 属性覆盖样式
import react from 'react';
import styled from '@emotion/styled'
const Button = styled.button`
width: 100px;
height: 30px;
background: ${props => props.bgColor || 'yellow'};
`
const Container = styled.div({
width: 1000,
background: 'pink',
margin: '0 auto',
}, props =>({
width: props.width,
background: props.bgColor
}));
function App() {
return
}
export default App;
Styled Components 样式化组件
import React from 'react'
import styled from '@emotion/styled'
function Demo({className}) {
return Demo
}
const Fancy = styled(Demo)`
color: red;
`
function App() {
return
}
export default App;
import React from 'react'
import styled from '@emotion/styled'
function Demo({className}) {
return Demo
}
const Fancy = styled(Demo)`
color: red;
`
const Fancy2 = styled(Demo)({
background: 'red',
color: 'white'
})
function App() {
return
}
export default App;
import React from 'react'
import styled from '@emotion/styled'
const Child = styled.div`
color: red;
`
const Parent = styled.div`
${Child} {
color: green;
}
`
function App() {
return
child
child parent
}
export default App;
import React from 'react'
import styled from '@emotion/styled'
const Child = styled.div({
color: 'red'
})
const Parent = styled.div({
[Child]: {
color: 'yellow'
}
})
function App() {
return
child
child parent
}
export default App;
import React from 'react'
import styled from '@emotion/styled'
const Container = styled.div`
width: 200px;
height: 200px;
color: pink;
background: skyblue;
&:hover {
background: pink;
}
& > span {
color: yellow;
}
`
function App() {
return
container
span
}
export default App;
要使⽤组件中的样式, 但要更改呈现的元素, 可以使⽤ as 属性
import React from 'react'
import styled from '@emotion/styled'
const Button = styled.button`
color: red;
`
function App() {
return
}
export default App;
import React from 'react'
import styled from '@emotion/styled'
import { css, Global } from '@emotion/core'
const styles = css`
body {
margin: 0;
}
a {
text-decoration: none;
color: red;
}
`
function App() {
return
}
export default App;
import React from 'react'
import styled from '@emotion/styled'
import { css, keyframes } from '@emotion/core'
const move = keyframes`
0% {
background: skyblue;
left: 0;
top: 0;
}
100% {
background: tomato;
left: 600px;
top: 300px;
}
`
const box = css`
width: 100px;
height: 100px;
position: absolute;
animation: ${move} 2s ease infinite alternate;
`
function App() {
return
app
}
export default App;
npm install emotion-theming
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import { ThemeProvider } from 'emotion-theming';
const theme = {
colors: {
primary: 'tomato'
}
};
ReactDOM.render(
<ThemeProvider theme={theme}><App /></ThemeProvider>,
document.getElementById('root')
);
App.js
import React from 'react';
import { css } from '@emotion/core';
import { useTheme } from 'emotion-theming';
const primaryColor = props => css`
color: ${props.colors.primary}
`
function App() {
console.log(useTheme());
return <div css={primaryColor}>App works</div>;
}
export default App;