¥Accordion
一组垂直堆叠的交互式标题,每个标题显示一个相关的内容部分。
import * as React from "react";import { Accordion } from "radix-ui";import classNames from "classnames";import { ChevronDownIcon } from "@radix-ui/react-icons";import "./styles.css";const AccordionDemo = () => (<Accordion.Root className="AccordionRoot" type="single" defaultValue="item-1" collapsible ><Accordion.Item className="AccordionItem" value="item-1"><AccordionTrigger>Is it accessible?</AccordionTrigger><AccordionContent>Yes. It adheres to the WAI-ARIA design pattern.</AccordionContent></Accordion.Item><Accordion.Item className="AccordionItem" value="item-2"><AccordionTrigger>Is it unstyled?</AccordionTrigger><AccordionContent>Yes. It's unstyled by default, giving you freedom over the look andfeel.</AccordionContent></Accordion.Item><Accordion.Item className="AccordionItem" value="item-3"><AccordionTrigger>Can it be animated?</AccordionTrigger><Accordion.Content className="AccordionContent"><div className="AccordionContentText">Yes! You can animate the Accordion with CSS or JavaScript.</div></Accordion.Content></Accordion.Item></Accordion.Root>);const AccordionTrigger = React.forwardRef(({ children, className, ...props }, forwardedRef) => (<Accordion.Header className="AccordionHeader"><Accordion.Trigger className={classNames("AccordionTrigger", className)} {...props} ref={forwardedRef} >{children}<ChevronDownIcon className="AccordionChevron" aria-hidden /></Accordion.Trigger></Accordion.Header>),);const AccordionContent = React.forwardRef(({ children, className, ...props }, forwardedRef) => (<Accordion.Content className={classNames("AccordionContent", className)} {...props} ref={forwardedRef} ><div className="AccordionContentText">{children}</div></Accordion.Content>),);export default AccordionDemo;
Full keyboard navigation.
Supports horizontal/vertical orientation.
Supports Right to Left direction.
Can expand one or multiple items.
Can be controlled or uncontrolled.
¥Installation
从命令行安装组件。
¥Install the component from your command line.
¥Anatomy
导入所有部分并将它们组合在一起。
¥Import all parts and piece them together.
¥API Reference
¥Root
包含手风琴折叠面板的所有部分。
¥Contains all the parts of an accordion.
¥Item
包含可折叠部分的所有部分。
¥Contains all the parts of a collapsible section.
¥Header
封装 Accordion.Trigger
。使用 asChild
属性将其更新为适合你页面的标题级别。
¥Wraps an Accordion.Trigger
. Use the asChild
prop to update it to the appropriate heading level for your page.
¥Trigger
切换其关联项目的折叠状态。它应该嵌套在 Accordion.Header
内部。
¥Toggles the collapsed state of its associated item. It should be nested inside of an Accordion.Header
.
¥Content
包含项目的可折叠内容。
¥Contains the collapsible content for an item.
¥Examples
¥Expanded by default
使用 defaultValue
属性定义默认打开项。
¥Use the defaultValue
prop to define the open item by default.
¥Allow collapsing all items
使用 collapsible
属性来允许关闭所有项目。
¥Use the collapsible
prop to allow all items to close.
¥Multiple items open at the same time
将 type
属性设置为 multiple
,以便能够一次打开多个项目。
¥Set the type
prop to multiple
to enable opening multiple items at once.
¥Rotated icon when open
你可以添加额外的装饰元素,例如 V 形,并在项目打开时旋转它。
¥You can add extra decorative elements, such as chevrons, and rotate it when the item is open.
¥Horizontal orientation
使用 orientation
属性创建水平手风琴。
¥Use the orientation
prop to create a horizontal accordion.
¥Animating content size
使用 --radix-accordion-content-width
和/或 --radix-accordion-content-height
CSS 变量来为内容打开/关闭时的大小设置动画:
¥Use the --radix-accordion-content-width
and/or --radix-accordion-content-height
CSS variables to animate the size of the content when it opens/closes:
¥Accessibility
¥Adheres to the Accordion WAI-ARIA design pattern.
¥Keyboard Interactions