Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/Draggable.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type DraggableProps = {
defaultPosition: ControlPosition,
positionOffset: PositionOffsetControlPosition,
position: ControlPosition,
draggableRef: Element,
scale: number
};

Expand Down Expand Up @@ -179,14 +180,14 @@ export default class Draggable extends React.Component<DraggableProps, Draggable
// Set x/y if a new position is provided in props that is different than the previous.
if (
position &&
(!prevPropsPosition ||
(!prevPropsPosition ||
position.x !== prevPropsPosition.x || position.y !== prevPropsPosition.y
)
) {
log('Draggable: getDerivedStateFromProps %j', {position, prevPropsPosition});
return {
x: position.x,
y: position.y,
x: position.x,
y: position.y,
prevPropsPosition: {...position}
};
}
Expand Down Expand Up @@ -222,11 +223,13 @@ export default class Draggable extends React.Component<DraggableProps, Draggable
'component effectively undraggable. Please attach `onDrag` or `onStop` handlers so you can adjust the ' +
'`position` of this element.');
}

this.draggableRef = React.createRef();
}

componentDidMount() {
// Check to see if the element passed is an instanceof SVGElement
if(typeof window.SVGElement !== 'undefined' && ReactDOM.findDOMNode(this) instanceof window.SVGElement) {
if(typeof window.SVGElement !== 'undefined' && this.draggableRef.current instanceof window.SVGElement) {
this.setState({isElementSVG: true});
}
}
Expand Down Expand Up @@ -373,7 +376,7 @@ export default class Draggable extends React.Component<DraggableProps, Draggable
// Reuse the child provided
// This makes it flexible to use whatever element is wanted (div, ul, etc)
return (
<DraggableCore {...draggableCoreProps} onStart={this.onDragStart} onDrag={this.onDrag} onStop={this.onDragStop}>
<DraggableCore ref={this.draggableRef} {...draggableCoreProps} onStart={this.onDragStart} onDrag={this.onDrag} onStop={this.onDragStop}>
{React.cloneElement(React.Children.only(children), {
className: className,
style: {...children.props.style, ...style},
Expand Down
22 changes: 17 additions & 5 deletions lib/DraggableCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export type DraggableCoreProps = {
offsetParent: HTMLElement,
grid: [number, number],
handle: string,
elementRef: Element,
onStart: DraggableEventHandler,
onDrag: DraggableEventHandler,
onStop: DraggableEventHandler,
Expand All @@ -78,6 +79,11 @@ export type DraggableCoreProps = {

export default class DraggableCore extends React.Component<DraggableCoreProps, DraggableCoreState> {

constructor(props) {
super(props);
this.elementRef = React.createRef();
}

static displayName = 'DraggableCore';

static propTypes = {
Expand Down Expand Up @@ -116,7 +122,7 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
* `grid` specifies the x and y that dragging should snap to.
*/
grid: PropTypes.arrayOf(PropTypes.number),

/**
* `handle` specifies a selector to be used as the handle that initiates drag.
*
Expand Down Expand Up @@ -185,6 +191,10 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
*/
onMouseDown: PropTypes.func,

/**
* The element ref to be used
*/
elementRef: PropTypes.func,
/**
* These properties should be defined on the child, not here.
*/
Expand All @@ -202,6 +212,7 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
handle: null,
grid: null,
transform: null,
elementRef: null,
onStart: function(){},
onDrag: function(){},
onStop: function(){},
Expand All @@ -218,7 +229,7 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
componentWillUnmount() {
// Remove any leftover event handlers. Remove both touch and mouse handlers in case
// some browser quirk caused a touch event to fire during a mouse move, or vice versa.
const thisNode = ReactDOM.findDOMNode(this);
const thisNode = this.elementRef.current;
if (thisNode) {
const {ownerDocument} = thisNode;
removeEvent(ownerDocument, eventsFor.mouse.move, this.handleDrag);
Expand All @@ -237,7 +248,7 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
if (!this.props.allowAnyClick && typeof e.button === 'number' && e.button !== 0) return false;

// Get nodes. Be sure to grab relative document (could be iframed)
const thisNode = ReactDOM.findDOMNode(this);
const thisNode = this.elementRef.current;
if (!thisNode || !thisNode.ownerDocument || !thisNode.ownerDocument.body) {
throw new Error('<DraggableCore> not mounted on DragStart!');
}
Expand Down Expand Up @@ -346,7 +357,7 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
const {x, y} = position;
const coreEvent = createCoreData(this, x, y);

const thisNode = ReactDOM.findDOMNode(this);
const thisNode = this.elementRef.current;
if (thisNode) {
// Remove user-select hack
if (this.props.enableUserSelectHack) removeUserSelectStyles(thisNode.ownerDocument);
Expand Down Expand Up @@ -410,7 +421,8 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
onMouseDown: this.onMouseDown,
onTouchStart: this.onTouchStart,
onMouseUp: this.onMouseUp,
onTouchEnd: this.onTouchEnd
onTouchEnd: this.onTouchEnd,
ref: this.elementRef
});
}
}