Skip to content

Commit

Permalink
refactor: Move component preview code into own component
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jun 3, 2020
1 parent f0b1969 commit dd0806b
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 109 deletions.
117 changes: 8 additions & 109 deletions src/components/ComponentExample.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React, { useState } from 'react';
import React from 'react';
import PropTypes from 'prop-types';
import formatCode from '../utils/formatCode';
import github from 'prism-react-renderer/themes/github';
import { LiveEditor, LiveError, LiveProvider, LivePreview } from 'react-live';
import { LiveEditor, LiveError, LiveProvider } from 'react-live';
import styles from './ComponentExample.module.scss';
import root from 'react-shadow';
import { CSS_BUNDLE } from '../utils/sdk';
import ComponentPreview from './ComponentPreview';

const platformStateContextMock = {
timeRange: {
Expand All @@ -21,96 +20,12 @@ const nerdletStateContextMock = {

const TRAILING_SEMI = /;\s*$/;

const EXAMPLE_CSS = `
.nr1-ComponentExample {
line-height: 1.36;
font-weight: 400;
background-color: #fff;
color: #000e0e;
font-size: 12px;
font-family: Open Sans,Segoe UI,Tahoma,sans-serif;
}
.nr1-ComponentExample-ToastManager > div {
position: fixed;
top: 0;
right: 0;
bottom: 0;
display: flex;
flex-direction: column;
padding-top: 16px;
padding-right: 16px;
pointer-events: none;
z-index: 200;
min-height: 9999px;
}
.nr1-Docs-prettify > * {
margin-right: 0.5rem;
}
.nr1-Docs-prettify > *:not(:first-child) {
margin-left: 0.5rem;
}
.nr1-Box,
.nr1-RedBox,
.nr1-Box--a,
.nr1-Box--b,
.nr1-Box--c {
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
padding: 0.5rem;
width: 100%;
height: 100%;
}
.nr1-RedBox {
color: red;
background: rgba(255, 0, 0, 0.15);
box-shadow: inset 0 0 0 1px rgba(255, 0, 0, 0.5);
margin-bottom: 0.5rem;
}
[class^=nr1-Example--stack--direction],
[class^=nr1-Example--stack--horizontal],
[class^=nr1-Example--stack--gap],
[class^=nr1-Example--stack--vertical] {
margin-bottom: 1.25rem;
}
.nr1-Example--stack--title {
display: block;
font-size: 1.5em;
margin-bottom: 1.25rem;
}
.nr1-Box--a {
min-height: 70px;
min-width: 70px;
}
.nr1-Box--b {
min-height: 90px;
min-width: 40px;
}
.nr1-Box--c {
min-height: 40px;
min-width: 90px;
}
`;

const ComponentExample = ({
className,
example,
useToastManager,
previewStyle,
}) => {
const [stylesLoaded, setStylesLoaded] = useState(false);
const ToastManager = window.__NR1_SDK__.ToastManager;
const {
PlatformStateContext,
NerdletStateContext,
Expand Down Expand Up @@ -142,27 +57,11 @@ const ComponentExample = ({
disabled={!live}
>
{live && (
<root.div className={styles.preview}>
<link
rel="stylesheet"
href={CSS_BUNDLE}
onLoad={() => setStylesLoaded(true)}
/>
<style type="text/css">{EXAMPLE_CSS}</style>
{useToastManager && (
<div className="nr1-ComponentExample-ToastManager">
<ToastManager />
</div>
)}
{stylesLoaded ? (
<LivePreview
className="nr1-ComponentExample"
style={previewStyle}
/>
) : (
'Loading...'
)}
</root.div>
<ComponentPreview
className={styles.preview}
style={previewStyle}
useToastManager={useToastManager}
/>
)}
<LiveEditor style={{ fontSize: '0.75rem' }} />
{live && <LiveError className={styles.error} />}
Expand Down
121 changes: 121 additions & 0 deletions src/components/ComponentPreview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import root from 'react-shadow';
import { LivePreview } from 'react-live';
import { CSS_BUNDLE } from '../utils/sdk';

const EXAMPLE_CSS = `
.nr1-ComponentExample {
line-height: 1.36;
font-weight: 400;
background-color: #fff;
color: #000e0e;
font-size: 12px;
font-family: Open Sans,Segoe UI,Tahoma,sans-serif;
}
.nr1-ComponentExample-ToastManager > div {
position: fixed;
top: 0;
right: 0;
bottom: 0;
display: flex;
flex-direction: column;
padding-top: 16px;
padding-right: 16px;
pointer-events: none;
z-index: 200;
min-height: 9999px;
}
.nr1-Docs-prettify > * {
margin-right: 0.5rem;
}
.nr1-Docs-prettify > *:not(:first-child) {
margin-left: 0.5rem;
}
.nr1-Box,
.nr1-RedBox,
.nr1-Box--a,
.nr1-Box--b,
.nr1-Box--c {
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
padding: 0.5rem;
width: 100%;
height: 100%;
}
.nr1-RedBox {
color: red;
background: rgba(255, 0, 0, 0.15);
box-shadow: inset 0 0 0 1px rgba(255, 0, 0, 0.5);
margin-bottom: 0.5rem;
}
[class^=nr1-Example--stack--direction],
[class^=nr1-Example--stack--horizontal],
[class^=nr1-Example--stack--gap],
[class^=nr1-Example--stack--vertical] {
margin-bottom: 1.25rem;
}
.nr1-Example--stack--title {
display: block;
font-size: 1.5em;
margin-bottom: 1.25rem;
}
.nr1-Box--a {
min-height: 70px;
min-width: 70px;
}
.nr1-Box--b {
min-height: 90px;
min-width: 40px;
}
.nr1-Box--c {
min-height: 40px;
min-width: 90px;
}
`;

const ComponentPreview = ({ className, style, useToastManager }) => {
const [stylesLoaded, setStylesLoaded] = useState(false);
const { ToastManager } = window.__NR1_SDK__;

return (
<root.div className={className}>
<link
rel="stylesheet"
href={CSS_BUNDLE}
onLoad={() => setStylesLoaded(true)}
/>
<style type="text/css">{EXAMPLE_CSS}</style>
{useToastManager && (
<div className="nr1-ComponentExample-ToastManager">
<ToastManager />
</div>
)}
{stylesLoaded ? (
<LivePreview className="nr1-ComponentExample" style={style} />
) : (
'Loading...'
)}
</root.div>
);
};

ComponentPreview.propTypes = {
className: PropTypes.string,
style: PropTypes.object,
useToastManager: PropTypes.bool,
};

export default ComponentPreview;

0 comments on commit dd0806b

Please sign in to comment.