-
Notifications
You must be signed in to change notification settings - Fork 726
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UILivePreview component snippet wrapper (#3351)
* UILivePreview component snippet wrapper * hide iframe scrollbar * livePreviewSource * sortableGridList snippet fix * fixed review notes
- Loading branch information
Showing
11 changed files
with
162 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import React, {useEffect, useRef, useState, useMemo} from 'react'; | ||
import {StyleSheet} from 'react-native'; | ||
import {LiveProvider, LiveEditor} from 'react-live'; | ||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; | ||
import {View, Colors} from 'react-native-ui-lib/core'; | ||
import ReactLiveScope from '../theme/ReactLiveScope'; | ||
|
||
export const IFRAME_MESSAGE_TYPE = 'LIVE_PREVIEW_CODE_UPDATE_MESSAGE'; | ||
|
||
export default function UILivePreview({code: codeProp}) { | ||
const [code, setCode] = useState(codeProp); | ||
const [iframeLoaded, setIframeLoaded] = useState(false); | ||
const {siteConfig} = useDocusaurusContext(); | ||
const iframeRef = useRef(null); | ||
const iframeSource = siteConfig?.customFields?.livePreviewSource as string; | ||
|
||
useEffect(() => { | ||
if (iframeLoaded) { | ||
sendMessageToIframe(code); | ||
} | ||
}, [iframeLoaded, code]); | ||
|
||
const sendMessageToIframe = code => { | ||
const message = {type: IFRAME_MESSAGE_TYPE, code}; | ||
iframeRef.current?.contentWindow.postMessage(message, '*'); | ||
}; | ||
|
||
const liveEditorStyle = useMemo(() => { | ||
return {overflowY: 'scroll', scrollbarWidth: 'none'}; | ||
}, []); | ||
|
||
return ( | ||
<View row gap-s2 style={styles.liveCodeWrapper}> | ||
<LiveProvider code={code} scope={ReactLiveScope}> | ||
<View flex style={styles.editorWrapper}> | ||
<LiveEditor | ||
className="font-mono" | ||
onChange={setCode} | ||
//@ts-ignore | ||
style={liveEditorStyle} | ||
/> | ||
</View> | ||
<View bg-$backgroundDefault margin-s2 style={styles.iframeWrapper}> | ||
<iframe | ||
ref={iframeRef} | ||
style={styles.iframe} | ||
src={iframeSource} | ||
title="Simulator" | ||
onLoad={() => setIframeLoaded(true)} | ||
/> | ||
</View> | ||
</LiveProvider> | ||
</View> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
liveCodeWrapper: { | ||
borderRadius: 20, | ||
borderWidth: 1, | ||
backgroundColor: '#011627', | ||
height: 725, | ||
width: 900 | ||
}, | ||
editorWrapper: {maxHeight: 700, padding: 10, borderRadius: 20, overflow: 'hidden'}, | ||
iframeWrapper: { | ||
alignSelf: 'center', | ||
overflow: 'hidden', | ||
borderRadius: 40, | ||
borderWidth: 4, | ||
borderColor: Colors.$outlineDisabledHeavy, | ||
width: 320, | ||
height: 700 | ||
}, | ||
iframe: { | ||
width: 335, // Slightly wider to hide scrollbar | ||
height: '100%', | ||
position: 'absolute', | ||
top: 0, | ||
left: 0, | ||
border: 0, | ||
padding: 10, | ||
background: 'transparent' | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, {useEffect, useState} from 'react'; | ||
import {StyleSheet} from 'react-native'; | ||
import {LiveProvider, LivePreview} from 'react-live'; | ||
import ReactLiveScope from '../theme/ReactLiveScope'; | ||
import {IFRAME_MESSAGE_TYPE} from '@site/src/components/UILivePreview'; | ||
|
||
export default function UILivePreview() { | ||
const [code, setCode] = useState(``); | ||
|
||
useEffect(() => { | ||
window.addEventListener('message', (e: MessageEvent) => { | ||
if (e.data.type === IFRAME_MESSAGE_TYPE) { | ||
setCode(e.data.code); | ||
} | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<LiveProvider code={code} scope={ReactLiveScope}> | ||
<LivePreview style={styles.livePreview}/> | ||
</LiveProvider> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
livePreview: { | ||
overflow: 'hidden' | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters