Update dice tray size calculation with full screen

This commit is contained in:
Mitchell McCaffrey 2020-09-06 15:47:31 +10:00
parent 58cb92d432
commit 68f6860c1f

View File

@ -24,6 +24,7 @@ import DiceTray from "../../dice/diceTray/DiceTray";
import DiceLoadingContext from "../../contexts/DiceLoadingContext"; import DiceLoadingContext from "../../contexts/DiceLoadingContext";
import { getDiceRoll } from "../../helpers/dice"; import { getDiceRoll } from "../../helpers/dice";
import useSetting from "../../helpers/useSetting";
function DiceTrayOverlay({ function DiceTrayOverlay({
isOpen, isOpen,
@ -45,6 +46,7 @@ function DiceTrayOverlay({
const { assetLoadStart, assetLoadFinish, isLoading } = useContext( const { assetLoadStart, assetLoadFinish, isLoading } = useContext(
DiceLoadingContext DiceLoadingContext
); );
const [fullScreen] = useSetting("map.fullScreen");
function handleAssetLoadStart() { function handleAssetLoadStart() {
assetLoadStart(); assetLoadStart();
@ -236,6 +238,8 @@ function DiceTrayOverlay({
}); });
useEffect(() => { useEffect(() => {
let renderTimeout;
let renderCleanup;
function handleResize() { function handleResize() {
const map = document.querySelector(".map"); const map = document.querySelector(".map");
const mapRect = map.getBoundingClientRect(); const mapRect = map.getBoundingClientRect();
@ -251,6 +255,11 @@ function DiceTrayOverlay({
height = diceTraySize === "single" ? width * 2 : width; height = diceTraySize === "single" ? width * 2 : width;
} }
// Debounce a timeout to force re-rendering on resize
renderTimeout = setTimeout(() => {
renderCleanup = forceRender();
}, 100);
setTraySize({ width, height }); setTraySize({ width, height });
} }
@ -260,8 +269,14 @@ function DiceTrayOverlay({
return () => { return () => {
window.removeEventListener("resize", handleResize); window.removeEventListener("resize", handleResize);
if (renderTimeout) {
clearTimeout(renderTimeout);
}
if (renderCleanup) {
renderCleanup();
}
}; };
}, [diceTraySize]); }, [diceTraySize, fullScreen, isOpen]);
// Update dice rolls // Update dice rolls
useEffect(() => { useEffect(() => {