Skip to content

Commit 1a7d0a6

Browse files
committed
fix: sandpack theme
1 parent e275e69 commit 1a7d0a6

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

src/app/layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default function RootLayout({
6161
<SandpackCSS />
6262
</head>
6363
<body className={cn(inter.className, 'bg-surface break-words')}>
64-
<ThemeProvider>{children}</ThemeProvider>
64+
<ThemeProvider attribute="class">{children}</ThemeProvider>
6565
</body>
6666
</html>
6767
)

src/components/mdx/Sandpack/Sandpack.tsx

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import cn from '@/lib/cn'
22
import { crawl } from '@/utils/docs'
33
import {
4-
Sandpack as SP,
5-
type SandpackFile,
4+
SandpackCodeEditor,
5+
SandpackLayout,
6+
SandpackPreview,
7+
SandpackProvider,
68
type SandpackFiles,
7-
type SandpackProps,
9+
type SandpackProviderProps,
810
} from '@codesandbox/sandpack-react'
911
import fs from 'node:fs'
1012
import path from 'node:path'
1113

12-
type Files = Record<string, Omit<SandpackFile, 'code'>>
14+
// https://tailwindcss.com/docs/configuration#referencing-in-java-script
15+
import resolveConfig from 'tailwindcss/resolveConfig'
16+
import tailwindConfig from '../../../../tailwind.config'
17+
const fullConfig = resolveConfig(tailwindConfig)
18+
// console.log('fullConfig', fullConfig.theme.colors)
19+
// console.log(fullConfig.theme.fontSize.sm)
20+
// console.log(fullConfig.theme.fontFamily.mono)
1321

1422
function getSandpackDependencies(folder: string) {
1523
const pkgPath = `${folder}/package.json`
@@ -21,7 +29,7 @@ function getSandpackDependencies(folder: string) {
2129

2230
async function getSandpackFiles(
2331
folder: string,
24-
files: Files = {},
32+
files: SandpackFiles = {},
2533
extensions = ['js', 'ts', 'jsx', 'tsx', 'css'],
2634
) {
2735
const filepaths = await crawl(
@@ -39,7 +47,7 @@ async function getSandpackFiles(
3947
return {
4048
...acc,
4149
[key]: {
42-
...file,
50+
...((typeof file !== 'string' && file) || undefined),
4351
code: fs.readFileSync(filepath, 'utf-8'),
4452
},
4553
}
@@ -50,17 +58,14 @@ async function getSandpackFiles(
5058
export const Sandpack = async ({
5159
className,
5260
folder,
53-
files,
5461
...props
55-
}: {
62+
}: SandpackProviderProps & {
5663
className: string
5764
folder?: string
58-
files?: Files
59-
} & Omit<SandpackProps, 'files'>) => {
65+
}) => {
6066
// console.log('folder', folder)
6167

62-
const _files = folder ? await getSandpackFiles(folder, files) : files
63-
// console.log('_files', _files)
68+
const _files = folder ? await getSandpackFiles(folder, props.files) : props.files
6469

6570
const pkgDeps = folder ? getSandpackDependencies(folder) : null
6671
const dependencies = pkgDeps ?? props.customSetup?.dependencies
@@ -77,7 +82,27 @@ export const Sandpack = async ({
7782

7883
return (
7984
<div className={cn(className, 'sandpack')}>
80-
<SP {...props} files={_files} customSetup={customSetup} options={options} />
85+
<SandpackProvider
86+
{...props}
87+
theme={{
88+
colors: {
89+
// @ts-ignore
90+
surface1: fullConfig.theme.colors['inverse-surface-light'],
91+
},
92+
font: {
93+
mono: fullConfig.theme.fontFamily.mono.join(', '),
94+
size: fullConfig.theme.fontSize.xs[0],
95+
},
96+
}}
97+
files={_files}
98+
customSetup={customSetup}
99+
options={options}
100+
>
101+
<SandpackLayout>
102+
<SandpackCodeEditor />
103+
<SandpackPreview />
104+
</SandpackLayout>
105+
</SandpackProvider>
81106
</div>
82107
)
83108
}

0 commit comments

Comments
 (0)