Chakra UI (v2) - Removing Default Styles and Tokens
Posted on Nov 28, 2023 in Blog Posts
At the time of writing, @chakra-ui/react
's version is 2.8.0
.
Overview
This post explores how to remove CSS color variables and other tokens from Chakra UI's default theme configuration. I originally wrote this as an answer on Stack Overflow on November 28, 2023, and expanded information for this post.
Maybe you like having your dev tools free of extra CSS variables that you don't intend to use. This article explores how you can override the Chakra UI theme via manual merging.
Chakra UI Theme Customization Options
ChakraProvider
The ChakraProvider
provides theme tokens and component theming, typically including two or more variants for each component.
ChakraBaseProvider
The ChakraBaseProvider
provides the theme tokens (colors, spacing, etc.) like ChakraProvider
, but omits the default component theming.
If you intend to theme component styles yourself and want to cut down size, ChakraBaseProvider
is worth exploring.
Theme Extension
Chakra UI's extendTheme
utility merges your changes with the default Chakra UI theme so you can enjoy the styling Chakra UI has already done for you, while still allowing you to override pieces of it with your own customized styling.
What if you don't want to simply replace a value for extra Chakra UI tokens you're not using, but omit them all together?
Using Object Merging To Omit Chakra UI Theme Items
Before we look at merging away Chakra UI tokens and styles, it's helpful to understand which ones are used in Chakra UI components. If we remove tokens that other Chakra UI styles rely on, then we're going to have rendering oddities.
Required Colors
Let's use colors as an example. If we take a look at Chakra UI's color semantic tokens definition , we can see a variety of color schemes and some utility tokens like transparent
and current
.
After spending a lot of time with Chakra UI's codebase, I've noticed some patterns in component styling.
- Many components default to
gray
colorScheme. -
black
andwhite
are used often. - Many components use
blackAlpha
andwhiteAlpha
for hover styles -
current
andtransparent
are used often for color inheritance and to maintain border widths.
If we omit these tokens, it would be wise to first do a refactor to look for where these color tokens are used and change them to something else, or to ensure that they're included in our own theme override.
Customize Theme with Spread Syntax
One method to combine two objects while selectively omitting properties is to use spread syntax . Spread syntax allows expanding an existing object into a new object.
While the upside to spread syntax is flexibility, the downside is verbosity. For each layer where a modification is applied, the base theme needs to be spread into that layer for it to still be applied.
The example below creates a merged theme with Chakra UI's imported default theme and custom colors. Chakra UI colors are omitted with the exception of the few commonly used colors, which are added back into the colors theme.
What about Object.assign()
?
Object.assign()
isn't a great fit for deeply nested objects, because it has the same layering limitations as spread syntax. Spread syntax would still need to be used to merge back in nested properties.
Customize Theme with Merge Utilities
There are many deep merge utilities available on npm, like lodash , or TypeScript-friendly equivalents moderndash , radash , and remeda that can merge deeply nested objects.
How to use these utilities won't be covered in this post, but it is helpful to know tools like this are out there.