Browse Source

[Beta] Fix Download button (#5013)

main
dan 2 years ago
committed by GitHub
parent
commit
00358d7c4c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      beta/src/components/MDX/Sandpack/CustomPreset.tsx
  2. 31
      beta/src/components/MDX/Sandpack/DownloadButton.tsx
  3. 4
      beta/src/components/MDX/Sandpack/NavigationBar.tsx
  4. 6
      beta/src/components/MDX/Sandpack/SandpackRoot.tsx

6
beta/src/components/MDX/Sandpack/CustomPreset.tsx

@ -22,15 +22,15 @@ import {useSandpackLint} from './useSandpackLint';
const emptyArray: Array<any> = []; const emptyArray: Array<any> = [];
export function CustomPreset({ export function CustomPreset({
isSingleFile,
showDevTools, showDevTools,
onDevToolsLoad, onDevToolsLoad,
devToolsLoaded, devToolsLoaded,
providedFiles,
}: { }: {
isSingleFile: boolean;
showDevTools: boolean; showDevTools: boolean;
devToolsLoaded: boolean; devToolsLoaded: boolean;
onDevToolsLoad: () => void; onDevToolsLoad: () => void;
providedFiles: Array<string>;
}) { }) {
const {lintErrors, lintExtensions} = useSandpackLint(); const {lintErrors, lintExtensions} = useSandpackLint();
const lineCountRef = React.useRef<{[key: string]: number}>({}); const lineCountRef = React.useRef<{[key: string]: number}>({});
@ -51,7 +51,7 @@ export function CustomPreset({
<div <div
className="shadow-lg dark:shadow-lg-dark rounded-lg" className="shadow-lg dark:shadow-lg-dark rounded-lg"
ref={containerRef}> ref={containerRef}>
<NavigationBar showDownload={isSingleFile} /> <NavigationBar providedFiles={providedFiles} />
<SandpackThemeProvider theme={CustomTheme}> <SandpackThemeProvider theme={CustomTheme}>
<div <div
ref={sandpack.lazyAnchorRef} ref={sandpack.lazyAnchorRef}

31
beta/src/components/MDX/Sandpack/DownloadButton.tsx

@ -33,12 +33,21 @@ function useSupportsImportMap() {
); );
} }
export const DownloadButton: React.FC<DownloadButtonProps> = () => { const SUPPORTED_FILES = ['/App.js', '/styles.css'];
export function DownloadButton({
providedFiles,
}: {
providedFiles: Array<string>;
}) {
const {sandpack} = useSandpack(); const {sandpack} = useSandpack();
const supported = useSupportsImportMap(); const supported = useSupportsImportMap();
if (!supported) { if (!supported) {
return null; return null;
} }
if (providedFiles.some((file) => !SUPPORTED_FILES.includes(file))) {
return null;
}
const downloadHTML = () => { const downloadHTML = () => {
const css = sandpack.files['/styles.css']?.code ?? ''; const css = sandpack.files['/styles.css']?.code ?? '';
@ -55,20 +64,22 @@ export const DownloadButton: React.FC<DownloadButtonProps> = () => {
<script type="importmap"> <script type="importmap">
{ {
"imports": { "imports": {
"react": "https://cdn.skypack.dev/react", "react": "https://esm.sh/react?dev",
"react-dom": "https://cdn.skypack.dev/react-dom" "react-dom/client": "https://esm.sh/react-dom/client?dev"
} }
} }
</script> </script>
<script type="text/babel" data-type="module"> <script type="text/babel" data-type="module">
import * as React from 'react'; import React, { StrictMode } from 'react';
import * as ReactDOM from 'react-dom'; import { createRoot } from 'react-dom/client';
${code.replace('export default ', 'let Root = ')} ${code.replace('export default ', 'let App = ')}
ReactDOM.render( const root = createRoot(document.getElementById('root'));
<Root />, root.render(
document.getElementById('root') <StrictMode>
<App />
</StrictMode>
); );
</script> </script>
<style> <style>
@ -95,4 +106,4 @@ ${css}
<IconDownload className="inline mr-1" /> Download <IconDownload className="inline mr-1" /> Download
</button> </button>
); );
}; }

4
beta/src/components/MDX/Sandpack/NavigationBar.tsx

@ -13,7 +13,7 @@ import {ResetButton} from './ResetButton';
import {DownloadButton} from './DownloadButton'; import {DownloadButton} from './DownloadButton';
import {FilesDropdown} from './FilesDropdown'; import {FilesDropdown} from './FilesDropdown';
export function NavigationBar({showDownload}: {showDownload: boolean}) { export function NavigationBar({providedFiles}: {providedFiles: Array<string>}) {
const {sandpack} = useSandpack(); const {sandpack} = useSandpack();
const [dropdownActive, setDropdownActive] = React.useState(false); const [dropdownActive, setDropdownActive] = React.useState(false);
const {openPaths, clients} = sandpack; const {openPaths, clients} = sandpack;
@ -54,7 +54,7 @@ export function NavigationBar({showDownload}: {showDownload: boolean}) {
<div <div
className="px-3 flex items-center justify-end grow text-right" className="px-3 flex items-center justify-end grow text-right"
translate="yes"> translate="yes">
{showDownload && <DownloadButton />} <DownloadButton providedFiles={providedFiles} />
<ResetButton onReset={handleReset} /> <ResetButton onReset={handleReset} />
<OpenInCodeSandboxButton /> <OpenInCodeSandboxButton />
</div> </div>

6
beta/src/components/MDX/Sandpack/SandpackRoot.tsx

@ -66,9 +66,7 @@ ul {
function SandpackRoot(props: SandpackProps) { function SandpackRoot(props: SandpackProps) {
let {children, setup, autorun = true, showDevTools = false} = props; let {children, setup, autorun = true, showDevTools = false} = props;
const [devToolsLoaded, setDevToolsLoaded] = React.useState(false); const [devToolsLoaded, setDevToolsLoaded] = React.useState(false);
let codeSnippets = React.Children.toArray(children) as React.ReactElement[]; const codeSnippets = React.Children.toArray(children) as React.ReactElement[];
let isSingleFile = true;
const files = createFileMap(codeSnippets); const files = createFileMap(codeSnippets);
files['/styles.css'] = { files['/styles.css'] = {
@ -87,10 +85,10 @@ function SandpackRoot(props: SandpackProps) {
bundlerURL="https://6b760a26.sandpack-bundler.pages.dev" bundlerURL="https://6b760a26.sandpack-bundler.pages.dev"
logLevel={SandpackLogLevel.None}> logLevel={SandpackLogLevel.None}>
<CustomPreset <CustomPreset
isSingleFile={isSingleFile}
showDevTools={showDevTools} showDevTools={showDevTools}
onDevToolsLoad={() => setDevToolsLoaded(true)} onDevToolsLoad={() => setDevToolsLoaded(true)}
devToolsLoaded={devToolsLoaded} devToolsLoaded={devToolsLoaded}
providedFiles={Object.keys(files)}
/> />
</SandpackProvider> </SandpackProvider>
</div> </div>

Loading…
Cancel
Save