How to add a dark mode toggle? #2574
-
I haven't been able to find an example in the documentation for how to add a dark mode toggle. It seems like I need to either import light_theme.scss or dark_theme.scss, but conditionally importing stylesheets doesn't seem very straightforward. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 1 reply
-
Hi @tonyjmartinez. This question is sort of out of scope for the library itself and is more a general React question so I'm closing this. EUI outputs compiled CSS files for light and dark as part of the dist, so there's no need to touch the Sass if you don't need. There's a couple different ways you could handle toggling that on and off with React. The EUI docs itself have an example using a reducer https://github.com/elastic/eui/blob/master/src-docs/src/store/reducers/theme_reducer.js . Search eui for the theme*.js files and you'll see generally how it works The EUi gatsby starter has a simpler example using context https://github.com/elastic/gatsby-eui-starter/blob/master/src/components/chrome/chrome.tsx Alternatively you could use a dynamic import of the css in a default html file. In short, the way you decide to load your CSS is tightly coupled to your app's build system, but you'll want to set up some way to manage what theme the user is on, and then load the appropriate css file in your page. If you're using custom sass on top of EUI, you'd want to build your own css files (likely through some webpack process in your app) and import EUI from there. Some of that can be described here. https://github.com/elastic/eui/blob/master/wiki/consuming.md#importing-css-or-scss |
Beta Was this translation helpful? Give feedback.
-
@snide : Thanks for the explanation: I'm trying the context example now from EUI docs. Might be good to add more examples for this kind of stuff to the docs. Update: the docs use a npm module: css-loader that adds some extra webpack config. Doesn't seem easy for a default CRA created react app. |
Beta Was this translation helpful? Give feedback.
-
@acidjunk The gatsby-eui-starter has a working context driven example with |
Beta Was this translation helpful? Give feedback.
-
@snide: yes I know: but it relies on custom webpack config. I'm crafting a POC based on: https://stackoverflow.com/questions/46835825/conditional-css-in-create-react-app |
Beta Was this translation helpful? Give feedback.
-
For me it works. I created a folder themes/ with these 2 files: ThemeDark.tsx
ThemeLight.tsx
in components: ThemeSelector.tsx
Then I wrapped the ThemeSelector component around To switch the themes I used a function that looks like this:
Which can be called without any context/state. |
Beta Was this translation helpful? Give feedback.
-
I also have a way that works. But it's kinda ugly -> I rely on loading the minified CSS from the package dist: With other ways I would have a brief flash without CSS. |
Beta Was this translation helpful? Give feedback.
Hi @tonyjmartinez. This question is sort of out of scope for the library itself and is more a general React question so I'm closing this. EUI outputs compiled CSS files for light and dark as part of the dist, so there's no need to touch the Sass if you don't need. There's a couple different ways you could handle toggling that on and off with React.
The EUI docs itself have an example using a reducer https://github.com/elastic/eui/blob/master/src-docs/src/store/reducers/theme_reducer.js . Search eui for the theme*.js files and you'll see generally how it works
The EUi gatsby starter has a simpler example using context https://github.com/elastic/gatsby-eui-starter/blob/master/src/components…