Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[beta] Sandpack - new bundler #4458

Merged
merged 13 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12.16.1
12.22.0
3 changes: 2 additions & 1 deletion beta/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"react": "experimental",
"react-collapsed": "3.1.0",
"react-dom": "experimental",
"scroll-into-view-if-needed": "^2.2.25"
"scroll-into-view-if-needed": "^2.2.25",
"use-timer": "^2.0.1"
},
"devDependencies": {
"@mdx-js/loader": "^1.6.16",
Expand Down
27 changes: 26 additions & 1 deletion beta/src/components/MDX/Sandpack/CustomPreset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,36 @@ import {
SandpackReactDevTools,
} from '@codesandbox/sandpack-react';
import scrollIntoView from 'scroll-into-view-if-needed';
import {useTimer} from 'use-timer';

import cn from 'classnames';

import {IconChevron} from 'components/Icon/IconChevron';
import {NavigationBar} from './NavigationBar';
import {Preview} from './Preview';
import {CustomTheme} from './Themes';
import {ReturnValue} from 'use-timer/lib/types';

/**
* Temp component: testing purposes only
*/
const Timer: React.FC<{timer: ReturnValue}> = ({timer}) => {
const {listen} = useSandpack();

React.useEffect(() => {
const unsub = listen((message) => {
if (message.type === 'start') {
timer.start();
} else if (message.type === 'done') {
timer.pause();
}
});

return () => unsub();
}, []);

return <>{timer.time}ms</>;
};

export function CustomPreset({
isSingleFile,
Expand All @@ -32,6 +55,7 @@ export function CustomPreset({
devToolsLoaded: boolean;
onDevToolsLoad: () => void;
}) {
const timer = useTimer({interval: 1});
const lineCountRef = React.useRef<{[key: string]: number}>({});
const containerRef = React.useRef<HTMLDivElement>(null);
const {sandpack} = useSandpack();
Expand All @@ -47,10 +71,11 @@ export function CustomPreset({

return (
<>
Time to load: <Timer timer={timer} />
<div
className="shadow-lg dark:shadow-lg-dark rounded-lg"
ref={containerRef}>
<NavigationBar showDownload={isSingleFile} />
<NavigationBar showDownload={isSingleFile} onReset={timer.reset} />
<SandpackThemeProvider theme={CustomTheme}>
<div
ref={sandpack.lazyAnchorRef}
Expand Down
9 changes: 8 additions & 1 deletion beta/src/components/MDX/Sandpack/NavigationBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import {ResetButton} from './ResetButton';
import {DownloadButton} from './DownloadButton';
import {FilesDropdown} from './FilesDropdown';

export function NavigationBar({showDownload}: {showDownload: boolean}) {
export function NavigationBar({
showDownload,
onReset,
}: {
showDownload: boolean;
onReset: () => void;
}) {
const {sandpack} = useSandpack();
const [dropdownActive, setDropdownActive] = React.useState(false);
const {openPaths, clients} = sandpack;
Expand Down Expand Up @@ -44,6 +50,7 @@ export function NavigationBar({showDownload}: {showDownload: boolean}) {
const handleReset = () => {
sandpack.resetAllFiles();
refresh();
onReset();
};

return (
Expand Down
30 changes: 30 additions & 0 deletions beta/src/components/MDX/Sandpack/NewBundlerToggle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {useEffect, useState} from 'react';

/**
* Temp component: testing purposes only
*/
export const NewBundlerToggle = () => {
const [state, setState] = useState(false);

useEffect(() => {
setState(window.localStorage.getItem('sandpack-new-bundler') === 'true');
}, []);

const onChange = () => {
const value = window.localStorage.getItem('sandpack-new-bundler');

window.localStorage.setItem(
'sandpack-new-bundler',
value === 'true' ? 'false' : 'true'
);

window.location.reload();
};

return (
<label>
<input onChange={onChange} checked={state} type="checkbox" /> Active new
bundler (for all pages)
</label>
);
};
7 changes: 6 additions & 1 deletion beta/src/components/MDX/Sandpack/SandpackRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ function SandpackRoot(props: SandpackProps) {
customSetup={{...setup, files: files}}
autorun={autorun}
initMode="user-visible"
initModeObserverOptions={{rootMargin: '1400px 0px'}}>
initModeObserverOptions={{rootMargin: '1400px 0px'}}
bundlerURL={
window.localStorage.getItem('sandpack-new-bundler') === 'true'
? 'https://cf56a978.sandpack-bundler.pages.dev'
: 'https://0-14-0-sandpack.codesandbox.io/'
}>
<CustomPreset
isSingleFile={isSingleFile}
showDevTools={showDevTools}
Expand Down
26 changes: 26 additions & 0 deletions beta/src/pages/sandpack.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {NewBundlerToggle} from '../components/MDX/Sandpack/NewBundlerToggle';

# New Sandpack bundler testing

<NewBundlerToggle />

<Sandpack>

```js
function Text() {
return <p>Lorem ipsum</p>;
}

export default function App() {
return (
<section>
<h1>Test</h1>
<Text />
<Text />
<Text />
</section>
);
}
```

</Sandpack>
5 changes: 5 additions & 0 deletions beta/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5044,6 +5044,11 @@ use-subscription@1.5.1:
dependencies:
object-assign "^4.1.1"

use-timer@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/use-timer/-/use-timer-2.0.1.tgz#eef400a83e3fa4af9896fdd6d07d34f676e9373c"
integrity sha512-ESFkDYZluJZ+G/CaoOBFxg0TjuG7kgNFNhIFZhqv14ho2WIZwuxkRnEURhyLm9I4vNz+ea9ZowNaOkExDAM+6Q==

util-deprecate@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
Expand Down