From 9e01ad1d0eb1a0bc1d056b2f20031a87ba997582 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Fri, 22 May 2020 13:46:52 +1000 Subject: [PATCH] Simplified menu interaction prevention --- src/components/map/Map.js | 4 ++-- src/components/map/MapMenu.js | 17 ++++++++++---- src/components/token/TokenMenu.js | 39 ++++++++++++++----------------- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/components/map/Map.js b/src/components/map/Map.js index 7d0586f..4cd04f1 100644 --- a/src/components/map/Map.js +++ b/src/components/map/Map.js @@ -163,12 +163,12 @@ function Map({ /> )); - const tokenMenu = isTokenMenuOpen && ( + const tokenMenu = ( setIsTokenMenuOpen(false)} onTokenChange={onMapTokenStateChange} - tokenState={mapState.tokens[tokenMenuOptions.tokenStateId]} + tokenState={mapState && mapState.tokens[tokenMenuOptions.tokenStateId]} tokenImage={tokenMenuOptions.tokenImage} /> ); diff --git a/src/components/map/MapMenu.js b/src/components/map/MapMenu.js index 909f141..f710585 100644 --- a/src/components/map/MapMenu.js +++ b/src/components/map/MapMenu.js @@ -4,6 +4,8 @@ import { useThemeUI } from "theme-ui"; import MapInteractionContext from "../../contexts/MapInteractionContext"; +import usePrevious from "../../helpers/usePrevious"; + function MapMenu({ isOpen, onRequestClose, @@ -22,7 +24,16 @@ function MapMenu({ // callback const [modalContentNode, setModalContentNode] = useState(null); + // Toggle map interaction when menu is opened + const wasOpen = usePrevious(isOpen); const { setPreventMapInteraction } = useContext(MapInteractionContext); + useEffect(() => { + if (isOpen && !wasOpen) { + setPreventMapInteraction(true); + } else if (wasOpen && !isOpen) { + setPreventMapInteraction(false); + } + }, [isOpen, setPreventMapInteraction, wasOpen]); useEffect(() => { // Close modal if interacting with any other element @@ -32,31 +43,29 @@ function MapMenu({ !path.includes(modalContentNode) && !(excludeNode && path.includes(excludeNode)) ) { - setPreventMapInteraction(false); onRequestClose(); document.body.removeEventListener("pointerdown", handlePointerDown); } } if (modalContentNode) { - setPreventMapInteraction(true); document.body.addEventListener("pointerdown", handlePointerDown); // Check for wheel event to close modal as well document.body.addEventListener( "wheel", () => { - setPreventMapInteraction(false); onRequestClose(); }, { once: true } ); } + return () => { if (modalContentNode) { document.body.removeEventListener("pointerdown", handlePointerDown); } }; - }, [modalContentNode, excludeNode, onRequestClose, setPreventMapInteraction]); + }, [modalContentNode, excludeNode, onRequestClose]); function handleModalContent(node) { setModalContentNode(node); diff --git a/src/components/token/TokenMenu.js b/src/components/token/TokenMenu.js index 0f14a7e..91df1c6 100644 --- a/src/components/token/TokenMenu.js +++ b/src/components/token/TokenMenu.js @@ -1,4 +1,4 @@ -import React, { useEffect, useState, useContext } from "react"; +import React, { useEffect, useState } from "react"; import { Box, Input, Slider, Flex, Text } from "theme-ui"; import MapMenu from "../map/MapMenu"; @@ -7,8 +7,6 @@ import colors, { colorOptions } from "../../helpers/colors"; import usePrevious from "../../helpers/usePrevious"; -import MapInteractionContext from "../../contexts/MapInteractionContext"; - const defaultTokenMaxSize = 6; /** @@ -34,7 +32,7 @@ function TokenMenu({ const [tokenMaxSize, setTokenMaxSize] = useState(defaultTokenMaxSize); useEffect(() => { - if (isOpen && !wasOpen) { + if (isOpen && !wasOpen && tokenState) { setTokenMaxSize(Math.max(tokenState.size, defaultTokenMaxSize)); } }, [isOpen, tokenState, wasOpen]); @@ -99,8 +97,6 @@ function TokenMenu({ } } - const { setPreventMapInteraction } = useContext(MapInteractionContext); - return ( { e.preventDefault(); - setPreventMapInteraction(false); onRequestClose(); }} sx={{ alignItems: "center" }} @@ -130,7 +125,7 @@ function TokenMenu({ handleStatusChange(color)} aria-label={`Token label Color ${color}`} > - {tokenState.statuses && tokenState.statuses.includes(color) && ( - - )} + {tokenState && + tokenState.statuses && + tokenState.statuses.includes(color) && ( + + )} ))} @@ -187,7 +184,7 @@ function TokenMenu({ Size: