Reputation: 392
Here's a snippet react code of how I coded the tooltip to have light and dark theme. It defines the position of the tooltip. I want to approach this differently by using theming but I'm not sure how:
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);
}
}
and the css for its positions :
.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;
}
Surely this can done efficiently by theming but I've read about it I'm not sure how to go about this.
Upvotes: 1
Views: 326
Reputation: 42352
You can allow CSS to do the heavy-lifting for applying themes by using CSS variables
:
create two variables for background
and color
- say --bg
and --color
(note that you'd be defining all the properties that change in the theme here),
define a .wrapper.light
and a wrapper.dark
rule that defines these variables according to the light or dark theme for instance,
now the theme will be applied to the wrapper
using:
.wrapper {
color: var(--color);
background-color: var(--bg);
}
See demo below:
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>
Upvotes: 1