fix: prevent flashing when updating code
This commit is contained in:
parent
1c233a28cf
commit
9dfdc8683d
@ -1,3 +1,4 @@
|
|||||||
|
import { useEffect, useRef } from 'react';
|
||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
import useThrottle from "../hooks/useThrottle";
|
import useThrottle from "../hooks/useThrottle";
|
||||||
|
|
||||||
@ -8,12 +9,22 @@ interface Props {
|
|||||||
|
|
||||||
function Preview({ code, device }: Props) {
|
function Preview({ code, device }: Props) {
|
||||||
const throttledCode = useThrottle(code, 200);
|
const throttledCode = useThrottle(code, 200);
|
||||||
|
const iframeRef = useRef<HTMLIFrameElement | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const iframe = iframeRef.current;
|
||||||
|
if (iframe && iframe.contentDocument) {
|
||||||
|
iframe.contentDocument.open();
|
||||||
|
iframe.contentDocument.write(throttledCode);
|
||||||
|
iframe.contentDocument.close();
|
||||||
|
}
|
||||||
|
}, [throttledCode]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex justify-center mx-2">
|
<div className="flex justify-center mx-2">
|
||||||
<iframe
|
<iframe
|
||||||
|
ref={iframeRef}
|
||||||
title="Preview"
|
title="Preview"
|
||||||
srcDoc={throttledCode}
|
|
||||||
className={classNames(
|
className={classNames(
|
||||||
"border-[4px] border-black rounded-[20px] shadow-lg",
|
"border-[4px] border-black rounded-[20px] shadow-lg",
|
||||||
"transform scale-[0.9] origin-top",
|
"transform scale-[0.9] origin-top",
|
||||||
@ -26,4 +37,5 @@ function Preview({ code, device }: Props) {
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Preview;
|
export default Preview;
|
||||||
Loading…
Reference in New Issue
Block a user