reactjs - 样式化组件 - 具有嵌套类的 css 变量
问题描述
这是代码沙箱的链接 - https://codesandbox.io/s/misty-shadow-lr4tv?file=/src/App.js
知道为什么在模板文字中嵌套 div 不能正常工作吗?我的问题的概述是:
- 选项 1:使用
css
标签(从样式组件导入)存储css
嵌套类并在父级 css 中返回的 const 不起作用。然而,这是我做到这一点的理想方式。 - 选项 2:在父级 css 中存储和返回的 const
styled.div
工作 - 选项 3:一个 const 存储为基本模板文字,
css
前面没有 out 或使用父 css 之外的任何主题变量,并在父 css 作品中返回
以下是我尝试过/不工作的 3 个示例:
import styled, { css } from 'styled-components/macro'
export const ExampleDiv = styled.div``
/**
* Option 1
* Does not work
*/
const expandedStyles1 = css`
${ExampleDiv} {
background-color: ${({ theme }) => theme.background.primary};
}
`
export const Option1 = styled.div<ItemProps>`
${({ $isExpanded }) => {
return `${$isExpanded && expandedStyles1}`
}}
`
/**
* Option 2
* Does work
*/
export const Option2 = styled.div<ItemProps>`
${({ $isExpanded }) => {
const expandedStyles2 = `
${ExampleDiv} {
background-color: ${({ theme }) => theme.background.primary};
}
`
return `${$isExpanded && expandedStyles2}`
}}
`
/**
* Option 3
* Does work
*/
const expandedStyles3 = `
${ExampleDiv} {
background-color: gree;
}
`
export const Option3 = styled.div<ItemProps>`
${({ $isExpanded }) => {
return `${$isExpanded && expandedStyles3}`
}}
`
解决方案
推荐阅读
- django - 无法为包含自定义模型字段的 django 模型编写测试
- vuejs2 - VueJS 插件冲突
- java - Eclipse IDE 未启动
- dialogflow-es - 如何使用spring java给出快速回复(Dialogflow)?
- android - 如何在不同类的回收站视图中添加项目
- java - 将相机捕获图像显示到 gridview
- java - Java 中 AutoCommit 语句的最佳实践,用于通过 JDBC 使用 MySQL
- python - Kivy 选择焦点
- python - 如何在 python 中拆分一个列表,知道它的子列表?
- python-3.x - 如何访问 Python 3 中函数外部的函数内部定义的变量?