Top-level navigation between the large sections of one area (GitHub Primer UnderlineNav): the current tab in the foreground color with a 2pt indicator underneath, the others muted. Tabs that don’t fit go into a trailing “More ⌄” menu, and a current tab in the overflow swaps into the last visible place; it never scrolls sideways. Controlled value + onChange.
Phone: Primary navigation between a few large sections of an area: a 44 bar, the current item in the foreground color with a 3 pt brand-colored indicator, others secondary; when they don’t fit, the bar scrolls horizontally and fades at the right edge. Controlled value + onChange.
Status: experimental · Components: Tabs
Import
Section titled “Import”import { Tabs } from 'nacre'Put it at the top of the area; the bottom divider spans the root. class only sets the side padding so the tab text lines up with the content below (each tab has 8 of its own padding). sm by default (a 40-high bar, small text); md is a 44-high bar with body text.
import { Text, View, signal } from 'nsh'import { Tabs, TEXT_SIZE, TOUCH } from 'nacre'import type { TabItem } from 'nacre'
type Section = 'profile' | 'assets' | 'trading' | 'access' | 'marketing' | 'service'
const sections = (): TabItem<Section>[] => [ { value: 'profile', label: 'Profile' }, { value: 'assets', label: 'Assets' }, { value: 'trading', label: 'Trading' }, { value: 'access', label: 'Accounts' }, { value: 'marketing', label: 'Marketing' }, { value: 'service', label: 'Service' },]
export function TabsUsage() { const section = signal<Section>('profile') return ( <View class="rounded-lg border border-border overflow-hidden bg-card"> <Tabs testID="tabs-usage" accessibilityLabel="Customer sections" items={sections()} value={section()} onChange={(value) => section.set(value)} class="px-2" /> <View class={TOUCH ? 'px-5 py-4' : 'p-4'}> <Text testID="tabs-usage-current" class={`${TEXT_SIZE.body} text-foreground`}> {sections().find((s) => s.value === section())?.label ?? ''} </Text> </View> </View> )}Examples
Section titled “Examples”When tabs don’t fit
Section titled “When tabs don’t fit”Tabs are packed in order and the rest go into “More ⌄”; a tab chosen from the menu swaps into the last visible place and the one it pushes out goes back into the menu. A width change redistributes them.
Phone: On phones nothing moves into “More”: the bar scrolls horizontally and fades at the right edge to show there are more items.
import { Text, View, signal } from 'nsh'import { Tabs, TEXT_SIZE, TOUCH } from 'nacre'import type { TabItem } from 'nacre'
type Section = 'profile' | 'assets' | 'trading' | 'access' | 'marketing' | 'service'
const sections = (): TabItem<Section>[] => [ { value: 'profile', label: 'Profile' }, { value: 'assets', label: 'Assets' }, { value: 'trading', label: 'Trading' }, { value: 'access', label: 'Accounts' }, { value: 'marketing', label: 'Marketing' }, { value: 'service', label: 'Service' },]
export function TabsOverflow() { const section = signal<Section>('profile') return ( <View class="w-[240px] rounded-lg border border-border bg-card"> <Tabs testID="tabs-overflow" size="md" accessibilityLabel="Customer sections" items={sections()} value={section()} onChange={(value) => section.set(value)} class="px-2" /> <View class={TOUCH ? 'px-5 py-4' : 'p-4'}> <Text testID="tabs-overflow-current" class={`${TEXT_SIZE.body} text-foreground`}> {sections().find((s) => s.value === section())?.label ?? ''} </Text> </View> </View> )}Keyboard
Section titled “Keyboard”| Key | Action |
|---|---|
| Tab | The group is one Tab stop: entering lands on the current tab; Tab again reaches “More”, again leaves |
| ← → | Move between the visible tabs and switch at once, wrapping at the ends |
| Home / End | Go to the first / last visible tab and switch to it |
| Space / Return | On “More”: open the menu, ↑↓ to pick, Return to switch |
Accessibility
Section titled “Accessibility”- The group is a tab list (nsh/base Tabs, tablist) and the current tab is read as selected; accessibilityLabel says what the group switches (“Customer sections”).
- “More” reads as a button with its expanded state; the menu holds the tabs that didn’t fit.
- The current tab is shown by color and the indicator; the weight never changes, so other tabs don’t move.
Do / Don’t
Section titled “Do / Don’t”Do
- Use a single row per area and put second-level views in the first row of the content (Segmented xs or a dropdown)
- Keep names to a couple of words
- Give the root a background matching the surface when it sticks in a scroll view
Don’t
- Stack two rows of tabs
- Scroll tabs sideways when they don’t fit
- Use it to pick a value (use Segmented)
Property descriptions follow the Chinese source comments.
| Prop | Type | Default | Description |
|---|---|---|---|
items * |
readonly TabItem<T>[] |
— | |
value * |
T |
— | 当前项(受控) |
onChange * |
(value: T) => void |
— | 选中另一项时调用;项的 testID 是 ${testID}-${value},「更多」是 ${testID}-more |
size |
'sm' | 'md' |
'sm' |
sm:栏高 40、项 28、字 small(默认);md:栏高 44、项 32、字 body |
accessibilityLabel |
string |
— | 整组的读屏名(「客户资料分区」) |
testID |
string |
— | |
trailing |
JSX.Element |
— | 栏尾的操作(这一块的刷新这类),排在「更多」之后、不参与溢出计算 |
class |
string |
— | 根节点的布局逃生口(左右内边距、外边距);底部分隔线总是撑满根节点 |
* required