Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

docs(Tab): describe a tab with a Tooltip anchored on it (#10734)

Cindy Zhang242d20d62b68
diff --git a/.changeset/tab-description-pattern.md b/.changeset/tab-description-pattern.md
new file mode 100644
index 0000000000..ebd2be37ab
--- /dev/null
+++ b/.changeset/tab-description-pattern.md
@@ -0,0 +1,8 @@
+---
+'@astryxdesign/cli': patch
+'@astryxdesign/core': patch
+---
+
+[docs] Tab's docs show how to give a tab help text: anchor a `Tooltip` on the `Tab` through its ref, so the description lands on the tab, and put a decorative info `Icon` in `endContent`. `endContent` is documented as non-interactive: a button, link, or `InfoTip` inside it would nest a control inside the tab's button (#10732).
+
+@cixzhang
diff --git a/apps/storybook/stories/TabList.stories.tsx b/apps/storybook/stories/TabList.stories.tsx
index 0358072d8f..c15bd5c535 100644
--- a/apps/storybook/stories/TabList.stories.tsx
+++ b/apps/storybook/stories/TabList.stories.tsx
@@ -1,6 +1,6 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
-import {useState, type CSSProperties} from 'react';
+import {useRef, useState, type CSSProperties} from 'react';
import type {Meta, StoryObj} from '@storybook/react';
import {TabList, Tab, TabMenu} from '@astryxdesign/core/TabList';
import {Button} from '@astryxdesign/core/Button';
@@ -11,6 +11,8 @@ import {
StackItem,
} from '@astryxdesign/core/Layout';
import {Heading} from '@astryxdesign/core/Text';
+import {Icon} from '@astryxdesign/core/Icon';
+import {Tooltip} from '@astryxdesign/core/Tooltip';
import {
PlusIcon,
FunnelIcon,
@@ -1049,6 +1051,72 @@ export const TabsPattern: Story = {
},
};
+/**
+ * A tab's help text: a Tooltip anchored on the Tab through its ref, so it
+ * describes the tab itself. Each Tooltip renders after the TabList, because the
+ * strip holds only tabs. The tab stays one button; the info glyph is decorative.
+ */
+export const DescribedTabs: Story = {
+ name: 'Tab With Help Text',
+ render: () => {
+ const [value, setValue] = useState('general');
+ const generalRef = useRef<HTMLButtonElement>(null);
+ const membersRef = useRef<HTMLButtonElement>(null);
+ const panels = {
+ general: 'Workspace name, language, and time zone.',
+ members: 'People with access to this workspace.',
+ advanced: 'Export, archive, and delete.',
+ };
+ return (
+ <div style={{display: 'grid', gap: '12px', maxWidth: '420px'}}>
+ <TabList
+ value={value}
+ onChange={setValue}
+ role="tablist"
+ aria-label="Settings">
+ <Tab
+ ref={generalRef}
+ value="general"
+ label="General"
+ panelId="panel-help-general"
+ endContent={<Icon icon="info" size="sm" />}
+ />
+ <Tab
+ ref={membersRef}
+ value="members"
+ label="Members"
+ panelId="panel-help-members"
+ endContent={<Icon icon="info" size="sm" />}
+ />
+ <Tab
+ value="advanced"
+ label="Advanced"
+ panelId="panel-help-advanced"
+ />
+ </TabList>
+ <Tooltip
+ content="Name, language, and time zone for this workspace."
+ anchorRef={generalRef}
+ />
+ <Tooltip
+ content="Who can open this workspace, and what they can change."
+ anchorRef={membersRef}
+ />
+ {Object.entries(panels).map(([key, text]) => (
+ <div
+ key={key}
+ id={`panel-help-${key}`}
+ role="tabpanel"
+ tabIndex={0}
+ hidden={key !== value}>
+ {text}
+ </div>
+ ))}
+ </div>
+ );
+ },
+};
+
export const PressedState: Story = {
name: 'Pressed state',
parameters: {
diff --git a/docs/specs/components/TabList/Tab.spec.md b/docs/specs/components/TabList/Tab.spec.md
index 4afaf64ef2..5e91ee8732 100644
--- a/docs/specs/components/TabList/Tab.spec.md
+++ b/docs/specs/components/TabList/Tab.spec.md
@@ -14,6 +14,7 @@ verified_by:
[
packages/core/src/TabList/TabList.test.tsx,
packages/core/src/TabList/__tests__/Tabs.a11y.test.tsx,
+ packages/core/src/TabList/__tests__/TabDescription.test.tsx,
packages/core/src/TabList/__tests__/Tabs.a11y.chromium.spec.ts,
packages/core/src/TabList/__tests__/TabListTextSize.interaction.browser.spec.ts,
apps/storybook/stories/TabList.stories.tsx,
@@ -152,6 +153,10 @@ Consumer migration instructions belong in consumer docs and release notes.
- **AR1 — Pattern semantics.** FR1 and FR2.
- **AR2 — One tab stop.** FR3.
- **AR3 — Names.** Visible label or, when hidden, `aria-label` from `label`.
+- **AR4 — Help text describes the tab.** A tab's help text is a Tooltip
+ anchored on the Tab through its forwarded ref, so the description lands on
+ the tab and the Tooltip renders outside the strip; `endContent` holds only
+ non-interactive content (FR2).
## Design relationships
@@ -172,14 +177,15 @@ FR1–FR2.
## Verification map
-| Contract | Verification | Representative states | Mutation or failure expectation | Audit section |
-| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------- | --------------------------------------------------------------------------- | ------------------------- |
-| FR1, FR2 | `TabList.test.tsx` aria-current, polymorphic link, and tabs-pattern tests; `Tabs.a11y.test.tsx`; `Tabs.a11y.chromium.spec.ts` for `aria-selected` | Navigation, link, tabs | Wrong role or state fails. | `audit:Tab/accessibility` |
-| FR3 | `TabList.test.tsx` single tab stop and roving tabindex tests | Selected | Extra tab stops, or a wrong starting stop, fail. | `audit:Tab/accessibility` |
-| FR4, FR5 | `TabList.test.tsx` onChange and disabled tests | Click, disabled button | Selection when disabled fails. | `audit:Tab/behavior` |
-| FR6, FR7 | `TabList.test.tsx` icon, icon-only, and pressed-overlay tests | Icon, selected icon, hidden label | Wrong icon, name, or overlay fails. | `audit:Tab/behavior` |
-| AV1 text size | `TabListTextSize.interaction.browser.spec.ts` at a 200% and 250% default font size | Default, size variants, with menu | A tab or tab strip that clips its label, or a label outside its tab, fails. | `audit:Tab/behavior` |
-| RTL relation | RTL audit curated D2 target (`apps/storybook/rtl-audit/targets.json`) on `TabList / With icons` | Tab icon and label; LTR and RTL | An icon that is not at the inline start of its label fails D2. | `audit:Tab/i18n-rtl` |
+| Contract | Verification | Representative states | Mutation or failure expectation | Audit section |
+| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- |
+| FR1, FR2 | `TabList.test.tsx` aria-current, polymorphic link, and tabs-pattern tests; `Tabs.a11y.test.tsx`; `Tabs.a11y.chromium.spec.ts` for `aria-selected` | Navigation, link, tabs | Wrong role or state fails. | `audit:Tab/accessibility` |
+| FR3 | `TabList.test.tsx` single tab stop and roving tabindex tests | Selected | Extra tab stops, or a wrong starting stop, fail. | `audit:Tab/accessibility` |
+| FR4, FR5 | `TabList.test.tsx` onChange and disabled tests | Click, disabled button | Selection when disabled fails. | `audit:Tab/behavior` |
+| FR6, FR7 | `TabList.test.tsx` icon, icon-only, and pressed-overlay tests | Icon, selected icon, hidden label | Wrong icon, name, or overlay fails. | `audit:Tab/behavior` |
+| AV1 text size | `TabListTextSize.interaction.browser.spec.ts` at a 200% and 250% default font size | Default, size variants, with menu | A tab or tab strip that clips its label, or a label outside its tab, fails. | `audit:Tab/behavior` |
+| RTL relation | RTL audit curated D2 target (`apps/storybook/rtl-audit/targets.json`) on `TabList / With icons` | Tab icon and label; LTR and RTL | An icon that is not at the inline start of its label fails D2. | `audit:Tab/i18n-rtl` |
+| AR4 | `TabDescription.test.tsx` | A Tooltip anchored on a Tab with an info glyph in `endContent` | A tab without the tooltip's `aria-describedby`, a focusable element inside the tab, a tooltip inside the strip, or a development warning fails. | `audit:Tab/accessibility` |
Read from source, with no focused test yet:
diff --git a/packages/core/src/TabList/Tab.doc.mjs b/packages/core/src/TabList/Tab.doc.mjs
index 13243201e6..4e122b6d1e 100644
--- a/packages/core/src/TabList/Tab.doc.mjs
+++ b/packages/core/src/TabList/Tab.doc.mjs
@@ -14,6 +14,48 @@ export const docs = {
defaults: {value: 'tab-1', label: 'Tab'},
wrapper: {component: 'TabList', props: {value: 'tab-1'}},
},
+ examples: [
+ {
+ label: 'Describe a tab with help text',
+ code: `import {useRef, useState} from 'react';
+import {Icon} from '@astryxdesign/core/Icon';
+import {Tab, TabList} from '@astryxdesign/core/TabList';
+import {Tooltip} from '@astryxdesign/core/Tooltip';
+
+// The tab is one button, so its help text is a Tooltip anchored on the Tab
+// through its ref, not a second button inside it. The tooltip describes the
+// tab itself. Render it after the TabList: the strip holds only tabs. The
+// info glyph in endContent is decorative.
+function SettingsTabs() {
+ const [value, setValue] = useState('general');
+ const generalRef = useRef(null);
+ return (
+ <>
+ <TabList value={value} onChange={setValue} role="tablist" aria-label="Settings">
+ <Tab
+ ref={generalRef}
+ value="general"
+ label="General"
+ panelId="panel-general"
+ endContent={<Icon icon="info" size="sm" />}
+ />
+ <Tab value="advanced" label="Advanced" panelId="panel-advanced" />
+ </TabList>
+ <Tooltip
+ content="Name, language, and time zone for this workspace."
+ anchorRef={generalRef}
+ />
+ <div id="panel-general" role="tabpanel" hidden={value !== 'general'}>
+ …
+ </div>
+ <div id="panel-advanced" role="tabpanel" hidden={value !== 'advanced'}>
+ …
+ </div>
+ </>
+ );
+}`,
+ },
+ ],
props: [
{
name: 'value',
@@ -87,7 +129,7 @@ export const docs = {
name: 'endContent',
type: 'ReactNode',
description:
- 'Content rendered after the label, such as a badge count or status dot.',
+ 'Content rendered after the label, such as a badge count or status dot. The tab is one button, so endContent holds only non-interactive content: no button, link, InfoTip, or other focusable control. To explain a tab, anchor a Tooltip on the Tab through its ref, render it after the TabList, and put a decorative info Icon here.',
slotElements: [
{
__element: 'Icon',
@@ -169,7 +211,8 @@ export const docsZh = {
{
name: 'endContent',
type: 'ReactNode',
- description: '在标签文本之后渲染的内容,例如徽章计数或状态点。',
+ description:
+ '在标签文本之后渲染的内容,例如徽章计数或状态点。标签本身是一个按钮,因此 endContent 只能放非交互内容:不能放按钮、链接、InfoTip 或其他可聚焦控件。要说明一个标签,请通过 ref 把 Tooltip 锚定在 Tab 上,把它渲染在 TabList 之后,并在这里放一个装饰性的信息 Icon。',
},
{
name: 'xstyle',
@@ -196,7 +239,8 @@ export const docsDense = {
as: 'Custom link component overriding LinkProvider; only w/ href.',
icon: 'Icon shown when not selected.',
selectedIcon: 'Icon shown when selected; falls back to icon.',
- endContent: 'Content after the label (badge, status dot, etc.).',
+ endContent:
+ 'Non-interactive content after the label (badge, status dot). No focusable control: explain a tab with a Tooltip anchored on the Tab (anchorRef, rendered after TabList) + a decorative info Icon here.',
xstyle:
'StyleX styles for layout customization. Must be stylex.create() value, not inline style.',
},
diff --git a/packages/core/src/TabList/__tests__/TabDescription.test.tsx b/packages/core/src/TabList/__tests__/TabDescription.test.tsx
new file mode 100644
index 0000000000..e5f20c1d84
--- /dev/null
+++ b/packages/core/src/TabList/__tests__/TabDescription.test.tsx
@@ -0,0 +1,97 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file TabDescription.test.tsx
+ * @input Uses vitest, @testing-library/react, Tab, TabList, Tooltip and Icon
+ * @output Tests for Tab's documented description pattern: a Tooltip anchored on
+ * the Tab through its forwarded ref, rendered outside the strip, with only
+ * non-interactive end content
+ * @position Guards the pattern in Tab.doc.mjs and the TabList story
+ */
+
+import {useRef, useState} from 'react';
+import {afterEach, beforeEach, describe, it, expect, vi} from 'vitest';
+import {act, fireEvent, render, screen} from '@testing-library/react';
+import {TabList} from '../TabList';
+import {Tab} from '../Tab';
+import {Tooltip} from '../../Tooltip/Tooltip';
+import {Icon} from '../../Icon/Icon';
+
+function SettingsTabs() {
+ const [value, setValue] = useState('general');
+ const generalRef = useRef<HTMLButtonElement>(null);
+ return (
+ <>
+ <TabList
+ value={value}
+ onChange={setValue}
+ role="tablist"
+ aria-label="Settings">
+ <Tab
+ ref={generalRef}
+ value="general"
+ label="General"
+ panelId="panel-general"
+ endContent={<Icon icon="info" size="sm" />}
+ />
+ <Tab value="advanced" label="Advanced" panelId="panel-advanced" />
+ </TabList>
+ <Tooltip content="What this tab controls." anchorRef={generalRef} />
+ <div id="panel-general" role="tabpanel" hidden={value !== 'general'}>
+ General settings
+ </div>
+ <div id="panel-advanced" role="tabpanel" hidden={value !== 'advanced'}>
+ Advanced settings
+ </div>
+ </>
+ );
+}
+
+const FOCUSABLE =
+ 'button, a[href], input, select, textarea, [tabindex], [contenteditable="true"]';
+
+let warn: ReturnType<typeof vi.spyOn>;
+beforeEach(() => {
+ warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+});
+afterEach(() => {
+ warn.mockRestore();
+});
+
+describe('Tab description pattern', () => {
+ it('describes the tab itself through aria-describedby', async () => {
+ render(<SettingsTabs />);
+ const tab = screen.getByRole('tab', {name: 'General'});
+ const ids = (tab.getAttribute('aria-describedby') ?? '')
+ .split(/\s+/)
+ .filter(Boolean);
+ expect(ids.length).toBeGreaterThan(0);
+
+ act(() => {
+ fireEvent.focus(tab);
+ fireEvent.pointerEnter(tab, {pointerType: 'mouse'});
+ fireEvent.mouseEnter(tab);
+ });
+ const described = await screen.findByText('What this tab controls.');
+ expect(ids.some(id => described.closest(`[id="${id}"]`) != null)).toBe(
+ true,
+ );
+ });
+
+ it('keeps the tab one control: its end content holds nothing focusable', () => {
+ render(<SettingsTabs />);
+ const tab = screen.getByRole('tab', {name: 'General'});
+ expect(tab.querySelectorAll(FOCUSABLE)).toHaveLength(0);
+ // The glyph is decorative, so the tab's name stays its label.
+ expect(tab.querySelector('svg')?.closest('[aria-hidden="true"]')).not.toBe(
+ null,
+ );
+ });
+
+ it('leaves the strip holding only tabs, with no development warning', () => {
+ render(<SettingsTabs />);
+ const strip = screen.getByRole('tablist', {name: 'Settings'});
+ expect(strip.querySelector('[role="tooltip"], template')).toBeNull();
+ expect(warn).not.toHaveBeenCalled();
+ });
+});