-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unregister default block in customizer to be able to empty widget are…
…as (#32979)
- Loading branch information
1 parent
890acf5
commit a39cb7e
Showing
3 changed files
with
48 additions
and
4 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
33 changes: 33 additions & 0 deletions
33
packages/customize-widgets/src/components/block-appender/index.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,33 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef, useEffect } from '@wordpress/element'; | ||
import { | ||
ButtonBlockAppender, | ||
store as blockEditorStore, | ||
} from '@wordpress/block-editor'; | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
export default function BlockAppender( props ) { | ||
const ref = useRef(); | ||
const isBlocksListEmpty = useSelect( | ||
( select ) => select( blockEditorStore ).getBlockCount() === 0 | ||
); | ||
|
||
// Move the focus to the block appender to prevent focus from | ||
// being lost when emptying the widget area. | ||
useEffect( () => { | ||
if ( isBlocksListEmpty && ref.current ) { | ||
const { ownerDocument } = ref.current; | ||
|
||
if ( | ||
! ownerDocument.activeElement || | ||
ownerDocument.activeElement === ownerDocument.body | ||
) { | ||
ref.current.focus(); | ||
} | ||
} | ||
}, [ isBlocksListEmpty ] ); | ||
|
||
return <ButtonBlockAppender { ...props } ref={ ref } />; | ||
} |
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