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) {
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 (
-