Skip to content

Commit e8cc276

Browse files
committed
fixed click outside to close modal
1 parent c8ce332 commit e8cc276

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

app/src/components/top/NavBarButtons.tsx

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ const StyledMenuItem = withStyles((theme) => ({
183183
// where the main function starts //
184184
function navbarDropDown(props) {
185185
const dispatch = useDispatch();
186-
const dropdownRef = useRef(null);
186+
187187

188188
const [modal, setModal] = React.useState(null);
189189
const [anchorEl, setAnchorEl] = React.useState(null);
@@ -201,21 +201,7 @@ function navbarDropDown(props) {
201201
setAnchorEl(event.currentTarget);
202202
};
203203

204-
useEffect(() => {
205-
function handleClickOutside(event) {
206-
if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
207-
props.setDropMenu(false);
208-
}
209-
}
210204

211-
// Attach the event listener
212-
document.addEventListener("mousedown", handleClickOutside);
213-
214-
// Clean up the event listener
215-
return () => {
216-
document.removeEventListener("mousedown", handleClickOutside);
217-
};
218-
}, [props]);
219205

220206

221207
const clearWorkspace = () => {
@@ -309,10 +295,35 @@ function navbarDropDown(props) {
309295
</svg>
310296
);
311297

312-
const showMenu = props.dropMenu ? 'navDropDown' : 'hideNavDropDown';
298+
let showMenu = props.dropMenu ? 'navDropDown' : 'hideNavDropDown';
299+
300+
//for closing the menu on clicks outside of it.
301+
const useOutsideClick = (callback) => {
302+
303+
const dropdownRef = useRef(null);
304+
305+
useEffect(() => {
306+
const handleClick = (event) => {
307+
if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
308+
callback();
309+
}
310+
}
311+
document.addEventListener('click', handleClick, true);
312+
313+
return () => {
314+
document.removeEventListener('click', handleClick, true);//cleanup for memory purposes. ensures handleclick isn't called after the component is no longer rendered
315+
};
316+
}, [dropdownRef]);
317+
318+
return dropdownRef
319+
320+
}
321+
322+
const ref = useOutsideClick(handleClose);
313323

314324
return (
315-
<div ref={dropdownRef} className={showMenu}>
325+
// <div ref={dropdownRef} className={showMenu}> dropdownRef making the menu fly off and anchorel messingup
326+
<div ref={ref} className={showMenu}>
316327
<Link to="/tutorial" style={{ textDecoration: 'none' }} target="_blank">
317328
<button>
318329
Tutorial

server/models/reactypeModels.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const projectSchema = new Schema(
9696
}
9797
]
9898
},
99-
{ minimize: true }
99+
{ minimize: false }//changed to false -dw
100100
);
101101

102102
export const Users = mongoose.model('Users', userSchema);

0 commit comments

Comments
 (0)