Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chakra UI colorScheme prop on button

i extended the theme provider in chakra_ui

import React from "react";
import ReactDOM from "react-dom";
import { ChakraProvider, extendTheme } from "@chakra-ui/react";

import App from "./App";

const theme = extendTheme({
  colors: {
    brand: {
      50: "#44337A",
      100: "#B794F4"
    }
  }
});

const rootElement = document.getElementById("root");
ReactDOM.render(
  <React.StrictMode>
    <ChakraProvider theme={theme}>
      <App />
    </ChakraProvider>
  </React.StrictMode>,
  rootElement
);

I used the Button Component like and set the colorScheme prop to the value my theme has:

 <Button size="sm" colorScheme="brand.100">
    Click Me
  </Button>

it produced the following in css: background: brand.50.500;. so it does not apply the color, any problem??

i noticed something, without the .number: e.g .50 or .100... the brand does not work, but other inbuilt colors work.

https://codesandbox.io/s/infallible-allen-1k0tx?file=/src/App.js:257-333

like image 455
AbdulAzeez Olanrewaju Avatar asked Nov 22 '20 07:11

AbdulAzeez Olanrewaju


People also ask

How do you increase the size of the chakra UI button?

Button SizesUse the size prop to change the size of the button. You can set the value to xs , sm , md , or lg .

What is the use of theming in Chakra UI?

Theming in Chakra UI follows the Styled System Theme Specification approach. We use a theme object to define our application's color palette, font, breakpoints, spacing, and more. To extend or override a token in the default theme, we import the extendTheme function and add the keys we want to override.

How do I manage color mode in Chakra UI?

Chakra UI comes with built-in support for managing color mode in your apps. By default, most of Chakra's components are dark mode compatible. In some scenario, you might need to make your component respond to color mode. Tip: Chakra stores the color mode in localStorage and appends a className to the body to ensure the color mode is persistent.

How does the colorscheme work in Chakra?

The colorScheme just accepts the color name. In your case, it will be colorScheme="brand". If we inspect the way chakra works to generate the colorScheme for a solid button, we can notice that it calls $ {c}.500.

Where does Chakra store the color mode?

Tip: Chakra stores the color mode in localStorage and appends a className to the body to ensure the color mode is persistent. Update your theme config to determine how Chakra should manage color mode updates.

How to change the color of an iconbutton in react?

IconButton composes the Button component except that it renders only an icon. Since IconButton only renders an icon, you must pass the aria-label prop, so screen readers can give meaning to the button. The IconButton component accepts most of the props from the Button component, so we can use colorScheme prop to change the color of the button.


1 Answers

The colorScheme just accepts the color name. In your case, it will be colorScheme="brand".

If we inspect the way chakra works to generate the colorScheme for a solid button, we can notice that it calls ${c}.500. It means that when you create your brand color scheme, you need to specify one color for 500 and you're missing that in your code example.

import React from "react";
import ReactDOM from "react-dom";
import { ChakraProvider, extendTheme } from "@chakra-ui/react";

import App from "./App";

const theme = extendTheme({
  colors: {
    brand: {
      50: "#44337A",
      100: "#B794F4",
      500: "#B794F4", // you need this
    }
  }
});

const rootElement = document.getElementById("root");
ReactDOM.render(
  <React.StrictMode>
    <ChakraProvider theme={theme}>
      <App />
    </ChakraProvider>
  </React.StrictMode>,
  rootElement
);

And when you're calling the button you just need to:

<Button size="sm" colorScheme="brand">
  Click Me
</Button
like image 170
Miuki Miu Avatar answered Oct 13 '22 06:10

Miuki Miu