Skip to content

Commit 0950f3e

Browse files
adids1221ethanshar
andauthored
Usage component support live preview for compatible components (#3581)
* Usage component support live preview for compatible components * Enhance UILivePreview to support codeBlock when react live scope not supported * Refactor UILivePreview component to use destructuring for props * Add default value for componentName prop in UILivePreview --------- Co-authored-by: Ethan Sharabi <[email protected]>
1 parent 0cf3b1f commit 0950f3e

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

docuilib/src/components/UILivePreview.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
55
import BrowserOnly from '@docusaurus/BrowserOnly';
66
import {View, Colors} from 'react-native-ui-lib/core';
77
import ReactLiveScope from '../theme/ReactLiveScope';
8+
import CodeBlock from '@theme/CodeBlock';
89

910
export const IFRAME_MESSAGE_TYPE = 'LIVE_PREVIEW_CODE_UPDATE_MESSAGE';
1011

11-
export default function UILivePreview({code: codeProp}) {
12+
export default function UILivePreview({code: codeProp, componentName = undefined, liveScopeSupport = false}) {
1213
const [code, setCode] = useState(codeProp);
1314
const [iframeLoaded, setIframeLoaded] = useState(false);
1415
const {siteConfig} = useDocusaurusContext();
1516
const iframeRef = useRef(null);
1617

18+
const supportedComponentNames = Object.keys(ReactLiveScope);
19+
const componentLivePlaygroundSupport =
20+
liveScopeSupport || (componentName && supportedComponentNames.includes(componentName));
21+
1722
useEffect(() => {
1823
if (iframeLoaded) {
1924
sendMessageToIframe(code);
@@ -29,6 +34,10 @@ export default function UILivePreview({code: codeProp}) {
2934
return {overflowY: 'scroll', scrollbarWidth: 'none'};
3035
}, []);
3136

37+
if (!componentLivePlaygroundSupport) {
38+
return <CodeBlock language="jsx">{code}</CodeBlock>;
39+
}
40+
3241
return (
3342
<BrowserOnly>
3443
{() => {

docuilib/src/components/pageComponents/Usage.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ import _ from 'lodash';
22
import React from 'react';
33
import CodeBlock from '@theme/CodeBlock';
44
import '../ComponentPage.module.scss';
5+
import ReactLiveScope from '../../theme/ReactLiveScope';
6+
import UILivePreview from '../UILivePreview';
57

68
export const Usage = ({component}) => {
9+
const supportedComponentNames = Object.keys(ReactLiveScope);
10+
const componentLivePlaygroundSupport = supportedComponentNames.includes(component.name);
711
if (component.snippet) {
8-
return (
9-
<div>
10-
<CodeBlock language="jsx">
11-
{component.snippet?.map(item => _.replace(item, new RegExp(/\$[1-9]/, 'g'), '')).join('\n')}
12-
</CodeBlock>
13-
</div>
12+
const code = component.snippet.map(item => _.replace(item, new RegExp(/\$[1-9]/, 'g'), '')).join('\n');
13+
return componentLivePlaygroundSupport ? (
14+
<UILivePreview code={code} liveScopeSupport/>
15+
) : (
16+
<CodeBlock language="jsx">{code}</CodeBlock>
1417
);
1518
}
1619
};

scripts/docs/buildDocsCommon.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ function buildOldDocs(component) {
193193
/* Snippet */
194194
if (component.snippet) {
195195
content += `### Usage\n`;
196-
content += `<UILivePreview code={\`${component.snippet
196+
content += `<UILivePreview componentName={"${component.name}"} code={\`${component.snippet
197197
?.map(item => _.replace(item, new RegExp(/\$[1-9]/, 'g'), ''))
198198
.join('\n')
199199
.toString()}\`}/>\n\n`;

0 commit comments

Comments
 (0)