Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added nicer frames for web and mobile #193

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 48 additions & 6 deletions frontend/src/components/Preview.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useRef } from "react";
import { useEffect, useRef, useState } from "react";
import classNames from "classnames";
import { FaSearch } from "react-icons/fa";
// import useThrottle from "../hooks/useThrottle";

interface Props {
Expand All @@ -12,31 +13,72 @@ function Preview({ code, device }: Props) {
// Temporary disable throttling for the preview not updating when the code changes
// useThrottle(code, 200);
const iframeRef = useRef<HTMLIFrameElement | null>(null);
const [backgroundColor, setBackgroundColor] = useState<string | null>(null);

useEffect(() => {
const iframe = iframeRef.current;
if (iframe && iframe.contentDocument) {
iframe.contentDocument.open();
iframe.contentDocument.write(throttledCode);
iframe.contentDocument.close();

setTimeout(() => {
const body = iframe.contentDocument!.body;
if (body) {
const bgColor = window.getComputedStyle(body).backgroundColor;
setBackgroundColor(bgColor);
}
}, 5);
}
}, [throttledCode]);

return (
<div className="flex justify-center mx-2">
<iframe
id={`preview-${device}`}
ref={iframeRef}
title="Preview"
<div
className={classNames(
"border-[4px] border-black rounded-[20px] shadow-lg",
"transform scale-[0.9] origin-top",

{
"w-full h-[832px]": device === "desktop",
"w-[400px] h-[832px]": device === "mobile",
}
)}
></iframe>
style={{ backgroundColor: `${backgroundColor}` }}
>
{device === "mobile" ? (
<div className="bg-black h-[16px] w-[70px] rounded-full mx-auto my-2.5"></div>
) : (
<div className="bg-gray-200 rounded-t-[20px] h-[64px] p-4 flex justify-between items-center">
<div className="flex items-center">
<div className="flex items-center">
<button className="w-4 h-4 bg-gray-400 rounded-full mr-3 ml-3"></button>
<button className="w-4 h-4 bg-gray-400 rounded-full mr-3"></button>
<button className="w-4 h-4 bg-gray-400 rounded-full"></button>
</div>
</div>
<div className="mx-auto">
<FaSearch className="absolute ml-3 mt-3" />
<input
type="text"
disabled
style={{ width: "500px" }}
placeholder="https://my-app.cool"
className="py-2 px-10 bg-gray-100 text-black rounded-full focus:outline-none focus:shadow-outline"
/>
</div>
</div>
)}
<iframe
id={`preview-${device}`}
ref={iframeRef}
title="Preview"
className={classNames("rounded-[20px] ", {
"w-full h-[760px]": device === "desktop",
"w-[392px] h-[788px]": device === "mobile",
})}
></iframe>
</div>
</div>
);
}
Expand Down