React styled-components
安装
npm install --save styled-components
import styled, { css } from 'styled-components'
const Button = styled.button`
background: transparent;
border-radius: 3px;
border: 2px solid palevioletred;
color: palevioletred;
margin: 0.5em 1em;
padding: 0.25em 1em;
${props => props.primary && css`
background: palevioletred;
color: white;
`}
`;
const Container = styled.div`
text-align: center;
`
render(
<Container>
<Button>Normal Button</Button>
<Button primary>Primary Button</Button>
</Container>
);
常用属性
// import styled from 'styled-components'
const Button = styled.button`
background: palevioletred;
border-radius: 3px;
border: none;
color: white;
`
const TomatoButton = styled(Button)`
background: tomato;
`
render(
<>
<Button>I'm purple.</Button>
<br />
<TomatoButton>I'm red.</TomatoButton>
</>
)
模板语法传参
// import styled from 'styled-components'
const padding = '3em'
const Section = styled.section`
color: white;
/* Pass variables as inputs */
padding: ${padding};
/* Adjust the background from the properties */
background: ${props => props.background};
`
render(
<Section background="cornflowerblue">
✨ Magic
</Section>
)
&
const Thing = styled.div.attrs((/* props */) => ({ tabIndex: 0 }))`
color: blue;
&:hover {
color: red; // <Thing> when hovered
}
& ~ & {
background: tomato; // <Thing> as a sibling of <Thing>, but maybe not directly next to it
}
& + & {
background: lime; // <Thing> next to <Thing>
}
&.something {
background: orange; // <Thing> tagged with an additional CSS class ".something"
}
.something-else & {
border: 1px solid; // <Thing> inside another element labeled ".something-else"
}
`
render(
<React.Fragment>
<Thing>Hello world!</Thing>
<Thing>How ya doing?</Thing>
<Thing className="something">The sun is shining...</Thing>
<div>Pretty nice day today.</div>
<Thing>Don't you think?</Thing>
<div className="something-else">
<Thing>Splendid.</Thing>
</div>
</React.Fragment>
)
&&
- &&单独一个双&符号有一个特殊的行为称为“优先级提升”;如果你正在处理混合样式的组件和可能有冲突样式的CSS环境,这可能是有用的:
const Thing = styled.div`
&& {
color: blue;
}
`
const GlobalStyle = createGlobalStyle`
div${Thing} {
color: red;
}
`
render(
<React.Fragment>
<GlobalStyle />
<Thing>
I'm blue, da ba dee da ba daa
</Thing>
</React.Fragment>
)
- 如果你在没有&号的情况下放入选择器,它们将指向组件的子组件。
const Thing = styled.div`
color: blue;
.something {
border: 1px solid; // an element labeled ".something" inside <Thing>
display: block;
}
`
render(
<Thing>
<label htmlFor="foo-button" className="something">Mystery button</label>
<button id="foo-button">What do I do?</button>
</Thing>
)
动画
- 带有@keyframes的CSS动画不会局限于单个组件,但你仍然不希望它们是全局的,以避免名称冲突。这就是为什么我们导出一个关键帧助手,它将生成一个唯一的实例,你可以在整个应用程序中使用:
// Create the keyframes
const rotate = keyframes`
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
`;
// Here we create a component that will rotate everything we pass in over two seconds
const Rotate = styled.div`
display: inline-block;
animation: ${rotate} 2s linear infinite;
padding: 2rem 1rem;
font-size: 1.2rem;
`;
render(
<Rotate><
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)