frontAnswerer/src/widgets/side/QuizSideButton.tsx

85 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-04-24 15:56:11 +00:00
import lightTheme from "@/utils/themes/light";
import { Button, ThemeProvider, useMediaQuery } from "@mui/material";
2024-04-24 15:56:11 +00:00
import { useState } from "react";
import { createPortal } from "react-dom";
import QuizDialog from "../QuizDialog";
2024-04-24 15:56:11 +00:00
const PADDING = 10;
2024-05-04 15:12:22 +00:00
const WIDGET_DEFAULT_WIDTH = "600px";
const WIDGET_DEFAULT_HEIGHT = "800px";
2024-04-24 15:56:11 +00:00
interface Props {
quizId: string;
position: "left" | "right";
buttonBackgroundColor?: string;
2024-05-04 15:12:22 +00:00
dimensions?: { width: string; height: string; };
2024-05-04 15:22:47 +00:00
fullScreen?: boolean;
2024-04-24 15:56:11 +00:00
}
2024-05-04 15:22:47 +00:00
export default function QuizSideButton({ quizId, position, buttonBackgroundColor, dimensions, fullScreen = false }: Props) {
2024-04-24 15:56:11 +00:00
const [isQuizShown, setIsQuizShown] = useState<boolean>(false);
const isMobile = useMediaQuery("(max-width: 600px)");
2024-04-24 15:56:11 +00:00
return createPortal(
<ThemeProvider theme={lightTheme}>
<QuizDialog
open={isQuizShown}
quizId={quizId}
onClose={() => setIsQuizShown(false)}
hideBackdrop
disableScrollLock
paperSx={[
{
m: 0,
},
2024-05-04 15:22:47 +00:00
(!isMobile && !fullScreen) && {
position: "absolute",
bottom: PADDING,
right: position === "right" ? PADDING : undefined,
left: position === "left" ? PADDING : undefined,
2024-05-04 15:12:22 +00:00
width: dimensions?.width ?? WIDGET_DEFAULT_WIDTH,
maxWidth: `calc(100% - ${PADDING * 2}px)`,
height: dimensions?.height ?? WIDGET_DEFAULT_HEIGHT,
maxHeight: `calc(100% - ${PADDING * 2}px)`,
},
2024-05-04 15:22:47 +00:00
(isMobile || fullScreen) && {
position: "relative",
2024-05-04 15:12:22 +00:00
width: "100%",
height: "100%",
maxHeight: "100%",
borderRadius: 0,
},
]}
/>
<Button
className="pena-quiz-widget-button"
variant="contained"
onClick={() => setIsQuizShown(true)}
2024-05-04 15:24:56 +00:00
disableFocusRipple
sx={[
{
display: isQuizShown ? "none" : "block",
position: "fixed",
height: "70px",
2024-05-04 15:12:22 +00:00
width: "600px",
maxWidth: `calc(100% - ${PADDING * 2}px)`,
backgroundColor: buttonBackgroundColor,
},
position === "left" && {
bottom: PADDING,
left: PADDING,
},
position === "right" && {
bottom: PADDING,
right: PADDING,
},
]}
>
Пройти квиз
</Button>
2024-04-24 15:56:11 +00:00
</ThemeProvider>,
document.body
);
}