import React from 'react' import ReactDOM from 'react-dom'; import { LiveEditor, LiveProvider, LivePreview, LiveError } from 'react-live'; import { PrismTheme } from 'prism-react-renderer'; import Components from '../../exports'; import { css } from '@emotion/css'; type Props = { codeText: string scope?: {} isOpenByDefault?: boolean noInline?: boolean } type State = { uniqueId: string isCodeCollapsed: boolean hasError: boolean codeText: string } const editorTheme: PrismTheme = { plain: { fontSize: '14px' }, styles: [] } // until // https://reactjs.org/docs/hooks-faq.html#do-hooks-cover-all-use-cases-for-classes // is implemented this has to be a class component class Playground extends React.Component { static defaultProps = { noInline: false, isOpenByDefault: true } constructor(props: Props) { super(props); this.state = { uniqueId: "asd", isCodeCollapsed: !props.isOpenByDefault, hasError: false, codeText: props.codeText } } componentDidCatch() { // Display fallback UI this.setState({ hasError: true }) } handleToggle = () => { this.setState(({ isCodeCollapsed }) => ({ isCodeCollapsed: !isCodeCollapsed })) } renderError = () => { return (

Oops, something went wrong in with this live preview.
Please reload the page and try again.

) } handleChange = (codeText: string) => { this.setState({ codeText }) } render() { const { scope, noInline } = this.props const { codeText, hasError, isCodeCollapsed, uniqueId } = this.state if (hasError) return this.renderError() return (
{!isCodeCollapsed && (
)}
{isCodeCollapsed ? 'Show code' : 'Hide code'}
) } } export default Playground