Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions e2e/tabs.e2e.ts
Original file line number Diff line number Diff line change
@@ -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);
});
}
});
2 changes: 1 addition & 1 deletion src/css/_mixins.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions src/theme/Tabs/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Tabs {...props} values={values?.map((tab) => ({ ...tab, attributes: withDataText(tab) }))}>
{Children.map(children, (child) =>
isValidElement<TabItemProps>(child) && 'value' in child.props
? cloneElement(child, { attributes: withDataText(child.props) })
: child
)}
</Tabs>
);
}
Loading