docs: fix scroll position and theming issue for code snippets (#2883)
- scrolling to a section was hiding the header under the top layout when clicking on the sidebar - dark mode wasn't hydrating correctly and we only render on client, for now ### Change Type - [x] `documentation` — Changes to the documentation only[^2]
This commit is contained in:
parent
d372010ad3
commit
9b65b0e20f
2 changed files with 15 additions and 3 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
import { SandpackCodeViewer, SandpackFiles, SandpackProvider } from '@codesandbox/sandpack-react'
|
||||
import { useTheme } from 'next-themes'
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
export const Code = (props: any) => {
|
||||
if (!props.className) {
|
||||
|
@ -13,7 +14,14 @@ export const Code = (props: any) => {
|
|||
}
|
||||
|
||||
export function CodeBlock({ code }: { code: 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 trimmedCode = Object.fromEntries(
|
||||
Object.entries(code).map(([key, value]) => [key, (value as string).trim()])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue