feat: migrate Toolbar with teleport-safe focus ownership

This commit is contained in:
maoyingjie
2026-10-23 10:50:42 +08:00
parent 9c93647ad2
commit b2525bb04d
33 changed files with 2168 additions and 30 deletions
+98
View File
@@ -0,0 +1,98 @@
---
title: Toolbar 工具栏
description: 用于承载可交互控件的容器,并支持方向键导航。
component: toolbar
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { Toolbar } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="toolbar-basic" />
## 示例
### 垂直方向
<ComponentPreview name="toolbar-vertical" />
### 组合模式
<ComponentPreview name="toolbar-attached" />
### 与 ButtonGroup 组合
<ComponentPreview name="toolbar-with-button-group" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="toolbar-custom-styles" />
### 全局 CSS
若要自定义组件类,可使用 `@layer components` 指令。
```css
@layer components {
.hv-toolbar {
@apply gap-4 rounded-lg bg-surface p-3;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
### CSS 类
Toolbar 使用以下 CSS 类:
- `.hv-toolbar` - 基础容器
- `.hv-toolbar--horizontal` - 水平方向(默认)
- `.hv-toolbar--vertical` - 垂直方向
- `.hv-toolbar--attached` - Attached 变体:surface 背景与完全圆角
## API 参考
### Toolbar
| Prop | 类型 | 默认值 | 描述 |
| ----------------- | ---------------------------- | ------------------- | ----------------------------------------- |
| `isAttached` | `boolean` | `false` | Toolbar 是否使用带完全圆角的 surface 背景 |
| `orientation` | `'horizontal' \| 'vertical'` | `'horizontal'` | Toolbar 的方向 |
| `dir` | `'ltr' \| 'rtl'` | 继承 ConfigProvider | 阅读方向,影响方向键 |
| `as` | `string` | `'div'` | 渲染的根元素 |
| `aria-label` | `string` | - | Toolbar 的无障碍标签 |
| `aria-labelledby` | `string` | - | 用于标注该 Toolbar 的元素 id |
| `class` | `string` | - | 额外的 CSS 类名 |
### 作用域插槽
| 名称 | 参数 | 描述 |
| --------- | ----------------- | ------------------------------------ |
| `default` | `{ orientation }` | 内容;`orientation` 为当前工具栏方向 |
## 说明
- Toolbar 提供 `role="toolbar"` 与 `aria-orientation`
- **焦点所有权**:工具栏内所有可聚焦按钮(Button、ToggleButton,含 ButtonGroup / ToggleButtonGroup 成员)由 Toolbar 统一做单 Tab 入口与方向键遍历,可跨子组移动;嵌套 Toolbar 各自隔离
- 子组在 Toolbar 内不再自行处理方向键,避免一次按键双重导航
- `ToggleButtonGroup` 未显式指定时继承 Toolbar 的 `orientation`;显式属性优先,取消覆盖后恢复继承
- `Separator` 在 Toolbar 内默认取与工具栏垂直的方向,可显式覆盖
- `ButtonGroup` 继承 Toolbar 方向(与上游 ToggleButtonGroup 缺省 orientation 一致)
- React 渲染 prop `children(values)` 转为作用域插槽 `{ orientation }`
## 相关组件
- [ButtonGroup 按钮组](./button-group)
- [ToggleButtonGroup 切换按钮组](./toggle-button-group)
- [Separator 分隔符](./separator)
- [Tabs 标签页](./tabs)