dc92e2c61f
This PR is a small but mighty improvement to our docs. ### Change Type - [ ] `patch` — Bug fix - [ ] `minor` — New feature - [ ] `major` — Breaking change - [ ] `dependencies` — Changes to package dependencies[^1] - [x] `documentation` — Changes to the documentation only[^2] - [ ] `tests` — Changes to any test code only[^2] - [ ] `internal` — Any other changes that don't affect the published package[^2] - [ ] I don't know [^1]: publishes a `patch` release, for devDependencies use `internal` [^2]: will not publish a new version --------- Co-authored-by: Mime Čuvalo <mimecuvalo@gmail.com>
50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
'use client'
|
|
|
|
import { SandpackCodeEditor, SandpackFiles, SandpackProvider } from '@codesandbox/sandpack-react'
|
|
import { useTheme } from 'next-themes'
|
|
import { useEffect, useState } from 'react'
|
|
|
|
export default function ExampleCodeBlock({
|
|
articleId,
|
|
files = {},
|
|
activeFile,
|
|
}: {
|
|
articleId: string
|
|
activeFile: string
|
|
files: SandpackFiles
|
|
}) {
|
|
const [isClientSide, setIsClientSide] = useState(false)
|
|
const { theme } = useTheme()
|
|
useEffect(() => setIsClientSide(true), [])
|
|
const SERVER =
|
|
process.env.NODE_ENV === 'development' ? 'http://localhost:5420' : 'https://examples.tldraw.com'
|
|
|
|
// This is to avoid hydration mismatch between the server and the client because of the useTheme.
|
|
if (!isClientSide) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<div className="code-example">
|
|
<iframe src={`${SERVER}/${articleId}/full`} />
|
|
<SandpackProvider
|
|
className="sandpack"
|
|
key={`sandpack-${theme}-${activeFile}`}
|
|
template="react-ts"
|
|
options={{ activeFile }}
|
|
customSetup={{
|
|
dependencies: {
|
|
'@tldraw/assets': 'latest',
|
|
'@tldraw/tldraw': 'latest',
|
|
},
|
|
}}
|
|
files={{
|
|
...files,
|
|
}}
|
|
theme={theme === 'dark' ? 'dark' : 'light'}
|
|
>
|
|
<SandpackCodeEditor readOnly />
|
|
</SandpackProvider>
|
|
</div>
|
|
)
|
|
}
|