Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sandpack ssr styles #298

Merged
merged 3 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
)