@@ -4,6 +4,7 @@ import {themes} from 'prism-react-renderer';
44import useDocusaurusContext from '@docusaurus/useDocusaurusContext' ;
55import BrowserOnly from '@docusaurus/BrowserOnly' ;
66import CodeBlock from '@theme/CodeBlock' ;
7+ import Button from 'react-native-ui-lib/Button' ;
78import ReactLiveScope from '../theme/ReactLiveScope' ;
89import { isComponentSupported } from '../utils/componentUtils' ;
910import useFormattedCode from '../hooks/useFormattedCode' ;
@@ -15,12 +16,8 @@ export default function UILivePreview({code: initialCode, componentName = undefi
1516 const [ iframeLoaded , setIframeLoaded ] = useState ( false ) ;
1617 const { siteConfig} = useDocusaurusContext ( ) ;
1718 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 ] ) ;
19+ const [ code , setCode ] = useState ( initialCode ) ;
20+ const { code : formattedCode } = useFormattedCode ( code , { printWidth : 100 } ) ;
2421
2522 useEffect ( ( ) => {
2623 if ( iframeLoaded ) {
@@ -33,6 +30,10 @@ export default function UILivePreview({code: initialCode, componentName = undefi
3330 iframeRef . current ?. contentWindow . postMessage ( message , '*' ) ;
3431 } ;
3532
33+ const handleFormat = ( ) => {
34+ setCode ( formattedCode ) ;
35+ } ;
36+
3637 if ( ! liveScopeSupport && ! isComponentSupported ( componentName ) ) {
3738 return < CodeBlock language = "jsx" > { code } </ CodeBlock > ;
3839 }
@@ -46,6 +47,15 @@ export default function UILivePreview({code: initialCode, componentName = undefi
4647 < LiveProvider code = { code } scope = { ReactLiveScope } theme = { themes . oceanicNext } >
4748 < div className = { styles . container } >
4849 < div className = { styles . codeContainer } >
50+ < div className = { styles . codeHeader } >
51+ < Button
52+ label = "Prettify"
53+ size = { Button . sizes . small }
54+ onPress = { handleFormat }
55+ backgroundColor = "#2d2d2d"
56+ borderRadius = { 4 }
57+ />
58+ </ div >
4959 < LiveEditor onChange = { setCode } className = { styles . liveEditor } />
5060 < div className = { styles . errorContainer } >
5161 < LiveError />
0 commit comments