abstract safe rotate

This commit is contained in:
Viktor Rådberg
2023-10-08 10:50:43 +02:00
parent 294fe94df7
commit 8f80936745
2 changed files with 289 additions and 254 deletions

View File

@@ -15,8 +15,9 @@ import {
Cross, Cross,
ResetGame, ResetGame,
} from '../../Icons/generated'; } from '../../Icons/generated';
import { useEffect, useRef } from 'react'; import { useRef } from 'react';
import { Spacer } from '../Misc/Spacer'; import { Spacer } from '../Misc/Spacer';
import { useSafeRotate } from '../../Hooks/useSafeRotate';
const SettingsContainer = styled.div<{ const SettingsContainer = styled.div<{
$rotation: Rotation; $rotation: Rotation;
@@ -71,8 +72,8 @@ const TogglesSection = styled.div`
`; `;
const ButtonsSections = styled.div` const ButtonsSections = styled.div`
position: relative;
display: flex; display: flex;
max-width: 100%;
gap: 1rem; gap: 1rem;
justify-content: space-between; justify-content: space-between;
padding: 3% 3%; padding: 3% 3%;
@@ -85,7 +86,6 @@ const ColorPicker = styled.input`
left: 5%; left: 5%;
height: 8vmax; height: 8vmax;
width: 8vmax; width: 8vmax;
border: none; border: none;
outline: none; outline: none;
cursor: pointer; cursor: pointer;
@@ -173,30 +173,14 @@ const PlayerMenu = ({ player, setShowPlayerMenu }: PlayerMenuProps) => {
const settingsContainerRef = useRef<HTMLDivElement | null>(null); const settingsContainerRef = useRef<HTMLDivElement | null>(null);
const dialogRef = useRef<HTMLDialogElement | null>(null); const dialogRef = useRef<HTMLDialogElement | null>(null);
const isSide = const { isSide } = useSafeRotate({
player.settings.rotation === Rotation.Side || rotation: player.settings.rotation,
player.settings.rotation === Rotation.SideFlipped; containerRef: settingsContainerRef,
});
useEffect(() => {
if (!settingsContainerRef.current) {
return;
}
if (isSide) {
//set height to 100% of the width of the parent
settingsContainerRef.current.style.height = `${settingsContainerRef.current.parentElement?.clientWidth}px`;
//set width to 100% of the height of the parent
settingsContainerRef.current.style.width = `${settingsContainerRef.current.parentElement?.clientHeight}px`;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [settingsContainerRef]);
const handleOnClick = () => { const handleOnClick = () => {
setShowPlayerMenu(false); setShowPlayerMenu(false);
}; };
const { fullscreen, wakeLock, goToStart } = useGlobalSettings(); const { fullscreen, wakeLock, goToStart } = useGlobalSettings();
const { updatePlayer, resetCurrentGame } = usePlayers(); const { updatePlayer, resetCurrentGame } = usePlayers();
@@ -231,7 +215,6 @@ const PlayerMenu = ({ player, setShowPlayerMenu }: PlayerMenuProps) => {
const closeButtonSize = isSide ? '6vmin' : '3vmax'; const closeButtonSize = isSide ? '6vmin' : '3vmax';
return ( return (
<>
<PlayerMenuWrapper $rotation={player.settings.rotation}> <PlayerMenuWrapper $rotation={player.settings.rotation}>
<CloseButton $rotation={player.settings.rotation}> <CloseButton $rotation={player.settings.rotation}>
<Button <Button
@@ -466,7 +449,6 @@ const PlayerMenu = ({ player, setShowPlayerMenu }: PlayerMenuProps) => {
</dialog> </dialog>
</SettingsContainer> </SettingsContainer>
</PlayerMenuWrapper> </PlayerMenuWrapper>
</>
); );
}; };

View File

@@ -0,0 +1,53 @@
import { useEffect } from 'react';
import { Rotation } from '../Types/Player';
type useSafeRotateProps = {
rotation: Rotation;
containerRef: React.MutableRefObject<HTMLDivElement | null>;
};
export const useSafeRotate = ({
rotation,
containerRef,
}: useSafeRotateProps) => {
const isSide =
rotation === Rotation.Side || rotation === Rotation.SideFlipped;
const calculateSize = (container: HTMLDivElement) => {
if (isSide) {
//set height to 100% of the width of the parent
container.style.height = `${container.parentElement?.clientWidth}px`;
//set width to 100% of the height of the parent
container.style.width = `${container.parentElement?.clientHeight}px`;
}
};
useEffect(() => {
if (!containerRef.current) {
return;
}
const container = containerRef.current;
const resizeObserver = new ResizeObserver(() => {
calculateSize(container);
console.log('resize by observer');
return;
});
// Initially calculate size
calculateSize(container);
resizeObserver.observe(container.parentElement as Element);
return () => {
// Cleanup: disconnect the ResizeObserver when the component unmounts.
resizeObserver.disconnect();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [containerRef]);
return { isSide };
};