frontAnswerer/lib/components/QuizView.tsx

49 lines
1.6 KiB
TypeScript
Raw Normal View History

import { Box } from "@mui/material";
2024-02-12 11:11:22 +00:00
import { startTransition, useEffect, useLayoutEffect, useRef, useState } from "react";
import { QuizIdContext } from "@contexts/QuizIdContext";
import { RootContainerWidthContext } from "@contexts/RootContainerWidthContext";
import QuizAnswerer from "./QuizAnswerer";
interface Props {
quizId: string;
}
export default function QuizView({ quizId }: Props) {
const [rootContainerSize, setRootContainerSize] = useState<number>(() => window.innerWidth);
const rootContainerRef = useRef<HTMLDivElement>(null);
2024-02-12 11:11:22 +00:00
useLayoutEffect(() => {
if (rootContainerRef.current) setRootContainerSize(rootContainerRef.current.clientWidth);
}, []);
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>
);
}