Skip to content

Usage component support live preview for compatible components #3581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion docuilib/src/components/UILivePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import BrowserOnly from '@docusaurus/BrowserOnly';
import {View, Colors} from 'react-native-ui-lib/core';
import ReactLiveScope from '../theme/ReactLiveScope';
import CodeBlock from '@theme/CodeBlock';

export const IFRAME_MESSAGE_TYPE = 'LIVE_PREVIEW_CODE_UPDATE_MESSAGE';

export default function UILivePreview({code: codeProp}) {
export default function UILivePreview({code: codeProp, componentName = undefined, liveScopeSupport = false}) {
const [code, setCode] = useState(codeProp);
const [iframeLoaded, setIframeLoaded] = useState(false);
const {siteConfig} = useDocusaurusContext();
const iframeRef = useRef(null);

const supportedComponentNames = Object.keys(ReactLiveScope);
const componentLivePlaygroundSupport =
liveScopeSupport || (componentName && supportedComponentNames.includes(componentName));

useEffect(() => {
if (iframeLoaded) {
sendMessageToIframe(code);
Expand All @@ -29,6 +34,10 @@ export default function UILivePreview({code: codeProp}) {
return {overflowY: 'scroll', scrollbarWidth: 'none'};
}, []);

if (!componentLivePlaygroundSupport) {
return <CodeBlock language="jsx">{code}</CodeBlock>;
}

return (
<BrowserOnly>
{() => {
Expand Down
15 changes: 9 additions & 6 deletions docuilib/src/components/pageComponents/Usage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import _ from 'lodash';
import React from 'react';
import CodeBlock from '@theme/CodeBlock';
import '../ComponentPage.module.scss';
import ReactLiveScope from '../../theme/ReactLiveScope';
import UILivePreview from '../UILivePreview';

export const Usage = ({component}) => {
const supportedComponentNames = Object.keys(ReactLiveScope);
const componentLivePlaygroundSupport = supportedComponentNames.includes(component.name);
if (component.snippet) {
return (
<div>
<CodeBlock language="jsx">
{component.snippet?.map(item => _.replace(item, new RegExp(/\$[1-9]/, 'g'), '')).join('\n')}
</CodeBlock>
</div>
const code = component.snippet.map(item => _.replace(item, new RegExp(/\$[1-9]/, 'g'), '')).join('\n');
return componentLivePlaygroundSupport ? (
<UILivePreview code={code} liveScopeSupport/>
) : (
<CodeBlock language="jsx">{code}</CodeBlock>
);
}
};
2 changes: 1 addition & 1 deletion scripts/docs/buildDocsCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function buildOldDocs(component) {
/* Snippet */
if (component.snippet) {
content += `### Usage\n`;
content += `<UILivePreview code={\`${component.snippet
content += `<UILivePreview componentName={"${component.name}"} code={\`${component.snippet
?.map(item => _.replace(item, new RegExp(/\$[1-9]/, 'g'), ''))
.join('\n')
.toString()}\`}/>\n\n`;
Expand Down