fix: menu closing when clicking on it

Signed-off-by: Avior <github@avior.me>
This commit is contained in:
Florian Bouillon 2022-10-16 12:59:54 +02:00
parent 6d7fc01517
commit 14f36f0bbb
Signed by: Florian Bouillon
GPG Key ID: BEEAF3722D0EBF64
2 changed files with 13 additions and 4 deletions

View File

@ -39,3 +39,6 @@ $height = 76px
.header
margin-right 16px
.menu
opacity 1

View File

@ -68,7 +68,7 @@ interface State {
/**
* Navbar Component
* @version 1.0.3
* @version 1.0.4
*/
export default class Navbar extends React.Component<Props, State> {
@ -162,13 +162,20 @@ export default class Navbar extends React.Component<Props, State> {
)}
{this.state.subMenu && (
<div style={{position: 'fixed', top: 76, right: this.state.subMenu.x}}>
<Menu outline items={this.state.subMenu.menu} />
<Menu className={css.menu} outline items={this.state.subMenu.menu} />
</div>
)}
</>
)
private onBodyClick = () => {
private onBodyClick = (ev: MouseEvent) => {
let target = ev.target as HTMLElement | null
do {
if (target && target.classList.contains(css.menu)) {
return
}
target = target?.parentElement as HTMLElement | null
} while (target)
this.setState({subMenu: undefined})
}
@ -176,7 +183,6 @@ export default class Navbar extends React.Component<Props, State> {
ev.stopPropagation()
const x = window.innerWidth - (ev.currentTarget.offsetLeft + ev.currentTarget.offsetWidth)
if (subMenu && (!this.state.subMenu || x !== this.state.subMenu?.x)) {
console.log(ev)
this.setState({
subMenu: {
x,