Skip to content

Commit

Permalink
Drop zone: rewrite with hooks and simplify (#26893)
Browse files Browse the repository at this point in the history
* Drop zone: rewrite with hooks and simplify

* Clean up
  • Loading branch information
ellatrix authored Nov 11, 2020
1 parent 5904051 commit 2296afb
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 239 deletions.
50 changes: 34 additions & 16 deletions packages/block-editor/src/components/use-on-block-drop/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
pasteHandler,
} from '@wordpress/blocks';
import { useDispatch, useSelect } from '@wordpress/data';
import { useCallback } from '@wordpress/element';

/** @typedef {import('@wordpress/element').WPSyntheticEvent} WPSyntheticEvent */

Expand Down Expand Up @@ -212,24 +213,41 @@ export default function useOnBlockDrop( targetRootClientId, targetBlockIndex ) {
} = useDispatch( 'core/block-editor' );

return {
onDrop: onBlockDrop(
targetRootClientId,
targetBlockIndex,
getBlockIndex,
getClientIdsOfDescendants,
moveBlocksToPosition
onDrop: useCallback(
onBlockDrop(
targetRootClientId,
targetBlockIndex,
getBlockIndex,
getClientIdsOfDescendants,
moveBlocksToPosition
),
[
targetRootClientId,
targetBlockIndex,
getBlockIndex,
getClientIdsOfDescendants,
moveBlocksToPosition,
]
),
onFilesDrop: onFilesDrop(
targetRootClientId,
targetBlockIndex,
hasUploadPermissions,
updateBlockAttributes,
insertBlocks
onFilesDrop: useCallback(
onFilesDrop(
targetRootClientId,
targetBlockIndex,
hasUploadPermissions,
updateBlockAttributes,
insertBlocks
),
[
targetRootClientId,
targetBlockIndex,
hasUploadPermissions,
updateBlockAttributes,
insertBlocks,
]
),
onHTMLDrop: onHTMLDrop(
targetRootClientId,
targetBlockIndex,
insertBlocks
onHTMLDrop: useCallback(
onHTMLDrop( targetRootClientId, targetBlockIndex, insertBlocks ),
[ targetRootClientId, targetBlockIndex, insertBlocks ]
),
};
}
37 changes: 15 additions & 22 deletions packages/components/src/drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { upload, Icon } from '@wordpress/icons';
/**
* Internal dependencies
*/
import { DropZoneConsumer, Context } from './provider';
import { Context } from './provider';

export function useDropZone( {
element,
Expand All @@ -22,9 +22,9 @@ export function useDropZone( {
onDrop,
isDisabled,
withPosition,
__unstableIsRelative = false,
__unstableIsRelative: isRelative = false,
} ) {
const { addDropZone, removeDropZone } = useContext( Context );
const dropZones = useContext( Context );
const [ state, setState ] = useState( {
isDraggingOverDocument: false,
isDraggingOverElement: false,
Expand All @@ -40,31 +40,26 @@ export function useDropZone( {
onHTMLDrop,
setState,
withPosition,
isRelative: __unstableIsRelative,
isRelative,
};
addDropZone( dropZone );
dropZones.add( dropZone );
return () => {
removeDropZone( dropZone );
dropZones.delete( dropZone );
};
}
}, [ isDisabled, onDrop, onFilesDrop, onHTMLDrop, withPosition ] );
}, [
isDisabled,
onDrop,
onFilesDrop,
onHTMLDrop,
withPosition,
isRelative,
] );

return state;
}

const DropZone = ( props ) => (
<DropZoneConsumer>
{ ( { addDropZone, removeDropZone } ) => (
<DropZoneComponent
addDropZone={ addDropZone }
removeDropZone={ removeDropZone }
{ ...props }
/>
) }
</DropZoneConsumer>
);

function DropZoneComponent( {
export default function DropZoneComponent( {
className,
label,
onFilesDrop,
Expand Down Expand Up @@ -115,5 +110,3 @@ function DropZoneComponent( {
</div>
);
}

export default DropZone;
Loading

0 comments on commit 2296afb

Please sign in to comment.