File size: 2,017 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { Button, Gridicon } from '@automattic/components';
import clsx from 'clsx';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { LiveProvider, LiveEditor, LiveError, LivePreview } from 'react-live';
import ClipboardButton from 'calypso/components/forms/clipboard-button';
import DocsExampleWrapper from 'calypso/devdocs/docs-example/wrapper';

class ComponentPlayground extends Component {
	static propTypes = {
		code: PropTypes.string,
	};

	state = {
		showCode: false,
	};

	handleClick() {
		alert( 'Copied to clipboard!' );
	}

	showCode = () => {
		this.setState( {
			showCode: ! this.state.showCode,
		} );
	};

	render() {
		const toggleCode = this.props.code.length > 200;
		const codeClassName = clsx( {
			'design__component-playground-code': true,
			'show-code': toggleCode ? this.state.showCode : true,
		} );
		const scope = require( 'calypso/devdocs/design/playground-scope' );

		return (
			<LiveProvider
				code={ this.props.code }
				scope={ scope }
				mountStylesheet={ false }
				className="design__component-playground"
			>
				<DocsExampleWrapper
					name={ this.props.name }
					unique={ this.props.unique }
					url={ this.props.url }
				>
					<LivePreview />
				</DocsExampleWrapper>

				{ this.props.component && (
					<div className={ codeClassName }>
						<ClipboardButton
							text={ this.props.code }
							borderless
							onClick={ this.handleClick }
							className="design__component-playground-clipboard"
						>
							<Gridicon icon="clipboard" />
						</ClipboardButton>

						<LiveError />
						<LiveEditor />
					</div>
				) }

				{ this.props.component && toggleCode && (
					<div className="design__component-playground-show-code">
						<Button onClick={ this.showCode }>
							{ this.state.showCode ? 'Hide' : 'Show' } code <Gridicon icon="code" />
						</Button>
					</div>
				) }
			</LiveProvider>
		);
	}
}

ComponentPlayground.displayName = 'ComponentPlayground';
export default ComponentPlayground;