Further improve menu toggling

You should be able to dismiss the menu by clicking the button again. Now you can.
This commit is contained in:
Justin Edmund 2022-12-27 17:43:15 -08:00
parent 95ef911781
commit 82e93de7fc
2 changed files with 9 additions and 4 deletions

View file

@ -1,4 +1,4 @@
import React, { useState } from 'react' import React, { useEffect, useState } from 'react'
import { useSnapshot } from 'valtio' import { useSnapshot } from 'valtio'
import { deleteCookie } from 'cookies-next' import { deleteCookie } from 'cookies-next'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
@ -40,7 +40,7 @@ const Header = () => {
} }
function onClickOutsideMenu() { function onClickOutsideMenu() {
setOpen(!open) setOpen(false)
} }
function copyToClipboard() { function copyToClipboard() {

View file

@ -36,16 +36,21 @@ const HeaderMenu = (props: Props) => {
useEffect(() => { useEffect(() => {
const handleClickOutside = (event: Event) => { const handleClickOutside = (event: Event) => {
const target = event.target instanceof Element ? event.target : null
const isButton = target && target.closest('.Button.Active')
if ( if (
ref.current && ref.current &&
event.target instanceof Element && target &&
!ref.current.contains(event.target) && !ref.current.contains(target) &&
!isButton &&
props.open props.open
) { ) {
props.onClickOutside() props.onClickOutside()
} }
} }
document.addEventListener('click', handleClickOutside, true) document.addEventListener('click', handleClickOutside, true)
return () => { return () => {
document.removeEventListener('click', handleClickOutside, true) document.removeEventListener('click', handleClickOutside, true)
} }