Skip to content

Commit

Permalink
make react-live client side
Browse files Browse the repository at this point in the history
  • Loading branch information
bruceharrison1984 committed Jul 24, 2023
1 parent 52237bc commit 78b7a77
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 94 deletions.
48 changes: 39 additions & 9 deletions apps/schedulely-docs/components/pre.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { Highlight, themes } from 'prism-react-renderer';
import { LiveEditor, LiveError, LivePreview, LiveProvider } from 'react-live';
import { Schedulely } from 'schedulely';
import { generateEvents } from './helpers.stories';
import React, { PropsWithChildren, isValidElement } from 'react';
import React, {
PropsWithChildren,
isValidElement,
useEffect,
useState,
} from 'react';
import ReactDOMServer from 'react-dom/server';
import he from 'he';

type ClientSideLiveReloadElements = {
liveProvider: any;
livePreview: any;
liveEditor: any;
liveError: any;
};

export const LivePre = (props: PropsWithChildren) => {
const children = props.children as JSX.Element;
const options = children.props.className.split(',');
Expand All @@ -15,24 +26,43 @@ export const LivePre = (props: PropsWithChildren) => {
ReactDOMServer.renderToString(children.props.children)
);

if (live && isValidElement(props.children) && children.type.name === 'Code') {
const [reactLiveComponents, setReactLiveComponents] = useState<
ClientSideLiveReloadElements | undefined
>();

useEffect(() => {
import('react-live').then((x) =>
setReactLiveComponents({
liveEditor: x.LiveEditor,
livePreview: x.LivePreview,
liveProvider: x.LiveProvider,
liveError: x.LiveError,
})
);
}, []);

if (
live &&
reactLiveComponents &&
isValidElement(props.children) &&
children.type.name === 'Code'
) {
return (
<LiveProvider
<reactLiveComponents.liveProvider
code={code}
language={language}
scope={{ React, generateEvents, Schedulely }} // <-- inject objects you need access to
noInline={true}
enableTypeScript={true}
theme={themes.vsDark}
>
<LivePreview />
<LiveError />
<LiveEditor
<reactLiveComponents.livePreview />
<reactLiveComponents.liveError />
<reactLiveComponents.liveEditor
code={code}
language={language}
style={{ borderRadius: '0.5em', overflow: 'hidden' }}
/>
</LiveProvider>
</reactLiveComponents.liveProvider>
);
}

Expand Down
3 changes: 3 additions & 0 deletions apps/schedulely-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@
},
"devDependencies": {
"@types/node": "18.11.10"
},
"overrides": {
"prism-react-renderer": "^2.0.6"
}
}
Loading

0 comments on commit 78b7a77

Please sign in to comment.