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

Snackbar: fix focus loss on dismiss #34736

Merged
merged 2 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/src/snackbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function Snackbar(
// It is distinct from onRemove, which _looks_ like a callback but is
// actually the function to call to remove the snackbar from the UI.
onDismiss = noop,
listRef,
},
ref
) {
Expand All @@ -63,6 +64,9 @@ function Snackbar(
event.preventDefault();
}

// Prevent focus loss by moving it to the list element.
listRef.current.focus();

onDismiss();
onRemove();
}
Expand Down
5 changes: 4 additions & 1 deletion packages/components/src/snackbar/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { omit, noop } from 'lodash';
* WordPress dependencies
*/
import { useReducedMotion } from '@wordpress/compose';
import { useRef } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -56,11 +57,12 @@ const SNACKBAR_REDUCE_MOTION_VARIANTS = {
* @return {Object} The rendered notices list.
*/
function SnackbarList( { notices, className, children, onRemove = noop } ) {
const listRef = useRef();
const isReducedMotion = useReducedMotion();
className = classnames( 'components-snackbar-list', className );
const removeNotice = ( notice ) => () => onRemove( notice.id );
return (
<div className={ className }>
<div className={ className } tabIndex={ -1 } ref={ listRef }>
{ children }
<AnimatePresence>
{ notices.map( ( notice ) => {
Expand All @@ -81,6 +83,7 @@ function SnackbarList( { notices, className, children, onRemove = noop } ) {
<Snackbar
{ ...omit( notice, [ 'content' ] ) }
onRemove={ removeNotice( notice ) }
listRef={ listRef }
>
{ notice.content }
</Snackbar>
Expand Down