Plus · DragMove
DragMove
Set elements to change their position by dragging

When to use

It is used to set the element that can be dragged to change its position. It supports limiting the drag range and customizing the elements that trigger dragging.

Demos

How to introduce

DragMove supported from v2.71.0.

Basic usage

Elements wrapped by DragMove will be able to change their position by dragging.
Notice
  1. DragMove will set the draggable element to absolute positioning
  2. DragMove needs to apply DOM event listeners to children. If the child element is a custom component, you need to ensure that it can pass properties to the underlying DOM element. The following types of children are supported:
    1. Class Component, it is not mandatory to bind ref, but you need to ensure that props can be transparently transmitted to the real DOM node
    2. Use the functional component wrapped by forwardRef to transparently transmit props and ref to the real DOM node in children
    3. Real DOM nodes, such as span, div, p...

Limit drag range

Passing in constrainer, this function returns the elements that limit the draggable range.
Note: The elements returned by the constrainer need to be positioned relative

Customize elements that trigger dragging

Passing in handler, this function returns the element that triggered the drag. If not set, you can click anywhere to drag; if set, only the part of the element returned by the handler can be dragged.

Customize position processing after dragging

You can customize the position processing after dragging through customMove. After this parameter is set, the DragMove component will only return the calculated position through the parameters without setting it. The user can set the new position as needed.

API

PropertyDescriptionTypeDefault value
allowInputDragWhether to allow dragging when clicking on native input/textareabooleanfalse
allowMoveDetermine whether dragging is allowed when clicking/touching.(event: TouchEvent |MouseEvent, element: ReactNode) => boolean-
constrainerReturns the element that limits the draggable range.() => ReactNode | 'parent'-
customMoveCustomize position processing after dragging(element: ReactNode, top: number, left: number) => void-
handlerReturns the element that triggers dragging.() => ReactNode-
onMouseDownCallback when mouse is pressed(e: MouseEvent) => void-
onMouseMoveCallback when mouse moves(e: MouseEvent) => void-
onMouseUpCallback when mouse is raised(e: MouseEvent) => void-
onTouchCancelCallback when touch cancels(e: TouchEvent) => void-
onTouchEndcallback when touch ends(e: TouchEvent) => void-
onTouchMoveCallback when touch moves(e: TouchEvent) => void-
onTouchStartCallback when touch starts(e: TouchEvent) => void-