Documentation for all components in Chakra UI v3. # Accordion ```tsx import { Accordion, Span } from "@chakra-ui/react" export const AccordionBasic = () => { return ( {items.map((item, index) => ( {item.title} {item.text} ))} ) } const items = [ { value: "a", title: "First Item", text: "Some value 1..." }, { value: "b", title: "Second Item", text: "Some value 2..." }, { value: "c", title: "Third Item", text: "Some value 3..." }, ] ``` ## Usage ```tsx import { Accordion } from "@chakra-ui/react" ``` ```tsx ``` ## Examples ### Controlled Set the accordion that shows up by default. ```tsx "use client" import { Accordion, Span, Stack, Text } from "@chakra-ui/react" import { useState } from "react" export const AccordionControlled = () => { const [value, setValue] = useState(["second-item"]) return ( Expanded: {value.join(", ")} setValue(e.value)}> {items.map((item, index) => ( {item.title} {item.text} ))} ) } const items = [ { value: "first-item", title: "First Item", text: "Some value 1..." }, { value: "second-item", title: "Second Item", text: "Some value 2..." }, { value: "third-item", title: "Third Item", text: "Some value 3..." }, ] ``` ### With Icon Here's an example of rendering a custom icon in each accordion item. ```tsx import { Accordion, Heading, Icon, Stack } from "@chakra-ui/react" import { LuChartBarStacked, LuTags } from "react-icons/lu" export const AccordionWithIcon = () => { return ( Product details {items.map((item) => ( {item.icon} {item.title} {item.content} ))} ) } const items = [ { value: "info", icon: , title: "Product Info", content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur nec odio vel dui euismod fermentum.", }, { value: "stats", icon: , title: "Stats", content: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur nec odio vel dui euismod fermentum.", }, ] ``` ### Expand Multiple Items Use the `multiple` prop to allow multiple items to be expanded at once. ```tsx import { Accordion, Span } from "@chakra-ui/react" export const AccordionWithMultiple = () => { return ( {items.map((item, index) => ( {item.title} {item.text} ))} ) } const items = [ { value: "a", title: "First Item", text: "Some value 1..." }, { value: "b", title: "Second Item", text: "Some value 2..." }, { value: "c", title: "Third Item", text: "Some value 3..." }, ] ``` ### Sizes Use the `size` prop to change the size of the accordion. ```tsx import { Accordion, For, Span, Stack, Text } from "@chakra-ui/react" export const AccordionSizes = () => { return ( {(size) => ( {size} {items.map((item, index) => ( {item.title} {item.text} ))} )} ) } const items = [ { value: "a", title: "First Item", text: "Some value 1..." }, { value: "b", title: "Second Item", text: "Some value 2..." }, { value: "c", title: "Third Item", text: "Some value 3..." }, ] ``` ### Variants Use the `variant` prop to change the visual style of the accordion. Values can be either `outline`, `subtle`, `enclosed` or `plain`. ```tsx import { Accordion, For, Span, Stack, Text } from "@chakra-ui/react" export const AccordionVariants = () => { return ( {(variant) => ( {variant} {items.map((item, index) => ( {item.title} {item.text} ))} )} ) } const items = [ { value: "a", title: "First Item", text: "Some value 1..." }, { value: "b", title: "Second Item", text: "Some value 2..." }, { value: "c", title: "Third Item", text: "Some value 3..." }, ] ``` ### Disabled Item Pass the `disabled` prop to any `Accordion.Item` to prevent it from being expanded or collapsed. ```tsx import { Accordion, Span } from "@chakra-ui/react" export const AccordionWithDisabledItem = () => { return ( {items.map((item, index) => ( {item.title} {item.text} ))} ) } const items = [ { value: "a", title: "First Item", text: "Some value 1..." }, { value: "b", title: "Second Item", text: "Some value 2..." }, { value: "c", title: "Third Item", text: "Some value 3...", disabled: true }, ] ``` ### With Avatar Here's an example of composing an accordion with an avatar. ```tsx import { Accordion, Avatar, Badge, HStack } from "@chakra-ui/react" import { LuTrophy } from "react-icons/lu" import { LoremIpsum } from "react-lorem-ipsum" export const AccordionWithAvatar = () => { return ( {items.map((item, index) => ( {item.name}{" "} {item.topRated && ( Top Rated )} {item.bio} ))} ) } const items = [ { name: "Alex", bio: , image: "https://i.pravatar.cc/150?u=a", topRated: false, }, { name: "Benji", bio: , image: "https://i.pravatar.cc/150?u=b", topRated: true, }, { name: "Charlie", bio: , image: "https://i.pravatar.cc/150?u=c", topRated: false, }, ] ``` ### With Subtext Here's an example of adding a subtext to an accordion item. ```tsx import { Accordion, Stack, Text } from "@chakra-ui/react" import { LoremIpsum } from "react-lorem-ipsum" const items = [ { value: "a", title: "First Item", text: }, { value: "b", title: "Second Item", text: }, { value: "c", title: "Third Item", text: }, ] export const AccordionWithSubtext = () => { return ( {items.map((item, index) => ( {item.title} Click to expand {item.text} ))} ) } ``` ### With Actions Here's an example of adding actions to an accordion item trigger. ```tsx import { AbsoluteCenter, Accordion, Box, Button, Span } from "@chakra-ui/react" import LoremIpsum from "react-lorem-ipsum" export const AccordionWithActions = () => { return ( {items.map((item, index) => ( {item.title} {item.text} ))} ) } const items = [ { value: "a", title: "First Item", text: }, { value: "b", title: "Second Item", text: }, { value: "c", title: "Third Item", text: }, ] ``` ## Props ### Root | Prop | Default | Type | Description | | --- | --- | --- | --- | | collapsible | false | `boolean` | Whether an accordion item can be closed after it has been expanded. | | lazyMount | false | `boolean` | Whether to enable lazy mounting | | multiple | false | `boolean` | Whether multiple accordion items can be expanded at the same time. | | orientation | "vertical" | `'horizontal' \| 'vertical'` | The orientation of the accordion items. | | unmountOnExit | false | `boolean` | Whether to unmount on exit. | | colorPalette | gray | `'gray' \| 'red' \| 'orange' \| 'yellow' \| 'green' \| 'teal' \| 'blue' \| 'cyan' \| 'purple' \| 'pink'` | The color palette of the component | | variant | outline | `'outline' \| 'subtle' \| 'enclosed' \| 'plain'` | The variant of the component | | size | md | `'sm' \| 'md' \| 'lg'` | The size of the component | | asChild | undefined | `boolean` | Use the provided child element as the default rendered element, combining their props and behavior. | | defaultValue | undefined | `string[]` | The initial value of the accordion when it is first rendered. Use when you do not need to control the state of the accordion. | | disabled | undefined | `boolean` | Whether the accordion items are disabled | | id | undefined | `string` | The unique identifier of the machine. | | ids | undefined | `Partial<{\n root: string\n item(value: string): string\n itemContent(value: string): string\n itemTrigger(value: string): string\n}>` | The ids of the elements in the accordion. Useful for composition. | | onFocusChange | undefined | `(details: FocusChangeDetails) => void` | The callback fired when the focused accordion item changes. | | onValueChange | undefined | `(details: ValueChangeDetails) => void` | The callback fired when the state of expanded/collapsed accordion items changes. | | value | undefined | `string[]` | The `value` of the accordion items that are currently being expanded. | ### Item | Prop | Default | Type | Description | | --- | --- | --- | --- | | value | undefined | `string` | The value of the accordion item. | | asChild | undefined | `boolean` | Use the provided child element as the default rendered element, combining their props and behavior. | | disabled | undefined | `boolean` | Whether the accordion item is disabled. | # Action Bar ```tsx "use client" import { ActionBar, Button, Checkbox, Portal } from "@chakra-ui/react" import { useState } from "react" import { LuShare, LuTrash2 } from "react-icons/lu" export const ActionBarBasic = () => { const [checked, setChecked] = useState(false) return ( <> setChecked(!!e.checked)}> Show Action bar 2 selected ) } ``` ## Usage The action bar is designed to be controlled by table or checkbox selections. It provides a set of actions that can be performed on the selected items. ```tsx import { ActionBar } from "@chakra-ui/react" ``` ```tsx ``` ## Examples ### Close Trigger Render the `ActionBar.CloseTrigger` to close the action bar, and pass the `onOpenChange` handler to control the visibility of the action bar. > The `open` and `onOpenChange` props control the visibility of the action bar. ```tsx "use client" import { ActionBar, Button, Checkbox, CloseButton, Portal, } from "@chakra-ui/react" import { useState } from "react" import { LuShare, LuTrash2 } from "react-icons/lu" export const ActionBarWithCloseTrigger = () => { const [checked, setChecked] = useState(false) return ( <> setChecked(!!e.checked)} > Show Action bar setChecked(e.open)} closeOnInteractOutside={false} > 2 selected ) } ``` ### Within Dialog Here's an example of composing the `ActionBar` and the `Dialog` to perform a delete action on a set of selected items. > Press the `Delete projects` button to open the dialog. ```tsx "use client" import { ActionBar, Button, Checkbox, Dialog, Portal } from "@chakra-ui/react" import { useState } from "react" import { LuSquarePlus, LuTrash2 } from "react-icons/lu" export const ActionBarWithDialog = () => { const [checked, setChecked] = useState(false) return ( <> setChecked(!!e.checked)}> Check to select projects 4 selected Delete projects Are you sure you want to delete 4 projects? ) } ``` ## Props ### Root | Prop | Default | Type | Description | | --- | --- | --- | --- | | autoFocus | true | `boolean` | Whether to automatically set focus on the first focusable content within the popover when opened. | | closeOnEscape | true | `boolean` | Whether to close the popover when the escape key is pressed. | | closeOnInteractOutside | true | `boolean` | Whether to close the popover when the user clicks outside of the popover. | | lazyMount | false | `boolean` | Whether to enable lazy mounting | | modal | false | `boolean` | Whether the popover should be modal. When set to `true`: - interaction with outside elements will be disabled - only popover content will be visible to screen readers - scrolling is blocked - focus is trapped within the popover | | portalled | true | `boolean` | Whether the popover is portalled. This will proxy the tabbing behavior regardless of the DOM position of the popover content. | | unmountOnExit | false | `boolean` | Whether to unmount on exit. | | colorPalette | gray | `'gray' \| 'red' \| 'orange' \| 'yellow' \| 'green' \| 'teal' \| 'blue' \| 'cyan' \| 'purple' \| 'pink'` | The color palette of the component | | size | md | `'xs' \| 'sm' \| 'md' \| 'lg'` | The size of the component | | defaultOpen | undefined | `boolean` | The initial open state of the popover when it is first rendered. Use when you do not need to control its open state. | | id | undefined | `string` | The unique identifier of the machine. | | ids | undefined | `Partial<{\n anchor: string\n trigger: string\n content: string\n title: string\n description: string\n closeTrigger: string\n positioner: string\n arrow: string\n}>` | The ids of the elements in the popover. Useful for composition. | | immediate | undefined | `boolean` | Whether to synchronize the present change immediately or defer it to the next frame | | initialFocusEl | undefined | `() => HTMLElement \| null` | The element to focus on when the popover is opened. | | onEscapeKeyDown | undefined | `(event: KeyboardEvent) => void` | Function called when the escape key is pressed | | onExitComplete | undefined | `() => void` | Function called when the animation ends in the closed state | | onFocusOutside | undefined | `(event: FocusOutsideEvent) => void` | Function called when the focus is moved outside the component | | onInteractOutside | undefined | `(event: InteractOutsideEvent) => void` | Function called when an interaction happens outside the component | | onOpenChange | undefined | `(details: OpenChangeDetails) => void` | Function invoked when the popover opens or closes | | onPointerDownOutside | undefined | `(event: PointerDownOutsideEvent) => void` | Function called when the pointer is pressed down outside the component | | open | undefined | `boolean` | Whether the popover is open | | persistentElements | undefined | `(() => Element \| null)[]` | Returns the persistent elements that: - should not have pointer-events disabled - should not trigger the dismiss event | | positioning | undefined | `PositioningOptions` | The user provided options used to position the popover content | | present | undefined | `boolean` | Whether the node is present (controlled by the user) | | as | undefined | `React.ElementType` | The underlying element to render. | | asChild | undefined | `boolean` | Use the provided child element as the default rendered element, combining their props and behavior. | | unstyled | undefined | `boolean` | Whether to remove the component's style. | # Alert ```tsx import { Alert } from "@chakra-ui/react" export const AlertBasic = () => { return ( This is the alert title ) } ``` ## Usage ```jsx import { Alert } from "@chakra-ui/react" ``` ```jsx ``` :::info If you prefer a closed component composition, check out the [snippet below](#closed-component). ::: ## Examples ### Description Render the `Alert.Description` component to provide additional context to the alert. ```tsx import { Alert } from "@chakra-ui/react" export const AlertWithDescription = () => { return ( Invalid Fields Your form has some errors. Please fix them and try again. ) } ``` ### Status Change the status of the alerts by passing the `status` prop. This affects the color scheme and icon used. Alert supports `error`, `success`, `warning`, and `info` statuses. ```tsx import { Alert, Stack } from "@chakra-ui/react" export const AlertWithStatus = () => { return ( There was an error processing your request Chakra is going live on August 30th. Get ready! Seems your account is about expire, upgrade now Data uploaded to the server. Fire on! ) } ``` ### Variants Use the `variant` prop to change the visual style of the alert. Values can be either `subtle`, `solid`, `outline` ```tsx import { Alert, Stack } from "@chakra-ui/react" export const AlertWithVariants = () => { return ( Data uploaded to the server. Fire on! Data uploaded to the server. Fire on! Data uploaded to the server. Fire on! ) } ``` ### With Close Button Here's and example of how to compose the `Alert` with a close button. ```tsx import { Alert, CloseButton } from "@chakra-ui/react" export const AlertWithCloseButton = () => { return ( Success! Your application has been received. We will review your application and respond within the next 48 hours. ) } ``` ### With Spinner Here's and example of how to compose the `Alert` with a spinner. ```tsx import { Alert, Spinner } from "@chakra-ui/react" export const AlertWithSpinner = () => { return ( We are loading something ) } ``` ### Custom Icon Use the `icon` prop to pass a custom icon to the alert. This will override the default icon for the alert status. ```tsx import { Alert } from "@chakra-ui/react" import { LuAlarmClockPlus } from "react-icons/lu" export const AlertWithCustomIcon = () => { return ( Submitting this form will delete your account ) } ``` ### Color Palette Override The default colorPalette is inferred from the `status` prop. To override the color palette, pass the `colorPalette` prop. ```tsx import { Alert } from "@chakra-ui/react" export const AlertWithColorPaletteOverride = () => { return ( This is an info alert but shown as teal ) } ``` ### Customization You can style the `Alert` component using style props. ```tsx import { Alert, Link, Stack } from "@chakra-ui/react" import { LuPercent } from "react-icons/lu" export const AlertWithCustomization = () => { return ( Black Friday Sale (20% off) Upgrade your plan to get access to the sale. Upgrade Heads up: Black Friday Sale (20% off) ) } ``` ### Closed Component Here's how to setup the `Alert` for a closed component composition. If you want to automatically add the closed component to your project, run the command: ```bash npx @chakra-ui/cli snippet add alert ``` ## Props ### Root | Prop | Default | Type | Description | | --- | --- | --- | --- | | colorPalette | gray | `'gray' \| 'red' \| 'orange' \| 'yellow' \| 'green' \| 'teal' \| 'blue' \| 'cyan' \| 'purple' \| 'pink'` | The color palette of the component | | status | info | `'info' \| 'warning' \| 'success' \| 'error' \| 'neutral'` | The status of the component | | variant | subtle | `'subtle' \| 'surface' \| 'outline' \| 'solid'` | The variant of the component | | size | md | `'sm' \| 'md' \| 'lg'` | The size of the component | | inline | false | `'true' \| 'false'` | The inline of the component | # Aspect Ratio ```tsx import { AspectRatio, Center } from "@chakra-ui/react" export const AspectRatioBasic = () => { return (
2 / 1
) } ``` ## Usage ```jsx import { AspectRatio } from "@chakra-ui/react" ``` ```jsx