Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Drawer] Close request ignored when clicking on Drawer overlay #4217

Closed
holywyvern opened this issue May 9, 2016 · 11 comments
Closed

[Drawer] Close request ignored when clicking on Drawer overlay #4217

holywyvern opened this issue May 9, 2016 · 11 comments
Labels
component: drawer This is the name of the generic UI component, not the React module!

Comments

@holywyvern
Copy link

Problem description

When I click on the Overlay of the drawer, it doesn't close as expected, but pressing ESC key calls the event without troubles.

Steps to reproduce

Let me show some bits of code:

class AppMenuComponent extends React.Component {

  render() {
    return (
      <Drawer 
        docked={false} 
        open={this.props.open} 
        onRequestChange={this.props.onRequestChange}
        disableSwipeToOpen={true}
      >
        Test
      </Drawer>
    );
  }

export default class Navigation extends React.Component {
  constructor(props) {
    super(props);
    this.openMenu        = this.openMenu.bind(this);
    this.requestMenuOpen = this.requestMenuOpen.bind(this);
    this.state           = { menuOpen: false };
  }

  openMenu() {
    this.setState({ menuOpen: true });
  }

  requestMenuOpen(menuOpen=false) {
    this.setState({ menuOpen });
  }

  render() {
    return (
      <div>
        <AppMenuComponent 
             open={this.state.menuOpen} onRequestChange={this.requestMenuOpen} />
        <AppBar 
          title="Menu"
          iconElementLeft={<IconButton  onClick={ this.openMenu }><MoreVertIcon /></IconButton>}
        />
      </div>
    );
  }

}

}

requestMenuOpen is called when I press ESC key, but not if I press outside of the Drawer.

Versions

  • Material-UI: master
  • React: 15.0.2
  • Browser: Chrome 50.0.2661.94
@patsul12
Copy link

+1 also seeing this behavior

Versions

  • Material-UI: master
  • React: 15.0.2

@nathanmarks
Copy link
Member

I haven't tested the code @holywyvern, but it looks like you have a scope issue there with the object that onRequestChange callback is bound to. In React, es6 class functions are not autobound, you have to explicitly bind them to the class using either the fat arrow syntax (es7 class property initializers) or using .bind() in the constructor() function of your class.

@patsul12
Copy link

@nathanmarks I am having the same issue.
Here is the relevant chunk of code. Everything else seems to be working but clicking on the overlay isn't working

export default class Sidebar extends React.Component {                                                                                                                                    
     constructor(props) {                                                                                                                                                                    
       super(props);                                                                                                                                                                         
       this.state = {open: false};                                                                                                                                                           
       this.handleToggle = this.handleToggle.bind(this);                                                                                                                                     
      this.handleClose = this.handleClose.bind(this);                                                                                                                                       
    }                                                                                                                                                                                       

    handleToggle() { this.setState({open: !this.state.open}); }                                                                                                                             

    handleClose() { this.setState({open: false}); }                                                                                                                                         

    render() {                                                                                                                                                                              
      return (                                                                                                                                                                              
        <div>                                                                                                                                                                               
          <div className="dashboard-header">                                                                                                                                                
            <ImageDehaze onClick={this.handleToggle} />                                                                                                                                     
            <span className="main-logo-dashboard">name</span>                                                                                                                          
          </div>                                                                                                                                                                            
          <Drawer                                                                                                                                                                           
            docked={false}                                                                                                                                                                  
            width={300}                                                                                                                                                                     
            open={this.state.open}                                                                                                                                                          
            onRequestChange={(open) => this.setState({open})}                                                                                                                               
            >                                                                                                                                                                               
            <ContentClear style={contentClearStyles} onClick={this.handleClose}/>                                                                                                           
            <MenuItem a href="/">Home</MenuItem>                                                                                                                                            
            <MenuItem onClick={this.handleClose}>Menu Item 2</MenuItem>                                                                                                                     
            <MenuItem>                                                                                                                                                                      
              <UploadFormModal />                                                                                                                                                           
            </MenuItem>                                                                                                                                                                     
          </Drawer>                                                                                                                                                                         
        </div>                                                                                                                                                                              
      );                                                                                                                                                                                    
    }                                                                                                                                                                                       
  }

@holywyvern
Copy link
Author

No I'm not having a scope issue...
This clearly shows I'm binding it at the constructor...

  constructor(props) {
    /* ... */
    this.requestMenuOpen = this.requestMenuOpen.bind(this);
   /* ... */
  }

@ck86
Copy link

ck86 commented May 31, 2016

The internal Overlay component still uses the onTouchTap event, so I guess it is still necessary to include react-tap-event-plugin.

@shivekkhurana
Copy link

shivekkhurana commented Jul 1, 2016

Did you find a solution for this ? My function has this bound and react-tap-event-plugin is also injected. onRequestChange fires only in pressing esc key.

class TopBar extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isDrawerOpen: false
    };

    this.onDrawerMenuButtonClick = this.onDrawerMenuButtonClick.bind(this);
  }

  onDrawerMenuButtonClick() {
    console.log('erer');
    console.log(this.state);
    console.log('gsdhjhskd');
    this.setState({isDrawerOpen: !this.state.isDrawerOpen});
  }

  render() {
    return <AppBar
      className="TopBar"
      title="Hello Shivek"
      iconElementLeft={<IconButton
        onTouchTap={this.onDrawerMenuButtonClick}
        onClick={this.onDrawerMenuButtonClick}
      >
        <MoreVertIcon />
      </IconButton>}

      onLeftIconButtonTouchTap={this.onDrawerMenuButtonClick}
    >
      <div className="drawer">
        <Drawer
          docked={false}
          open={this.state.isDrawerOpen}
          onRequestChange={(open) => {
            console.log(open);
            this.setState({isDrawerOpen: open});
          }}
        >
          <MenuItem>Team Management</MenuItem>

        </Drawer>
      </div>
    </AppBar>;
  }
}
  • React 15.1
  • material-ui ^0.15.1
  • react-tap-event-plugin ^1.0.0
  • Chrome on MacBook

Thank you


Update 0 :
react-tap-event-plugin doesn't seem to work at all with React 15.1. onTouchTap events are not firing.


Update 1:
Don't know the internals but, I changed the location of react-tap-event-plugin injection from app bootstrap file (main.js in my case) to the file where initial ReactDom.render was being called and it started working !

@petermikitsh
Copy link
Contributor

petermikitsh commented Jul 13, 2016

For me, moving the injectTapEventPlugin(); call before calling render solved the problem.

e.g.,

injectTapEventPlugin();
render(<Index />, document.getElementById('app'));

Working with [email protected], [email protected], and [email protected].

@mpontikes
Copy link

@holywyvern Can this issue be issue be closed by @petermikitsh and @shivekkhurana 's solutions?

@mpontikes mpontikes mentioned this issue Aug 5, 2016
13 tasks
@aahan96 aahan96 closed this as completed Aug 16, 2016
@kpennell
Copy link

If anyone comes here from the material-ui-next examples onRequestClose instead of onClose is the trick to getting the overlay to work.

@mbrookes
Copy link
Member

Only if you're using an older release. From v1.0.0-beta.24 onwards it's onClose (#9451).

@kpennell
Copy link

kpennell commented Dec 28, 2017 via email

@oliviertassinari oliviertassinari added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Dec 21, 2022
@zannager zannager added component: drawer This is the name of the generic UI component, not the React module! and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: drawer This is the name of the generic UI component, not the React module!
Projects
None yet
Development

No branches or pull requests