-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clear selected block when focusing on element outside the editor (#31530
) * Clear selected block when focusing on element outside the editor * Add comments * Add tests * Fix lint errors
- Loading branch information
1 parent
30e6eb0
commit a3ad0b5
Showing
4 changed files
with
203 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/customize-widgets/src/components/customize-widgets/use-clear-selected-block.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect } from '@wordpress/element'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { store as blockEditorStore } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* We can't just use <BlockSelectionClearer> because the customizer has | ||
* many root nodes rather than just one in the post editor. | ||
* We need to listen to the focus events in all those roots, and also in | ||
* the preview iframe. | ||
* | ||
* @param {Object} sidebarControl The sidebar control instance. | ||
* @param {Object} popoverRef The ref object of the popover node container. | ||
*/ | ||
export default function useClearSelectedBlock( sidebarControl, popoverRef ) { | ||
const { hasSelectedBlock, hasMultiSelection } = useSelect( | ||
blockEditorStore | ||
); | ||
const { clearSelectedBlock } = useDispatch( blockEditorStore ); | ||
|
||
useEffect( () => { | ||
if ( popoverRef.current && sidebarControl ) { | ||
const inspectorContainer = | ||
sidebarControl.inspector.contentContainer[ 0 ]; | ||
const container = sidebarControl.container[ 0 ]; | ||
const ownerDocument = container.ownerDocument; | ||
const ownerWindow = ownerDocument.defaultView; | ||
|
||
function handleClearSelectedBlock( element ) { | ||
if ( | ||
// 1. Make sure there are blocks being selected. | ||
( hasSelectedBlock() || hasMultiSelection() ) && | ||
// 2. The element should exist in the DOM (not deleted). | ||
element && | ||
ownerDocument.contains( element ) && | ||
// 3. It should also not exist in the container, inspector, nor the popover. | ||
! container.contains( element ) && | ||
! popoverRef.current.contains( element ) && | ||
! inspectorContainer.contains( element ) | ||
) { | ||
clearSelectedBlock(); | ||
} | ||
} | ||
|
||
// Handle focusing in the same document. | ||
function handleFocus( event ) { | ||
handleClearSelectedBlock( event.target ); | ||
} | ||
// Handle focusing outside the current document, like to iframes. | ||
function handleBlur() { | ||
handleClearSelectedBlock( ownerDocument.activeElement ); | ||
} | ||
|
||
ownerDocument.addEventListener( 'focusin', handleFocus ); | ||
ownerWindow.addEventListener( 'blur', handleBlur ); | ||
|
||
return () => { | ||
ownerDocument.removeEventListener( 'focusin', handleFocus ); | ||
ownerWindow.removeEventListener( 'blur', handleBlur ); | ||
}; | ||
} | ||
}, [ | ||
popoverRef, | ||
sidebarControl, | ||
hasSelectedBlock, | ||
hasMultiSelection, | ||
clearSelectedBlock, | ||
] ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters