2024-02-12 14:30:55 +00:00
|
|
|
'use client'
|
|
|
|
|
|
|
|
import { SandpackCodeViewer, SandpackFiles, SandpackProvider } from '@codesandbox/sandpack-react'
|
|
|
|
import { useTheme } from 'next-themes'
|
2024-02-20 14:24:54 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2024-02-12 14:30:55 +00:00
|
|
|
|
|
|
|
export const Code = (props: any) => {
|
2024-02-29 16:28:45 +00:00
|
|
|
return <code {...props} />
|
2024-02-12 14:30:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function CodeBlock({ code }: { code: SandpackFiles }) {
|
2024-02-20 14:24:54 +00:00
|
|
|
const [isClientSide, setIsClientSide] = useState(false)
|
2024-02-12 14:30:55 +00:00
|
|
|
const { theme } = useTheme()
|
2024-02-20 14:24:54 +00:00
|
|
|
useEffect(() => setIsClientSide(true), [])
|
|
|
|
|
|
|
|
// This is to avoid hydration mismatch between the server and the client because of the useTheme.
|
|
|
|
if (!isClientSide) {
|
|
|
|
return null
|
|
|
|
}
|
2024-02-12 14:30:55 +00:00
|
|
|
|
|
|
|
const trimmedCode = Object.fromEntries(
|
|
|
|
Object.entries(code).map(([key, value]) => [key, (value as string).trim()])
|
|
|
|
)
|
|
|
|
return (
|
|
|
|
<div className="code-example">
|
|
|
|
<SandpackProvider
|
|
|
|
className="sandpack"
|
|
|
|
key={`sandpack-${theme}`}
|
|
|
|
template="react-ts"
|
|
|
|
options={{ activeFile: Object.keys(code)[0] }}
|
|
|
|
customSetup={{
|
|
|
|
dependencies: {
|
|
|
|
'@tldraw/assets': 'latest',
|
2024-02-29 16:06:19 +00:00
|
|
|
tldraw: 'latest',
|
2024-02-12 14:30:55 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
files={trimmedCode}
|
|
|
|
theme={theme === 'dark' ? 'dark' : 'light'}
|
|
|
|
>
|
|
|
|
<SandpackCodeViewer />
|
|
|
|
</SandpackProvider>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|