Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Composing Custom Components | Real World Examples
Styling React Apps with Chakra UI

bookComposing Custom Components

Swipe to show menu

Chakra UI encourages building reusable components by composing its primitive building blocks. By extending primitives such as Box, custom components can encapsulate both layout and styling while remaining fully compatible with Chakra UI's design system.

Custom components help reduce repetition and enforce visual consistency across an application. Because they are built on Chakra UI primitives, they continue to support responsive props, theme tokens, and interaction styling out of the box.

This approach makes it easier to maintain and scale complex interfaces by centralizing shared UI patterns into reusable components.

Step 1: Create the Reusable Component

// src/components/Card.jsx
import { Box } from "@chakra-ui/react"

export function Card({ children, ...props }) {
  return (
    <Box
      bg="white"
      borderWidth="1px"
      borderRadius="xl"
      p="6"
      boxShadow="sm"
      {...props}
    >
      {children}
    </Box>
  )
}

Step 2: Use It in the Landing Page

Before (inline styles everywhere):

<Box bg="white" borderWidth="1px" borderRadius="xl" p="6">
  ...
</Box>

After (clean, reusable):

import { Card } from "@/components/Card"

<Card>
  <Stack gap="2">
    <Heading size="sm">Responsive Props</Heading>
    <Text color="gray.700" fontSize="sm">
      Adapt layouts and typography using breakpoint-based values.
    </Text>
  </Stack>
</Card>
import { SimpleGrid, Stack, Heading, Text } from "@chakra-ui/react"
import { Card } from "@/components/Card"

function Features() {
  const items = [
    {
      title: "Responsive Props",
      text: "Adapt layout and spacing across screen sizes.",
    },
    {
      title: "Pseudo Props",
      text: "Style hover, focus, and active states directly.",
    },
    {
      title: "Conditional Styling",
      text: "Reflect UI state without CSS files.",
    },
  ]

  return (
    <SimpleGrid columns={{ base: 1, md: 3 }} gap="6">
      {items.map((item) => (
        <Card key={item.title}>
          <Stack gap="2">
            <Heading size="sm">{item.title}</Heading>
            <Text color="gray.700" fontSize="sm">
              {item.text}
            </Text>
          </Stack>
        </Card>
      ))}
    </SimpleGrid>
  )
}
question mark

When creating a custom component as described above, which Chakra UI primitive is commonly used as a flexible container

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Sectionย 4. Chapterย 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Sectionย 4. Chapterย 2
some-alt