From 37b2dc481f3abf9d8a241116739d82b1b7d003ad Mon Sep 17 00:00:00 2001 From: Phil Ewels Date: Wed, 18 Nov 2026 09:48:48 +0100 Subject: [PATCH] code-tabs: keep the block in place when the tab changes Starlight gives each block after a sibling a top margin, also when the sibling is hidden, so every tab but the first moved the block down by 1rem. Fixes #26 Co-Authored-By: Claude Opus 5.5 --- docs/e2e/code-tabs.test.ts | 10 ++++++++++ .../src/expressive-code/code-tabs.ts | 2 ++ 2 files changed, 12 insertions(+) diff --git a/docs/e2e/code-tabs.test.ts b/docs/e2e/code-tabs.test.ts index e880cc3..3e8a397 100644 --- a/docs/e2e/code-tabs.test.ts +++ b/docs/e2e/code-tabs.test.ts @@ -26,6 +26,16 @@ test('a tab shows its file, with focus on its tab, and the copy button copies it await expect.poll(() => clipboard(page)).toContain('import sys'); }); +test('the block stays in place when the tab changes', async ({ page }) => { + await load(page); + const top = () => visible(page, 0).evaluate((block) => block.getBoundingClientRect().top + window.scrollY); + const first = await top(); + for (const name of ['greet.py', 'greet.js']) { + await visible(page, 0).getByRole('tab', { name }).click(); + expect(await top()).toBe(first); + } +}); + test('label tabs have no icon, and synced blocks switch together and remember the choice', async ({ page }) => { await load(page); await expect(visible(page, 1).getByRole('tab')).toHaveText(['npm', 'pnpm', 'Yarn']); diff --git a/packages/starlight-codeblocks/src/expressive-code/code-tabs.ts b/packages/starlight-codeblocks/src/expressive-code/code-tabs.ts index 04f9b87..7a503f8 100644 --- a/packages/starlight-codeblocks/src/expressive-code/code-tabs.ts +++ b/packages/starlight-codeblocks/src/expressive-code/code-tabs.ts @@ -46,6 +46,8 @@ export function pluginCodeTabs(fileIcons?: Partial): Codeblock return { name: 'starlight-codeblocks:code-tabs', baseStyles: ({ cssVar }) => ` +/* Starlight spaces each block after a sibling, even a hidden one, so the block would move when the tab changes. */ +[data-scb-code-tabs] > & { margin-top: 0; } .${FIELD} { position: relative; display: inline-flex;