Meh*_*d77 3 css reactjs styled-components
我有看起来像这样的旧 CSS:
.btn_1 {
color: #fff;
background: #004dda;
display: inline-block;
}
.btn_1:hover {
background-color: #FFC107;
color: #222 !important;
}
.btn_1.full-width {
display: block;
width: 100%;
}
Run Code Online (Sandbox Code Playgroud)
我想迁移到在 React 中使用样式组件(CSS-in-JS 样式)。
到目前为止,这是我的迁移:
.btn_1 {
color: #fff;
background: #004dda;
display: inline-block;
:hover {
background-color: #FFC107;
color: #222 !important;
}
}
Run Code Online (Sandbox Code Playgroud)
但是在样式化组件中是否有针对选择器的解决方案,.btn_1.full-width或者我应该为此创建另一个 CSS 块?
我认为做这样的事情(或更好)会很酷:
.btn_1 {
${this}.full-width {
display: block;
width: 100%;
}
}
Run Code Online (Sandbox Code Playgroud)
但我似乎无法在文档中找到解决方案。你知道这是怎么可能的,或者这甚至是一个好主意吗?
Mul*_*lli 10
对于复杂的选择器模式,与号 (&) 可用于指回主要组件。以下是其潜在用途的更多示例:
const Thing = styled.div.attrs({ 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"
}
`
Run Code Online (Sandbox Code Playgroud)