TabBar 底部 tab 栏
固定底部主导航 · 50px 高 · 各 tab icon + 文字 垂直居中。
导航默认采用 surface="glass" 系统材质;贴边实色导航可显式选择 solid。安全区、外边距和定位由宿主负责。
代码演示
视觉规范
来源:src/components/ui/TabBar/styles.ts、TabBar.tsx。
| 元素 | 规则 |
|---|---|
| 高度 | 50px(fixed.tabbarH,物理常量不缩放) |
| 背景 | 默认 glass 为胶囊玻璃表面;solid 使用主题 c.surface |
| 顶部边线 | 仅 solid 使用 hairline c.outline;glass 不绘制顶部边线 |
| Tab 项 | flex: 1,垂直居中 icon + 文字 |
| Icon | r(22),未选中使用 c.foregroundMuted,选中使用 c.primary |
| 文字 | 10px(type.nano)/ 500,与 icon 同色 |
| Badge | c.error 底数字徽章(number | string),叠在 icon 右上 |
| 安全区 | 实际 bottom inset 不计入 50px;宿主可用 <SafeAreaView edges={['bottom']}> 包裹 |
用法
import {
HomeOutlined,
LocationOutlined,
SparkOutlined,
UserOutlined,
} from '@unif/react-native-icons';
import { TabBar } from '@unif/react-native-design';
<TabBar
active="home"
onChange={setActive}
items={[
{ id: 'home', icon: HomeOutlined, label: '首页' },
{ id: 'visits', icon: LocationOutlined, label: '拜访', badge: 3 },
{ id: 'agent', icon: SparkOutlined, label: 'AI' },
{ id: 'me', icon: UserOutlined, label: '我的' },
]}
/>;
API
来源:src/components/ui/TabBar/types.ts、TabBar.tsx。
TabBarProps:
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
active | string | — | 当前激活 tab 的 id(必填) |
onChange | (id: string) => void | — | 切换回调,回传点中 tab 的 id(必填) |
items | TabBarItem[] | — | tab 列表(必填) |
surface | 'solid' | 'glass' | 'glass' | 系统玻璃或主题实色表面 |
style | StyleProp<ViewStyle> | — | 根容器的布局与外观样式 |
testID | string? | — | 容器 testID;item testID 自动派生为 ${testID}-${id} |
TabBarItem:
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
id | string | — | 唯一标识,用作 key 与选中判定 |
icon | ComponentType<IconProps> | — | 图标组件(见 图标) |
label | string | — | tab 文字(同时作 accessibilityLabel) |
badge | number | string | — | 图标右上角数字角标(用 string 才能写 '99+') |
testID | string? | — | 单 tab testID(默认 ${barTestID}-${id}) |
无障碍(a11y)
来源:src/components/ui/TabBar/TabBar.tsx、types.ts。
- 默认
accessibilityRole:外层容器为'tablist',每个 tab 的<Pressable>为'tab'。 - a11y 名称:没有角标时使用必填的
item.label;badge != null时组合为${item.label},${item.badge}条未读,包括数值0。 - 状态语义:
accessibilityState={{ selected: on }}——selected由item.id === active派生。无disabled语义(TabBarItem无disabled字段)。
import { HomeOutlined, LocationOutlined } from '@unif/react-native-icons';
// 每个 tab 自动 role=tab、label=item.label、selected 由 active 决定
<TabBar
active={active}
onChange={setActive}
items={[
{ id: 'home', icon: HomeOutlined, label: '首页' },
{ id: 'visits', icon: LocationOutlined, label: '拜访', badge: 3 },
]}
/>;
使用注意
- ❌ 不要做 5 个以上 tab——超过就用左侧 Drawer
- ❌ 不要在 active 状态用其他颜色——必须主橙
- ❌ 不要在 TabBar 里加按钮("发布"等)——用单独的 FAB 或 NavBar 右槽