Skip to content

Commit

Permalink
feat: add a way to provide global scope for live editor
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Aug 9, 2019
1 parent cc10055 commit 397d754
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/components/Code.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,21 @@ const LivePreview = styled(BaseLivePreview)`
}
`

const globalScope = {}

export function LiveScope({ scope }) {
Object.assign(globalScope, scope)
return null
}

export function Code({ children, lang = 'markup', live, noInline }) {
const scope = useMDXScope()
if (live) {
return (
<LiveProvider
code={children.trim()}
transformCode={code => `/* @jsx mdx */${code}`}
scope={{ mdx, ...scope }}
scope={{ mdx, ...scope, ...globalScope }}
language={lang}
theme={theme}
noInline={noInline}
Expand Down
6 changes: 6 additions & 0 deletions src/components/Playground.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react'
import { Code } from './Code'

export function Playground(props) {
return <Code lang="jsx" live {...props} />
}
1 change: 1 addition & 0 deletions src/components/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './Article'
export * from './BaseLayout'
export * from './DocLayout'
export * from './Code'
export * from './GlobalStyle'
export * from './Head'
export * from './HomeHero'
Expand Down
7 changes: 5 additions & 2 deletions src/pages/docs/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ order: 1
---

import styled from 'styled-components'
import Button, { Foo } from './Button'
import Button from './Button'
import { LiveScope } from '../../components/Code'

<LiveScope scope={{ styled }} />

# Getting Started

Expand All @@ -32,7 +35,7 @@ Etiam elementum, odio sed maximus fringilla, massa dui efficitur ante, sed dictu
## Code example

```jsx live
<Button>Good</Button>
<Button>Hello</Button>
```

```jsx
Expand Down

0 comments on commit 397d754

Please sign in to comment.