Skip to content
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
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- `ColorPalette`, `BorderControl`: Don't hyphenate hex value in `aria-label` ([#52932](https://github.com/WordPress/gutenberg/pull/52932)).

### Bug Fix

- `Modal`: Fix loss of focus when clicking outside ([#52653](https://github.com/WordPress/gutenberg/pull/52653)).

## 25.4.0 (2023-07-20)

### Enhancements
Expand Down
16 changes: 16 additions & 0 deletions packages/components/src/modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ function UnforwardedModal(
[ hasScrolledContent ]
);

const onOverlayPress: React.PointerEventHandler< HTMLDivElement > = (
event
) => {
if ( event.target === event.currentTarget ) {
event.preventDefault();
onRequestClose( event );
}
};

return createPortal(
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
<div
Expand All @@ -179,6 +188,13 @@ function UnforwardedModal(
overlayClassName
) }
onKeyDown={ handleEscapeKeyDown }
// Avoids loss of focus from clicking the overlay and also obviates
// `useFocusOutside` aside from cases of focus programmatically
// moving outside. TODO ideally both the hook and this handler
// won't be needed and one can be removed.
onPointerDown={

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we add a TODO comment here? Something to the effect of "Reconcile with useFocusOutside" and "Ideally this should be an onClick".

@stokesman stokesman Jul 24, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I gave it my best and hopefully it captures what you had in mind. At least regarding the first part. I left out the bit about preferring onClick. That could provide some nuance to the interaction but I think handling pointerdown will still be necessary. That’s because if the default isn't prevented from pointerdown focus will move and be left unreturned by useFocusReturn.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It does 👍 I'm mostly concerned that we're treating a mere pointerdown as a click intent, which isn't technically true. For example I sometimes mousedown on a button but change my mind and drag away so it doesn't go through as a click.

But I guess we could even resolve that right now, if we just event.preventDefault() on the pointerdown, and wait until the click event to actually call onRequestClose().

@stokesman stokesman Jul 25, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep, that's just what I meant about nuance in the interaction. I do the same sometimes. To keep current behavior and this PR focused, I'd left it as is.

Since you agree it should be resolved I went ahead and tried it. It's not quite as simple as we likely both thought. It's also an opportunity to try a bit of further nuance as suggested by Aki #51602 (comment). So I've got a branch for it and will make it a separate PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

…and that PR #52994

shouldCloseOnClickOutside ? onOverlayPress : undefined
}
>
<StyleProvider document={ document }>
<div
Expand Down
33 changes: 33 additions & 0 deletions packages/components/src/modal/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import { render, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -82,4 +87,32 @@ describe( 'Modal', () => {
await user.keyboard( '[Escape]' );
expect( onRequestClose ).toHaveBeenCalled();
} );

it( 'should return focus when dismissed by clicking outside', async () => {
const user = userEvent.setup();
const ReturnDemo = () => {
const [ isShown, setIsShown ] = useState( false );
return (
<div>
<button onClick={ () => setIsShown( true ) }>📣</button>
{ isShown && (
<Modal onRequestClose={ () => setIsShown( false ) }>
<p>Modal content</p>
</Modal>
) }
</div>
);
};
render( <ReturnDemo /> );

const opener = screen.getByRole( 'button' );
await user.click( opener );
const modalFrame = screen.getByRole( 'dialog' );
expect( modalFrame ).toHaveFocus();

// Disable reason: No semantic query can reach the overlay.
// eslint-disable-next-line testing-library/no-node-access
await user.click( modalFrame.parentElement! );
expect( opener ).toHaveFocus();
} );
} );