chakra ui custom theme doesn't work at all in my next js project

Viewed 1129

I'm trying to build nextjs project with chakra UI, but my custom theme doesn't working at all, I've tried everything in chakra docs, even custom color which I've added doesn't work, but default colors of the chakra works correctly, I wanna info that I've setup dark/light mode using chakra docs, if it has something to to with my problem

My theme.js file

import { extendTheme } from "@chakra-ui/react";
import { mode } from "@chakra-ui/theme-tools";

const styles = {
  global: (props) => ({
    body: {
      bg: mode("#a8dadc", "#006d77")(props),
      color: mode("#006d77", "#a8dadc")(props),
    },
  }),
};

const colors = {
  primary: "#e29578",
};

const theme = extendTheme({ styles, colors });
export default theme;

My index.tsx file

import {
  Box,
  Button,
  useColorMode,
  Text,
} from "@chakra-ui/react";
import type { NextPage } from "next";
import Head from "next/head";


const Home: NextPage = () => {
  const { colorMode, toggleColorMode } = useColorMode();

  return (
    <div>
      <Head>
        <title>Create Next App</title>
        <meta name="description" content="Generated by create next app" />
        <link rel="icon" href="/favicon.ico" />
      </Head>
      <h1>Home Page</h1>
      <Box mb={4}>This boxs style will change based on the color mode.</Box>
      <Button onClick={toggleColorMode} color="primary">
        Toggle {colorMode === "light" ? "Dark" : "Light"}
      </Button>
      <Text fontSize="6xl" color="primary">
        {" "}
        Custom color not working
      </Text>
    </div>
  );
};

export default Home;

My _app.tsx file

import type { AppProps } from "next/app";
import { ChakraProvider } from "@chakra-ui/react";

import { Chakra } from "../components/wrappers/Chakra";
import theme from "../libs/theme";

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <ChakraProvider theme={theme}>
      <Chakra cookies={pageProps.cookies}>
        <Component {...pageProps} />
      </Chakra>
    </ChakraProvider>
  );
}

export { getServerSideProps } from "../components/wrappers/Chakra";

export default MyApp;

1 Answers

I have the same issue!

I've noticed that there are some default css variables attached to :root an the ChakraProvider is not able to override them by default, so I've set the cssVarsRoot as a workarround.

  <ChakraProvider theme={theme} cssVarsRoot="body">

Related