File size: 4,479 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 198 199 200 201 202 203 204 205 206 207 208 |
---
title: Refactoring Snippets
description:
We're refactoring the snippets to make Chakra UI more consistent and easier to
use
type: article
authors: ["sage"]
publishedAt: "2025-03-13"
---
After the launch of Chakra UI v3, a common feedback we received was around
snippets. Users felt that snippets were required to make components work, unlike
in v2.
This wasn't our intention - snippets were designed to be helpful pre-made
component compositions, not mandatory boilerplate. We want to clarify that
snippets are entirely optional and simply provide convenient starting points.
For this reason, we've reworked the documentation to always show the complete
code that imports directly from `@chakra-ui/react`.
:::info
**TLDR:** Migrate to the most recent version of Chakra UI to get the best
experience.
:::
This way, you can copy and paste the code without having to run any commands.
## Replacing Snippets
Here's a quick overview of the changes:
### Accordion
Before:
```tsx
import {
AccordionItem,
AccordionItemContent,
AccordionItemTrigger,
AccordionRoot,
} from "@/components/ui/accordion"
const Demo = () => {
return (
<AccordionRoot>
<AccordionItem>
<AccordionItemTrigger>Item</AccordionItemTrigger>
<AccordionItemContent>Content</AccordionItemContent>
</AccordionItem>
</AccordionRoot>
)
}
```
After:
```tsx
import { Accordion } from "@chakra-ui/react"
const Demo = () => {
return (
<Accordion.Root>
<Accordion.Item>
<Accordion.ItemTrigger>
Item
<Accordion.ItemIndicator />
</Accordion.ItemTrigger>
<Accordion.ItemContent>
<Accordion.ItemBody>Content</Accordion.ItemBody>
</Accordion.ItemContent>
</Accordion.Item>
</Accordion.Root>
)
}
```
### Menu
Before:
```tsx
import {
MenuContent,
MenuItem,
MenuRoot,
MenuTrigger,
} from "@/components/ui/menu"
const Demo = () => {
return (
<MenuRoot>
<MenuTrigger asChild>
<Button>Open</Button>
</MenuTrigger>
<MenuContent>
<MenuItem value="..." />
</MenuContent>
</MenuRoot>
)
}
```
After:
```tsx
import { Menu, Portal } from "@chakra-ui/react"
const Demo = () => {
return (
<Menu.Root>
<Menu.Trigger asChild>
<Button>Open</Button>
</Menu.Trigger>
<Portal>
<Menu.Positioner>
<Menu.Content>
<Menu.Item value="..." />
</Menu.Content>
</Menu.Positioner>
</Portal>
</Menu.Root>
)
}
```
## Shortcuts
To make your code more concise and easier to write, we've introduced shortcut
components for common use cases.
We achieved that by setting the default `children` prop, or creating sugar
components that compose the existing components.
:::info
Shortcuts are optional but help make your code more concise and readable
:::
### Checkbox
Here's a checkbox example.
This works:
```tsx
<Checkbox.Control>
<Checkbox.Indicator />
</Checkbox.Control>
```
but can be simplified if you don't need to customize the checkbox icons:
```tsx
<Checkbox.Control />
```
### Rating
The same goes for the `Rating` component.
This works:
```tsx
<RatingGroup.Control>
{Array.from({ length: 5 }).map((_, index) => (
<RatingGroup.Item key={index} index={index + 1}>
<RatingGroup.ItemIndicator />
</RatingGroup.Item>
))}
</RatingGroup.Control>
```
but can be simplified if you don't need to customize the rating icons:
```tsx
<RatingGroup.Control />
```
## Exceptions
The following components are exceptions to the rule and will continue to use
snippets:
- `Provider`: The provider is the root component that you need to wrap your app
- `Toaster`: The toaster is a component that is used to display notifications
- `PasswordInput`: The password input is used to collect passwords
- `Tooltip`: The tooltip is a component that is used to display a tooltip
- `Toggletip`: The toggletip looks like a tooltip, but works like a popover
These components will remain as snippets due to the ease of use they provide.
## What's Next?
Now that we've refactored the snippets, we're moving on to next phase of Chakra.
- Bump `@ark-ui/react` to v5 to boost render performance
- Implement `Combobox` and `CommandMenu` components
- Build and launch `@chakra-ui/charts` for data visualization
If you have any feedback, please
[open an discussion](https://github.com/chakra-ui/chakra-ui/discussions/new) and
let us know.
|