tldraw/apps/docs/components/ExampleCodeBlock.tsx
David Sheldrick 1c35ed27f9
[bemo] No public shared rooms in examples app (#4152)
As discussed in #4135 I'm doing a quick follow up to help me sleep at
night.

Sorry sorry sorry sorry

![Kapture 2024-07-12 at 12 47
45](https://github.com/user-attachments/assets/ee9babf0-6b7e-4ddb-a427-5aef9436f922)

i couldn't figure out the magic overlay css so I reverted to ugly static
toolbar. again so sorry

### Change type

- [ ] `bugfix`
- [x] `improvement`
- [ ] `feature`
- [ ] `api`
- [ ] `other`

### Test plan

1. Create a shape...
2.

- [ ] Unit tests
- [ ] End to end tests

### Release notes

- Fixed a bug with…
2024-07-12 13:07:07 +00:00

58 lines
1.4 KiB
TypeScript

'use client'
import { SandpackCodeViewer, 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), [])
// This is to avoid hydration mismatch between the server and the client because of the useTheme.
if (!isClientSide) {
return null
}
const SERVER =
process.env.NODE_ENV === 'development'
? `http://${location.hostname}:5420`
: location.host.includes('staging') || location.host.includes('canary')
? `https://examples-canary.tldraw.com`
: 'https://examples.tldraw.com'
return (
<div className="code-example">
<iframe
src={`${SERVER}/${articleId}/full`}
allow={`clipboard-read; clipboard-write self ${SERVER}`}
/>
<SandpackProvider
className="sandpack"
key={`sandpack-${theme}-${activeFile}`}
template="react-ts"
options={{ activeFile }}
customSetup={{
dependencies: {
'@tldraw/assets': 'latest',
tldraw: 'latest',
},
}}
files={{
...files,
}}
theme={theme === 'dark' ? 'dark' : 'light'}
>
<SandpackCodeViewer />
</SandpackProvider>
</div>
)
}