-
Notifications
You must be signed in to change notification settings - Fork 7.8k
[beta] upgrade sandpack and refactor sandpack.css #4843
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
Changes from all commits
f874531
7d8f311
cdafd74
dbc0794
53f8055
6fc22f3
5600c8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,20 +8,17 @@ import { | |
| useSandpack, | ||
| useActiveCode, | ||
| SandpackCodeEditor, | ||
| SandpackThemeProvider, | ||
| SandpackReactDevTools, | ||
| // SandpackReactDevTools, | ||
| SandpackLayout, | ||
| } from '@codesandbox/sandpack-react'; | ||
| import scrollIntoView from 'scroll-into-view-if-needed'; | ||
| import cn from 'classnames'; | ||
|
|
||
| import {IconChevron} from 'components/Icon/IconChevron'; | ||
| import {NavigationBar} from './NavigationBar'; | ||
| import {Preview} from './Preview'; | ||
| import {CustomTheme} from './Themes'; | ||
| import {useSandpackLint} from './useSandpackLint'; | ||
|
|
||
| // Workaround for https://github.com/reactjs/reactjs.org/issues/4686#issuecomment-1137402613. | ||
| const emptyArray: Array<any> = []; | ||
| import {useSandpackLint} from './useSandpackLint'; | ||
|
|
||
| export function CustomPreset({ | ||
| isSingleFile, | ||
|
|
@@ -41,11 +38,11 @@ export function CustomPreset({ | |
| const {code} = useActiveCode(); | ||
| const [isExpanded, setIsExpanded] = React.useState(false); | ||
|
|
||
| const {activePath} = sandpack; | ||
| if (!lineCountRef.current[activePath]) { | ||
| lineCountRef.current[activePath] = code.split('\n').length; | ||
| const {activeFile} = sandpack; | ||
| if (!lineCountRef.current[activeFile]) { | ||
| lineCountRef.current[activeFile] = code.split('\n').length; | ||
| } | ||
| const lineCount = lineCountRef.current[activePath]; | ||
| const lineCount = lineCountRef.current[activeFile]; | ||
| const isExpandable = lineCount > 16 || isExpanded; | ||
|
|
||
| return ( | ||
|
|
@@ -54,59 +51,57 @@ export function CustomPreset({ | |
| className="shadow-lg dark:shadow-lg-dark rounded-lg" | ||
| ref={containerRef}> | ||
| <NavigationBar showDownload={isSingleFile} /> | ||
| <SandpackThemeProvider theme={CustomTheme}> | ||
| <div | ||
| ref={sandpack.lazyAnchorRef} | ||
| className={cn( | ||
| 'sp-layout sp-custom-layout', | ||
| showDevTools && devToolsLoaded && 'sp-layout-devtools', | ||
| isExpanded && 'sp-layout-expanded' | ||
| )}> | ||
| <SandpackCodeEditor | ||
| showLineNumbers | ||
| showInlineErrors | ||
| showTabs={false} | ||
| showRunButton={false} | ||
| extensions={lintExtensions} | ||
| extensionsKeymap={emptyArray} | ||
| /> | ||
| <Preview | ||
| className="order-last xl:order-2" | ||
| isExpanded={isExpanded} | ||
| lintErrors={lintErrors} | ||
| /> | ||
| {isExpandable && ( | ||
| <button | ||
| translate="yes" | ||
| className="flex text-base justify-between dark:border-card-dark bg-wash dark:bg-card-dark items-center z-10 rounded-t-none p-1 w-full order-2 xl:order-last border-b-1 relative top-0" | ||
| onClick={() => { | ||
| const nextIsExpanded = !isExpanded; | ||
| flushSync(() => { | ||
| setIsExpanded(nextIsExpanded); | ||
| }); | ||
| if (!nextIsExpanded && containerRef.current !== null) { | ||
| scrollIntoView(containerRef.current, { | ||
| scrollMode: 'if-needed', | ||
| block: 'nearest', | ||
| inline: 'nearest', | ||
| }); | ||
| } | ||
| }}> | ||
| <span className="flex p-2 focus:outline-none text-primary dark:text-primary-dark"> | ||
| <IconChevron | ||
| className="inline mr-1.5 text-xl" | ||
| displayDirection={isExpanded ? 'up' : 'down'} | ||
| /> | ||
| {isExpanded ? 'Show less' : 'Show more'} | ||
| </span> | ||
| </button> | ||
| )} | ||
| </div> | ||
|
|
||
| {showDevTools && ( | ||
| <SandpackReactDevTools onLoadModule={onDevToolsLoad} /> | ||
| <SandpackLayout | ||
| className={cn( | ||
| showDevTools && devToolsLoaded && 'sp-layout-devtools', | ||
| !isExpandable && 'rounded-b-lg overflow-hidden', | ||
| isExpanded && 'sp-layout-expanded' | ||
| )}> | ||
| <SandpackCodeEditor | ||
| showLineNumbers | ||
| showInlineErrors | ||
| showTabs={false} | ||
| showRunButton={false} | ||
| extensions={lintExtensions} | ||
| /> | ||
| <Preview | ||
| className="order-last xl:order-2" | ||
| isExpanded={isExpanded} | ||
| lintErrors={lintErrors} | ||
| /> | ||
| {isExpandable && ( | ||
| <button | ||
| translate="yes" | ||
| className="sandpack-expand flex text-base justify-between dark:border-card-dark bg-wash dark:bg-card-dark items-center z-10 p-1 w-full order-2 xl:order-last border-b-1 relative top-0" | ||
| onClick={() => { | ||
| const nextIsExpanded = !isExpanded; | ||
| flushSync(() => { | ||
| setIsExpanded(nextIsExpanded); | ||
| }); | ||
| if (!nextIsExpanded && containerRef.current !== null) { | ||
| scrollIntoView(containerRef.current, { | ||
| scrollMode: 'if-needed', | ||
| block: 'nearest', | ||
| inline: 'nearest', | ||
| }); | ||
| } | ||
| }}> | ||
| <span className="flex p-2 focus:outline-none text-primary dark:text-primary-dark"> | ||
| <IconChevron | ||
| className="inline mr-1.5 text-xl" | ||
| displayDirection={isExpanded ? 'up' : 'down'} | ||
| /> | ||
| {isExpanded ? 'Show less' : 'Show more'} | ||
| </span> | ||
| </button> | ||
| )} | ||
| </SandpackThemeProvider> | ||
| </SandpackLayout> | ||
|
|
||
| {/* {showDevTools && ( | ||
| // @ts-ignore TODO(@danilowoz): support devtools | ||
|
||
| <SandpackReactDevTools onLoadModule={onDevToolsLoad} /> | ||
| )} */} | ||
| </div> | ||
| </> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,19 @@ | |
|
|
||
| /* eslint-disable react-hooks/exhaustive-deps */ | ||
| import * as React from 'react'; | ||
| import {useSandpack, LoadingOverlay} from '@codesandbox/sandpack-react'; | ||
| import { | ||
| useSandpack, | ||
| LoadingOverlay, | ||
| SandpackStack, | ||
| } from '@codesandbox/sandpack-react'; | ||
| import cn from 'classnames'; | ||
| import {Error} from './Error'; | ||
| import {SandpackConsole} from './Console'; | ||
| import type {LintDiagnostic} from './useSandpackLint'; | ||
|
|
||
| /** | ||
| * TODO: can we use React.useId? | ||
|
||
| */ | ||
| const generateRandomId = (): string => | ||
| Math.floor(Math.random() * 10000).toString(); | ||
|
|
||
|
|
@@ -149,16 +156,16 @@ export function Preview({ | |
| // The best way to test it is to actually go through some challenges. | ||
|
|
||
| return ( | ||
| <div | ||
| className={cn('sp-stack', className)} | ||
| <SandpackStack | ||
| className={className} | ||
| style={{ | ||
| // TODO: clean up this mess. | ||
| ...customStyle, | ||
| ...overrideStyle, | ||
| }}> | ||
| <div | ||
| className={cn( | ||
| 'p-0 sm:p-2 md:p-4 lg:p-8 md:bg-card md:dark:bg-wash-dark h-full relative md:rounded-b-lg lg:rounded-b-none', | ||
| 'p-0 sm:p-2 md:p-4 lg:p-8 bg-card dark:bg-wash-dark h-full relative md:rounded-b-lg lg:rounded-b-none', | ||
| // Allow content to be scrolled if it's too high to fit. | ||
| // Note we don't want this in the expanded state | ||
| // because it breaks position: sticky (and isn't needed anyway). | ||
|
|
@@ -172,7 +179,7 @@ export function Preview({ | |
| : isExpanded | ||
| ? 'sticky' | ||
| : undefined, | ||
| top: isExpanded ? '2rem' : undefined, | ||
| top: isExpanded ? '4rem' : undefined, | ||
| }}> | ||
| <iframe | ||
| ref={iframeRef} | ||
|
|
@@ -206,11 +213,12 @@ export function Preview({ | |
| </div> | ||
| )} | ||
| <LoadingOverlay | ||
| showOpenInCodeSandbox | ||
| clientId={clientId.current} | ||
| loading={!isReady && iframeComputedHeight === null} | ||
| /> | ||
| </div> | ||
| {!error && <SandpackConsole />} | ||
| </div> | ||
| </SandpackStack> | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SandpackLayoutpretty much does the same thing of the previous code