Recommended way to have drawer resizable?

前端 未结 4 1506
故里飘歌
故里飘歌 2021-01-05 16:00

I would like to have the material ui drawer\'s width resizable through a draggable handle. My current approach is to have a mousevent listener on the whole app which checks

4条回答
  •  清酒与你
    2021-01-05 16:33

    You can use indicator dragger with mousedown on it.

    Here for example

    // styles
    dragger: {
      width: '5px',
      cursor: 'ew-resize',
      padding: '4px 0 0',
      borderTop: '1px solid #ffffd',
      position: 'absolute',
      top: 0,
      left: 0,
      bottom: 0,
      zIndex: '100',
      backgroundColor: '#f4f7f9'
    }
    
    ...
    
    state = {
      isResizing: false,
      lastDownX: 0,
      newWidth: {}
    };
    
    handleMousedown = e => {
      this.setState({ isResizing: true, lastDownX: e.clientX });
    };
    
    handleMousemove = e => {
      // we don't want to do anything if we aren't resizing.
      if (!this.state.isResizing) {
        return;
      }
    
      let offsetRight =
        document.body.offsetWidth - (e.clientX - document.body.offsetLeft);
      let minWidth = 50;
      let maxWidth = 600;
      if (offsetRight > minWidth && offsetRight < maxWidth) {
        this.setState({ newWidth: { width: offsetRight } });
      }
    };
    
    handleMouseup = e => {
      this.setState({ isResizing: false });
    };
    
    componentDidMount() {
      document.addEventListener('mousemove', e => this.handleMousemove(e));
      document.addEventListener('mouseup', e => this.handleMouseup(e));
    }
    
    ...
    
    
      
    { this.handleMousedown(event); }} className={classes.dragger} /> {drawer}

    The idea is, when click the dragger, it will resize width Drawer followed mouse move.

    Play DEMO.

提交回复
热议问题