Skip to content

Commit

Permalink
Stop dragging on unmount
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Nov 26, 2019
1 parent e2a8e8e commit afc50e8
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions packages/block-editor/src/components/block-draggable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
*/
import { Draggable } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect, useRef } from '@wordpress/element';

const BlockDraggable = ( { children, clientIds } ) => {
const { srcRootClientId, index, isDraggable } = useSelect( ( select ) => {
const {
srcRootClientId,
index,
isDraggable,
} = useSelect( ( select ) => {
const {
getBlockIndex,
getBlockRootClientId,
Expand All @@ -20,8 +25,18 @@ const BlockDraggable = ( { children, clientIds } ) => {
isDraggable: clientIds.length === 1 && 'all' !== templateLock,
};
}, [ clientIds ] );
const isDragging = useRef( false );
const { startDraggingBlocks, stopDraggingBlocks } = useDispatch( 'core/block-editor' );

// Stop dragging blocks if the block draggable is unmounted
useEffect( () => {
return () => {
if ( isDragging.current ) {
stopDraggingBlocks();
}
};
}, [] );

if ( ! isDraggable ) {
return null;
}
Expand All @@ -38,8 +53,14 @@ const BlockDraggable = ( { children, clientIds } ) => {
<Draggable
elementId={ blockElementId }
transferData={ transferData }
onDragStart={ startDraggingBlocks }
onDragEnd={ stopDraggingBlocks }
onDragStart={ () => {
startDraggingBlocks();
isDragging.current = true;
} }
onDragEnd={ () => {
stopDraggingBlocks();
isDragging.current = false;
} }
>
{
( { onDraggableStart, onDraggableEnd } ) => {
Expand Down

0 comments on commit afc50e8

Please sign in to comment.