Skip to content
Merged
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
6 changes: 6 additions & 0 deletions src/components/MDX/Sandpack/NavigationBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {ResetButton} from './ResetButton';
import {DownloadButton} from './DownloadButton';
import {IconChevron} from '../../Icon/IconChevron';
import {Listbox} from '@headlessui/react';
import {OpenInTypeScriptPlaygroundButton} from './OpenInTypeScriptPlayground';

export function useEvent(fn: any): any {
const ref = useRef(null);
Expand Down Expand Up @@ -182,6 +183,11 @@ export function NavigationBar({providedFiles}: {providedFiles: Array<string>}) {
<DownloadButton providedFiles={providedFiles} />
<ResetButton onReset={handleReset} />
<OpenInCodeSandboxButton />
{activeFile.endsWith('.tsx') && (
<OpenInTypeScriptPlaygroundButton
content={sandpack.files[activeFile]?.code || ''}
/>
)}
</div>
</div>
);
Expand Down
26 changes: 26 additions & 0 deletions src/components/MDX/Sandpack/OpenInTypeScriptPlayground.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*/

import {IconNewPage} from '../../Icon/IconNewPage';

export const OpenInTypeScriptPlaygroundButton = (props: {content: string}) => {
const contentWithReactImport = `import * as React from 'react';\n\n${props.content}`;
return (
<a
className="text-sm text-primary dark:text-primary-dark inline-flex items-center hover:text-link duration-100 ease-in transition mx-1 ml-2 md:ml-1"
href={`https://www.typescriptlang.org/play#src=${encodeURIComponent(
contentWithReactImport
)}`}
title="Open in TypeScript Playground"
target="_blank"
rel="noreferrer">
<IconNewPage
className="inline ml-1 mr-1 relative top-[1px]"
width="1em"
height="1em"
/>
<span className="hidden md:block">TypeScript Playground</span>
</a>
);
};
Loading