Language: JavaScript
UI Framework
Chakra UI was created by Segun Adebayo in 2019 to improve the developer experience when building React applications. It emphasizes simplicity, accessibility (a11y), and a consistent design system. Chakra UI uses a style props API to make styling components intuitive and fast.
Chakra UI is a simple, modular, and accessible component library for React applications. It provides composable components with built-in theming, responsive styles, and accessibility support by default.
npm install @chakra-ui/react @emotion/react @emotion/styled framer-motionyarn add @chakra-ui/react @emotion/react @emotion/styled framer-motionChakra UI does not provide an official CDN build; use npm or yarn for installation.Install the package from npm and wrap your app with ChakraProvider to enable theming.Chakra UI provides accessible React components out-of-the-box, including buttons, forms, modals, navigation, and layout primitives. Styling is done using props like `colorScheme`, `variant`, and `size` instead of writing CSS.
import { Button } from '@chakra-ui/react';
<Button colorScheme="teal" size="md">Click Me</Button>Renders a button with a teal color scheme and medium size.
import { Stack, Button } from '@chakra-ui/react';
<Stack direction="row" spacing={4}>
<Button colorScheme="blue">Save</Button>
<Button colorScheme="gray">Cancel</Button>
</Stack>Uses a horizontal stack to space out buttons evenly.
import { extendTheme, ChakraProvider, Button } from '@chakra-ui/react';
const theme = extendTheme({
colors: { brand: { 500: '#1a365d' } }
});
function App() {
return (
<ChakraProvider theme={theme}>
<Button colorScheme="brand">Brand Button</Button>
</ChakraProvider>
);
}Extends the default Chakra theme with a custom color and applies it to a button.
import { Box } from '@chakra-ui/react';
<Box w={{ base: '100%', md: '50%' }} h="100px" bg="tomato" />Sets the box width to 100% on small screens and 50% on medium and larger screens using responsive props.
import { useDisclosure, Button, Modal, ModalOverlay, ModalContent, ModalHeader, ModalCloseButton, ModalBody } from '@chakra-ui/react';
function Example() {
const { isOpen, onOpen, onClose } = useDisclosure();
return (
<>
<Button onClick={onOpen}>Open Modal</Button>
<Modal isOpen={isOpen} onClose={onClose}>
<ModalOverlay />
<ModalContent>
<ModalHeader>Chakra Modal</ModalHeader>
<ModalCloseButton />
<ModalBody>Hello from Chakra!</ModalBody>
</ModalContent>
</Modal>
</>
);
}Demonstrates a modal component controlled by Chakra's useDisclosure hook.
Wrap your app in ChakraProvider to enable theme and context.
Use style props (`colorScheme`, `variant`, `size`, `spacing`) for quick styling instead of writing CSS.
Leverage responsive props to handle different screen sizes without media queries.
Use built-in hooks like `useDisclosure` for handling modal, drawer, and popover states.
Extend the theme for consistent branding instead of overriding styles individually.