reset preview position on resize
This commit is contained in:
parent
e6d32dd72d
commit
bea970bafa
@ -1,16 +1,21 @@
|
|||||||
import { PointsIcon } from "@icons/questionsPage/PointsIcon";
|
import { PointsIcon } from "@icons/questionsPage/PointsIcon";
|
||||||
|
import VisibilityIcon from '@mui/icons-material/Visibility';
|
||||||
import { Box, IconButton } from "@mui/material";
|
import { Box, IconButton } from "@mui/material";
|
||||||
import { toggleQuizPreview, useQuizPreviewStore } from "@root/quizPreview";
|
import { toggleQuizPreview, useQuizPreviewStore } from "@root/quizPreview";
|
||||||
import { useLayoutEffect, useRef } from "react";
|
import { useLayoutEffect, useRef } from "react";
|
||||||
import { Rnd } from "react-rnd";
|
import { Rnd } from "react-rnd";
|
||||||
|
import { useWindowSize } from "../../utils/hooks/useWindowSize";
|
||||||
import QuizPreviewLayout from "./QuizPreviewLayout";
|
import QuizPreviewLayout from "./QuizPreviewLayout";
|
||||||
import ResizeIcon from "./ResizeIcon";
|
import ResizeIcon from "./ResizeIcon";
|
||||||
import VisibilityIcon from '@mui/icons-material/Visibility';
|
|
||||||
|
|
||||||
|
|
||||||
const DRAG_PARENT_MARGIN = 25;
|
const DRAG_PARENT_MARGIN = 25;
|
||||||
const NAVBAR_HEIGHT = 81;
|
const NAVBAR_HEIGHT = 81;
|
||||||
const DRAG_PARENT_BOTTOM_MARGIN = 65;
|
const DRAG_PARENT_BOTTOM_MARGIN = 65;
|
||||||
|
const PREVIEW_MIN_WIDTH = 340;
|
||||||
|
const PREVIEW_MIN_HEIGHT = 300;
|
||||||
|
const PREVIEW_DEFAULT_WIDTH = "340";
|
||||||
|
const PREVIEW_DEFAULT_HEIGHT = "480";
|
||||||
|
|
||||||
interface RndPositionAndSize {
|
interface RndPositionAndSize {
|
||||||
x: number;
|
x: number;
|
||||||
@ -23,13 +28,19 @@ export default function QuizPreview() {
|
|||||||
const isPreviewShown = useQuizPreviewStore(state => state.isPreviewShown);
|
const isPreviewShown = useQuizPreviewStore(state => state.isPreviewShown);
|
||||||
const rndParentRef = useRef<HTMLDivElement>(null);
|
const rndParentRef = useRef<HTMLDivElement>(null);
|
||||||
const rndRef = useRef<Rnd | null>(null);
|
const rndRef = useRef<Rnd | null>(null);
|
||||||
const rndPositionAndSizeRef = useRef<RndPositionAndSize>({ x: 0, y: 0, width: "340", height: "480" });
|
const rndPositionAndSizeRef = useRef<RndPositionAndSize>({
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
width: PREVIEW_DEFAULT_WIDTH,
|
||||||
|
height: PREVIEW_DEFAULT_HEIGHT
|
||||||
|
});
|
||||||
const isFirstShowRef = useRef<boolean>(true);
|
const isFirstShowRef = useRef<boolean>(true);
|
||||||
|
const windowSize = useWindowSize();
|
||||||
|
|
||||||
useLayoutEffect(function stickPreviewToBottomRight() {
|
function resetPreviewPositionAndSize() {
|
||||||
const rnd = rndRef.current;
|
const rnd = rndRef.current;
|
||||||
const rndSelfElement = rnd?.getSelfElement();
|
const rndSelfElement = rnd?.getSelfElement();
|
||||||
if (!rnd || !rndSelfElement || !rndParentRef.current || !isFirstShowRef.current) return;
|
if (!rnd || !rndSelfElement || !rndParentRef.current) return;
|
||||||
|
|
||||||
const rndParentRect = rndParentRef.current.getBoundingClientRect();
|
const rndParentRect = rndParentRef.current.getBoundingClientRect();
|
||||||
const rndRect = rndSelfElement.getBoundingClientRect();
|
const rndRect = rndSelfElement.getBoundingClientRect();
|
||||||
@ -38,12 +49,25 @@ export default function QuizPreview() {
|
|||||||
const y = rndParentRect.height - rndRect.height;
|
const y = rndParentRect.height - rndRect.height;
|
||||||
|
|
||||||
rnd.updatePosition({ x, y });
|
rnd.updatePosition({ x, y });
|
||||||
|
rnd.updateSize({ width: PREVIEW_DEFAULT_WIDTH, height: PREVIEW_DEFAULT_HEIGHT });
|
||||||
rndPositionAndSizeRef.current.x = x;
|
rndPositionAndSizeRef.current.x = x;
|
||||||
rndPositionAndSizeRef.current.y = y;
|
rndPositionAndSizeRef.current.y = y;
|
||||||
|
rndPositionAndSizeRef.current.width = PREVIEW_DEFAULT_WIDTH.toString();
|
||||||
|
rndPositionAndSizeRef.current.height = PREVIEW_DEFAULT_HEIGHT.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
useLayoutEffect(() => {
|
||||||
|
if (!isFirstShowRef.current) return;
|
||||||
|
|
||||||
|
resetPreviewPositionAndSize();
|
||||||
|
|
||||||
isFirstShowRef.current = false;
|
isFirstShowRef.current = false;
|
||||||
}, [isPreviewShown]);
|
}, [isPreviewShown]);
|
||||||
|
|
||||||
|
useLayoutEffect(() => {
|
||||||
|
resetPreviewPositionAndSize();
|
||||||
|
}, [windowSize.height, windowSize.width]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
ref={rndParentRef}
|
ref={rndParentRef}
|
||||||
@ -61,8 +85,8 @@ export default function QuizPreview() {
|
|||||||
>
|
>
|
||||||
{isPreviewShown &&
|
{isPreviewShown &&
|
||||||
<Rnd
|
<Rnd
|
||||||
minHeight={300}
|
minHeight={PREVIEW_MIN_HEIGHT}
|
||||||
minWidth={340}
|
minWidth={PREVIEW_MIN_WIDTH}
|
||||||
bounds="parent"
|
bounds="parent"
|
||||||
ref={rndRef}
|
ref={rndRef}
|
||||||
dragHandleClassName="quiz-preview-draghandle"
|
dragHandleClassName="quiz-preview-draghandle"
|
||||||
|
|||||||
35
src/utils/hooks/useWindowSize.ts
Normal file
35
src/utils/hooks/useWindowSize.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { useEffect, useLayoutEffect, useState } from "react";
|
||||||
|
|
||||||
|
|
||||||
|
interface WindowSize {
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useWindowSize(): WindowSize {
|
||||||
|
const [windowSize, setWindowSize] = useState<WindowSize>({
|
||||||
|
width: 0,
|
||||||
|
height: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
const handleSize = () => {
|
||||||
|
setWindowSize({
|
||||||
|
width: window.innerWidth,
|
||||||
|
height: window.innerHeight,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
window.addEventListener("resize", handleSize);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("resize", handleSize);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
useLayoutEffect(() => {
|
||||||
|
handleSize();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return windowSize;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user