Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Playground encapsulation / iframe around the playground #1304

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions core/gatsby-theme-docz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"prop-types": "^15.7.2",
"re-resizable": "^6.1.0",
"react-feather": "^2.0.3",
"react-frame-component": "^4.1.1",
"react-helmet-async": "^1.0.4",
"react-live": "^2.2.1",
"rehype-docz": "^2.1.0",
Expand Down
5 changes: 4 additions & 1 deletion core/gatsby-theme-docz/src/components/Playground/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import copy from 'copy-text-to-clipboard'
import { usePrismTheme } from '~utils/theme'
import * as styles from './styles'
import * as Icons from '../Icons'
import { Preview } from '../Preview'

const transformCode = code => {
if (code.startsWith('()') || code.startsWith('class')) return code
Expand Down Expand Up @@ -67,7 +68,9 @@ export const Playground = ({ code, scope, language }) => {
<div sx={styles.previewWrapper}>
<div sx={styles.previewInner(showingCode)}>
{showLivePreview && (
<LivePreview sx={styles.preview} data-testid="live-preview" />
<Preview>
<LivePreview sx={styles.preview} data-testid="live-preview" />
</Preview>
)}
</div>
<div sx={styles.buttons}>
Expand Down
17 changes: 17 additions & 0 deletions core/gatsby-theme-docz/src/components/Preview/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** @jsx jsx */
import { jsx } from 'theme-ui'
import Frame from 'react-frame-component'

export const Preview = ({ children }) => {
return (
<Frame
style={{
width: '100%',
height: '100%',
border: 'none',
}}
>
{children}
</Frame>
)
}
1 change: 1 addition & 0 deletions examples/styled-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"prop-types": "^15.7.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-frame-component": "^4.1.1",
"styled-components": "^4.3.2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/** @jsx jsx */
import { jsx } from 'theme-ui'
import * as React from 'react'
import Frame, { FrameContextConsumer } from 'react-frame-component'
import { StyleSheetManager } from 'styled-components'

export const Preview = ({ children }) => {
return (
<Frame
style={{
width: '100%',
height: '100%',
border: 'none',
}}
>
<FrameContextConsumer>
{frameContext => (
<StyleSheetManager target={frameContext.document.head}>
<>{children}</>
</StyleSheetManager>
)}
</FrameContextConsumer>
</Frame>
)
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15840,6 +15840,11 @@ react-feather@^2.0.3:
dependencies:
prop-types "^15.7.2"

react-frame-component@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/react-frame-component/-/react-frame-component-4.1.1.tgz#ea8f7c518ef6b5ad72146dd1f648752369826894"
integrity sha512-NfJp90AvYA1R6+uSYafQ+n+UM2HjHqi4WGHeprVXa6quU9d8o6ZFRzQ36uemY82dlkZFzf2jigFx6E4UzNFajA==

react-helmet-async@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/react-helmet-async/-/react-helmet-async-1.0.4.tgz#079ef10b7fefcaee6240fefd150711e62463cc97"
Expand Down