Skip to content

Commit 2bd35c0

Browse files
author
Hector Arce De Las Heras
committed
Set focus to first page element on manual Snackbar close
This commit ensures that when the Snackbar component is closed manually, the focus is automatically set to the first element of the page. This improves accessibility by guiding the user's focus in a logical order, enhancing the user experience especially for keyboard and screen reader users.
1 parent 9bc6156 commit 2bd35c0

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/components/snackbar/snackbarStandAlone.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ const SnackbarStandAloneComponent = (
8484
clickOverlayClose={false}
8585
component={PopoverComponentType.DIV}
8686
dataTestId={`${props.dataTestId}Popover`}
87-
focusScreenFirstDescendantAfterClose={true}
87+
focusLastElementFocusedAfterClose={false}
88+
focusScreenFirstDescendantAfterClose={false}
8889
open={props.open}
8990
pressEscapeClose={false}
9091
variant={props.styles?.popoverVariants?.[align]}

src/components/snackbar/snackbarUnControlled.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as React from 'react';
22

3+
import { focusFirstDescendant } from '@/utils';
4+
35
import { SnackbarControlled } from './snackbarControlled';
46
import { ISnackbarUnControlled } from './types';
57

@@ -53,6 +55,8 @@ const SnackbarUnControlledComponent = <V extends string | unknown>(
5355
const handleCloseButton: (_open: boolean) => React.MouseEventHandler<HTMLButtonElement> =
5456
_open => event => {
5557
props.onOpenClose?.(_open, event);
58+
// Only after the user closes the snackbar manually, the focus will be set to the first focusable element of the page
59+
focusFirstDescendant(document.body);
5660
};
5761

5862
const handleMouseEnter = (event: React.MouseEvent<HTMLDivElement>) => {

0 commit comments

Comments
 (0)