From 1ed159ea09810154be6ba8d0958c8dfc65a6e144 Mon Sep 17 00:00:00 2001 From: Maria Hutt Date: Tue, 24 Nov 2026 17:31:13 -0800 Subject: [PATCH 1/3] refactor(playground): use Docusaurus Tabs for the file tabs --- .../global/PlaygroundTabs/TabItem.tsx | 35 --- .../global/PlaygroundTabs/index.tsx | 264 ------------------ .../PlaygroundTabs/playgroundTabs.types.ts | 20 -- .../global/PlaygroundTabs/styles.module.css | 67 ----- 4 files changed, 386 deletions(-) delete mode 100644 src/components/global/PlaygroundTabs/TabItem.tsx delete mode 100644 src/components/global/PlaygroundTabs/index.tsx delete mode 100644 src/components/global/PlaygroundTabs/playgroundTabs.types.ts delete mode 100644 src/components/global/PlaygroundTabs/styles.module.css diff --git a/src/components/global/PlaygroundTabs/TabItem.tsx b/src/components/global/PlaygroundTabs/TabItem.tsx deleted file mode 100644 index 88fe23723e3..00000000000 --- a/src/components/global/PlaygroundTabs/TabItem.tsx +++ /dev/null @@ -1,35 +0,0 @@ -/** - * A single tab panel within PlaygroundTabs. - * - * PlaygroundTabs decides which panel is visible by cloning these with a - * className, so this renders the panel and nothing else. Use it in place of - * `@theme/TabItem`, which only works inside Docusaurus' own Tabs. - */ - -import React, { type ReactNode } from 'react'; -import clsx from 'clsx'; - -import styles from './styles.module.css'; - -export interface TabItemProps { - /** Identifies the tab. Must be unique among sibling tabs. */ - value: string; - /** Text shown in the tab. Falls back to `value`. */ - label?: string; - /** Rendered before the label in the tab. */ - icon?: ReactNode; - /** Selects this tab when nothing else is selected. */ - default?: boolean; - /** Extra attributes spread onto the tab itself. */ - attributes?: { [key: string]: unknown }; - className?: string; - children?: ReactNode; -} - -export default function TabItem({ children, className }: TabItemProps): ReactNode { - return ( -
- {children} -
- ); -} diff --git a/src/components/global/PlaygroundTabs/index.tsx b/src/components/global/PlaygroundTabs/index.tsx deleted file mode 100644 index f2667b4035b..00000000000 --- a/src/components/global/PlaygroundTabs/index.tsx +++ /dev/null @@ -1,264 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -import React, { - useState, - cloneElement, - isValidElement, - type ReactElement, - type ReactNode, - useRef, - createRef, - useEffect, -} from 'react'; -import useIsBrowser from '@docusaurus/useIsBrowser'; -import { duplicates } from '@docusaurus/theme-common'; -import { useScrollPositionBlocker } from '@docusaurus/theme-common/internal'; -import type { Props } from '@theme/Tabs'; -import type { TabItemProps } from './TabItem'; -import type { PlaygroundTabValue } from './playgroundTabs.types'; - -import clsx from 'clsx'; - -import styles from './styles.module.css'; - -// A very rough duck type, but good enough to guard against mistakes while -// allowing customization -function isTabItem(comp: ReactElement): comp is ReactElement { - const { props } = comp; - return !!props && typeof props === 'object' && 'value' in props; -} - -function TabsComponent(props: Props): ReactNode { - const { lazy, block, defaultValue: defaultValueProp, values: valuesProp, groupId, className } = props; - const [leftNavVisible, setLeftNavVisible] = useState(false); - const [rightNavVisible, setRightNavVisible] = useState(false); - const tabsNavEl = createRef(); - const children = - React.Children.map(props.children, (child) => { - if (isValidElement(child) && isTabItem(child)) { - return child; - } - // child.type.name will give non-sensical values in prod because of - // minification, but we assume it won't throw in prod. - throw new Error( - `Docusaurus error: Bad child <${ - // @ts-expect-error: guarding against unexpected cases - typeof child.type === 'string' ? child.type : child.type.name - }>: all children of the component should be , and every should have a unique "value" prop.` - ); - }) ?? []; - const values: readonly PlaygroundTabValue[] = - valuesProp ?? - // Only pick keys that we recognize. MDX would inject some keys by default - children.map(({ props: { value, label, icon, attributes } }) => ({ - value, - label, - attributes, - icon, - })); - const dup = duplicates(values, (a, b) => a.value === b.value); - if (dup.length > 0) { - throw new Error( - `Docusaurus error: Duplicate values "${dup - .map((a) => a.value) - .join(', ')}" found in . Every value needs to be unique.` - ); - } - // When defaultValueProp is null, don't show a default tab - const defaultValue = - defaultValueProp === null - ? defaultValueProp - : (defaultValueProp ?? children.find((child) => child.props.default)?.props.value ?? children[0]?.props.value); - if (defaultValue !== null && !values.some((a) => a.value === defaultValue)) { - throw new Error( - `Docusaurus error: The has a defaultValue "${defaultValue}" but none of its children has the corresponding value. Available values are: ${values - .map((a) => a.value) - .join(', ')}. If you intend to show no default tab, use defaultValue={null} instead.` - ); - } - - const [selectedValue, setSelectedValue] = useState(defaultValue); - const tabRefs: (HTMLLIElement | null)[] = []; - const { blockElementScrollPositionUntilNextRender } = useScrollPositionBlocker(); - - const handleTabChange = (event: React.FocusEvent | React.MouseEvent) => { - const newTab = event.currentTarget; - const newTabIndex = tabRefs.indexOf(newTab); - const newTabValue = values[newTabIndex]!.value; - - if (newTabValue !== selectedValue) { - blockElementScrollPositionUntilNextRender(newTab); - setSelectedValue(newTabValue); - } - }; - - const handleKeydown = (event: React.KeyboardEvent) => { - let focusElement: HTMLLIElement | null = null; - - switch (event.key) { - case 'ArrowRight': { - const nextTab = tabRefs.indexOf(event.currentTarget) + 1; - focusElement = tabRefs[nextTab] || tabRefs[0]!; - break; - } - case 'ArrowLeft': { - const prevTab = tabRefs.indexOf(event.currentTarget) - 1; - focusElement = tabRefs[prevTab] || tabRefs[tabRefs.length - 1]!; - break; - } - default: - break; - } - - focusElement?.focus(); - }; - - useEffect(() => { - setLeftNavVisible((tabsNavEl.current?.scrollLeft ?? 0) > 40); - setRightNavVisible((tabsNavEl.current?.scrollWidth ?? 0) > (tabsNavEl.current?.offsetWidth ?? 0)); - }, [groupId]); - - /** - * If the selected value is not in the available tabs, fall back to the first tab. - * This can happen if the tab children are changed after the initial render. - * - * Note that actually updating selectedValue (for example, when defaultValue or the - * children are changed) would defer the fallback selection to the next render cycle, - * adding flicker. - */ - const useFallback = values.find((item) => item.value === selectedValue) === undefined; - const isTabSelected = (value: string) => (useFallback ? value === values[0].value : value === selectedValue); - - return ( -
-
-
    { - setLeftNavVisible((tabsNavEl.current?.scrollLeft ?? 0) > 40); - setRightNavVisible((tabsNavEl.current?.scrollWidth ?? 0) > (tabsNavEl.current?.offsetWidth ?? 0)); - }} - > - {leftNavVisible && ( -
    - -
    - )} - {values.map(({ value, label, icon, attributes }) => { - const isSelected = isTabSelected(value); - return ( -
  • { - tabRefs.push(tabControl); - }} - onKeyDown={handleKeydown} - onFocus={handleTabChange} - onClick={handleTabChange} - {...attributes} - className={clsx('tabs__item', styles.tabItem, attributes?.className as string, { - 'tabs__item--active': isSelected, - })} - > - {/* Ionic extended portion to add icon support to tab items */} - {icon && {icon}} - {label ?? value} -
  • - ); - })} - {rightNavVisible && ( -
    - -
    - )} -
-
- - {lazy ? ( - cloneElement(children.filter((tabItem) => isTabSelected(tabItem.props.value))[0]!, { - className: 'margin-top--md', - }) - ) : ( -
- {children.map((tabItem, i) => - cloneElement(tabItem, { - key: i, - className: `${tabItem.props.className} ${!isTabSelected(tabItem.props.value) ? 'hidden' : ''}`, - }) - )} -
- )} -
- ); -} - -export default function PlaygroundTabs(props: Props): ReactNode { - const isBrowser = useIsBrowser(); - return ( - - ); -} diff --git a/src/components/global/PlaygroundTabs/playgroundTabs.types.ts b/src/components/global/PlaygroundTabs/playgroundTabs.types.ts deleted file mode 100644 index 33c7ad04ac5..00000000000 --- a/src/components/global/PlaygroundTabs/playgroundTabs.types.ts +++ /dev/null @@ -1,20 +0,0 @@ -import type { ReactNode } from 'react'; - -/** - * What each entry of `values` holds, whether it arrives as a prop or is derived from - * the `` children. - * - * Annotating `values` with this keeps it a single array type. Left to inference, - * `valuesProp ?? children.map(...)` is a union of two array types, and calling `.map()` - * on a union intersects both callback signatures, which no single callback satisfies - * under `strictFunctionTypes`. - * - * Declared here rather than reusing Docusaurus's `TabValue`, which is only reachable - * through `@docusaurus/theme-common/internal`. - */ -export type PlaygroundTabValue = { - value: string; - label?: string; - attributes?: { [key: string]: unknown }; - icon?: ReactNode; -}; diff --git a/src/components/global/PlaygroundTabs/styles.module.css b/src/components/global/PlaygroundTabs/styles.module.css deleted file mode 100644 index d95477e8f57..00000000000 --- a/src/components/global/PlaygroundTabs/styles.module.css +++ /dev/null @@ -1,67 +0,0 @@ -/* Dark theme overrides */ -[data-theme="dark"] .tabNav { - --tab-nav-btn-background: var(--c-carbon-90); - --tab-nav-btn-color: var(--c-carbon-20); -} - -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -.tabNav { - position: relative; - - /** - * @prop --tab-nav-btn-background: The background color of the next/prev arrow buttons - * @prop --tab-nav-btn-color: The text color of the next/prev arrow buttons. - */ - --tab-nav-btn-background: linear-gradient(90deg, rgba(233, 237, 243, 0.9), var(--c-indigo-30)); - --tab-nav-btn-color: var(--c-carbon-100); -} - -.tabList { - margin-bottom: var(--ifm-leading); -} - -.tabItem { - margin-top: 0 !important; -} - -.tabPanel > *:last-child { - margin-bottom: 0; -} - -.tabIcon { - display: inline-flex; - padding: 0 8px; -} - -.tabNavItem { - position: sticky; - /* Places the nav item above the tab item */ - z-index: 10; - top: 0; - right: 0; - height: 40px; - width: 32px; - display: flex; - align-items: center; - justify-content: flex-end; - background: var(--tab-nav-btn-background); -} - -.tabNavItemLeft { - left: 0; - right: initial; -} - -.tabNavButton { - appearance: none; - background: transparent; - border: 0; - cursor: pointer; - color: var(--tab-nav-btn-color); -} From 6715b3fe2818cd1aea48122518ab83caeb739c5b Mon Sep 17 00:00:00 2001 From: Maria Hutt Date: Tue, 24 Nov 2026 17:31:13 -0800 Subject: [PATCH 2/3] style(tabs): use one focus ring for every tab and toggle button --- src/components/global/ToggleButton/styles.module.scss | 4 ++++ src/css/_mixins.scss | 7 +++++++ src/css/theme/Tabs.scss | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/src/components/global/ToggleButton/styles.module.scss b/src/components/global/ToggleButton/styles.module.scss index 026aa2cb78b..677c9581704 100644 --- a/src/components/global/ToggleButton/styles.module.scss +++ b/src/components/global/ToggleButton/styles.module.scss @@ -4,6 +4,10 @@ @include mixins.toggle-button(':global(html[data-theme="dark"])'); } +.button:focus-visible { + @include mixins.focus-ring; +} + .button:disabled { opacity: 0.4; cursor: not-allowed; diff --git a/src/css/_mixins.scss b/src/css/_mixins.scss index 3a6877b0d3a..d36a54565a6 100644 --- a/src/css/_mixins.scss +++ b/src/css/_mixins.scss @@ -80,3 +80,10 @@ color: var(--toggle-button-selected-color); font-weight: 500; } + +/// The keyboard focus ring on every tab and toggle button. It sits inside the +/// element, because tab lists scroll sideways and would clip a ring outside it. +@mixin focus-ring { + outline: 2px solid var(--ifm-link-color); + outline-offset: -2px; +} diff --git a/src/css/theme/Tabs.scss b/src/css/theme/Tabs.scss index 8cdd5521e99..68c56e2a9af 100644 --- a/src/css/theme/Tabs.scss +++ b/src/css/theme/Tabs.scss @@ -33,6 +33,11 @@ html[data-theme='dark'] { } } +// Every tab, including the playground's file tabs, shows the same focus ring. +.tabs__item:focus-visible { + @include mixins.focus-ring; +} + .tabs-container > div { margin-block-start: 0.75rem !important; } From 420418e26a33e6fefdf5db8a7567772624498599 Mon Sep 17 00:00:00 2001 From: Maria Hutt Date: Tue, 24 Nov 2026 17:33:01 -0800 Subject: [PATCH 3/3] refactor(playground): render the file tabs with Docusaurus Tabs --- .../global/Playground/icons/IconCss.tsx | 28 ------------ .../global/Playground/icons/IconDefault.tsx | 7 --- .../global/Playground/icons/IconHtml.tsx | 20 --------- .../global/Playground/icons/IconTs.tsx | 12 ------ .../global/Playground/icons/IconVue.tsx | 14 ------ .../global/Playground/icons/file-css.svg | 24 +++++++++++ .../global/Playground/icons/file-default.svg | 3 ++ .../global/Playground/icons/file-html.svg | 16 +++++++ .../global/Playground/icons/file-ts.svg | 8 ++++ .../global/Playground/icons/file-vue.svg | 10 +++++ .../global/Playground/icons/index.ts | 5 --- src/components/global/Playground/index.tsx | 35 +++++++++------ .../global/Playground/playground.css | 43 ++++++++++++++++--- 13 files changed, 118 insertions(+), 107 deletions(-) delete mode 100644 src/components/global/Playground/icons/IconCss.tsx delete mode 100644 src/components/global/Playground/icons/IconDefault.tsx delete mode 100644 src/components/global/Playground/icons/IconHtml.tsx delete mode 100644 src/components/global/Playground/icons/IconTs.tsx delete mode 100644 src/components/global/Playground/icons/IconVue.tsx create mode 100644 src/components/global/Playground/icons/file-css.svg create mode 100644 src/components/global/Playground/icons/file-default.svg create mode 100644 src/components/global/Playground/icons/file-html.svg create mode 100644 src/components/global/Playground/icons/file-ts.svg create mode 100644 src/components/global/Playground/icons/file-vue.svg diff --git a/src/components/global/Playground/icons/IconCss.tsx b/src/components/global/Playground/icons/IconCss.tsx deleted file mode 100644 index b99980fc4c2..00000000000 --- a/src/components/global/Playground/icons/IconCss.tsx +++ /dev/null @@ -1,28 +0,0 @@ -import React from 'react'; - -export const IconCss = () => ( - - - - - - - - - -); diff --git a/src/components/global/Playground/icons/IconDefault.tsx b/src/components/global/Playground/icons/IconDefault.tsx deleted file mode 100644 index 8fac1f9c53b..00000000000 --- a/src/components/global/Playground/icons/IconDefault.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import React from 'react'; - -export const IconDefault = () => ( - - - -); diff --git a/src/components/global/Playground/icons/IconHtml.tsx b/src/components/global/Playground/icons/IconHtml.tsx deleted file mode 100644 index cb2d87168b3..00000000000 --- a/src/components/global/Playground/icons/IconHtml.tsx +++ /dev/null @@ -1,20 +0,0 @@ -import React from 'react'; - -export const IconHtml = () => ( - - - - -); diff --git a/src/components/global/Playground/icons/IconTs.tsx b/src/components/global/Playground/icons/IconTs.tsx deleted file mode 100644 index ec2aca67bc3..00000000000 --- a/src/components/global/Playground/icons/IconTs.tsx +++ /dev/null @@ -1,12 +0,0 @@ -import React from 'react'; - -export const IconTs = () => ( - - - -); diff --git a/src/components/global/Playground/icons/IconVue.tsx b/src/components/global/Playground/icons/IconVue.tsx deleted file mode 100644 index 6211180201d..00000000000 --- a/src/components/global/Playground/icons/IconVue.tsx +++ /dev/null @@ -1,14 +0,0 @@ -import React from 'react'; - -export const IconVue = () => ( - - - - -); diff --git a/src/components/global/Playground/icons/file-css.svg b/src/components/global/Playground/icons/file-css.svg new file mode 100644 index 00000000000..2ff1b332580 --- /dev/null +++ b/src/components/global/Playground/icons/file-css.svg @@ -0,0 +1,24 @@ + + + + + + + + + diff --git a/src/components/global/Playground/icons/file-default.svg b/src/components/global/Playground/icons/file-default.svg new file mode 100644 index 00000000000..a2c2c79a5b9 --- /dev/null +++ b/src/components/global/Playground/icons/file-default.svg @@ -0,0 +1,3 @@ + + + diff --git a/src/components/global/Playground/icons/file-html.svg b/src/components/global/Playground/icons/file-html.svg new file mode 100644 index 00000000000..cdadf8783f5 --- /dev/null +++ b/src/components/global/Playground/icons/file-html.svg @@ -0,0 +1,16 @@ + + + + diff --git a/src/components/global/Playground/icons/file-ts.svg b/src/components/global/Playground/icons/file-ts.svg new file mode 100644 index 00000000000..d1242039106 --- /dev/null +++ b/src/components/global/Playground/icons/file-ts.svg @@ -0,0 +1,8 @@ + + + diff --git a/src/components/global/Playground/icons/file-vue.svg b/src/components/global/Playground/icons/file-vue.svg new file mode 100644 index 00000000000..043479a4efc --- /dev/null +++ b/src/components/global/Playground/icons/file-vue.svg @@ -0,0 +1,10 @@ + + + + diff --git a/src/components/global/Playground/icons/index.ts b/src/components/global/Playground/icons/index.ts index 456d3fedabc..e6a561bff7e 100644 --- a/src/components/global/Playground/icons/index.ts +++ b/src/components/global/Playground/icons/index.ts @@ -1,6 +1 @@ -export * from './IconHtml'; -export * from './IconTs'; -export * from './IconVue'; -export * from './IconDefault'; -export * from './IconCss'; export * from './IconDots'; diff --git a/src/components/global/Playground/index.tsx b/src/components/global/Playground/index.tsx index 763f854a101..c69f0f67512 100644 --- a/src/components/global/Playground/index.tsx +++ b/src/components/global/Playground/index.tsx @@ -18,7 +18,7 @@ * For the props and how to write a playground, refer to the README. */ -import React, { type ReactElement, type ReactNode, useEffect, useRef, useState } from 'react'; +import React, { type FocusEvent, type ReactElement, type ReactNode, useEffect, useRef, useState } from 'react'; import Link from '@docusaurus/Link'; import useBaseUrl from '@docusaurus/useBaseUrl'; @@ -31,10 +31,10 @@ import { FRAMEWORKS, useFrameworkChoice } from '@site/src/utils/frameworks'; import Tooltip from '../Tooltip'; import ToggleButton from '../ToggleButton'; import FrameworkSwitcher from '../FrameworkSwitcher'; -import PlaygroundTabs from '../PlaygroundTabs'; -import TabItem from '../PlaygroundTabs/TabItem'; +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; -import { IconHtml, IconTs, IconVue, IconDefault, IconCss, IconDots } from './icons'; +import { IconDots } from './icons'; import useIsBrowser from '@docusaurus/useIsBrowser'; @@ -567,20 +567,22 @@ export default function Playground({ return `playground_${usageTarget}_${fileNameId}`; } - function getFileIcon(fileName: string) { + /** + * The file type that picks a tab's icon in `playground.css`. Docusaurus tab + * labels are text only, so the icon is drawn by CSS rather than rendered here. + */ + function getFileType(fileName: string) { const extension = fileName.slice(fileName.lastIndexOf('.') + 1); switch (extension) { case 'ts': case 'tsx': - return ; + return 'ts'; case 'html': - return ; case 'vue': - return ; case 'css': - return ; + return extension; default: - return ; + return undefined; } } @@ -600,19 +602,24 @@ export default function Playground({ } const fileSnippets = snippets as Record; return ( - + // Each framework has its own files, so remounting on a switch selects its first file. + {Object.keys(fileSnippets).map((fileName) => ( ) => + event.currentTarget.scrollIntoView({ block: 'nearest', inline: 'nearest' }), + }} >
{fileSnippets[fileName]}
))} -
+
); } } diff --git a/src/components/global/Playground/playground.css b/src/components/global/Playground/playground.css index 8e8b6c3c132..2fb9307e0c4 100644 --- a/src/components/global/Playground/playground.css +++ b/src/components/global/Playground/playground.css @@ -282,10 +282,22 @@ */ border-radius: 16px 16px 0px 0px; transform: translateZ(0); + + /* Keeps a tab the keyboard scrolls to clear of the rounded corners, so its focus ring isn't cut off. */ + scroll-padding-inline: 16px; +} + +/* + * The first and last tabs can sit under the bar's rounded corners, which would cut + * off their focus ring. The first one takes the corner's shape, and the last one + * leaves room after it so it never reaches the corner. + */ +.playground .tabs__item:first-child { + border-top-left-radius: 16px; } -.playground .tabs-nav { - margin-block-start: 0px !important; +.playground .tabs__item:last-child { + margin-right: 16px; } .playground .tabs__item { @@ -312,13 +324,30 @@ opacity: 0.7; } -.playground .tabs__item:last-child { - margin-right: 40px; +/* The file's icon, picked by the `data-file-type` that `getFileType` sets. */ +.playground .tabs__item::before { + content: ''; + flex-shrink: 0; + width: 12px; + height: 12px; + margin: 0 8px; + background: url('./icons/file-default.svg') center / contain no-repeat; } -.playground__tab-item.hidden { - height: 0px; - overflow: hidden; +.playground .tabs__item[data-file-type='css']::before { + background-image: url('./icons/file-css.svg'); +} + +.playground .tabs__item[data-file-type='html']::before { + background-image: url('./icons/file-html.svg'); +} + +.playground .tabs__item[data-file-type='ts']::before { + background-image: url('./icons/file-ts.svg'); +} + +.playground .tabs__item[data-file-type='vue']::before { + background-image: url('./icons/file-vue.svg'); } .playground .tabs__item:not(.tabs__item--active) {