-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[v3] Add
<Playground />
component (#3174)
* [nextra] Playground * move to nextra.site * polish * prettier * more * fix types * more * rename * rename * more * more * more * more * add mermaid code block * it works * aaa * dada * ad * fix prettier * polish * more * pnpm i * fix * fix tests * do not increase global bundle size * add changeset --------- Co-authored-by: hariria <[email protected]>
- Loading branch information
1 parent
177fe84
commit 452e5bd
Showing
23 changed files
with
285 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'nextra-theme-docs': patch | ||
--- | ||
|
||
Add `<Playground />` component | ||
|
||
https://nextra-v2-9x7fp6hti-shud.vercel.app/docs/guide/advanced/playground |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { Mermaid, Playground, Code, Pre, Tabs } from 'nextra/components' | ||
import { useRef, useCallback, useState, useEffect } from 'react' | ||
|
||
export function Demo() { | ||
const [rawMdx, setRawMdx] = useState(`Playground components allow you to write Nextra compatible MDX that renders only on the client. It's modeled after the functionality found in [MDX Playground](https://mdxjs.com/playground). | ||
In some instances where remote loading MDX is not an option, this may work as a great alternative. | ||
Here's an example of a codeblock. | ||
\`\`\`ts | ||
console.log("Hello world, this is a playground component!"); | ||
\`\`\` | ||
## Caveats | ||
Due to the purely client-side nature of this component, features "Table of Contents" and "Frontmatter" will not work. | ||
## Mermaid Example | ||
\`\`\`mermaid | ||
graph TD | ||
subgraph AA [Consumers] | ||
A[Mobile App] | ||
B[Web App] | ||
C[Node.js Client] | ||
end | ||
subgraph BB [Services] | ||
E[REST API] | ||
F[GraphQL API] | ||
G[SOAP API] | ||
end | ||
Z[GraphQL API] | ||
A --> Z | ||
B --> Z | ||
C --> Z | ||
Z --> E | ||
Z --> F | ||
Z --> G | ||
\`\`\``) | ||
const handleInput = useCallback(e => { | ||
setRawMdx(e.currentTarget.textContent ?? '') | ||
}, []) | ||
|
||
const spanRef = useRef(null) | ||
const initialRender = useRef(false) | ||
|
||
useEffect(() => { | ||
if (!initialRender.current && spanRef.current) { | ||
initialRender.current = true | ||
spanRef.current.textContent = rawMdx | ||
} | ||
}, []); | ||
|
||
return ( | ||
<div className="grid grid-cols-2 gap-2 mt-6"> | ||
<Pre data-filename="MDX" icon={MdxIcon}> | ||
<Code> | ||
<span | ||
ref={spanRef} | ||
contentEditable | ||
suppressContentEditableWarning | ||
className="outline-none" | ||
onInput={handleInput} | ||
/> | ||
</Code> | ||
</Pre> | ||
<div> | ||
<Playground | ||
fallback={ | ||
<div className="flex h-full items-center justify-center text-4xl"> | ||
Loading playground... | ||
</div> | ||
} | ||
source={rawMdx} | ||
components={{ Mermaid, $Tabs: Tabs }} | ||
/> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
# Playground | ||
|
||
<Demo /> | ||
|
||
## Usage | ||
|
||
```mdx filename="Basic Usage" | ||
import { Playground } from 'nextra/components' | ||
|
||
# Playground | ||
|
||
Below is a playground component. It mixes into the rest of your MDX perfectly. | ||
|
||
<Playground source="## Hello world" /> | ||
``` | ||
|
||
You may also specify a fallback component like so: | ||
|
||
```mdx filename="Usage with Fallback" | ||
import { Playground } from 'nextra/components' | ||
|
||
<Playground | ||
source="## Hello world" | ||
fallback={<div>Loading playground...</div>} | ||
/> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ | |
"prettier-plugin-pkg": "0.18.1", | ||
"prettier-plugin-tailwindcss": "0.6.5", | ||
"rimraf": "6.0.1", | ||
"tsup": "8.1.0", | ||
"tsup": "8.2.4", | ||
"tsx": "^4.7.0", | ||
"turbo": "2.1.1", | ||
"typescript": "5.5.4" | ||
|
@@ -54,7 +54,7 @@ | |
}, | ||
"patchedDependencies": { | ||
"@changesets/[email protected]": "patches/@[email protected]", | ||
"tsup@8.1.0": "patches/tsup@8.0.1.patch" | ||
"tsup@8.2.4": "patches/tsup.patch" | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { useEffect, useState } from 'react' | ||
import type { ReactElement } from 'react' | ||
import { CrossCircledIcon } from '../icons/index.js' | ||
import type { MDXComponents } from '../mdx.js' | ||
import { Code } from './code.js' | ||
import { Pre } from './pre.js' | ||
import { evaluate } from './remote-content.js' | ||
|
||
export function Playground({ | ||
source, | ||
scope, | ||
components, | ||
fallback = null | ||
}: { | ||
/** | ||
* String with source MDX | ||
* | ||
* @example '# hello world <br /> nice to see you' | ||
*/ | ||
source: string | ||
/** | ||
* An object mapping names to React components. | ||
* The key used will be the name accessible to MDX. | ||
* | ||
* @example `{ ComponentName: Component }` will be accessible in the MDX as `<ComponentName/>`. | ||
*/ | ||
components?: MDXComponents | ||
/** | ||
* Pass-through variables for use in the MDX content | ||
*/ | ||
scope?: Record<string, unknown> | ||
/** | ||
* Fallback component for loading | ||
*/ | ||
fallback?: ReactElement | null | ||
}) { | ||
const [compiledSource, setCompiledSource] = useState('') | ||
const [error, setError] = useState<unknown>() | ||
|
||
useEffect(() => { | ||
async function doCompile() { | ||
// Importing in useEffect to not increase global bundle size | ||
const { compileMdx } = await import('../../server/compile.js') | ||
try { | ||
const mdx = await compileMdx(source) | ||
setCompiledSource(mdx.result) | ||
setError(null) | ||
} catch (error) { | ||
setError(error) | ||
} | ||
} | ||
|
||
doCompile() | ||
}, [source]) | ||
|
||
if (error) { | ||
return ( | ||
<div className="[&_svg]:_text-red-500"> | ||
<Pre | ||
data-filename="Could not compile code" | ||
icon={CrossCircledIcon} | ||
className="_whitespace-pre-wrap" | ||
> | ||
<Code> | ||
<span> | ||
{error instanceof Error | ||
? `${error.name}: ${error.message}` | ||
: String(error)} | ||
</span> | ||
</Code> | ||
</Pre> | ||
</div> | ||
) | ||
} | ||
|
||
if (compiledSource) { | ||
const MDXContent = evaluate(compiledSource, scope).default | ||
return <MDXContent components={components} /> | ||
} | ||
|
||
return fallback | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import type { ComponentProps, ReactElement } from 'react' | ||
import type { FC, SVGProps } from 'react' | ||
|
||
declare const ReactComponent: (props: ComponentProps<'svg'>) => ReactElement | ||
declare const ReactComponent: FC<SVGProps<SVGElement>> | ||
|
||
export { ReactComponent } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.