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

Widget Editor: Focus toggle button when the global inserter is closed #68216

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,47 @@ import {
__experimentalUseDialog as useDialog,
} from '@wordpress/compose';
import { useCallback, useRef } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import { ESCAPE } from '@wordpress/keycodes';

/**
* Internal dependencies
*/
import useWidgetLibraryInsertionPoint from '../../hooks/use-widget-library-insertion-point';
import { store as editWidgetsStore } from '../../store';
import { unlock } from '../../lock-unlock';

export default function InserterSidebar() {
const isMobileViewport = useViewportMatch( 'medium', '<' );
const { rootClientId, insertionIndex } = useWidgetLibraryInsertionPoint();

const inserterSidebarToggleRef = useSelect( ( select ) => {
return unlock(
select( editWidgetsStore )
).getInserterSidebarToggleRef();
}, [] );

const { setIsInserterOpened } = useDispatch( editWidgetsStore );

const closeInserter = useCallback( () => {
return setIsInserterOpened( false );
}, [ setIsInserterOpened ] );
setIsInserterOpened( false );
inserterSidebarToggleRef.current?.focus();
}, [ setIsInserterOpened, inserterSidebarToggleRef ] );

const closeOnEscape = useCallback(
( event ) => {
if ( event.keyCode === ESCAPE && ! event.defaultPrevented ) {
event.preventDefault();
closeInserter();
}
},
[ closeInserter ]
);

const [ inserterDialogRef, inserterDialogProps ] = useDialog( {
onClose: closeInserter,
focusOnMount: true,
onKeyDown: closeOnEscape,
} );

const libraryRef = useRef();
Expand Down
32 changes: 32 additions & 0 deletions test/e2e/specs/widgets/editing-widgets.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ test.describe( 'Widgets screen', () => {
{ name: /^Empty block/ }
);

// When the block is focused, the global inserter is closed,
// which in turn focuses the global inserter toggle.
// So we need to explicitly focus the paragraph block again.
await addedParagraphBlockInFirstWidgetArea.focus();
await addedParagraphBlockInFirstWidgetArea.focus();
Comment on lines +86 to 90
Copy link
Contributor Author

@t-hamano t-hamano Dec 22, 2024

Choose a reason for hiding this comment

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

This may seem really weird, but in the widget editor, when the content area is clicked, the global inserter is closed. As a result of this PR, the global inserter toggle is focused, so we have to focus the block again.

dfb65b4f1f445abe11a98aa0492327ff.mp4

I've also submitted #67838 to not close the global inserter when the content area is focused. If we can move forward with that PR, we won't need this hack anymore.

await page.keyboard.type( 'First Paragraph' );

Expand All @@ -91,6 +95,10 @@ test.describe( 'Widgets screen', () => {
// TODO: We can add a test for the insertion indicator here.
await addParagraphBlock.click();

// When the block is focused, the global inserter is closed,
// which in turn focuses the global inserter toggle.
// So we need to explicitly focus the paragraph block again.
await addedParagraphBlockInFirstWidgetArea.focus();
await addedParagraphBlockInFirstWidgetArea.focus();
await page.keyboard.type( 'Second Paragraph' );

Expand All @@ -116,6 +124,11 @@ test.describe( 'Widgets screen', () => {
secondWidgetArea.getByRole( 'document', {
name: /^Empty block/,
} );

// When the block is focused, the global inserter is closed,
// which in turn focuses the global inserter toggle.
// So we need to explicitly focus the paragraph block again.
await addedParagraphBlockInSecondWidgetArea.focus();
await addedParagraphBlockInSecondWidgetArea.focus();
await page.keyboard.type( 'Third Paragraph' );

Expand Down Expand Up @@ -145,6 +158,25 @@ test.describe( 'Widgets screen', () => {
} );
} );

test( 'Should focus the global inserter toggle when the global inserter is closed', async ( {
page,
} ) => {
const blockInserterToggle = page
.getByRole( 'toolbar', { name: 'Document tools' } )
.getByRole( 'button', { name: 'Block Inserter', exact: true } );
await blockInserterToggle.click();
await page
.getByRole( 'button', { name: 'Close Block Inserter' } )
.click();

await expect( blockInserterToggle ).toBeFocused();

await blockInserterToggle.click();
await page.keyboard.press( 'Escape' );

await expect( blockInserterToggle ).toBeFocused();
} );

test( 'Should insert content using the inline inserter', async ( {
page,
widgetsScreen,
Expand Down
Loading