Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid adding default block to empty widget areas in customizer #32979

Merged
merged 4 commits into from
Jun 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ function* ensureDefaultBlock() {
// To avoid a focus loss when removing the last block, assure there is
// always a default block if the last of the blocks have been removed.
if ( count === 0 ) {
const { __unstableHasCustomAppender } = yield controls.select(
blockEditorStoreName,
'getSettings'
);

// If there's an custom appender, don't insert default block.
// We have to remember to manually move the focus elsewhere to
// prevent it from being lost though.
if ( __unstableHasCustomAppender ) {
return;
}

return yield insertDefaultBlock();
}
}
Expand Down
33 changes: 33 additions & 0 deletions packages/customize-widgets/src/components/block-appender/index.js
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
Comment on lines +24 to +25
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we need this, but in case if the focus is intentionally moved to other element then we don't want to hijack it either.

) {
ref.current.focus();
}
}
}, [ isBlocksListEmpty ] );

return <ButtonBlockAppender { ...props } ref={ ref } />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
WritingFlow,
BlockEditorKeyboardShortcuts,
__unstableBlockSettingsMenuFirstItem,
ButtonBlockAppender,
} from '@wordpress/block-editor';
import { uploadMedia } from '@wordpress/media-utils';

Expand All @@ -32,6 +31,7 @@ import SidebarEditorProvider from './sidebar-editor-provider';
import { store as customizeWidgetsStore } from '../../store';
import WelcomeGuide from '../welcome-guide';
import KeyboardShortcuts from '../keyboard-shortcuts';
import BlockAppender from '../block-appender';

export default function SidebarBlockEditor( {
blockEditorSettings,
Expand Down Expand Up @@ -80,6 +80,7 @@ export default function SidebarBlockEditor( {
mediaUpload: mediaUploadBlockEditor,
hasFixedToolbar: isFixedToolbarActive,
keepCaretInsideBlock,
__unstableHasCustomAppender: true,
};
}, [
hasUploadPermissions,
Expand Down Expand Up @@ -117,9 +118,7 @@ export default function SidebarBlockEditor( {
<BlockSelectionClearer>
<WritingFlow>
<ObserveTyping>
<BlockList
renderAppender={ ButtonBlockAppender }
/>
<BlockList renderAppender={ BlockAppender } />
</ObserveTyping>
</WritingFlow>
</BlockSelectionClearer>
Expand Down