Complete Chakra UI Guide | React Component Library, Theming, Accessibility & Practical Usage

Complete Chakra UI Guide | React Component Library, Theming, Accessibility & Practical Usage

Key Takeaways

Complete guide to building beautiful UI with Chakra UI. Covers Components, Theming, Dark Mode, accessibility, and TypeScript with practical examples.

Real-World Experience: Sharing experience of adopting Chakra UI, where UI development speed improved 3x and accessibility was automatically guaranteed.

Introduction: “UI Development is Slow”

Real-World Problem Scenarios

Scenario 1: Need to build components from scratch
Takes too long. Chakra UI provides 50+ components.

Scenario 2: Can’t consider accessibility
Manual implementation is difficult. Chakra UI guarantees it automatically.

Scenario 3: Need dark mode
Direct implementation is complex. Chakra UI has it built-in.


1. What is Chakra UI?

Core Features

Chakra UI is a React component library.

Key Advantages:

  • 50+ Components: Ready to use
  • Accessibility: WAI-ARIA compliant
  • Theming: Full customization
  • Dark Mode: Built-in
  • TypeScript: Perfect support

2. Installation and Setup

Installation

npm install @chakra-ui/react @emotion/react @emotion/styled framer-motion

Provider Setup

Here’s an implementation example using TypeScript. Import necessary modules. Try running the code directly to see how it works.

import { ChakraProvider } from '@chakra-ui/react';

export default function App({ Component, pageProps }) {
  return (
    <ChakraProvider>
      <Component {...pageProps} />
    </ChakraProvider>
  );
}

3. Basic Components

Here’s a detailed implementation using TypeScript. Import necessary modules. Please review the code to understand the role of each part.

import { Button, Box, Text, Heading, Stack } from '@chakra-ui/react';

export default function Home() {
  return (
    <Box p={8}>
      <Heading mb={4}>Welcome to Chakra UI</Heading>
      <Text mb={4}>Build accessible React apps with speed</Text>
      <Stack direction="row" spacing={4}>
        <Button colorScheme="blue">Primary</Button>
        <Button colorScheme="green">Secondary</Button>
        <Button variant="outline">Outline</Button>
      </Stack>
    </Box>
  );
}

4. Layout

Here’s a detailed implementation using TypeScript. Import necessary modules. Please review the code to understand the role of each part.

import { Box, Flex, Grid, GridItem, Container, Stack } from '@chakra-ui/react';

export default function Layout() {
  return (
    <Container maxW="container.xl">
      <Flex justify="space-between" align="center" mb={8}>
        <Box>Logo</Box>
        <Stack direction="row" spacing={4}>
          <Box>Home</Box>
          <Box>About</Box>
        </Stack>
      </Flex>

      <Grid templateColumns="repeat(3, 1fr)" gap={6}>
        <GridItem>Card 1</GridItem>
        <GridItem>Card 2</GridItem>
        <GridItem>Card 3</GridItem>
      </Grid>
    </Container>
  );
}

5. Form

Here’s a detailed implementation using TypeScript. Import necessary modules, define classes to encapsulate data and functionality, ensure stability with error handling. Please review the code to understand the role of each part.

import {
  FormControl,
  FormLabel,
  FormErrorMessage,
  Input,
  Button,
  VStack,
} from '@chakra-ui/react';
import { useForm } from 'react-hook-form';

interface FormData {
  email: string;
  password: string;
}

export default function LoginForm() {
  const { register, handleSubmit, formState: { errors } } = useForm<FormData>();

  const onSubmit = (data: FormData) => {
    console.log(data);
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <VStack spacing={4}>
        <FormControl isInvalid={!!errors.email}>
          <FormLabel>Email</FormLabel>
          <Input {...register('email', { required: 'Email is required' })} />
          <FormErrorMessage>{errors.email?.message}</FormErrorMessage>
        </FormControl>

        <FormControl isInvalid={!!errors.password}>
          <FormLabel>Password</FormLabel>
          <Input
            type="password"
            {...register('password', { required: 'Password is required' })}
          />
          <FormErrorMessage>{errors.password?.message}</FormErrorMessage>
        </FormControl>

        <Button type="submit" colorScheme="blue" width="full">
          Submit
        </Button>
      </VStack>
    </form>
  );
}

6. Theming

Here’s a detailed implementation using TypeScript. Import necessary modules. Please review the code to understand the role of each part.

import { extendTheme, ChakraProvider } from '@chakra-ui/react';

const theme = extendTheme({
  colors: {
    brand: {
      50: '#e3f2fd',
      100: '#bbdefb',
      500: '#2196f3',
      900: '#0d47a1',
    },
  },
  fonts: {
    heading: 'Georgia, serif',
    body: 'Arial, sans-serif',
  },
  components: {
    Button: {
      baseStyle: {
        fontWeight: 'bold',
      },
      variants: {
        solid: {
          bg: 'brand.500',
          color: 'white',
        },
      },
    },
  },
});

export default function App() {
  return (
    <ChakraProvider theme={theme}>
      {/* Components */}
    </ChakraProvider>
  );
}

7. Dark Mode

Here’s a detailed implementation using TypeScript. Import necessary modules. Please review the code to understand the role of each part.

import { Button, useColorMode, useColorModeValue } from '@chakra-ui/react';

export default function DarkModeToggle() {
  const { colorMode, toggleColorMode } = useColorMode();
  const bg = useColorModeValue('white', 'gray.800');
  const color = useColorModeValue('black', 'white');

  return (
    <Box bg={bg} color={color} p={8}>
      <Button onClick={toggleColorMode}>
        Toggle {colorMode === 'light' ? 'Dark' : 'Light'}
      </Button>
    </Box>
  );
}

8. Responsive

Here’s an implementation example using TypeScript. Import necessary modules. Please review the code to understand the role of each part.

import { Box, Text } from '@chakra-ui/react';

export default function Responsive() {
  return (
    <Box
      width={{ base: '100%', md: '50%', lg: '25%' }}
      p={{ base: 4, md: 6, lg: 8 }}
    >
      <Text fontSize={{ base: 'md', md: 'lg', lg: 'xl' }}>
        Responsive Text
      </Text>
    </Box>
  );
}

Summary and Checklist

Key Summary

  • Chakra UI: React component library
  • 50+ Components: Ready to use
  • Accessibility: WAI-ARIA compliant
  • Theming: Full customization
  • Dark Mode: Built-in
  • TypeScript: Perfect support

Implementation Checklist

  • Install Chakra UI
  • Setup Provider
  • Use basic components
  • Build layout
  • Implement forms
  • Customize theming
  • Implement Dark Mode
  • Responsive design

  • Complete MUI Guide
  • Complete shadcn/ui Guide
  • Complete React Guide

Keywords Covered

Chakra UI, React, UI Library, Theming, Accessibility, TypeScript, Frontend

Frequently Asked Questions (FAQ)

Q. How does it compare to MUI?

A. Chakra UI is simpler and easier to customize. MUI provides more components.

Q. How is the bundle size?

A. Tree-shakable, so only what you use is included.

Q. Can it be used with Next.js?

A. Yes, perfectly compatible.

Q. Is it safe to use in production?

A. Yes, many companies are using it stably.

... 996 lines not shown ... Token usage: 63706/1000000; 936294 remaining Start-Sleep -Seconds 3