Skip to content

Commit f3420fd

Browse files
danilowozgaearon
andauthored
[beta] Sandpack: provide bare components and move styles (reactjs#5124)
* Update 6 files * Update _document.tsx * Update _document.tsx * Update _document.tsx * Update .env.production * Update package.json and yarn.lock * editor style * file tab * loading * loading * tweaks * tweak * bump sandpack * remove test page * Remove stitches from the bundle * apply ios fix only for playground Co-authored-by: Dan Abramov <[email protected]>
1 parent e6bc8db commit f3420fd

16 files changed

+281
-329
lines changed

beta/.env.development

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SANDPACK_BARE_COMPONENTS=true

beta/.env.production

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
NEXT_PUBLIC_GA_TRACKING_ID = 'UA-41298772-4'
1+
NEXT_PUBLIC_GA_TRACKING_ID = 'UA-41298772-4'
2+
SANDPACK_BARE_COMPONENTS=true

beta/next.config.js

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const nextConfig = {
1616
legacyBrowsers: false,
1717
browsersListForSwc: true,
1818
},
19+
env: {
20+
SANDPACK_BARE_COMPONENTS: process.env.SANDPACK_BARE_COMPONENTS,
21+
},
1922
async redirects() {
2023
return redirects.redirects;
2124
},
@@ -47,6 +50,10 @@ const nextConfig = {
4750

4851
const {IgnorePlugin, NormalModuleReplacementPlugin} = require('webpack');
4952
config.plugins.push(
53+
new NormalModuleReplacementPlugin(
54+
/^@stitches\/core$/,
55+
require.resolve('./src/utils/emptyShim.js')
56+
),
5057
new NormalModuleReplacementPlugin(
5158
/^raf$/,
5259
require.resolve('./src/utils/rafShim.js')

beta/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"check-all": "npm-run-all prettier lint:fix tsc"
2323
},
2424
"dependencies": {
25-
"@codesandbox/sandpack-react": "1.11.0",
25+
"@codesandbox/sandpack-react": "1.14.1",
2626
"@docsearch/css": "3.0.0-alpha.41",
2727
"@docsearch/react": "3.0.0-alpha.41",
2828
"@headlessui/react": "^1.7.0",

beta/src/components/MDX/ConsoleBlock.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function ConsoleBlock({level = 'error', children}: ConsoleBlockProps) {
3838
}
3939

4040
return (
41-
<div className="mb-4" translate="no">
41+
<div className="mb-4 text-secondary" translate="no">
4242
<div className="flex w-full rounded-t-lg bg-gray-200 dark:bg-gray-80">
4343
<div className="px-4 py-2 border-gray-300 dark:border-gray-90 border-r">
4444
<Box className="bg-gray-300 dark:bg-gray-90" width="15px" />

beta/src/components/MDX/Sandpack/Console.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ export const SandpackConsole = ({visible}: {visible: boolean}) => {
181181
</button>
182182
</div>
183183
{isExpanded && (
184-
<div className="w-full h-full border-y bg-white dark:border-gray-700 dark:bg-gray-95 min-h-[28px] console">
184+
<div className="w-full h-full border-t bg-white dark:border-gray-700 dark:bg-gray-95 min-h-[28px] console">
185185
<div className="max-h-40 h-auto overflow-auto" ref={wrapperRef}>
186186
{logs.map(({data, id, method}) => {
187187
return (
188188
<div
189189
key={id}
190190
className={cn(
191-
'last:border-none border-b dark:border-gray-700 text-md p-1 pl-2 leading-6 font-mono min-h-[32px] whitespace-pre-wrap',
191+
'first:border-none border-t dark:border-gray-700 text-md p-1 pl-2 leading-6 font-mono min-h-[32px] whitespace-pre-wrap',
192192
`console-${getType(method)}`,
193193
getColor(method)
194194
)}>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const SandboxShell = memo(function SandboxShell({
111111
}
112112
}
113113
}}>
114-
<span className="flex p-2 focus:outline-none text-primary dark:text-primary-dark">
114+
<span className="flex p-2 focus:outline-none text-primary dark:text-primary-dark leading-[20px]">
115115
<IconChevron
116116
className="inline mr-1.5 text-xl"
117117
displayDirection={isExpanded ? 'up' : 'down'}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export function NavigationBar({providedFiles}: {providedFiles: Array<string>}) {
125125
// space that's taken by the (invisible) tab list.
126126
<button
127127
className={cn(
128-
'absolute top-0 left-0',
128+
'absolute top-0 left-[2px]',
129129
!showDropdown && 'invisible'
130130
)}>
131131
<span

beta/src/components/MDX/Sandpack/OpenInCodeSandboxButton.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const OpenInCodeSandboxButton = () => {
1111
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"
1212
title="Open in CodeSandbox">
1313
<IconNewPage
14-
className="inline ml-1 mr-1 relative"
14+
className="inline ml-1 mr-1 relative top-[1px]"
1515
width="1em"
1616
height="1em"
1717
/>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function SandpackRoot(props: SandpackProps) {
7878
};
7979

8080
return (
81-
<div className="sandpack sandpack--playground sandbox my-8">
81+
<div className="sandpack sandpack--playground my-8">
8282
<SandpackProvider
8383
template="react"
8484
files={files}

beta/src/components/MDX/Sandpack/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {createFileMap} from './createFileMap';
88
const SandpackRoot = lazy(() => import('./SandpackRoot'));
99

1010
const SandpackGlimmer = ({code}: {code: string}) => (
11-
<div className="sandpack sandpack--playground sandbox my-8">
11+
<div className="sandpack sandpack--playground my-8">
1212
<div className="sp-wrapper">
1313
<div className="shadow-lg dark:shadow-lg-dark rounded-lg">
1414
<div className="bg-wash h-10 dark:bg-card-dark flex justify-between items-center relative z-10 border-b border-border dark:border-border-dark rounded-t-lg rounded-b-none">

beta/src/pages/_app.tsx

-36
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ if (typeof window !== 'undefined') {
2020
window.addEventListener(terminationEvent, function () {
2121
ga('send', 'timing', 'JS Dependencies', 'unload');
2222
});
23-
24-
disableAllRuntimeStyleInjection();
2523
}
2624

2725
export default function MyApp({Component, pageProps}: AppProps) {
@@ -55,37 +53,3 @@ export default function MyApp({Component, pageProps}: AppProps) {
5553

5654
return <Component {...pageProps} />;
5755
}
58-
59-
// HACK(css-in-js): We use Sandpack, which uses Stitches (css-in-js lib).
60-
// This causes style recalc during hydration which is bad for perf.
61-
// Instead, let's rely on the SSR'd <style> tag defined in _document.tsx.
62-
// This is obviously quite fragile but hopefully it'll be solved upstream.
63-
let didWarn = false;
64-
function disableAllRuntimeStyleInjection() {
65-
// Prevent Stitches from finding the <style> tag from the server:
66-
Object.defineProperty(document, 'styleSheets', {
67-
get() {
68-
return [];
69-
},
70-
});
71-
// It will try to create a new <style> tag and insert stuff there...
72-
const realInsertRule = CSSStyleSheet.prototype.insertRule;
73-
CSSStyleSheet.prototype.insertRule = function () {
74-
if (process.env.NODE_ENV !== 'production') {
75-
if (!didWarn) {
76-
console.warn(
77-
'Something is trying to inject runtime CSS-in-JS.\n' +
78-
'All <style> insertions will be ignored.',
79-
arguments
80-
);
81-
}
82-
didWarn = true;
83-
}
84-
// ... but we'll prevent it from affecting the document:
85-
this.disabled = true;
86-
// @ts-ignore
87-
return realInsertRule.apply(this, arguments);
88-
};
89-
// We're not supposed to use any other library that inserts <style> tags.
90-
// In longer term, ideally, Sandpack should offer a zero-runtime option.
91-
}

0 commit comments

Comments
 (0)