1- import React , { useCallback , useMemo , useState } from 'react' ;
1+ import React , { ComponentProps , useCallback , useMemo , useState } from 'react' ;
22import '../ComponentPage.module.scss' ;
33import { LiveProvider , LivePreview } from 'react-live' ;
44import styles from './ContentItem.module.scss' ;
@@ -8,11 +8,12 @@ import CodeIcon from '../../assets/icons/code';
88
99type 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
3334const 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} ;
8085type 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+
8599export 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 }
0 commit comments