|
| 1 | +import React, {useEffect, useRef, useState, useMemo} from 'react'; |
| 2 | +import {StyleSheet} from 'react-native'; |
| 3 | +import {LiveProvider, LiveEditor} from 'react-live'; |
| 4 | +import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; |
| 5 | +import {View, Colors} from 'react-native-ui-lib/core'; |
| 6 | +import ReactLiveScope from '../theme/ReactLiveScope'; |
| 7 | + |
| 8 | +export const IFRAME_MESSAGE_TYPE = 'LIVE_PREVIEW_CODE_UPDATE_MESSAGE'; |
| 9 | + |
| 10 | +export default function UILivePreview({code: codeProp}) { |
| 11 | + const [code, setCode] = useState(codeProp); |
| 12 | + const [iframeLoaded, setIframeLoaded] = useState(false); |
| 13 | + const {siteConfig} = useDocusaurusContext(); |
| 14 | + const iframeRef = useRef(null); |
| 15 | + const iframeSource = siteConfig?.customFields?.livePreviewSource as string; |
| 16 | + |
| 17 | + useEffect(() => { |
| 18 | + if (iframeLoaded) { |
| 19 | + sendMessageToIframe(code); |
| 20 | + } |
| 21 | + }, [iframeLoaded, code]); |
| 22 | + |
| 23 | + const sendMessageToIframe = code => { |
| 24 | + const message = {type: IFRAME_MESSAGE_TYPE, code}; |
| 25 | + iframeRef.current?.contentWindow.postMessage(message, '*'); |
| 26 | + }; |
| 27 | + |
| 28 | + const liveEditorStyle = useMemo(() => { |
| 29 | + return {overflowY: 'scroll', scrollbarWidth: 'none'}; |
| 30 | + }, []); |
| 31 | + |
| 32 | + return ( |
| 33 | + <View row gap-s2 style={styles.liveCodeWrapper}> |
| 34 | + <LiveProvider code={code} scope={ReactLiveScope}> |
| 35 | + <View flex style={styles.editorWrapper}> |
| 36 | + <LiveEditor |
| 37 | + className="font-mono" |
| 38 | + onChange={setCode} |
| 39 | + //@ts-ignore |
| 40 | + style={liveEditorStyle} |
| 41 | + /> |
| 42 | + </View> |
| 43 | + <View bg-$backgroundDefault margin-s2 style={styles.iframeWrapper}> |
| 44 | + <iframe |
| 45 | + ref={iframeRef} |
| 46 | + style={styles.iframe} |
| 47 | + src={iframeSource} |
| 48 | + title="Simulator" |
| 49 | + onLoad={() => setIframeLoaded(true)} |
| 50 | + /> |
| 51 | + </View> |
| 52 | + </LiveProvider> |
| 53 | + </View> |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +const styles = StyleSheet.create({ |
| 58 | + liveCodeWrapper: { |
| 59 | + borderRadius: 20, |
| 60 | + borderWidth: 1, |
| 61 | + backgroundColor: '#011627', |
| 62 | + height: 725, |
| 63 | + width: 900 |
| 64 | + }, |
| 65 | + editorWrapper: {maxHeight: 700, padding: 10, borderRadius: 20, overflow: 'hidden'}, |
| 66 | + iframeWrapper: { |
| 67 | + alignSelf: 'center', |
| 68 | + overflow: 'hidden', |
| 69 | + borderRadius: 40, |
| 70 | + borderWidth: 4, |
| 71 | + borderColor: Colors.$outlineDisabledHeavy, |
| 72 | + width: 320, |
| 73 | + height: 700 |
| 74 | + }, |
| 75 | + iframe: { |
| 76 | + width: 335, // Slightly wider to hide scrollbar |
| 77 | + height: '100%', |
| 78 | + position: 'absolute', |
| 79 | + top: 0, |
| 80 | + left: 0, |
| 81 | + border: 0, |
| 82 | + padding: 10, |
| 83 | + background: 'transparent' |
| 84 | + } |
| 85 | +}); |
0 commit comments