본문으로 건너뛰기 Complete Chakra UI Complete Guide | React Component Library

Complete Chakra UI Complete Guide | React Component Library

Complete Chakra UI Complete Guide | React Component Library

이 글의 핵심

Complete guide to building beautiful UI with Chakra UI. Components, Theming, Dark Mode, accessibility, TypeScript with practical examples. Focused on Ch...

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

import { ChakraProvider } from '@chakra-ui/react';
export default function App({ Component, pageProps }) {
  return (
    <ChakraProvider>
      <Component {...pageProps} />
    </ChakraProvider>
  );
}

3. Basic Components

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

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

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

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

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

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.


Frequently Asked Questions (FAQ)

Q. When would I use this in practice?

A. Complete guide to building beautiful UI with Chakra UI.

Q. What should I read before this?

A. Follow the previous article or related articles links at the bottom of each post to learn in sequence.

Q. Where can I study this more deeply?

A. Check cppreference and the relevant library’s official documentation. The reference links at the end of the article are also worth using.


Other articles related to this topic.


Keywords Covered in This Article (Related Search Terms)

This article covers Chakra UI, React, UI Library, Theming, Accessibility, TypeScript, Frontend.