frontAnswerer/src/WidgetApp.tsx

45 lines
1.4 KiB
TypeScript
Raw Normal View History

import { Box } from "@mui/material";
import { startTransition, useEffect, useRef, useState } from "react";
2024-02-02 14:35:02 +00:00
import QuizAnswerer from "./QuizAnswerer";
2024-02-01 19:14:27 +00:00
import { QuizIdContext } from "./contexts/QuizIdContext";
import { RootContainerWidthContext } from "./contexts/RootContainerWidthContext";
interface Props {
quizId: string;
}
export default function WidgetApp({ quizId }: Props) {
const [rootContainerSize, setRootContainerSize] = useState<number>(() => window.innerWidth);
const rootContainerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const handleWindowResize = () => {
startTransition(() => {
if (rootContainerRef.current) setRootContainerSize(rootContainerRef.current.clientWidth);
});
};
window.addEventListener("resize", handleWindowResize);
return () => {
window.removeEventListener("resize", handleWindowResize);
};
}, []);
return (
<RootContainerWidthContext.Provider value={rootContainerSize}>
<QuizIdContext.Provider value={quizId}>
<Box
ref={rootContainerRef}
sx={{
width: "100%",
height: "100%",
}}
>
<QuizAnswerer />
</Box>
</QuizIdContext.Provider>
</RootContainerWidthContext.Provider>
);
}