diff --git a/docs/getting-started/authoring.mdx b/docs/getting-started/authoring.mdx index 407305a7..8350e67d 100644 --- a/docs/getting-started/authoring.mdx +++ b/docs/getting-started/authoring.mdx @@ -469,6 +469,79 @@ It will render the [`SandpackCodeViewer`](https://sandpack.codesandbox.io/docs/a You can also pass [any `codeViewer={options: ComponentProps}`](https://sandpack.codesandbox.io/docs/advanced-usage/components#code-viewer:~:text=Preview-,Options,-CodeMirror%20decorations). +##### `Sandpack[codeViewer][filesDecorators]` + +You can define per-file [decorators](https://sandpack.codesandbox.io/docs/advanced-usage/components#codemirror-decorations) through `filesDecorators` prop: + +```tsx + +``` + +
+ Result + + + +
+ #### `Sandpack[preview]` You can pass [any `preview={options: ComponentProps}`](https://sandpack.codesandbox.io/docs/advanced-usage/components#preview:~:text=Preview-,Options,-Additional%20buttons). diff --git a/src/components/mdx/Sandpack/Sandpack.css b/src/components/mdx/Sandpack/Sandpack.css new file mode 100644 index 00000000..e2b2fbae --- /dev/null +++ b/src/components/mdx/Sandpack/Sandpack.css @@ -0,0 +1,28 @@ +.highlight { + background: #1ea7fd2b; + border-radius: 4px; +} +.widget { + border: 1px solid #1ea7fd; + border-radius: 2px; + padding: 2px 4px 2px 12px; + margin-left: 6px; + position: relative; + cursor: pointer; +} + +.widget:before { + content: attr(data-id); + background: #1ea7fd; + border-radius: 100%; + position: absolute; + width: 16px; + display: block; + height: 16px; + left: -8px; + top: 2px; + font-size: 11px; + text-align: center; + color: white; + line-height: 17px; +} diff --git a/src/components/mdx/Sandpack/Sandpack.tsx b/src/components/mdx/Sandpack/Sandpack.tsx index 739f4e69..fa7a39b9 100644 --- a/src/components/mdx/Sandpack/Sandpack.tsx +++ b/src/components/mdx/Sandpack/Sandpack.tsx @@ -2,7 +2,6 @@ import cn from '@/lib/cn' import { crawl } from '@/utils/docs' import { SandpackCodeEditor, - SandpackCodeViewer, SandpackFileExplorer, SandpackLayout, SandpackPreview, @@ -13,6 +12,8 @@ import { import fs from 'node:fs' import path from 'node:path' +import { SandpackCodeViewer } from './SandpackCodeViewer' + // https://tailwindcss.com/docs/configuration#referencing-in-java-script import { ComponentProps } from 'react' import resolveConfig from 'tailwindcss/resolveConfig' diff --git a/src/components/mdx/Sandpack/SandpackCodeViewer.tsx b/src/components/mdx/Sandpack/SandpackCodeViewer.tsx new file mode 100644 index 00000000..226ebc1a --- /dev/null +++ b/src/components/mdx/Sandpack/SandpackCodeViewer.tsx @@ -0,0 +1,31 @@ +'use client' + +import { + CodeViewerProps, + SandpackFiles, + SandpackCodeViewer as SPCodeViewer, + useSandpack, +} from '@codesandbox/sandpack-react' + +import { useEffect, useState } from 'react' +import './Sandpack.css' + +type Decorators = CodeViewerProps['decorators'] + +export function SandpackCodeViewer( + props: CodeViewerProps & { filesDecorators?: Record }, +) { + const { sandpack } = useSandpack() + const { activeFile } = sandpack + + const { filesDecorators, ...restCodeViewerProps } = props ?? {} + + const [decorators, setDecorators] = useState(undefined) + useEffect(() => { + if (!filesDecorators) return + + setDecorators(filesDecorators[activeFile]) + }, [activeFile, filesDecorators]) + + return +}