From 189bc89d570a1a6e92bef0d78f42aff93d1c293d Mon Sep 17 00:00:00 2001 From: Maria Hutt Date: Tue, 1 Dec 2026 16:28:09 -0800 Subject: [PATCH] fix(tabs): keep tabs in place when one is selected --- e2e/tabs.e2e.ts | 23 +++++++++++++++++++++++ src/css/_mixins.scss | 2 +- src/theme/Tabs/index.tsx | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 e2e/tabs.e2e.ts create mode 100644 src/theme/Tabs/index.tsx diff --git a/e2e/tabs.e2e.ts b/e2e/tabs.e2e.ts new file mode 100644 index 0000000000..4e164c1e43 --- /dev/null +++ b/e2e/tabs.e2e.ts @@ -0,0 +1,23 @@ +import { expect, test, type Locator } from '@playwright/test'; + +/** The left edge of each tab in a tab list. */ +const tabPositions = (tabList: Locator) => + tabList.getByRole('tab').evaluateAll((tabs) => tabs.map((tab) => tab.getBoundingClientRect().left)); + +test.describe('docs tabs', () => { + for (const { source, path } of [ + { source: 'tab items', path: 'deployment/app-store' }, + { source: 'a values prop', path: 'theming/dark-mode' }, + ]) { + test(`stay in place when one is selected, with labels from ${source}`, async ({ page }) => { + await page.goto(path); + await page.evaluate(() => document.fonts.ready); + const tabList = page.locator('ul.tabs:not(.playground__tabs)').first(); + const before = await tabPositions(tabList); + + await tabList.getByRole('tab').last().click(); + await expect(tabList.getByRole('tab').last()).toHaveAttribute('aria-selected', 'true'); + expect(await tabPositions(tabList)).toEqual(before); + }); + } +}); diff --git a/src/css/_mixins.scss b/src/css/_mixins.scss index bebd2732d6..518aed6789 100644 --- a/src/css/_mixins.scss +++ b/src/css/_mixins.scss @@ -57,7 +57,7 @@ // Reserves the width of the bold selected label, so selecting one doesn't // shift the ones after it. Reads the label from the `data-text` attribute - // `ToggleButton` sets. Docusaurus's tabs have none, so theirs can still shift. + // that `ToggleButton` and the `Tabs` wrapper in `src/theme` set. &::after { content: attr(data-text); font-weight: 500; diff --git a/src/theme/Tabs/index.tsx b/src/theme/Tabs/index.tsx new file mode 100644 index 0000000000..0980937f6c --- /dev/null +++ b/src/theme/Tabs/index.tsx @@ -0,0 +1,40 @@ +/** + * This file wraps the original Tabs so we don't need to modify the original code. + * + * Reason for modifying: + * - Give each tab its label in a `data-text` attribute, as `ToggleButton` does. + * The `toggle-button` mixin reads it to reserve the width of the bold selected + * label, so selecting a tab doesn't shift the ones after it. + */ + +import React, { Children, cloneElement, isValidElement, type ReactNode } from 'react'; +import Tabs from '@theme-original/Tabs'; +import type { Props } from '@theme/Tabs'; +import type { Props as TabItemProps } from '@theme/TabItem'; + +interface Tab { + value: string; + label?: string; + attributes?: { [key: string]: unknown }; +} + +/** The tab's attributes plus `data-text`, set to the text Docusaurus shows on the tab. */ +function withDataText({ value, label, attributes }: Tab) { + return { 'data-text': label ?? value, ...attributes }; +} + +/* + * Docusaurus reads each tab's attributes from the `values` prop when a page + * passes one, and from the `TabItem` children otherwise, so both get them. + */ +export default function TabsWrapper({ values, children, ...props }: Props): ReactNode { + return ( + ({ ...tab, attributes: withDataText(tab) }))}> + {Children.map(children, (child) => + isValidElement(child) && 'value' in child.props + ? cloneElement(child, { attributes: withDataText(child.props) }) + : child + )} + + ); +}