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