Skip to content

Commit 6df613e

Browse files
authored
Added jsx rendering in docs (#3579)
* Added jsx rendering in docs * Refactor Figma embed function to simplify parameters * Update getImage function to make style parameter optional * Rename 'jsx' prop to 'snippet' in ComponentItem and ContentItem components for clarity * Replace image URL with JSX snippet in test.api.json for improved component representation * Update snippet description in test.api.json for clarity
1 parent 1386fa2 commit 6df613e

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

docuilib/src/components/pageComponents/ContentItem.tsx

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useCallback, useMemo, useState} from 'react';
1+
import React, {ComponentProps, useCallback, useMemo, useState} from 'react';
22
import '../ComponentPage.module.scss';
33
import {LiveProvider, LivePreview} from 'react-live';
44
import styles from './ContentItem.module.scss';
@@ -8,11 +8,12 @@ import CodeIcon from '../../assets/icons/code';
88

99
type ComponentItemProps = {
1010
componentName: string;
11-
props: Record<string, unknown> | Record<string, unknown>[];
11+
props?: Record<string, unknown> | Record<string, unknown>[];
12+
snippet?: string
1213
showCodeButton?: boolean;
1314
};
1415

15-
function generateComponentCodeSnippet(componentName, componentProps) {
16+
function generateComponentCodeSnippet(componentName: string, componentProps: Record<string, unknown>) {
1617
const propString = Object.keys(componentProps).reduce((acc, key) => {
1718
let propValue = componentProps[key];
1819
switch (typeof propValue) {
@@ -31,19 +32,21 @@ function generateComponentCodeSnippet(componentName, componentProps) {
3132
}
3233

3334
const ComponentItem = (props: ComponentItemProps) => {
34-
const {componentName, props: componentProps, showCodeButton = false} = props;
35+
const {componentName, props: componentProps, snippet, showCodeButton = false} = props;
3536
const [showCode, setShowCode] = useState(false);
3637

3738
const code = useMemo(() => {
38-
if (Array.isArray(componentProps)) {
39+
if (typeof snippet === 'string') {
40+
return snippet;
41+
} else if (Array.isArray(componentProps)) {
3942
const snippet = componentProps
4043
.map(componentPropsItem => generateComponentCodeSnippet(componentName, componentPropsItem))
4144
.join('');
4245
return `<View center gap-s1>${snippet}</View>`;
4346
} else {
4447
return generateComponentCodeSnippet(componentName, componentProps);
4548
}
46-
}, [componentName, componentProps]);
49+
}, [componentName, componentProps, snippet]);
4750

4851
const toggleCode = useCallback(() => {
4952
setShowCode(prev => !prev);
@@ -76,21 +79,30 @@ type Item = {
7679
component?: string;
7780
props?: any;
7881
value?: any;
82+
snippet?: string;
83+
height?: number;
7984
};
8085
type ContentItemProps = {
8186
item: Item;
8287
componentName: string;
8388
showCodeButton?: boolean;
8489
};
90+
91+
const extractComponentFromSnippet = (snippet: string) => {
92+
if (!snippet.startsWith('<')) {
93+
return;
94+
}
95+
const firstWord = snippet.split(' ')[0];
96+
return firstWord.slice(1);
97+
};
98+
8599
export const ContentItem = ({item, componentName, showCodeButton}: ContentItemProps) => {
86-
const getFigmaEmbed = item => {
87-
const value = item.value;
88-
const height = item.height || 450;
100+
const getFigmaEmbed = (value: string, height = 450) => {
89101

90102
return <iframe width={'100%'} height={height} src={value}/>;
91103
};
92104

93-
const getImage = (value, style = undefined) => {
105+
const getImage = (value: string, style?: ComponentProps<'img'>['style']) => {
94106
return (
95107
<div className={styles.image}>
96108
<img src={value} style={{display: 'block', ...style}}/>
@@ -100,12 +112,14 @@ export const ContentItem = ({item, componentName, showCodeButton}: ContentItemPr
100112

101113
const value = item.value;
102114

103-
if (item.props) {
104-
const name = item.component ?? componentName;
115+
if (item.props || item.snippet) {
116+
const name = item.snippet ? extractComponentFromSnippet(item.snippet) : item.component ?? componentName;
105117
const isComponentExists = !!ReactLiveScope[name];
106118

107119
if (isComponentExists) {
108-
return <ComponentItem componentName={name} props={item.props} showCodeButton={showCodeButton}/>;
120+
return (
121+
<ComponentItem componentName={name} props={item.props} snippet={item.snippet} showCodeButton={showCodeButton}/>
122+
);
109123
} else if (!value) {
110124
return <div style={{color: 'red'}}>Component Not Supported</div>;
111125
}
@@ -114,7 +128,7 @@ export const ContentItem = ({item, componentName, showCodeButton}: ContentItemPr
114128
if (value) {
115129
if (typeof value === 'string') {
116130
if (value.includes('embed.figma.com')) {
117-
return getFigmaEmbed(item);
131+
return getFigmaEmbed(value, item.height);
118132
} else {
119133
return getImage(value);
120134
}

src/components/test.api.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
"description": "Item #2 Subtitle",
144144
"content": [
145145
{
146-
"value": "https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/Docs/thumbnail-horizontal.png?raw=true"
146+
"snippet": "<Text text30>This is a code snippet</Text>"
147147
}
148148
]
149149
},

0 commit comments

Comments
 (0)