Skip to content

Commit

Permalink
fix: sandpack ssr styles (#298)
Browse files Browse the repository at this point in the history
* fb sandpack.css

* SandpackCSS

* doc
  • Loading branch information
abernier committed Aug 25, 2024
1 parent 0364106 commit 4e3b34e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
12 changes: 11 additions & 1 deletion docs/getting-started/authoring.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ Responsive grid.

#### `Sandpack`

See [Sandpack docs](https://sandpack.codesandbox.io/docs/getting-started/usage).
Inline sandboxes.

> [!NOTE]
> See Sandpack [official documentation](https://sandpack.codesandbox.io/docs/getting-started/usage) for full usage.
```tsx
<Sandpack
Expand Down Expand Up @@ -120,8 +123,15 @@ export default function App() {
}}
/>

> [!TIP]
> Sandpack UI SSR-rendering[^1] is also supported (out of the box).
[^1]: https://sandpack.codesandbox.io/docs/guides/ssr#nextjs-app-dir

</details>



#### `Codesandbox`

```md
Expand Down
4 changes: 4 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Metadata } from 'next'
import { ThemeProvider } from 'next-themes'
import { Inconsolata, Inter } from 'next/font/google'
import './globals.css'
import { SandpackCSS } from './sandpack-styles'

const inter = Inter({ subsets: ['latin'] })
const inconsolata = Inconsolata({ subsets: ['latin'] })
Expand Down Expand Up @@ -56,6 +57,9 @@ export default function RootLayout({
}>) {
return (
<html lang="en">
<head>
<SandpackCSS />
</head>
<body className={cn(inter.className, 'bg-surface')}>
<ThemeProvider
// attribute="class"
Expand Down
16 changes: 16 additions & 0 deletions src/app/sandpack-styles.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// https://sandpack.codesandbox.io/docs/guides/ssr#nextjs-app-dir

'use client'

import { getSandpackCssText } from '@codesandbox/sandpack-react'
import { useServerInsertedHTML } from 'next/navigation'

/**
* Ensures CSSinJS styles are loaded server side.
*/
export const SandpackCSS = () => {
useServerInsertedHTML(() => {
return <style dangerouslySetInnerHTML={{ __html: getSandpackCssText() }} id="sandpack" />
})
return null
}
8 changes: 6 additions & 2 deletions src/components/mdx/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export * from './Toc'

import cn from '@/lib/cn'
import { MARKDOWN_REGEX } from '@/utils/docs'
import { Sandpack } from '@codesandbox/sandpack-react'
import { Sandpack as SP } from '@codesandbox/sandpack-react'
import { ComponentProps } from 'react'

type Hn = 'h2' | 'h3' | 'h4' | 'h5' | 'h6'
Expand Down Expand Up @@ -108,4 +108,8 @@ export const summary = (props: ComponentProps<'summary'>) => (
<summary className="my-2 -ml-4 cursor-pointer select-none" {...props} />
)

export { Sandpack }
export const Sandpack = (props: ComponentProps<typeof SP>) => (
<div className="sandpack">
<SP {...props} />
</div>
)

0 comments on commit 4e3b34e

Please sign in to comment.