Chakra UI
Whenever we build a UI for an application, we must decide what to use for styling our components. In addition, we must also consider whether we want to make all components from scratch or use a component library with pre-made components.
The advantage of using a component library is that it gives us a productivity boost as we don’t have to implement components that have already been implemented, such as buttons, dialogs, and tabs. Also, some libraries come with great accessibility defaults out of the box, so we don’t have to think about it as much as we would if we built everything from scratch. These libraries can come with costs, such as difficult customizability or a significant impact on the final bundle size. On the other hand, they save us a lot of development time.
For our application, we will use Chakra UI, a component library built on top of a combination of emotion and styled-system, which will allow us to write CSS in JavaScript in a consistent way.
Chakra UI setup
We already have the Chakra UI library installed, and now we need to configure it.
To use Chakra UI, first, we need to configure its theme provider to enable styles for its components. Since all our providers and wrappers are defined in src/providers/app.tsx
, we can add ChakraProvider
there:
import { ChakraProvider, GlobalStyle, } from '@chakra-ui/react'; import { ReactNode } from 'react'; import { theme } from '@/config/theme'; type AppProviderProps = { children: ReactNode; }; export const AppProvider = ({ children, }: AppProviderProps) => { return ( <ChakraProvider theme={theme}> <GlobalStyle /> {children} </ChakraProvider> ); };
Here, we are wrapping the entire application with the provider to apply theming and styles to all Chakra UI components. We are also rendering the GlobalStyles
component, which will take any global styles from our theme and apply it to the application.
Chakra UI settings and components are very customizable and can be configured in a custom theme, which we can pass to the provider. It will override the default theme configuration. Let’s configure the theme in src/config/theme.ts
by adding the following:
import { extendTheme } from '@chakra-ui/react'; const colors = { primary: '#1a365d', primaryAccent: '#ffffff', }; const styles = { global: { 'html, body': { height: '100%', bg: 'gray.50', }, '#__next': { height: '100%', bg: 'gray.50', }, }, }; export const theme = extendTheme({ colors, styles });
We are defining some global styles that will be injected via the GlobalStyles
component, which we have already added in AppProvider
. We also define the theme colors we want to have available in the components. Then, we combine these configurations with the default theme values by using the extendTheme
utility, which will merge all configurations and give us the complete theme object.
It is useful to centralize theme configuration since it is easy to use and change if the branding of the application changes. For example, we can easily change the primary color value in one place and apply it to the entire application without any additional changes.