Skip to content

How to add a dark mode toggle? #2574

Closed Answered by tkajtoch
tonyjmartinez asked this question in Q&A
Discussion options

You must be logged in to vote

The modern and officially supported solution is to update the colorMode prop of <EuiProvider>. You can toggle it between light and dark with a simple useState.

A clean way to solve this would be to create an app or theme provider and include the logic there, like this:

import { createContext, useState, type PropsWithChildren } from 'react';
import { type EuiThemeColorMode } from '@elastic/eui';

type MyAppContextType = {
  setColorMode(colorMode: EuiThemeColorMode): void;
};

// Use this context to control the active color mode from within your application
export const MyAppContext = createContext<MyAppContextType>(null);

export const MyAppProvider = ({ children }: PropsWithChildren) => {

Replies: 8 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@AlessandroStaffolani
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by tkajtoch
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants
Converted from issue

This discussion was converted from issue #2574 on November 11, 2020 17:48.