9

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?

4

2 回答 2

18

在最新版本的 chakra ui 中,我们可以通过这种方式覆盖颜色

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;

然后我们theme传入ChakraProvider

于 2020-12-02T07:46:45.490 回答
1

正如文档所说(它实际上有效),您还需要用另一个ColorModeProvider包装您的组件。

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

并在您的组件中使用名为useColorMode的钩子(如果您愿意,可以使用嵌套)来获取当前颜色模式并在它们之间切换。

const { colorMode, toggleColorMode } = useColorMode();
于 2020-11-08T18:56:45.100 回答