From b8504fee707db4536cccbf3190728f82c41e2d65 Mon Sep 17 00:00:00 2001 From: Joshua Sigona Date: Sun, 11 Jul 2021 15:53:51 +0900 Subject: [PATCH] Click outside of a class selector --- src/App.css | 2 +- src/App.js | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/App.css b/src/App.css index 490303b..63911ea 100644 --- a/src/App.css +++ b/src/App.css @@ -326,7 +326,7 @@ input#editBox{ .popup{ position:absolute; - background-color: rgba(128,128,128,1); + background-color: rgba(128,128,128,0.9); max-width: 250px; min-width: 100px; padding: 20px; diff --git a/src/App.js b/src/App.js index 96fe7d6..f7ce9bd 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,7 @@ import './App.css'; -import React, {useState,useEffect} from 'react'; +import React, {useState,useEffect,useRef} from 'react'; +import ReactWindow from 'reactjs-windows' +import 'reactjs-windows/dist/index.css' function Col(p) { return
@@ -113,18 +115,47 @@ const ABILITIES = { const ABILITY_DEFAULT_ICON = "/ngsplanner/icons/UINGSItemSpecialAbility.png" +/** + * Hook that alerts clicks outside of the passed ref + */ +function useOutsideAlerter(ref,setEdit) { + useEffect(() => { + /** + * Alert if clicked on outside of element + */ + function handleClickOutside(event) { + if (ref.current && !ref.current.contains(event.target)) { + setEdit(false) + } + } + + // Bind the event listener + document.addEventListener("mousedown", handleClickOutside); + return () => { + // Unbind the event listener on clean up + document.removeEventListener("mousedown", handleClickOutside); + }; + }, [ref]); +} + + function Class(p) { const class_obj = CLASSES[p.name] return <>{class_obj.name} } function ClassSelector(p){ - return
+ const wrapperRef = useRef(null); + useOutsideAlerter(wrapperRef,p.setEdit); + return
Class Selector
{Object.keys(CLASSES).map((cl,i)=>{ return })}
+ /*return + This is a test window. + */ } function EditableClass(p){ @@ -245,7 +276,7 @@ function EquippedWeaponBox(p) { },[currentPage,p.armorSlot1,p.armorSlot1EnhancementLv,p.armorSlot1AbilityList,p.armorSlot2,p.armorSlot2EnhancementLv,p.armorSlot2AbilityList,p.armorSlot3,p.armorSlot3EnhancementLv,p.armorSlot3AbilityList,p.weapon,p.weaponEnhancementLv,p.weaponAbilityList]) return -

{selectedEquip}+{selectedEquipEnhancementLv}

+

{selectedEquip}+{selectedEquipEnhancementLv}

@@ -297,7 +328,7 @@ function ListRow(p) { } -function App() { +function App() { const [author,setAuthor] = useState("Dudley") const [buildName,setBuildName] = useState("Fatimah") const [className,setClassName] = useState("RANGER")