Accordion 手风琴
用于在紧凑空间中组织信息的可折叠内容面板
用法
import { Accordion } from '@lenso/ui';此预览复用英文版适配,不代表中文源示例已完成本地实现。
"use client";
import { ArrowsRotateLeft, Box, ChevronDown, CreditCard, PlanetEarth, Receipt, ShoppingBag,} from "@gravity-ui/icons";import { Accordion } from "@lenso/ui";import * as stylex from "@stylexjs/stylex";
const styles = stylex.create({ root: { width: "100%", maxWidth: 448 }, icon: { width: 16, height: 16, flexShrink: 0, marginInlineEnd: 12, color: "var(--muted)" },});const items = [ { title: "How do I place an order?", Icon: ShoppingBag, content: "Browse our products, add items to your cart, and proceed to checkout. You'll need to provide shipping and payment information to complete your purchase.", }, { title: "Can I modify or cancel my order?", Icon: Receipt, content: "Yes, you can modify or cancel your order before it's shipped. Once your order is processed, you can't make changes.", }, { title: "What payment methods do you accept?", Icon: CreditCard, content: "We accept all major credit cards, including Visa, Mastercard, and American Express.", }, { title: "How much does shipping cost?", Icon: Box, content: "Shipping costs vary based on your location and the size of your order. We offer free shipping for orders over $50.", }, { title: "Do you ship internationally?", Icon: PlanetEarth, content: "Yes, we ship to most countries. Please check our shipping rates and policies for more information.", }, { title: "How do I request a refund?", Icon: ArrowsRotateLeft, content: "If you're not satisfied with your purchase, you can request a refund within 30 days of purchase. Please contact our customer support team for assistance.", },];export function Basic() { return ( <Accordion xstyle={styles.root}> {items.map(({ title, content, Icon }) => ( <Accordion.Item key={title} value={title}> <Accordion.Heading> <Accordion.Trigger> <Icon aria-hidden="true" {...stylex.props(styles.icon)} /> {title} <Accordion.Indicator> <ChevronDown /> </Accordion.Indicator> </Accordion.Trigger> </Accordion.Heading> <Accordion.Panel> <Accordion.Body>{content}</Accordion.Body> </Accordion.Panel> </Accordion.Item> ))} </Accordion> );}Local adaptation source above. Derived from HeroUI v3.2.6 source.
组件结构
import { Accordion } from '@lenso/ui';
export default () => ( <Accordion> <Accordion.Item> <Accordion.Heading> <Accordion.Trigger> <Accordion.Indicator /> </Accordion.Trigger> </Accordion.Heading> <Accordion.Panel> <Accordion.Body/> </Accordion.Panel> </Accordion.Item> </Accordion>)示例
表面样式
此预览复用英文版适配,不代表中文源示例已完成本地实现。
"use client";export { Surface } from "./source";Local adaptation source above. Derived from HeroUI v3.2.6 source.
无分隔线
此预览复用英文版适配,不代表中文源示例已完成本地实现。
"use client";export { WithoutSeparator } from "./source";Local adaptation source above. Derived from HeroUI v3.2.6 source.
多项展开
此预览复用英文版适配,不代表中文源示例已完成本地实现。
"use client";export { Multiple } from "./source";Local adaptation source above. Derived from HeroUI v3.2.6 source.
禁用状态
此预览复用英文版适配,不代表中文源示例已完成本地实现。
"use client";export { Disabled } from "./source";Local adaptation source above. Derived from HeroUI v3.2.6 source.
受控组件
此预览复用英文版适配,不代表中文源示例已完成本地实现。
"use client";export { Controlled } from "./source";Local adaptation source above. Derived from HeroUI v3.2.6 source.
自定义指示器
此预览复用英文版适配,不代表中文源示例已完成本地实现。
"use client";export { CustomIndicator } from "./source";Local adaptation source above. Derived from HeroUI v3.2.6 source.
渲染函数
此预览复用英文版适配,不代表中文源示例已完成本地实现。
"use client";export { RenderFunction } from "./source";Local adaptation source above. Derived from HeroUI v3.2.6 source.
常见问题布局
此预览复用英文版适配,不代表中文源示例已完成本地实现。
"use client";export { FAQ } from "./source";Local adaptation source above. Derived from HeroUI v3.2.6 source.
自定义样式
Tailwind CSS
此预览复用英文版适配,不代表中文源示例已完成本地实现。
"use client";export { CustomStyles } from "./source";Local adaptation source above. Derived from HeroUI v3.2.6 source.
全局 CSS
若要自定义 Accordion 组件类,可使用 @layer components 指令。
了解更多。
@layer components { .accordion { @apply rounded-xl bg-gray-50; }
.accordion__trigger { @apply font-semibold text-lg; }
.accordion--outline { @apply shadow-lg border-2; }}样式参考
HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。
CSS 类
Accordion 组件使用以下 CSS 类(查看源码样式):
基础类 [!toc]
.accordion- Accordion 根容器.accordion__body- 正文容器.accordion__heading- 标题包裹层.accordion__indicator- 展开/收起指示图标.accordion__item- 单个 Accordion 项.accordion__panel- 可折叠面板容器.accordion__trigger- 可点击的触发按钮
变体类 [!toc]
.accordion--outline- 描边变体(边框与背景)
状态类 [!toc]
.accordion__trigger[aria-expanded="true"]- 展开状态.accordion__panel[aria-hidden="false"]- 面板可见状态
交互状态
该组件同时支持 CSS 伪类与 data 属性:
- 悬停:触发器上
:hover或[data-hovered="true"] - 聚焦:触发器上
:focus-visible或[data-focus-visible="true"] - 禁用:触发器上
:disabled或[aria-disabled="true"] - 展开:触发器上
[aria-expanded="true"]
API 参考
Accordion
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
allowsMultipleExpanded | boolean | false | 是否允许多项同时展开 |
defaultExpandedKeys | Iterable<Key> | - | 初始展开的 key |
expandedKeys | Iterable<Key> | - | 受控的展开 key |
onExpandedChange | (keys: Set<Key>) => void | - | 展开 key 变化时调用的事件处理函数 |
isDisabled | boolean | false | 是否禁用整个 Accordion |
variant | "default" | "surface" | "default" | Accordion 的视觉变体 |
hideSeparator | boolean | false | 是否隐藏 Accordion 项之间的分隔线 |
className | string | - | 额外的 CSS 类 |
children | ReactNode | - | Accordion 项 |
render | DOMRenderFunction<keyof React.JSX.IntrinsicElements, AccordionRenderProps> | - | 使用自定义渲染函数覆盖默认 DOM 元素。 |
Accordion.Item
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
id | Key | - | 该项的唯一标识 |
isDisabled | boolean | false | 是否禁用该项 |
defaultExpanded | boolean | false | 初始是否展开 |
isExpanded | boolean | - | 受控展开状态 |
onExpandedChange | (isExpanded: boolean) => void | - | 展开状态变化时调用的事件处理函数 |
className | string | - | 额外的 CSS 类 |
children | ReactNode | - | 项内容 |
render | DOMRenderFunction<keyof React.JSX.IntrinsicElements, AccordionItemRenderProps> | - | 使用自定义渲染函数覆盖默认 DOM 元素。 |
Accordion.Trigger
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
className | string | - | 额外的 CSS 类 |
children | ReactNode | RenderFunction | - | 触发器内容或渲染函数 |
onPress | () => void | - | 额外的按下事件处理函数 |
isDisabled | boolean | - | 是否禁用触发器 |
render | DOMRenderFunction<keyof React.JSX.IntrinsicElements, TriggerRenderProps> | - | 使用自定义渲染函数覆盖默认 DOM 元素。 |
Accordion.Panel
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
className | string | - | 额外的 CSS 类 |
children | ReactNode | - | 面板内容 |
render | DOMRenderFunction<keyof React.JSX.IntrinsicElements, AccordionPanelRenderProps> | - | 使用自定义渲染函数覆盖默认 DOM 元素。 |
Accordion.Indicator
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
className | string | - | 额外的 CSS 类 |
children | ReactNode | - | 自定义指示图标 |
Accordion.Body
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
className | string | - | 额外的 CSS 类 |
children | ReactNode | - | 正文内容 |