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 { deleteCookie } from 'cookies-next'
import { useRouter } from 'next/router'
@ -40,7 +40,7 @@ const Header = () => {
}
function onClickOutsideMenu() {
setOpen(!open)
setOpen(false)
}
function copyToClipboard() {

View file

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