diff --git a/docusaurus.config.js b/docusaurus.config.js index 87c6df43d8..330720bd97 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -86,6 +86,20 @@ module.exports = { }, }, favicon: 'img/meta/favicon-96x96.png', + /** + * Copies the reader's saved framework onto `` before the page paints, so + * framework tabs and playgrounds can show it before the scripts load. The key + * is `STORAGE_KEY` in `src/utils/frameworks.ts`, and the styles that read the + * attribute are in `FrameworkSwitcher` and `FrameworkTabs`. + */ + headTags: [ + { + tagName: 'script', + attributes: {}, + innerHTML: + "try{var f=localStorage.getItem('docusaurus.tab.framework');f&&document.documentElement.setAttribute('data-saved-framework',f)}catch(e){}", + }, + ], presets: [ [ '@docusaurus/preset-classic', diff --git a/e2e/playground.e2e.ts b/e2e/playground.e2e.ts index a589595c4f..bacdc99c35 100644 --- a/e2e/playground.e2e.ts +++ b/e2e/playground.e2e.ts @@ -106,6 +106,26 @@ test.describe('framework choice', () => { await expect(frameworkTab(page.locator('.playground').first(), 'React')).toHaveAttribute('aria-selected', 'true'); }); + test('is shown before the page loads its scripts', async ({ page }) => { + await page.addInitScript(() => { + // Demo frames share the origin, so only the page itself saves the pick. + if (window === window.top) { + localStorage.setItem('docusaurus.tab.framework', 'react'); + } + }); + // Without its bundles, the page stays as the server rendered it. + await page.route('**/assets/js/**', (route) => route.abort()); + await page.goto('utilities/gestures'); + + // The selected look is the only cue before hydration, since `aria-selected` still names the server's default. + const docsTabs = docsFrameworkTabs(page).first(); + await expect(docsTabs.getByRole('tab', { name: 'React', exact: true })).toHaveCSS('font-weight', '500'); + await expect(docsTabs.getByRole('tab', { name: 'JavaScript', exact: true })).toHaveCSS('font-weight', '400'); + await expect(page.locator('[role=tabpanel][data-framework=react]').first()).toBeVisible(); + await expect(page.locator('[role=tabpanel][data-framework=javascript]').first()).toBeHidden(); + await expect(frameworkTab(page.locator('.playground').first(), 'React')).toHaveCSS('font-weight', '500'); + }); + test('disables a framework the playground has no code for, and skips it with the arrow keys', async ({ page }) => { const playground = await openChildStatePlayground(page); diff --git a/src/components/global/FrameworkSwitcher/index.tsx b/src/components/global/FrameworkSwitcher/index.tsx index da6b54c9a0..f99ea2caab 100644 --- a/src/components/global/FrameworkSwitcher/index.tsx +++ b/src/components/global/FrameworkSwitcher/index.tsx @@ -10,10 +10,11 @@ import React, { useRef, type KeyboardEvent } from 'react'; import clsx from 'clsx'; +import useIsBrowser from '@docusaurus/useIsBrowser'; import ToggleButton from '../ToggleButton'; -import styles from './styles.module.css'; +import styles from './styles.module.scss'; export interface FrameworkOption { value: string; @@ -30,6 +31,12 @@ interface Props { options: readonly FrameworkOption[]; selected: string; onSelect: (value: string) => void; + /** + * Whether `selected` is the reader's saved framework once the page's scripts + * read it. The server can't read it, so until the scripts load, the stylesheet + * shows the saved framework instead. + */ + followsSavedFramework?: boolean; className?: string; } @@ -48,8 +55,9 @@ function keepInPlace(element: HTMLElement | null, update: () => void) { } } -export default function FrameworkSwitcher({ options, selected, onSelect, className }: Props) { +export default function FrameworkSwitcher({ options, selected, onSelect, followsSavedFramework, className }: Props) { const buttons = useRef<(HTMLButtonElement | null)[]>([]); + const isBrowser = useIsBrowser(); // One tab must be reachable with the Tab key, even if none is selected. const focusable = options.some((option) => option.value === selected) @@ -84,7 +92,12 @@ export default function FrameworkSwitcher({ options, selected, onSelect, classNa }; return ( -
+
{options.map((option, index) => ( + /** + * The server can't read the reader's saved framework, so until the page's + * scripts load, `data-framework-pending` lets the stylesheet show it instead. + */ +
- +
{tabs.map((tab) => ( /** @@ -89,6 +95,7 @@ export default function FrameworkTabs({ children, defaultValue, className }: Pro