Skip to content

Commit e275e69

Browse files
committed
feat(Sandpack): individual files options
1 parent 0577670 commit e275e69

File tree

2 files changed

+72
-22
lines changed

2 files changed

+72
-22
lines changed

docs/getting-started/authoring.mdx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,43 @@ Instead of `files`, a `folder` prop allow you to pass a folder containing all th
376376

377377
</details>
378378

379+
It is also possible to pass some individual [file format](https://sandpack.codesandbox.io/docs/getting-started/usage#file-format) configuration:
380+
381+
```tsx
382+
<Sandpack
383+
template="react-ts"
384+
folder="authoring-sandpack-cloud"
385+
files={{
386+
'/App.tsx': {
387+
readOnly: true,
388+
active: true,
389+
},
390+
'/styles.css': {
391+
hidden: true
392+
}
393+
}}
394+
/>
395+
```
396+
397+
<details>
398+
<summary>Result</summary>
399+
400+
<Sandpack
401+
template="react-ts"
402+
folder="authoring-sandpack-cloud"
403+
files={{
404+
'/App.tsx': {
405+
readOnly: true,
406+
active: true,
407+
},
408+
'/styles.css': {
409+
hidden: true
410+
}
411+
}}
412+
/>
413+
414+
</details>
415+
379416
### `Codesandbox`
380417

381418
```md
Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import cn from '@/lib/cn'
22
import { crawl } from '@/utils/docs'
3-
import { Sandpack as SP } from '@codesandbox/sandpack-react'
3+
import {
4+
Sandpack as SP,
5+
type SandpackFile,
6+
type SandpackFiles,
7+
type SandpackProps,
8+
} from '@codesandbox/sandpack-react'
49
import fs from 'node:fs'
510
import path from 'node:path'
6-
import { ComponentProps } from 'react'
11+
12+
type Files = Record<string, Omit<SandpackFile, 'code'>>
713

814
function getSandpackDependencies(folder: string) {
915
const pkgPath = `${folder}/package.json`
@@ -13,49 +19,56 @@ function getSandpackDependencies(folder: string) {
1319
return JSON.parse(str).dependencies as Record<string, string>
1420
}
1521

16-
type File = { code: string }
17-
18-
async function getSandpackFiles(folder: string, extensions = ['js', 'ts', 'jsx', 'tsx', 'css']) {
22+
async function getSandpackFiles(
23+
folder: string,
24+
files: Files = {},
25+
extensions = ['js', 'ts', 'jsx', 'tsx', 'css'],
26+
) {
1927
const filepaths = await crawl(
2028
folder,
2129
(dir) =>
2230
!dir.includes('node_modules') && extensions.map((ext) => dir.endsWith(ext)).some(Boolean),
2331
)
2432
// console.log('filepaths', filepaths)
2533

26-
const files = filepaths.reduce(
27-
(acc, filepath) => {
28-
const relativeFilepath = path.relative(folder, filepath)
29-
30-
return {
31-
...acc,
32-
[`/${relativeFilepath}`]: {
33-
code: fs.readFileSync(filepath, 'utf-8'),
34-
},
35-
}
36-
},
37-
{} as Record<string, File>,
38-
)
34+
return filepaths.reduce((acc, filepath) => {
35+
const relativeFilepath = path.relative(folder, filepath)
3936

40-
return files
37+
const key = `/${relativeFilepath}`
38+
const file = files[key]
39+
return {
40+
...acc,
41+
[key]: {
42+
...file,
43+
code: fs.readFileSync(filepath, 'utf-8'),
44+
},
45+
}
46+
}, {} as SandpackFiles)
4147
}
4248

4349
// https://sandpack.codesandbox.io/docs/getting-started/usage
4450
export const Sandpack = async ({
4551
className,
4652
folder,
53+
files,
4754
...props
48-
}: { className: string; folder?: string } & ComponentProps<typeof SP>) => {
55+
}: {
56+
className: string
57+
folder?: string
58+
files?: Files
59+
} & Omit<SandpackProps, 'files'>) => {
4960
// console.log('folder', folder)
5061

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

5365
const pkgDeps = folder ? getSandpackDependencies(folder) : null
5466
const dependencies = pkgDeps ?? props.customSetup?.dependencies
5567
const customSetup = {
5668
...props.customSetup,
5769
dependencies,
5870
}
71+
// console.log('customSetup', customSetup)
5972

6073
const options = {
6174
...props.options,
@@ -64,7 +77,7 @@ export const Sandpack = async ({
6477

6578
return (
6679
<div className={cn(className, 'sandpack')}>
67-
<SP {...props} files={files} customSetup={customSetup} options={options} />
80+
<SP {...props} files={_files} customSetup={customSetup} options={options} />
6881
</div>
6982
)
7083
}

0 commit comments

Comments
 (0)