react-resizable icon indicating copy to clipboard operation
react-resizable copied to clipboard

Resizable nested inside of draggable creates drag with resize handler

Open blparker opened this issue 10 years ago • 4 comments

Given the following code:

<Draggable bounds="parent">
  <Resizable className="box" width={150} height={150} onResizeStart={this.onStart}>
    <div style={{width: '150px', height: '150px' }}>Test</div>
  </Resizable>
</Draggable>

Where <Draggable /> is the react-draggable module, when resizing using the react-resizable-handle, it also triggers the drag of the parent element. Is this an issue or is there a way to 'cancel' the resize from bubbling upwards to the parent draggable?

blparker avatar Nov 04 '15 06:11 blparker

Same problem here, any resolution?

geohuz avatar Dec 30 '15 15:12 geohuz

I guess I just found some clues, first you should put cancel prop in the draggable, like this

<Draggable  bounds="parent" cancel=".react-resizable-handle">

Then you need to have a customised onResize function, which draws the resized box, like this:

onResize: function (event: Event, data: Object) {
      let {element, size} = data;
      let {width, height} = size;
      let widthChanged = width !== this.state.width, heightChanged = height !== this.state.height;
      if (!widthChanged && !heightChanged) return;

      //[width, height] = this.runConstraints(width, height);
      console.log(width, height);

      this.setState({width, height}, () => {
        if (this.props.onResize) {
          this.props.onResize(event, {element, size: {width, height}});
        }
      });
    },

the Resizable component will be like this:

<div style={{width: this.state.width + 'px', height: this.state.height + 'px'}}>
    text
</div>

This is very roughly implementation, I found the code in this link:

https://github.com/STRML/react-resizable/blob/master/lib/ResizableBox.jsx

geohuz avatar Dec 30 '15 17:12 geohuz

Hey. I've just pushed #82 for this. Please check it out and let me know your comments. Cheers!

xedef avatar Dec 19 '17 12:12 xedef

I reproduced my scenario in here.

I think it is a special case. I use Resizable inside a useDrag element (react-dnd). And when resizing, the drag event is propagates to the useDrag-element.

I tried to use e.stopPropagation(), but it didn't work. Then I gave e.preventDefault() a try and It worked !!

Anyone can help me know why?

rtxu avatar Aug 20 '19 14:08 rtxu