davidtheclark / react-aria-menubutton

A fully accessible, easily themeable, React-powered menu button

Home Page:https://davidtheclark.github.io/react-aria-menubutton/demo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Button event propagation

mostafaelassar opened this issue · comments

I have used Wrapper alongside other components inside MyComponent where it has its own click event handler.

<MyComponent onClick={this.handleClick}>
    {otherComponents}
    <Wrapper>
        <Button>
          click me
        </Button>
        <Menu className='MyMenuButton-menu'>
          <ul>{menuItems}</ul>
        </Menu>
    </Wrapper>
</MyComponent>

It would seem that a click on Button propagates event to MyComponent & triggers its click event handler.

You can handle this in your MyComponent ?

At first I got around this by using this onClick handler on Button

    handleClick = (e: React.MouseEvent) => {
        const { disabled } = this.props;
        e.stopPropagation();
        if (disabled) {
            return;
        }
        if (document.querySelector(`#${this.wrapperId} > [role="menu"]`)) {
            closeMenu(this.wrapperId);
        } else {
            openMenu(this.wrapperId);
        }
    };

But then I used this one instead on Wrapper since I had other buttons inside Menu itself.

handleClick = (e: React.MouseEvent) => {
    e.stopPropagation();
};

Is it possible to share a working example ?