diff --git a/packages/block-editor/src/components/block-list-appender/index.js b/packages/block-editor/src/components/block-list-appender/index.js index 68f36f7dd25058..928f9fc29d1707 100644 --- a/packages/block-editor/src/components/block-list-appender/index.js +++ b/packages/block-editor/src/components/block-list-appender/index.js @@ -39,77 +39,12 @@ function DefaultAppender( { rootClientId } ) { ); } -function useAppender( rootClientId, CustomAppender ) { - const isVisible = useSelect( - ( select ) => { - const { - getTemplateLock, - getSelectedBlockClientId, - __unstableGetEditorMode, - getBlockEditingMode, - } = select( blockEditorStore ); - - if ( ! CustomAppender ) { - const selectedBlockClientId = getSelectedBlockClientId(); - const isParentSelected = - rootClientId === selectedBlockClientId || - ( ! rootClientId && ! selectedBlockClientId ); - if ( ! isParentSelected ) { - return false; - } - } - - if ( - getTemplateLock( rootClientId ) || - getBlockEditingMode( rootClientId ) === 'disabled' || - __unstableGetEditorMode() === 'zoom-out' - ) { - return false; - } - - return true; - }, - [ rootClientId, CustomAppender ] - ); - - if ( ! isVisible ) { - return null; - } - - return CustomAppender ? ( - - ) : ( - - ); -} - -function BlockListAppender( { +export default function BlockListAppender( { rootClientId, - renderAppender, + CustomAppender, className, tagName: TagName = 'div', } ) { - if ( renderAppender === false ) { - return null; - } - - return ( - - ); -} - -function BlockListAppenderInner( { - rootClientId, - renderAppender, - className, - tagName: TagName, -} ) { - const appender = useAppender( rootClientId, renderAppender ); const isDragOver = useSelect( ( select ) => { const { @@ -130,10 +65,6 @@ function BlockListAppenderInner( { [ rootClientId ] ); - if ( ! appender ) { - return null; - } - return ( - { appender } + { CustomAppender ? ( + + ) : ( + + ) } ); } - -export default BlockListAppender; diff --git a/packages/block-editor/src/components/block-list/index.js b/packages/block-editor/src/components/block-list/index.js index 09a4712a3dd6dd..a966c2850289de 100644 --- a/packages/block-editor/src/components/block-list/index.js +++ b/packages/block-editor/src/components/block-list/index.js @@ -151,29 +151,51 @@ export default function BlockList( settings ) { function Items( { placeholder, rootClientId, - renderAppender, + renderAppender: CustomAppender, __experimentalAppenderTagName, layout = defaultLayout, } ) { - const { order, selectedBlocks, visibleBlocks, temporarilyEditingAsBlocks } = - useSelect( - ( select ) => { - const { - getBlockOrder, - getSelectedBlockClientIds, - __unstableGetVisibleBlocks, - __unstableGetTemporarilyEditingAsBlocks, - } = select( blockEditorStore ); - return { - order: getBlockOrder( rootClientId ), - selectedBlocks: getSelectedBlockClientIds(), - visibleBlocks: __unstableGetVisibleBlocks(), - temporarilyEditingAsBlocks: - __unstableGetTemporarilyEditingAsBlocks(), - }; - }, - [ rootClientId ] - ); + // Avoid passing CustomAppender to useSelect because it could be a new + // function on every render. + const hasAppender = CustomAppender !== false; + const hasCustomAppender = !! CustomAppender; + const { + order, + selectedBlocks, + visibleBlocks, + temporarilyEditingAsBlocks, + shouldRenderAppender, + } = useSelect( + ( select ) => { + const { + getBlockOrder, + getSelectedBlockClientId, + getSelectedBlockClientIds, + __unstableGetVisibleBlocks, + __unstableGetTemporarilyEditingAsBlocks, + getTemplateLock, + getBlockEditingMode, + __unstableGetEditorMode, + } = select( blockEditorStore ); + const selectedBlockClientId = getSelectedBlockClientId(); + return { + order: getBlockOrder( rootClientId ), + selectedBlocks: getSelectedBlockClientIds(), + visibleBlocks: __unstableGetVisibleBlocks(), + temporarilyEditingAsBlocks: + __unstableGetTemporarilyEditingAsBlocks(), + shouldRenderAppender: + hasAppender && + ( hasCustomAppender + ? ! getTemplateLock( rootClientId ) && + getBlockEditingMode( rootClientId ) !== 'disabled' && + __unstableGetEditorMode() !== 'zoom-out' + : rootClientId === selectedBlockClientId || + ( ! rootClientId && ! selectedBlockClientId ) ), + }; + }, + [ rootClientId, hasAppender, hasCustomAppender ] + ); return ( @@ -199,11 +221,13 @@ function Items( { clientId={ temporarilyEditingAsBlocks } /> ) } - + { shouldRenderAppender && ( + + ) } ); }