Moved to useBlur helper

This commit is contained in:
Mitchell McCaffrey 2021-03-19 13:29:07 +11:00
parent 0bc572cec2
commit cfdff1e549
3 changed files with 31 additions and 24 deletions

View File

@ -74,4 +74,21 @@ export function useKeyboard(onKeyDown, onKeyUp) {
}); });
} }
/**
* Handler to handle a blur event. Useful when using a shortcut that uses the Alt or Cmd
* @param {FocusEvent} onBlur
*/
export function useBlur(onBlur) {
useEffect(() => {
if (onBlur) {
window.addEventListener("blur", onBlur);
}
return () => {
if (onBlur) {
window.removeEventListener("blur", onBlur);
}
};
});
}
export default KeyboardContext; export default KeyboardContext;

View File

@ -1,4 +1,4 @@
import React, { useRef, useState, useEffect } from "react"; import React, { useRef, useState } from "react";
import { Button, Flex, Label } from "theme-ui"; import { Button, Flex, Label } from "theme-ui";
import shortid from "shortid"; import shortid from "shortid";
import Case from "case"; import Case from "case";
@ -26,7 +26,7 @@ import useResponsiveLayout from "../hooks/useResponsiveLayout";
import { useMapData } from "../contexts/MapDataContext"; import { useMapData } from "../contexts/MapDataContext";
import { useAuth } from "../contexts/AuthContext"; import { useAuth } from "../contexts/AuthContext";
import { useKeyboard } from "../contexts/KeyboardContext"; import { useKeyboard, useBlur } from "../contexts/KeyboardContext";
const defaultMapProps = { const defaultMapProps = {
showGrid: false, showGrid: false,
@ -382,17 +382,12 @@ function SelectMapModal({
useKeyboard(handleKeyDown, handleKeyUp); useKeyboard(handleKeyDown, handleKeyUp);
// Set select mode to single when alt+tabing // Set select mode to single when cmd+tabing
useEffect(() => {
function handleBlur() { function handleBlur() {
setSelectMode("single"); setSelectMode("single");
} }
window.addEventListener("blur", handleBlur); useBlur(handleBlur);
return () => {
window.removeEventListener("blur", handleBlur);
};
}, []);
const layout = useResponsiveLayout(); const layout = useResponsiveLayout();

View File

@ -1,4 +1,4 @@
import React, { useRef, useState, useEffect } from "react"; import React, { useRef, useState } from "react";
import { Flex, Label, Button } from "theme-ui"; import { Flex, Label, Button } from "theme-ui";
import shortid from "shortid"; import shortid from "shortid";
import Case from "case"; import Case from "case";
@ -20,7 +20,7 @@ import useResponsiveLayout from "../hooks/useResponsiveLayout";
import { useTokenData } from "../contexts/TokenDataContext"; import { useTokenData } from "../contexts/TokenDataContext";
import { useAuth } from "../contexts/AuthContext"; import { useAuth } from "../contexts/AuthContext";
import { useKeyboard } from "../contexts/KeyboardContext"; import { useKeyboard, useBlur } from "../contexts/KeyboardContext";
function SelectTokensModal({ isOpen, onRequestClose }) { function SelectTokensModal({ isOpen, onRequestClose }) {
const { userId } = useAuth(); const { userId } = useAuth();
@ -224,17 +224,12 @@ function SelectTokensModal({ isOpen, onRequestClose }) {
useKeyboard(handleKeyDown, handleKeyUp); useKeyboard(handleKeyDown, handleKeyUp);
// Set select mode to single when alt+tabing // Set select mode to single when cmd+tabing
useEffect(() => {
function handleBlur() { function handleBlur() {
setSelectMode("single"); setSelectMode("single");
} }
window.addEventListener("blur", handleBlur); useBlur(handleBlur);
return () => {
window.removeEventListener("blur", handleBlur);
};
}, []);
const layout = useResponsiveLayout(); const layout = useResponsiveLayout();