scripter
scripter

Reputation: 1531

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?

Upvotes: 12

Views: 20167

Answers (3)

Schrax
Schrax

Reputation: 21

Just in case you want to override only the dark mode background without affecting the default value of the light mode you can retrieve the theme props like the following:

import type { StyleFunctionProps } from '@chakra-ui/styled-system';
import { mode } from '@chakra-ui/theme-tools';

const styles = {
  global: (props: StyleFunctionProps) => ({
    body: {
      // sets a custom bg color for dark mode only
      bg: mode(
        // light mode value retrieved from theme
        props.theme.semanticTokens.colors['chakra-body-bg']._light,
        // your custom value for dark mode
        '#252C32',
      )(props),
    },
  }),
};

Upvotes: 0

scripter
scripter

Reputation: 1531

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

Upvotes: 26

FluffyArt
FluffyArt

Reputation: 11

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();

Upvotes: 1

Related Questions