Skip to content

Commit

Permalink
Make chip transparent when dragging over undroppable area.
Browse files Browse the repository at this point in the history
  • Loading branch information
tellthemachines committed Dec 12, 2023
1 parent d4887ec commit 082f718
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import BlockIcon from '../block-icon';
export default function BlockDraggableChip( { count, icon, isPattern } ) {
const patternLabel = isPattern && __( 'Pattern' );
return (
<div className="block-editor-block-draggable-chip-wrapper">
<div
className="block-editor-block-draggable-chip-wrapper"
style={ { opacity: 'var(--wp--block-draggable-valid-opacity)' } }
>
<div
className="block-editor-block-draggable-chip"
data-testid="block-draggable-chip"
Expand Down
18 changes: 16 additions & 2 deletions packages/block-editor/src/components/block-draggable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const BlockDraggable = ( {
}, [] );

const blockRef = useBlockRef( clientIds[ 0 ] );
const editorRoot = blockRef.current?.closest( '.is-root-container' );
const editorRoot = blockRef.current?.closest( 'body' );

// Add a dragover event listener to the editor root to track the blocks being dragged over.
// The listener has to be inside the editor iframe otherwise the target isn't accessible.
Expand All @@ -98,6 +98,18 @@ const BlockDraggable = ( {
) {
setTargetClientId( newTargetClientId );
}
// Update the body class to reflect if drop target is valid.
// This has to be done on the document body because the draggable
// chip is rendered outside of the editor iframe.
if ( isDropTargetValid ) {
window?.document?.body?.classList?.remove(
'block-draggable-invalid-drag-token'
);
} else {
window?.document?.body?.classList?.add(
'block-draggable-invalid-drag-token'
);
}
};

editorRoot.addEventListener( 'dragover', onDragOver );
Expand Down Expand Up @@ -151,9 +163,11 @@ const BlockDraggable = ( {
<BlockDraggableChip
count={ clientIds.length }
icon={ icon }
isValid={ isDropTargetValid }
clientIds={ clientIds }
targetClientId={ targetClientId }
/>
}
isValid={ isDropTargetValid }
>
{ ( { onDraggableStart, onDraggableEnd } ) => {
return children( {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,11 @@
font-size: $default-font-size;
}
}

:root {
--wp--block-draggable-valid-opacity: 1;
}

.block-draggable-invalid-drag-token {
--wp--block-draggable-valid-opacity: 0.6;
}

0 comments on commit 082f718

Please sign in to comment.