Chakra UI colorScheme prop on button
Asked Answered
D

2

9

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

Dripdry answered 22/11, 2020 at 7:9 Comment(0)
G
25

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
Gennie answered 12/12, 2020 at 19:58 Comment(1)
Yeah, thanks that works, what about the box-shadow how does chakra render that, and what key needs to be set for that between 50 - 900Dripdry
F
1

I believe the new and propper way to add custom styles for ChakraUI is with the styleconfig for component styles.

For example:

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

const Button = defineStyleConfig({
  // The styles all button have in common
  baseStyle: {
    fontWeight: 'bold',
    textTransform: 'uppercase',
    borderRadius: 'base', // <-- border radius is same for all variants and sizes
  },
  // Two sizes: sm and md
  sizes: {
    sm: {
      fontSize: 'sm',
      px: 4, // <-- px is short for paddingLeft and paddingRight
      py: 3, // <-- py is short for paddingTop and paddingBottom
    },
    md: {
      fontSize: 'md',
      px: 6, // <-- these values are tokens from the design system
      py: 4, // <-- these values are tokens from the design system
    },
  },
  // Two variants: outline and solid
  variants: {
    outline: {
      border: '2px solid',
      borderColor: 'purple.500',
      color: 'purple.500',
    },
    solid: {
      bg: 'purple.500',
      color: 'white',
    },
  },
  // The default size and variant values
  defaultProps: {
    size: 'md',
    variant: 'outline',
  },
})

Read more here: https://chakra-ui.com/docs/styled-system/component-style

Fetch answered 18/7, 2023 at 6:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.