1
1
import cn from '@/lib/cn'
2
2
import { crawl } from '@/utils/docs'
3
3
import {
4
- Sandpack as SP ,
5
- type SandpackFile ,
4
+ SandpackCodeEditor ,
5
+ SandpackLayout ,
6
+ SandpackPreview ,
7
+ SandpackProvider ,
6
8
type SandpackFiles ,
7
- type SandpackProps ,
9
+ type SandpackProviderProps ,
8
10
} from '@codesandbox/sandpack-react'
9
11
import fs from 'node:fs'
10
12
import path from 'node:path'
11
13
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)
13
21
14
22
function getSandpackDependencies ( folder : string ) {
15
23
const pkgPath = `${ folder } /package.json`
@@ -21,7 +29,7 @@ function getSandpackDependencies(folder: string) {
21
29
22
30
async function getSandpackFiles (
23
31
folder : string ,
24
- files : Files = { } ,
32
+ files : SandpackFiles = { } ,
25
33
extensions = [ 'js' , 'ts' , 'jsx' , 'tsx' , 'css' ] ,
26
34
) {
27
35
const filepaths = await crawl (
@@ -39,7 +47,7 @@ async function getSandpackFiles(
39
47
return {
40
48
...acc ,
41
49
[ key ] : {
42
- ...file ,
50
+ ...( ( typeof file !== 'string' && file ) || undefined ) ,
43
51
code : fs . readFileSync ( filepath , 'utf-8' ) ,
44
52
} ,
45
53
}
@@ -50,17 +58,14 @@ async function getSandpackFiles(
50
58
export const Sandpack = async ( {
51
59
className,
52
60
folder,
53
- files,
54
61
...props
55
- } : {
62
+ } : SandpackProviderProps & {
56
63
className : string
57
64
folder ?: string
58
- files ?: Files
59
- } & Omit < SandpackProps , 'files' > ) => {
65
+ } ) => {
60
66
// console.log('folder', folder)
61
67
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
64
69
65
70
const pkgDeps = folder ? getSandpackDependencies ( folder ) : null
66
71
const dependencies = pkgDeps ?? props . customSetup ?. dependencies
@@ -77,7 +82,27 @@ export const Sandpack = async ({
77
82
78
83
return (
79
84
< 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 >
81
106
</ div >
82
107
)
83
108
}
0 commit comments