Skip to content

Instantly share code, notes, and snippets.

@Swapnull
Last active November 28, 2020 18:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swapnull/b25fc0d408c9caade4bd3be59e3beb62 to your computer and use it in GitHub Desktop.
Save Swapnull/b25fc0d408c9caade4bd3be59e3beb62 to your computer and use it in GitHub Desktop.
Code Ouput for docz
// @flow
import React from 'react';
import SyntaxHighlighter from 'react-syntax-highlighter/prism';
import { dark } from 'react-syntax-highlighter/styles/prism';
import { renderToStaticMarkup } from 'react-dom/server';
import pretty from 'pretty';
import jsxToString from 'jsx-to-string';
type Props = { children: any, showReact?: boolean, showHTML?: boolean, showIcon?: boolean };
type State = { active: number };
class CodeOutput extends React.Component<Props, State> {
state = { active: 1 }
displayName="CodeOutput";
render () {
const { active } = this.state;
const { children, showReact=true, showHTML=true, showIcon=true } = this.props;
return (
<div>
<div className="tabs">
<ul>
{ showReact && <li className={ active === 1 && "is-active" }><a onClick={() => this.setState({ active: 1 })}>React</a></li> }
{ showHTML && <li className={ active === 2 && "is-active" }><a onClick={() => this.setState({ active: 2 })}>HTML</a> </li> }
</ul>
</div>
{ showIcon && this.props.children}
{ active === 1 && showReact && <SyntaxHighlighter language="jsx" style={dark}>{ Array.isArray(children) ? children.map(child => jsxToString(child)).join('\n') : jsxToString(children) }</SyntaxHighlighter> }
{ active === 2 && showHTML && <SyntaxHighlighter language="html" style={dark}>{ pretty(renderToStaticMarkup(children)) } </SyntaxHighlighter>}
</div>
)
}
}
export default CodeOutput;
name route
Button
/button

import Button from './Button.jsx' import CodeOutput from '../helpers/CodeOutput.jsx';

Button

Basic Example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment