Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix <Tabs> sync issue with inconsistent use of icon on <TabItem> components #1811

Merged
merged 3 commits into from
May 6, 2024
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
5 changes: 5 additions & 0 deletions .changeset/blue-otters-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Fixes a `<Tabs>` sync issue when inconsistently using the `icon` prop or not on `<TabItem>` components.
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,20 @@ A set of tabs using the `style` sync key.
<TabItem label="css">css code</TabItem>
<TabItem label="tailwind">tailwind code</TabItem>
</Tabs>

Another set of tabs using the `pkg` sync key and using icons.

<Tabs syncKey="pkg">
<TabItem label="npm" icon="seti:npm">
another npm command
</TabItem>
<TabItem label="pnpm" icon="pnpm">
another pnpm command
</TabItem>
<TabItem label="bun" icon="bun">
another bun command
</TabItem>
<TabItem label="yarn" icon="seti:yarn">
another yarn command
</TabItem>
</Tabs>
23 changes: 23 additions & 0 deletions packages/starlight/__e2e__/tabs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,29 @@ test('preserves tabs position when alternating between tabs with different conte
expect((await tabs.boundingBox())?.y).toBe(initialBoundingBox?.y);
});

test('syncs tabs with the same sync key if they do not consistenly use icons', async ({
page,
starlight,
}) => {
await starlight.goto('/tabs');

const tabs = page.locator('starlight-tabs');
const pkgTabsA = tabs.nth(0); // This set does not use icons for tab items.
const pkgTabsB = tabs.nth(4); // This set uses icons for tab items.

// Select the pnpm tab in the first set of synced tabs.
await pkgTabsA.getByRole('tab').filter({ hasText: 'pnpm' }).click();

await expectSelectedTab(pkgTabsA, 'pnpm', 'pnpm command');
await expectSelectedTab(pkgTabsB, 'pnpm', 'another pnpm command');

// Select the yarn tab in the second set of synced tabs.
await pkgTabsB.getByRole('tab').filter({ hasText: 'yarn' }).click();

await expectSelectedTab(pkgTabsB, 'yarn', 'another yarn command');
await expectSelectedTab(pkgTabsA, 'yarn', 'yarn command');
});

async function expectSelectedTab(tabs: Locator, label: string, panel: string) {
expect((await tabs.getByRole('tab', { selected: true }).textContent())?.trim()).toBe(label);
expect((await tabs.getByRole('tabpanel').textContent())?.trim()).toBe(panel);
Expand Down
4 changes: 2 additions & 2 deletions packages/starlight/user-components/Tabs.astro
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const { html, panels } = processPanels(panelHtml);
newTab.setAttribute('aria-selected', 'true');
if (shouldSync) {
newTab.focus();
StarlightTabs.#syncTabs(this, newTab.textContent);
StarlightTabs.#syncTabs(this, newTab.innerText);
window.scrollTo({
top: window.scrollY + (this.getBoundingClientRect().top - previousTabsOffset),
});
Expand All @@ -173,7 +173,7 @@ const { html, panels } = processPanels(panelHtml);

for (const receiver of syncedTabs) {
if (receiver === emitter) continue;
const labelIndex = receiver.tabs.findIndex((tab) => tab.textContent === label);
const labelIndex = receiver.tabs.findIndex((tab) => tab.innerText === label);
if (labelIndex === -1) continue;
receiver.switchTab(receiver.tabs[labelIndex], labelIndex, false);
}
Expand Down
Loading