From 27d3903e660cf491ce8902c476ec0a50d7bc415a Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Mon, 20 Apr 2020 10:56:51 +1000 Subject: [PATCH] Refactored token menu overlay into two components --- src/components/MapMenu.js | 69 +++++++++++++++++++++++++++++++++++++ src/components/TokenMenu.js | 44 +++++------------------ 2 files changed, 77 insertions(+), 36 deletions(-) create mode 100644 src/components/MapMenu.js diff --git a/src/components/MapMenu.js b/src/components/MapMenu.js new file mode 100644 index 0000000..7e528dc --- /dev/null +++ b/src/components/MapMenu.js @@ -0,0 +1,69 @@ +import React from "react"; +import Modal from "react-modal"; + +function MapMenu({ + isOpen, + onRequestClose, + onModalContent, + top, + left, + bottom, + right, + children, +}) { + function handleModalContent(node) { + if (node) { + // Close modal if interacting with any other element + function handlePointerDown(event) { + const path = event.composedPath(); + if (!path.includes(node)) { + onRequestClose(); + document.body.removeEventListener("pointerdown", handlePointerDown); + } + } + document.body.addEventListener("pointerdown", handlePointerDown); + + // Check for wheel event to close modal as well + document.body.addEventListener( + "wheel", + () => { + onRequestClose(); + }, + { once: true } + ); + } + onModalContent(node); + } + return ( + + {children} + + ); +} + +MapMenu.defaultProps = { + onModalContent: () => {}, + top: "initial", + left: "initial", + right: "initial", + bottom: "initial", +}; + +export default MapMenu; diff --git a/src/components/TokenMenu.js b/src/components/TokenMenu.js index 350cadf..63a2ab9 100644 --- a/src/components/TokenMenu.js +++ b/src/components/TokenMenu.js @@ -1,8 +1,9 @@ import React, { useEffect, useState } from "react"; -import Modal from "react-modal"; import interact from "interactjs"; import { Box, Input } from "theme-ui"; +import MapMenu from "./MapMenu"; + import colors, { colorOptions } from "../helpers/colors"; function TokenMenu({ tokenClassName, onTokenChange }) { @@ -85,29 +86,11 @@ function TokenMenu({ tokenClassName, onTokenChange }) { function handleModalContent(node) { if (node) { + // Focus input const tokenLabelInput = node.querySelector("#changeTokenLabel"); tokenLabelInput.focus(); tokenLabelInput.setSelectionRange(7, 8); - // Close modal if interacting with any other element - function handlePointerDown(event) { - const path = event.composedPath(); - if (!path.includes(node)) { - setIsOpen(false); - document.body.removeEventListener("pointerdown", handlePointerDown); - } - } - document.body.addEventListener("pointerdown", handlePointerDown); - - // Check for wheel event to close modal as well - document.body.addEventListener( - "wheel", - () => { - setIsOpen(false); - }, - { once: true } - ); - // Ensure menu is in bounds const nodeRect = node.getBoundingClientRect(); const map = document.querySelector(".map"); @@ -125,23 +108,12 @@ function TokenMenu({ tokenClassName, onTokenChange }) { } return ( - - + ); }