1
- import React , { useEffect , useRef , useState , useMemo } from 'react' ;
2
- import { StyleSheet } from 'react-native ' ;
3
- import { LiveProvider , LiveEditor } from 'react-live ' ;
1
+ import React , { useEffect , useRef , useState } from 'react' ;
2
+ import { LiveProvider , LiveEditor , LiveError } from 'react-live ' ;
3
+ import { themes } from 'prism- react-renderer ' ;
4
4
import useDocusaurusContext from '@docusaurus/useDocusaurusContext' ;
5
5
import BrowserOnly from '@docusaurus/BrowserOnly' ;
6
6
import CodeBlock from '@theme/CodeBlock' ;
7
- import { View , Colors } from 'react-native-ui-lib/core' ;
8
7
import ReactLiveScope from '../theme/ReactLiveScope' ;
9
8
import { isComponentSupported } from '../utils/componentUtils' ;
9
+ import useFormattedCode from '../hooks/useFormattedCode' ;
10
+ import styles from './UILivePreview.module.scss' ;
10
11
11
12
export const IFRAME_MESSAGE_TYPE = 'LIVE_PREVIEW_CODE_UPDATE_MESSAGE' ;
12
13
13
- export default function UILivePreview ( { code : codeProp , componentName = undefined , liveScopeSupport = false } ) {
14
- const [ code , setCode ] = useState ( codeProp ) ;
14
+ export default function UILivePreview ( { code : initialCode , componentName = undefined , liveScopeSupport = false } ) {
15
15
const [ iframeLoaded , setIframeLoaded ] = useState ( false ) ;
16
16
const { siteConfig} = useDocusaurusContext ( ) ;
17
17
const iframeRef = useRef ( null ) ;
18
+ const { code : formattedCode } = useFormattedCode ( initialCode , { printWidth : 100 } ) ;
19
+ const [ code , setCode ] = useState ( formattedCode ) ;
20
+
21
+ useEffect ( ( ) => {
22
+ setCode ( formattedCode ) ;
23
+ } , [ formattedCode ] ) ;
18
24
19
25
useEffect ( ( ) => {
20
26
if ( iframeLoaded ) {
@@ -27,10 +33,6 @@ export default function UILivePreview({code: codeProp, componentName = undefined
27
33
iframeRef . current ?. contentWindow . postMessage ( message , '*' ) ;
28
34
} ;
29
35
30
- const liveEditorStyle = useMemo ( ( ) => {
31
- return { overflowY : 'scroll' , scrollbarWidth : 'none' } ;
32
- } , [ ] ) ;
33
-
34
36
if ( ! liveScopeSupport && ! isComponentSupported ( componentName ) ) {
35
37
return < CodeBlock language = "jsx" > { code } </ CodeBlock > ;
36
38
}
@@ -41,59 +43,27 @@ export default function UILivePreview({code: codeProp, componentName = undefined
41
43
const iframeSource = `${ window . location . origin } ${ siteConfig ?. baseUrl } livePreview` ;
42
44
43
45
return (
44
- < View row gap-s2 style = { styles . liveCodeWrapper } >
45
- < LiveProvider code = { code } scope = { ReactLiveScope } >
46
- < View flex style = { styles . editorWrapper } >
47
- < LiveEditor
48
- className = "font-mono"
49
- onChange = { setCode }
50
- //@ts -ignore
51
- style = { liveEditorStyle }
52
- />
53
- </ View >
54
- < View bg-$backgroundDefault margin-s2 style = { styles . iframeWrapper } >
46
+ < LiveProvider code = { code } scope = { ReactLiveScope } theme = { themes . oceanicNext } >
47
+ < div className = { styles . container } >
48
+ < div className = { styles . codeContainer } >
49
+ < LiveEditor onChange = { setCode } className = { styles . liveEditor } />
50
+ < div className = { styles . errorContainer } >
51
+ < LiveError />
52
+ </ div >
53
+ </ div >
54
+ < div className = { styles . preview } >
55
55
< iframe
56
56
ref = { iframeRef }
57
- style = { styles . iframe }
57
+ className = { styles . iframe }
58
58
src = { iframeSource }
59
59
title = "Simulator"
60
60
onLoad = { ( ) => setIframeLoaded ( true ) }
61
61
/>
62
- </ View >
63
- </ LiveProvider >
64
- </ View >
62
+ </ div >
63
+ </ div >
64
+ </ LiveProvider >
65
65
) ;
66
66
} }
67
67
</ BrowserOnly >
68
68
) ;
69
69
}
70
-
71
- const styles = StyleSheet . create ( {
72
- liveCodeWrapper : {
73
- borderRadius : 20 ,
74
- borderWidth : 1 ,
75
- backgroundColor : '#011627' ,
76
- height : 725 ,
77
- width : 900
78
- } ,
79
- editorWrapper : { maxHeight : 700 , padding : 10 , borderRadius : 20 , overflow : 'hidden' } ,
80
- iframeWrapper : {
81
- alignSelf : 'center' ,
82
- overflow : 'hidden' ,
83
- borderRadius : 40 ,
84
- borderWidth : 4 ,
85
- borderColor : Colors . $outlineDisabledHeavy ,
86
- width : 320 ,
87
- height : 700
88
- } ,
89
- iframe : {
90
- width : 335 , // Slightly wider to hide scrollbar
91
- height : '100%' ,
92
- position : 'absolute' ,
93
- top : 0 ,
94
- left : 0 ,
95
- border : 0 ,
96
- padding : 10 ,
97
- background : 'transparent'
98
- }
99
- } ) ;
0 commit comments