按钮组 · ButtonGroup
将关联操作组合展示,统一尺寸、边界和键盘焦点。
安装与前置条件
npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0
需要 React 19+,按顺序导入基础 CSS 和主题,并设置 Theme Provider。参见开始使用。
示例与使用方式
- 按钮组 · 综合展示 / 分组 1:首先体验这个示例,再查看代码理解组件的组合方式。
- 按钮组 · 基础用法 / 分组 2:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 按钮组 · 分组 / 浮动 3:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
API 与上游详细参考
优先使用 0.7.1 官方中文说明;上游尚未提供中文的条目保留英文。属性名、类型及默认值保持原生 API,不把示例状态名称当成组件属性。
Button Group
ButtonGroup 将相关操作连接成一个控件。当多个按钮表示对同一对象的相关操作时使用,如复制/剪切/粘贴,或撤销/重做。
使用建议
- 将对同一对象执行相关操作的按钮分组,如对选中文本的复制、剪切、粘贴。
- 组内所有按钮使用相同的变体,使其看起来像一个连接的整体。
- 保持组较小(2-4 个按钮)。更多操作请使用工具栏或下拉菜单。
- 避免:不要混合差异很大的操作,将保存按钮和删除按钮放在同一组中会令人困惑。
- 避免:不要使用 ButtonGroup 进行导航,使用 SegmentedControl 或 TabList 切换视图。
- 避免:不要嵌套 ButtonGroup。如需多个组,请并排放置并留有间隔。
- 按钮组的标签应说明这些按钮作用于什么。该标签是按钮组的无障碍名称,屏幕阅读器会在每个成员之前朗读它。
- 按钮组是单个 Tab 停靠点。方向键沿排列方向在成员间移动焦点,Home/End 跳到首尾,禁用成员会被跳过——即 WAI-ARIA APG 的 roving tabindex 技术。
- 避免:不要用禁用整个按钮组来表示操作进行中。禁用的成员会失去焦点,键盘用户会丢失位置;请保持按钮组可用,并在发起操作的按钮上显示进度。
主题 API
{
"targets": [
{
"className": "astryx-button-group",
"visualProps": [
"size",
"orientation",
"elevation"
]
}
]
}
Button Group
将多个按钮组合在一起,带有连接样式:共享边框、正确的圆角处理(仅外边缘),以及水平或垂直方向。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| children * | ReactNode | Button 或 IconButton 子元素。 | |
| label * | string | 按钮组的无障碍标签 (aria-label)。 | |
| orientation | 'horizontal' | 'vertical' | 'horizontal' | 按钮组的布局方向。 |
| size | 'sm' | 'md' | 'lg' | 'md' | 组内按钮的默认尺寸。单个按钮可覆盖。 |
| isDisabled | boolean | false | 组内所有按钮是否禁用。 |
| elevation | 'none' | 'low' | 'med' | 'high' | 'none' | 整个组的静止阴影深度。连接的按钮共享一个表面,阴影将它们作为整体抬起。 |
| ref | React.Ref<HTMLDivElement> | 转发到根元素的引用。 | |
| xstyle | StyleXStyles | StyleX 样式,用于布局自定义。必须是 stylex.create() 的值。 | |
| data-testid | string | 自动化测试的选择器。 |
无障碍
- Text label: Text in each button must have at least 4.5:1 contrast with its background in every state. For Hover and Pointer down, measure the final background after the overlay is applied. — 4.5:1
- Essential icon or spinner arc: An icon used instead of text must have at least 3:1 contrast with the button background. The moving spinner arc must also meet 3:1. An icon beside a visible label does not need its own check. — 3:1
- Visible control boundary: Some groups need a divider or edge to show each button. That divider or edge must have at least 3:1 contrast. — 3:1 if needed
- Keyboard focus indicator: The focus outline must have at least 3:1 contrast with the area around it. — 3:1
- Disabled appearance: Disabled controls do not need to meet these contrast ratios. — Not required
组件结构
- 按钮: 一个或多个 Button 或 IconButton 子元素,形成连接的组。
- 分隔线: 按钮之间的细边框,由组自动渲染。
完整示例的额外依赖
部分示例使用独立图标包。示例清单中的每条安装命令包含该示例实际需要的依赖;若要运行本页全部示例,还需安装:
npm install @heroicons/react@2.2.0