Skip to content

Commit

Permalink
Add live tab -> implement renderLive() -> render renderLive()
Browse files Browse the repository at this point in the history
  • Loading branch information
Marvin9 committed Mar 14, 2020
1 parent cc9082e commit 56313ca
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src-docs/src/components/guide_section/guide_section.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types';
import { LiveProvider, LiveEditor, LivePreview, LiveError } from 'react-live';

import {
EuiCode,
Expand All @@ -18,6 +19,8 @@ import {
EuiTextColor,
EuiTitle,
EuiLink,
EuiFlexGroup,
EuiFlexItem,
} from '../../../../src/components';

function markup(text) {
Expand Down Expand Up @@ -99,6 +102,13 @@ export class GuideSection extends Component {

this.tabs = [];

if (props.live) {
this.tabs.push({
name: 'live',
displayName: 'Live',
});
}

if (props.demo) {
this.tabs.push({
name: 'demo',
Expand Down Expand Up @@ -408,6 +418,50 @@ export class GuideSection extends Component {
);
}

renderLive() {
let code = '<p>No code defined</p>';
const dependency = this.props.live.dependency;

if (this.props.live.snippet) {
code = '<>\n';
const snippet = this.props.live.snippet;
if (typeof snippet === 'object') {
code += snippet.join('\n');
code += '\n</>';
} else if (typeof snippet === 'string') code = snippet;
else code = '<p> Snippet not defined. </p>';
}

if (this.props.live.code) {
code = this.props.live.code;
}

const scope = dependency;

return (
<Fragment>
<EuiFlexGroup
style={{ marginTop: '10px' }}
direction={this.props.live.snippet ? 'row' : 'column'}>
<LiveProvider code={code} scope={scope}>
<EuiFlexItem>
<LiveEditor
style={{
border: '1px solid rgba(0, 0, 0, 0.2)',
fontSize: '14px',
}}
/>
</EuiFlexItem>
<EuiFlexItem>
<LivePreview />
<LiveError />
</EuiFlexItem>
</LiveProvider>
</EuiFlexGroup>
</Fragment>
);
}

renderContent() {
if (typeof this.state.selectedTab === 'undefined') {
return;
Expand All @@ -429,6 +483,10 @@ export class GuideSection extends Component {
return <EuiErrorBoundary>{this.renderProps()}</EuiErrorBoundary>;
}

if (this.state.selectedTab.name === 'live') {
return <EuiErrorBoundary> {this.renderLive()} </EuiErrorBoundary>;
}

return (
<EuiErrorBoundary>
<div>
Expand Down

0 comments on commit 56313ca

Please sign in to comment.