File size: 5,036 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
---
title: Styling External Libraries
description:
Learn how to style external libraries using Chakra UI's styling system
publishedAt: "2025-04-01"
collection: styling
---
In this guide, we'll learn how to style external libraries using Chakra UI's
styling system.
With the rise of headless component libraries like Ark UI, Radix UI, etc. it's
common to want to use them while styling with Chakra UI.
Let's assume we want to style the Carousel component from Ark UI (an headless
component library).
## Install Ark UI
Start by installing the React version of Ark UI:
```bash
npm install @ark-ui/react
```
## Understand the Component Anatomy
Ark UI breaks each headless component into smaller, functional parts. For
example, some parts of a
(carousel)[https://ark-ui.com/react/docs/components/carousel#anatomy] would be:
- `Root`: The root component of the carousel
- `Item`: The item component of the carousel
- `PrevTrigger`: The previous trigger component of the carousel
- `NextTrigger`: The next trigger component of the carousel
Each part is flexible and can be styled independently. Once you know what each
one does, it becomes easier to add custom styles to the parts.
```tsx
import { Carousel } from "@ark-ui/react/carousel"
const images = Array.from(
{ length: 5 },
(_, i) => `https://picsum.photos/seed/${i + 1}/500/300`,
)
export const Demo = () => {
return (
<Carousel.Root defaultPage={0} slideCount={images.length}>
<Carousel.Control>
<Carousel.PrevTrigger>Previous</Carousel.PrevTrigger>
<Carousel.NextTrigger>Next</Carousel.NextTrigger>
</Carousel.Control>
<Carousel.IndicatorGroup>
{images.map((_, index) => (
<Carousel.Indicator key={index} index={index} />
))}
</Carousel.IndicatorGroup>
<Carousel.ItemGroup>
{images.map((image, index) => (
<Carousel.Item key={index} index={index}>
<img src={image} alt={`Slide ${index}`} />
</Carousel.Item>
))}
</Carousel.ItemGroup>
</Carousel.Root>
)
}
```
## Styling the primitives
One way you can easily style headless primitives is to wrap within the
[`chakra` factory](https://www.chakra-ui.com/docs/styling/chakra-factory).
With the factory, you can apply a style object or recipe to the component.
```tsx title="components/ui/carousel.tsx"
import { Carousel as ArkCarousel } from "@ark-ui/react/carousel"
import { chakra } from "@chakra-ui/react"
const CarouselRoot = chakra(ArkCarousel.Root, {
base: {
display: "flex",
flexDirection: "column",
alignItems: "center",
},
variants: {},
})
const CarouselItem = chakra(ArkCarousel.Item, {
base: {
width: "100%",
height: "100%",
},
})
const CarouselIndicatorGroup = chakra(ArkCarousel.IndicatorGroup, {
base: {
display: "flex",
gap: 2,
},
})
const CarouselIndicator = chakra(ArkCarousel.Indicator, {
base: {
borderRadius: "full",
bg: "bg.subtle",
_selected: {
bg: "teal.solid",
},
},
variants: {
size: {
sm: {
width: 3,
height: 3,
},
md: {
width: 4,
height: 4,
},
},
},
})
const CarouselItemGroup = chakra(ArkCarousel.ItemGroup, {
base: {
display: "flex",
gap: 2,
},
})
export const Carousel = {
Root: CarouselRoot,
IndicatorGroup: CarouselIndicatorGroup,
Indicator: CarouselIndicator,
ItemGroup: CarouselItemGroup,
Item: CarouselItem,
NextTrigger: ArkCarousel.NextTrigger,
PrevTrigger: ArkCarousel.PrevTrigger,
}
```
## Using the styled components
Now that we have the styled components, we can use them in our app.
For the `PrevTrigger` and `NextTrigger` components, we can render them as
`IconButton` components from Chakra UI.
```tsx title="pages/index.tsx"
import { Carousel } from "@/components/ui/carousel"
import { IconButton } from "@chakra-ui/react"
import { LuArrowLeft, LuArrowRight } from "react-icons/lu"
const images = Array.from(
{ length: 5 },
(_, i) => `https://picsum.photos/seed/${i + 1}/500/300`,
)
export const Demo = () => {
return (
<Carousel.Root defaultPage={0} slideCount={images.length}>
<Carousel.Control>
<Carousel.PrevTrigger asChild>
<IconButton>
<LuArrowLeft />
</IconButton>
</Carousel.PrevTrigger>
<Carousel.NextTrigger asChild>
<IconButton>
<LuArrowRight />
</IconButton>
</Carousel.NextTrigger>
</Carousel.Control>
<Carousel.IndicatorGroup>
{images.map((_, index) => (
<Carousel.Indicator key={index} index={index} />
))}
</Carousel.IndicatorGroup>
<Carousel.ItemGroup>
{images.map((image, index) => (
<Carousel.Item key={index} index={index}>
<img src={image} alt={`Slide ${index}`} />
</Carousel.Item>
))}
</Carousel.ItemGroup>
</Carousel.Root>
)
}
```
And that's it! You've now styled a headless component library using Chakra UI.
|