Skip to content

Commit a3db346

Browse files
authored
Refactor UILivePreview component to support code format (#3722)
* Refactor UILivePreview component to support code format * Refactor to use prettify button * bump docs version
1 parent 51ba1d6 commit a3db346

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

docuilib/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "uilib-docs",
3-
"version": "3.27.0",
3+
"version": "3.28.0",
44
"main": "./src/index.ts",
55
"scripts": {
66
"docusaurus": "docusaurus",

docuilib/src/components/UILivePreview.module.scss

+18-8
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ $editor-height: 700px;
44

55
.container {
66
display: flex;
7-
background-color: #1D1E21;
7+
background-color: #1d1e21;
88
border-radius: 8px;
9-
padding: 50px
9+
padding: 50px;
1010
}
1111

1212
.liveEditor {
1313
height: $editor-height;
1414

1515
span {
16-
font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
16+
font-family: SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
1717
font-size: 14px;
1818
font-weight: 600;
1919
line-height: 24px;
@@ -32,19 +32,29 @@ $editor-height: 700px;
3232
pre {
3333
min-height: 100%;
3434
padding: 20px !important;
35-
background-color: #1D1E21 !important;
36-
border-right: 1px solid #D2D6D8;
35+
background-color: #1d1e21 !important;
36+
border-right: 1px solid #d2d6d8;
3737
}
3838
}
3939

40+
.codeHeader {
41+
position: sticky;
42+
top: 5px;
43+
right: 16px;
44+
z-index: 1;
45+
margin-right: 24px;
46+
display: flex;
47+
justify-content: flex-end;
48+
}
49+
4050
.errorContainer {
4151
position: absolute;
4252
bottom: 0;
4353
left: 0;
4454
right: 0;
4555

4656
pre {
47-
background-color: rgba(255, 255, 255, 0.80) !important;
57+
background-color: rgba(255, 255, 255, 0.8) !important;
4858
margin: 10px;
4959
}
5060
}
@@ -54,7 +64,7 @@ $editor-height: 700px;
5464
width: $preview-width;
5565
margin-left: $preview-margin;
5666
border-radius: 36px;
57-
border: 10px solid rgba(255, 255, 255, 0.20);
67+
border: 10px solid rgba(255, 255, 255, 0.2);
5868
box-shadow: 0 0 15px rgba(110, 120, 129, 0.05);
5969
overflow: hidden;
6070
}
@@ -68,4 +78,4 @@ $editor-height: 700px;
6878
border: 0;
6979
padding: 10;
7080
background-color: #fff;
71-
}
81+
}

docuilib/src/components/UILivePreview.tsx

+16-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {themes} from 'prism-react-renderer';
44
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
55
import BrowserOnly from '@docusaurus/BrowserOnly';
66
import CodeBlock from '@theme/CodeBlock';
7+
import Button from 'react-native-ui-lib/Button';
78
import ReactLiveScope from '../theme/ReactLiveScope';
89
import {isComponentSupported} from '../utils/componentUtils';
910
import 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

Comments
 (0)