Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,6 @@ exports[`<ComponentPlayground /> Should render with a custom background color 1`
>
<div
class="react-live-preview css-cv4xj2"
previewbackgroundcolor="#ff0"
>
<div>
<h1
Expand Down Expand Up @@ -1591,7 +1590,6 @@ exports[`<ComponentPlayground /> Should render with a custom code block 1`] = `
>
<div
class="react-live-preview css-cv4xj2"
previewbackgroundcolor="#ff0"
>

</div>
Expand Down
105 changes: 60 additions & 45 deletions src/components/component-playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ export const PlaygroundProvider = styled(LiveProvider)`
overflow: hidden;
`;

const PlaygroundPreview = styled(LivePreview)`
const PlaygroundPreview = styled(({ className }) => (
<LivePreview className={className} />
))`
padding: 0.5rem;
min-height: 100%;

background: ${(props) => (
props.previewBackgroundColor || '#fff'
)};
background: ${p => p.previewBackgroundColor || '#fff'};
`;

const PlaygroundEditor = styled(LiveEditor)`
Expand Down Expand Up @@ -86,48 +86,63 @@ const PlaygroundError = styled(LiveError)`
padding: 0.5rem;
`;

const ComponentPlayground = ({
code,
previewBackgroundColor,
scope = {},
theme = 'dark'
}) => {
const useDarkTheme = theme === 'dark';

if (useDarkTheme) {
require('../themes/default/prism.dark.css');
} else {
require('../themes/default/prism.light.css');
class ComponentPlayground extends Component {
onKeyUp = evt => {
evt.stopPropagation();
};

onKeyDown = evt => {
evt.stopPropagation();
};

render() {
const {
code,
previewBackgroundColor,
scope = {},
theme = 'dark'
} = this.props;

const useDarkTheme = theme === 'dark';

if (useDarkTheme) {
require('../themes/default/prism.dark.css');
} else {
require('../themes/default/prism.light.css');
}

return (
<PlaygroundProvider
className={`react-live-${useDarkTheme ? 'dark' : 'light'}`}
mountStylesheet={false}
code={(code || defaultCode).trim()}
scope={{ Component, ...scope }}
noInline
>
<PlaygroundRow>
<Title>Live Preview</Title>
<Title useDarkTheme={useDarkTheme}>Source Code</Title>
</PlaygroundRow>

<PlaygroundRow>
<PlaygroundColumn>
<PlaygroundPreview
previewBackgroundColor={previewBackgroundColor}
/>
<PlaygroundError />
</PlaygroundColumn>

<PlaygroundColumn>
<PlaygroundEditor
onKeyUp={this.onKeyUp}
onKeyDown={this.onKeyDown}
/>
</PlaygroundColumn>
</PlaygroundRow>
</PlaygroundProvider>
);
}

return (
<PlaygroundProvider
className={`react-live-${useDarkTheme ? 'dark' : 'light'}`}
mountStylesheet={false}
code={(code || defaultCode).trim()}
scope={{ Component, ...scope }}
noInline
>
<PlaygroundRow>
<Title>Live Preview</Title>
<Title useDarkTheme={useDarkTheme}>Source Code</Title>
</PlaygroundRow>

<PlaygroundRow>
<PlaygroundColumn>
<PlaygroundPreview
previewBackgroundColor={previewBackgroundColor}
/>
<PlaygroundError />
</PlaygroundColumn>

<PlaygroundColumn>
<PlaygroundEditor useDarkTheme={useDarkTheme} />
</PlaygroundColumn>
</PlaygroundRow>
</PlaygroundProvider>
);
};
}

ComponentPlayground.propTypes = {
code: PropTypes.string,
Expand Down