Skip to content

Commit 64b0c50

Browse files
authored
feat: files decorators (#365)
1 parent ad556d0 commit 64b0c50

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-1
lines changed

docs/getting-started/authoring.mdx

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,79 @@ It will render the [`SandpackCodeViewer`](https://sandpack.codesandbox.io/docs/a
469469

470470
You can also pass [any `codeViewer={options: ComponentProps<SandpackCodeViewer>}`](https://sandpack.codesandbox.io/docs/advanced-usage/components#code-viewer:~:text=Preview-,Options,-CodeMirror%20decorations).
471471

472+
##### `Sandpack[codeViewer][filesDecorators]`
473+
474+
You can define per-file [decorators](https://sandpack.codesandbox.io/docs/advanced-usage/components#codemirror-decorations) through `filesDecorators` prop:
475+
476+
```tsx
477+
<Sandpack
478+
template="react-ts"
479+
folder="authoring-sandpack-cloud"
480+
codeViewer={{
481+
filesDecorators: {
482+
'/App.tsx': [
483+
{ className: "highlight", line: 1 },
484+
{ className: "highlight", line: 7 },
485+
{
486+
className: "widget",
487+
elementAttributes: { "data-id": "1" },
488+
line: 7,
489+
startColumn: 13,
490+
endColumn: 24,
491+
},
492+
{
493+
className: "widget",
494+
elementAttributes: { "data-id": "2" },
495+
line: 7,
496+
startColumn: 25,
497+
endColumn: 35,
498+
},
499+
500+
],
501+
'/styles.css': [
502+
{ className: "highlight", line: 4 },
503+
]
504+
}
505+
}}
506+
/>
507+
```
508+
509+
<details>
510+
<summary>Result</summary>
511+
512+
<Sandpack
513+
template="react-ts"
514+
folder="authoring-sandpack-cloud"
515+
codeViewer={{
516+
filesDecorators: {
517+
'/App.tsx': [
518+
{ className: "highlight", line: 1 },
519+
{ className: "highlight", line: 7 },
520+
{
521+
className: "widget",
522+
elementAttributes: { "data-id": "1" },
523+
line: 7,
524+
startColumn: 13,
525+
endColumn: 24,
526+
},
527+
{
528+
className: "widget",
529+
elementAttributes: { "data-id": "2" },
530+
line: 7,
531+
startColumn: 25,
532+
endColumn: 35,
533+
},
534+
535+
],
536+
'/styles.css': [
537+
{ className: "highlight", line: 4 },
538+
]
539+
}
540+
}}
541+
/>
542+
543+
</details>
544+
472545
#### `Sandpack[preview]`
473546

474547
You can pass [any `preview={options: ComponentProps<SandpackPreview>}`](https://sandpack.codesandbox.io/docs/advanced-usage/components#preview:~:text=Preview-,Options,-Additional%20buttons).
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.highlight {
2+
background: #1ea7fd2b;
3+
border-radius: 4px;
4+
}
5+
.widget {
6+
border: 1px solid #1ea7fd;
7+
border-radius: 2px;
8+
padding: 2px 4px 2px 12px;
9+
margin-left: 6px;
10+
position: relative;
11+
cursor: pointer;
12+
}
13+
14+
.widget:before {
15+
content: attr(data-id);
16+
background: #1ea7fd;
17+
border-radius: 100%;
18+
position: absolute;
19+
width: 16px;
20+
display: block;
21+
height: 16px;
22+
left: -8px;
23+
top: 2px;
24+
font-size: 11px;
25+
text-align: center;
26+
color: white;
27+
line-height: 17px;
28+
}

src/components/mdx/Sandpack/Sandpack.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import cn from '@/lib/cn'
22
import { crawl } from '@/utils/docs'
33
import {
44
SandpackCodeEditor,
5-
SandpackCodeViewer,
65
SandpackFileExplorer,
76
SandpackLayout,
87
SandpackPreview,
@@ -13,6 +12,8 @@ import {
1312
import fs from 'node:fs'
1413
import path from 'node:path'
1514

15+
import { SandpackCodeViewer } from './SandpackCodeViewer'
16+
1617
// https://tailwindcss.com/docs/configuration#referencing-in-java-script
1718
import { ComponentProps } from 'react'
1819
import resolveConfig from 'tailwindcss/resolveConfig'
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use client'
2+
3+
import {
4+
CodeViewerProps,
5+
SandpackFiles,
6+
SandpackCodeViewer as SPCodeViewer,
7+
useSandpack,
8+
} from '@codesandbox/sandpack-react'
9+
10+
import { useEffect, useState } from 'react'
11+
import './Sandpack.css'
12+
13+
type Decorators = CodeViewerProps['decorators']
14+
15+
export function SandpackCodeViewer(
16+
props: CodeViewerProps & { filesDecorators?: Record<keyof SandpackFiles, Decorators> },
17+
) {
18+
const { sandpack } = useSandpack()
19+
const { activeFile } = sandpack
20+
21+
const { filesDecorators, ...restCodeViewerProps } = props ?? {}
22+
23+
const [decorators, setDecorators] = useState<Decorators>(undefined)
24+
useEffect(() => {
25+
if (!filesDecorators) return
26+
27+
setDecorators(filesDecorators[activeFile])
28+
}, [activeFile, filesDecorators])
29+
30+
return <SPCodeViewer {...restCodeViewerProps} decorators={decorators} />
31+
}

0 commit comments

Comments
 (0)