Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing the dark mode color in chakra ui

I am using the "dark mode" feature provided by the Chakra UI library. However, I can't figure out how to change the "dark mode" colors. In the documentation, I see that Chakra UI is based on something called "styled-system", so I tried to pass a new theme to themeProvider like this:

const theme = {
  ...defaultTheme,
  modes: {
    dark: {
      background: '#000',
    },
  },
};
 <ThemeProvider theme={theme}></ThemeProvider>

However, that didn't work. I also tried to wrap the modes object with a colors object, but that didn't work either. How can I customize the "dark mode" colors?

like image 618
SalahEddineBeriani Avatar asked Oct 27 '20 16:10

SalahEddineBeriani


People also ask

How do colors change in dark mode?

Dark mode is a setting that shifts an interface's color palette to display content in high contrast using dark background colors and light foreground. Black text becomes white, and white backgrounds go dark.

How do I change the color of my chakra UI buttons?

Button ColorsUse the colorScheme prop to change the color scheme of the Button.


2 Answers

in the latest version of chakra ui we can overwrite colors this way

import { mode } from '@chakra-ui/theme-tools';
import { extendTheme } from '@chakra-ui/core';

const styles = {
  global: props => ({
    body: {
      color: mode('gray.800', 'whiteAlpha.900')(props),
      bg: mode('gray.100', '#141214')(props),
    },
  }),
};

const components = {
  Drawer: {
    // setup light/dark mode component defaults
    baseStyle: props => ({
      dialog: {
        bg: mode('white', '#141214')(props),
      },
    }),
  },
};

const theme = extendTheme({
  components,
  styles,
});

export default theme;

then we pass the theme into ChakraProvider

like image 88
SalahEddineBeriani Avatar answered Oct 18 '22 06:10

SalahEddineBeriani


As doc say (and it actually works), you also need to wrap your component with another ColorModeProvider.

 <ThemeProvider theme={customTheme}>
      <ColorModeProvider><YourCompoent/></ColorModeProvider>
 </ThemeProvider>

And use the hook called useColorMode inside your component (or nested if you wish) to get current color mode and toggle between.

const { colorMode, toggleColorMode } = useColorMode();
like image 28
FluffyArt Avatar answered Oct 18 '22 08:10

FluffyArt