简体   繁体   English

你可以使用 Chakra-UI 的 colorMode 将背景颜色更改为任何颜色,还是仅适用于明暗模式?

[英]Can you use Chakra-UI's colorMode to change the background color to any color or is it only for light and dark mode?

This is my theme file.这是我的主题文件。 For now it only toggles between light and dark mode.目前它只在明暗模式之间切换。 I was wondering if I can pass in different colors or do I need to create a hook myself to do that?我想知道我是否可以传入不同的 colors 还是我需要自己创建一个挂钩来做到这一点?

import { extendTheme } from '@chakra-ui/react'

// 2. Add your color mode config
const config = {
  initialColorMode: '#C53030',
  useSystemColorMode: true,
}

// 3. extend the theme
const theme = extendTheme({ config })

export default theme

If you intend to customise the default theme object to match your design requirements, you need to extend the theme.如果您打算自定义默认主题对象以匹配您的设计要求,则需要扩展主题。

Chakra UI provides an extendTheme function that deep merges the default theme with your customizations. Chakra UI 提供了一个 extendTheme 功能,可以将默认主题与您的自定义深度合并。

// pages/_app.js
import { ChakraProvider } from '@chakra-ui/react'

// 1. Import the extendTheme function
import { extendTheme } from '@chakra-ui/react'

// 2. Extend the theme to include custom colors, fonts, etc
const colors = {
brand: {
900: '#1a365d',
800: '#153e75',
700: '#2a69ac',
  },
}

const theme = extendTheme({ colors })

// 3. Pass the `theme` prop to the `ChakraProvider`
function MyApp({ Component, pageProps }) {
  return (
    <ChakraProvider theme={theme}>
      <Component {...pageProps} />
    </ChakraProvider>
  ) 
}

export default MyApp;

that comes from the get-started page of chakra and I hope it answers "how to change the background color to any color"来自脉轮的入门页面,我希望它回答“如何将背景颜色更改为任何颜色”

You can set the background and text colour using the semantic tokens: "chakra-body-text" and "chakra-body-bg", with their respective _light and _dark keys.您可以使用语义标记设置背景和文本颜色:“chakra-body-text”和“chakra-body-bg”,以及它们各自的_light_dark键。

You can see this in action (and toggle the theme) here: https://codesandbox.io/s/chakra-ui-theme-bg-ty5qt0您可以在此处查看实际效果(并切换主题): https://codesandbox.io/s/chakra-ui-theme-bg-ty5qt0

I couldn't find this specific issue documented, but here are related links:我找不到这个特定问题的记录,但这里有相关链接:

import * as React from "react";
import * as ReactDOMClient from "react-dom/client";
import { ChakraProvider, extendTheme } from "@chakra-ui/react";
import App from "./App";

const theme = extendTheme({
  semanticTokens: {
    colors: {
      "chakra-body-text": {
        _light: "purple.800",
        _dark: "pink.100",
      },
      "chakra-body-bg": {
        _light: "pink.100",
        _dark: "purple.800",
      },
    },
  },
});

const rootElement = document.getElementById("root");
const root = ReactDOMClient.createRoot(rootElement);

root.render(
  <ChakraProvider theme={theme}>
    <App />
  </ChakraProvider>
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM