使用 css 个后代选择器反应主题
React theming with css descendant selectors
这是一个片段反应代码,说明我如何将工具提示编码为具有浅色和深色主题。它定义工具提示的位置。我想通过使用主题来以不同的方式处理这个问题,但我不确定如何:
static defaultProps = {
theme: 'light',
eventsEnabled: true,
};
firstOrderPlacement(placement) {
if (!placement) return null;
return placement.split('-')[0];
}
arrowDirectionClass(firstOrderPlacement) {
const { theme } = this.props;
switch (firstOrderPlacement) {
case 'right':
return cx(css.arrowLeft, theme === 'dark' ? css.arrowLeftDark : css.arrowLeftLight);
case 'left':
return cx(css.arrowRight, theme === 'dark' ? css.arrowRightDark : css.arrowRightLight);
case 'top':
return cx(css.arrowDown, theme === 'dark' ? css.arrowDownDark : css.arrowDownLight);
case 'bottom':
return cx(css.arrowUp, theme === 'dark' ? css.arrowUpDark : css.arrowUpLight);
default:
return cx(css.arrowUp, theme === 'dark' ? css.arrowUpDark : css.arrowUpLight);
}
}
和 css 的位置:
.backgroundLight {
background: white;
color: #262626;
}
.backgroundDark {
background: #2d2d2d;
color: #ffffff;
}
.arrow {
position: relative;
width: 0;
height: 0;
}
.arrowRight {
border-top: 0.4375rem solid transparent;
border-bottom: 0.4375rem solid transparent;
}
.arrowRightLight {
border-left: 0.4375rem solid var(--color-white);
}
.arrowRightDark {
border-left: 0.4375rem solid #2d2d2d;
}
.arrowLeft {
border-top: 0.4375rem solid transparent;
border-bottom: 0.4375rem solid transparent;
}
.arrowLeftLight {
border-right: 0.4375rem solid var(--color-white);
}
.arrowLeftDark {
border-right: 0.4375rem solid #2d2d2d;
}
.arrowDown {
margin: 0 auto;
border-left: 0.4375rem solid transparent;
border-right: 0.4375rem solid transparent;
}
.arrowDownLight {
border-top: 0.4375rem solid var(--color-white);
}
.arrowDownDark {
border-top: 0.4375rem solid #2d2d2d;
}
.arrowUp {
margin: 0 auto;
border-left: 0.4375rem solid transparent;
border-right: 0.4375rem solid transparent;
}
.arrowUpLight {
border-bottom: 0.4375rem solid var(--color-white);
}
.arrowUpDark {
border-bottom: 0.4375rem solid #2d2d2d;
}
当然这可以通过主题化有效地完成,但我已经读过它我不确定如何去做。
您可以允许 CSS 使用 CSS variables
:
执行 heavy-lifting 以应用 主题
为 background
和 color
创建两个变量 - 比如说 --bg
和 --color
(请注意,您将定义所有属性此处更改主题),
定义一个 .wrapper.light
和一个 wrapper.dark
规则,根据 light 或 dark 定义这些变量例如主题,
现在主题将应用于 wrapper
使用:
.wrapper {
color: var(--color);
background-color: var(--bg);
}
参见下面的演示:
const App = ({theme}) => {
return (
<div className={'wrapper ' + theme}>
<h1>Hello {theme}!</h1>
<span className="arrow arrowLeft"/>
<span className="arrow arrowUp"/>
<span className="arrow arrowRight"/>
<span className="arrow arrowDown"/>
</div>
)
}
ReactDOM.render(<App theme="light"/>, document.getElementById('theme1'));
ReactDOM.render(<App theme="dark"/>, document.getElementById('theme2'));
.wrapper.light {
--bg: white;
--color: #262626;
}
.wrapper.dark {
--bg: #2d2d2d;
--color: #ffffff;
}
.wrapper {
color: var(--color);
background-color: var(--bg);
}
.arrow {
display: inline-block;
width: 0;
height: 0;
margin: 10px;
}
.arrowRight {
border-top: 0.4375rem solid transparent;
border-bottom: 0.4375rem solid transparent;
border-left: 0.4375rem solid var(--color);
}
.arrowLeft {
border-top: 0.4375rem solid transparent;
border-bottom: 0.4375rem solid transparent;
border-right: 0.4375rem solid var(--color);
}
.arrowDown {
border-left: 0.4375rem solid transparent;
border-right: 0.4375rem solid transparent;
border-top: 0.4375rem solid var(--color);
}
.arrowUp {
border-left: 0.4375rem solid transparent;
border-right: 0.4375rem solid transparent;
border-bottom: 0.4375rem solid var(--color);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="theme1"></div>
<div id="theme2"></div>
这是一个片段反应代码,说明我如何将工具提示编码为具有浅色和深色主题。它定义工具提示的位置。我想通过使用主题来以不同的方式处理这个问题,但我不确定如何:
static defaultProps = {
theme: 'light',
eventsEnabled: true,
};
firstOrderPlacement(placement) {
if (!placement) return null;
return placement.split('-')[0];
}
arrowDirectionClass(firstOrderPlacement) {
const { theme } = this.props;
switch (firstOrderPlacement) {
case 'right':
return cx(css.arrowLeft, theme === 'dark' ? css.arrowLeftDark : css.arrowLeftLight);
case 'left':
return cx(css.arrowRight, theme === 'dark' ? css.arrowRightDark : css.arrowRightLight);
case 'top':
return cx(css.arrowDown, theme === 'dark' ? css.arrowDownDark : css.arrowDownLight);
case 'bottom':
return cx(css.arrowUp, theme === 'dark' ? css.arrowUpDark : css.arrowUpLight);
default:
return cx(css.arrowUp, theme === 'dark' ? css.arrowUpDark : css.arrowUpLight);
}
}
和 css 的位置:
.backgroundLight {
background: white;
color: #262626;
}
.backgroundDark {
background: #2d2d2d;
color: #ffffff;
}
.arrow {
position: relative;
width: 0;
height: 0;
}
.arrowRight {
border-top: 0.4375rem solid transparent;
border-bottom: 0.4375rem solid transparent;
}
.arrowRightLight {
border-left: 0.4375rem solid var(--color-white);
}
.arrowRightDark {
border-left: 0.4375rem solid #2d2d2d;
}
.arrowLeft {
border-top: 0.4375rem solid transparent;
border-bottom: 0.4375rem solid transparent;
}
.arrowLeftLight {
border-right: 0.4375rem solid var(--color-white);
}
.arrowLeftDark {
border-right: 0.4375rem solid #2d2d2d;
}
.arrowDown {
margin: 0 auto;
border-left: 0.4375rem solid transparent;
border-right: 0.4375rem solid transparent;
}
.arrowDownLight {
border-top: 0.4375rem solid var(--color-white);
}
.arrowDownDark {
border-top: 0.4375rem solid #2d2d2d;
}
.arrowUp {
margin: 0 auto;
border-left: 0.4375rem solid transparent;
border-right: 0.4375rem solid transparent;
}
.arrowUpLight {
border-bottom: 0.4375rem solid var(--color-white);
}
.arrowUpDark {
border-bottom: 0.4375rem solid #2d2d2d;
}
当然这可以通过主题化有效地完成,但我已经读过它我不确定如何去做。
您可以允许 CSS 使用 CSS variables
:
为
background
和color
创建两个变量 - 比如说--bg
和--color
(请注意,您将定义所有属性此处更改主题),定义一个
.wrapper.light
和一个wrapper.dark
规则,根据 light 或 dark 定义这些变量例如主题,现在主题将应用于
wrapper
使用:.wrapper { color: var(--color); background-color: var(--bg); }
参见下面的演示:
const App = ({theme}) => {
return (
<div className={'wrapper ' + theme}>
<h1>Hello {theme}!</h1>
<span className="arrow arrowLeft"/>
<span className="arrow arrowUp"/>
<span className="arrow arrowRight"/>
<span className="arrow arrowDown"/>
</div>
)
}
ReactDOM.render(<App theme="light"/>, document.getElementById('theme1'));
ReactDOM.render(<App theme="dark"/>, document.getElementById('theme2'));
.wrapper.light {
--bg: white;
--color: #262626;
}
.wrapper.dark {
--bg: #2d2d2d;
--color: #ffffff;
}
.wrapper {
color: var(--color);
background-color: var(--bg);
}
.arrow {
display: inline-block;
width: 0;
height: 0;
margin: 10px;
}
.arrowRight {
border-top: 0.4375rem solid transparent;
border-bottom: 0.4375rem solid transparent;
border-left: 0.4375rem solid var(--color);
}
.arrowLeft {
border-top: 0.4375rem solid transparent;
border-bottom: 0.4375rem solid transparent;
border-right: 0.4375rem solid var(--color);
}
.arrowDown {
border-left: 0.4375rem solid transparent;
border-right: 0.4375rem solid transparent;
border-top: 0.4375rem solid var(--color);
}
.arrowUp {
border-left: 0.4375rem solid transparent;
border-right: 0.4375rem solid transparent;
border-bottom: 0.4375rem solid var(--color);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="theme1"></div>
<div id="theme2"></div>