Skip to content
Open
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
28 changes: 0 additions & 28 deletions src/components/global/Playground/icons/IconCss.tsx

This file was deleted.

7 changes: 0 additions & 7 deletions src/components/global/Playground/icons/IconDefault.tsx

This file was deleted.

20 changes: 0 additions & 20 deletions src/components/global/Playground/icons/IconHtml.tsx

This file was deleted.

12 changes: 0 additions & 12 deletions src/components/global/Playground/icons/IconTs.tsx

This file was deleted.

14 changes: 0 additions & 14 deletions src/components/global/Playground/icons/IconVue.tsx

This file was deleted.

24 changes: 24 additions & 0 deletions src/components/global/Playground/icons/file-css.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/components/global/Playground/icons/file-default.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions src/components/global/Playground/icons/file-html.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions src/components/global/Playground/icons/file-ts.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/components/global/Playground/icons/file-vue.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 0 additions & 5 deletions src/components/global/Playground/icons/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
export * from './IconHtml';
export * from './IconTs';
export * from './IconVue';
export * from './IconDefault';
export * from './IconCss';
export * from './IconDots';
35 changes: 21 additions & 14 deletions src/components/global/Playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';

Expand Down Expand Up @@ -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 <IconTs />;
return 'ts';
case 'html':
return <IconHtml />;
case 'vue':
return <IconVue />;
case 'css':
return <IconCss />;
return extension;
default:
return <IconDefault />;
return undefined;
}
}

Expand All @@ -600,19 +602,24 @@ export default function Playground({
}
const fileSnippets = snippets as Record<string, ReactElement>;
return (
<PlaygroundTabs groupId={usageTarget} className="playground__tabs">
// Each framework has its own files, so remounting on a switch selects its first file.
<Tabs key={usageTarget} className="playground__tabs">

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switching to Docusaurus Tabs drops the scroll arrows, so overflowing file tabs now only scroll natively: a trackpad swipe, Shift and the mouse wheel, or arrow keys once a tab is focused. A mouse user without a horizontal wheel has no visible cue that more tabs are hidden. Should we add the arrows back, or is native scrolling enough? It only matters for playgrounds with many files, such as Angular examples on a narrow window.

{Object.keys(fileSnippets).map((fileName) => (
<TabItem
className="playground__tab-item"
value={fileName}
label={fileName}
key={fileName}
icon={getFileIcon(fileName)}
attributes={{
'data-file-type': getFileType(fileName),
// The browser only partly scrolls a focused tab into view, which can leave it cut off.
onFocus: (event: FocusEvent<HTMLLIElement>) =>
event.currentTarget.scrollIntoView({ block: 'nearest', inline: 'nearest' }),
}}
>
<div id={getCodeSnippetId(usageTarget, fileName)}>{fileSnippets[fileName]}</div>
</TabItem>
))}
</PlaygroundTabs>
</Tabs>
);
}
}
Expand Down
43 changes: 36 additions & 7 deletions src/components/global/Playground/playground.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
35 changes: 0 additions & 35 deletions src/components/global/PlaygroundTabs/TabItem.tsx

This file was deleted.

Loading
Loading