Skip to content

Commit 4e2a07f

Browse files
committed
Fix ComponentPlayground invalid props and key events
- Prevent passing invalid HTML attributes as props to emotion - Stop propagation of key events in the Editor to be able to use all keys (Otherwise space, arrows, etc, would still change slides/modes)
1 parent eaefcb5 commit 4e2a07f

File tree

1 file changed

+60
-45
lines changed

1 file changed

+60
-45
lines changed

src/components/component-playground.js

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ export const PlaygroundProvider = styled(LiveProvider)`
1818
overflow: hidden;
1919
`;
2020

21-
const PlaygroundPreview = styled(LivePreview)`
21+
const PlaygroundPreview = styled(({ className }) => (
22+
<LivePreview className={className} />
23+
))`
2224
padding: 0.5rem;
2325
min-height: 100%;
2426
25-
background: ${(props) => (
26-
props.previewBackgroundColor || '#fff'
27-
)};
27+
background: ${p => p.previewBackgroundColor || '#fff'};
2828
`;
2929

3030
const PlaygroundEditor = styled(LiveEditor)`
@@ -86,48 +86,63 @@ const PlaygroundError = styled(LiveError)`
8686
padding: 0.5rem;
8787
`;
8888

89-
const ComponentPlayground = ({
90-
code,
91-
previewBackgroundColor,
92-
scope = {},
93-
theme = 'dark'
94-
}) => {
95-
const useDarkTheme = theme === 'dark';
96-
97-
if (useDarkTheme) {
98-
require('../themes/default/prism.dark.css');
99-
} else {
100-
require('../themes/default/prism.light.css');
89+
class ComponentPlayground extends Component {
90+
onKeyUp = evt => {
91+
evt.stopPropagation();
92+
};
93+
94+
onKeyDown = evt => {
95+
evt.stopPropagation();
96+
};
97+
98+
render() {
99+
const {
100+
code,
101+
previewBackgroundColor,
102+
scope = {},
103+
theme = 'dark'
104+
} = this.props;
105+
106+
const useDarkTheme = theme === 'dark';
107+
108+
if (useDarkTheme) {
109+
require('../themes/default/prism.dark.css');
110+
} else {
111+
require('../themes/default/prism.light.css');
112+
}
113+
114+
return (
115+
<PlaygroundProvider
116+
className={`react-live-${useDarkTheme ? 'dark' : 'light'}`}
117+
mountStylesheet={false}
118+
code={(code || defaultCode).trim()}
119+
scope={{ Component, ...scope }}
120+
noInline
121+
>
122+
<PlaygroundRow>
123+
<Title>Live Preview</Title>
124+
<Title useDarkTheme={useDarkTheme}>Source Code</Title>
125+
</PlaygroundRow>
126+
127+
<PlaygroundRow>
128+
<PlaygroundColumn>
129+
<PlaygroundPreview
130+
previewBackgroundColor={previewBackgroundColor}
131+
/>
132+
<PlaygroundError />
133+
</PlaygroundColumn>
134+
135+
<PlaygroundColumn>
136+
<PlaygroundEditor
137+
onKeyUp={this.onKeyUp}
138+
onKeyDown={this.onKeyDown}
139+
/>
140+
</PlaygroundColumn>
141+
</PlaygroundRow>
142+
</PlaygroundProvider>
143+
);
101144
}
102-
103-
return (
104-
<PlaygroundProvider
105-
className={`react-live-${useDarkTheme ? 'dark' : 'light'}`}
106-
mountStylesheet={false}
107-
code={(code || defaultCode).trim()}
108-
scope={{ Component, ...scope }}
109-
noInline
110-
>
111-
<PlaygroundRow>
112-
<Title>Live Preview</Title>
113-
<Title useDarkTheme={useDarkTheme}>Source Code</Title>
114-
</PlaygroundRow>
115-
116-
<PlaygroundRow>
117-
<PlaygroundColumn>
118-
<PlaygroundPreview
119-
previewBackgroundColor={previewBackgroundColor}
120-
/>
121-
<PlaygroundError />
122-
</PlaygroundColumn>
123-
124-
<PlaygroundColumn>
125-
<PlaygroundEditor useDarkTheme={useDarkTheme} />
126-
</PlaygroundColumn>
127-
</PlaygroundRow>
128-
</PlaygroundProvider>
129-
);
130-
};
145+
}
131146

132147
ComponentPlayground.propTypes = {
133148
code: PropTypes.string,

0 commit comments

Comments
 (0)