Skip to content

Commit

Permalink
refactor: fully migrate to CodeBlock in ReferenceExample
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jul 8, 2020
1 parent f1a8db7 commit 1bceb66
Showing 1 changed file with 19 additions and 64 deletions.
83 changes: 19 additions & 64 deletions src/components/ReferenceExample.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import React, { useMemo } from 'react';
import React, { useCallback } from 'react';
import PropTypes from 'prop-types';
import formatCode from '../utils/formatCode';
import lightTheme from 'prism-react-renderer/themes/github';
import darkTheme from 'prism-react-renderer/themes/nightOwl';
import { LiveEditor, LiveError, LiveProvider } from 'react-live';
import styles from './ReferenceExample.module.scss';
import ReferencePreview from './ReferencePreview';
import useDarkMode from 'use-dark-mode';
import CodeBlock from './CodeBlock';

const platformStateContextMock = {
Expand All @@ -21,9 +16,12 @@ const nerdletStateContextMock = {
entityGuid: 'MTIzNDU2fEZPT3xCQVJ8OTg3NjU0MzIx',
};

const TRAILING_SEMI = /;\s*$/;

const USE_CODE_BLOCK = true;
const SCOPE = {
...window.__NR1_SDK__.default,
navigation: {
getOpenLauncherLocation: () => {},
},
};

const ReferenceExample = ({
className,
Expand All @@ -36,27 +34,9 @@ const ReferenceExample = ({
NerdletStateContext,
} = window.__NR1_SDK__.default;
const { live } = example.options;
let formattedCode;
const darkMode = useDarkMode();
const scope = useMemo(
() => ({
...window.__NR1_SDK__.default,
navigation: {
// eslint-disable-next-line no-empty-function
getOpenLauncherLocation() {},
},
}),
[]
);

try {
formattedCode = formatCode(example.sourceCode).replace(TRAILING_SEMI, '');
} catch (e) {
formattedCode = example.sourceCode;
}

const Preview = useMemo(
() => ({ className }) => (
const Preview = useCallback(
({ className }) => (
<ReferencePreview
className={className}
style={previewStyle}
Expand All @@ -71,41 +51,16 @@ const ReferenceExample = ({
<NerdletStateContext.Provider value={nerdletStateContextMock}>
<div className={className}>
<h3 className={styles.title}>{example.label}</h3>
{USE_CODE_BLOCK ? (
<CodeBlock
lineNumbers
language="jsx"
live={live}
preview={live}
scope={scope}
components={{ Preview }}
>
{example.sourceCode}
</CodeBlock>
) : (
<LiveProvider
scope={scope}
code={formattedCode}
theme={darkMode.value ? darkTheme : lightTheme}
disabled={!live}
>
{live && (
<ReferencePreview
className={styles.preview}
style={previewStyle}
useToastManager={useToastManager}
/>
)}
<LiveEditor
style={{
fontSize: '0.75rem',
maxHeight: '30rem',
overflow: 'auto',
}}
/>
{live && <LiveError className={styles.error} />}
</LiveProvider>
)}
<CodeBlock
lineNumbers
language="jsx"
live={live}
preview={live}
scope={SCOPE}
components={{ Preview }}
>
{example.sourceCode}
</CodeBlock>
</div>
</NerdletStateContext.Provider>
</PlatformStateContext.Provider>
Expand Down

0 comments on commit 1bceb66

Please sign in to comment.