Skip to content

Commit b9bd855

Browse files
LFDanLudevongovett
andauthored
fix: make Toasts announce when rendered in NVDA (#7859)
* Fix so that Toasts are properly announced by NVDA when they render * get rid of direct toast dep in favor of monopackage * Update packages/@react-aria/toast/src/useToast.ts Co-authored-by: Devon Govett <devongovett@gmail.com> --------- Co-authored-by: Devon Govett <devongovett@gmail.com>
1 parent 5f30d47 commit b9bd855

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

packages/@react-aria/toast/src/useToast.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {AriaLabelingProps, DOMAttributes, FocusableElement, RefObject} from '@re
1515
// @ts-ignore
1616
import intlMessages from '../intl/*.json';
1717
import {QueuedToast, ToastState} from '@react-stately/toast';
18-
import {useEffect} from 'react';
18+
import {useEffect, useState} from 'react';
1919
import {useId, useSlotId} from '@react-aria/utils';
2020
import {useLocalizedStringFormatter} from '@react-aria/i18n';
2121

@@ -64,6 +64,14 @@ export function useToast<T>(props: AriaToastProps<T>, state: ToastState<T>, ref:
6464
let descriptionId = useSlotId();
6565
let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/toast');
6666

67+
// This is required for NVDA announcements, without it NVDA will NOT announce the toast when it appears.
68+
// Originally was tied to animationStart/End via https://github.com/adobe/react-spectrum/pull/6223/commits/e22e319df64958e822ab7cd9685e96818cae9ba5
69+
// but toasts don't always have animations.
70+
let [isVisible, setIsVisible] = useState(false);
71+
useEffect(() => {
72+
setIsVisible(true);
73+
}, []);
74+
6775
return {
6876
toastProps: {
6977
role: 'alertdialog',
@@ -76,7 +84,8 @@ export function useToast<T>(props: AriaToastProps<T>, state: ToastState<T>, ref:
7684
},
7785
contentProps: {
7886
role: 'alert',
79-
'aria-atomic': 'true'
87+
'aria-atomic': 'true',
88+
'aria-hidden': isVisible ? undefined : 'true'
8089
},
8190
titleProps: {
8291
id: titleId

packages/react-aria-components/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"@react-aria/interactions": "^3.23.0",
4949
"@react-aria/live-announcer": "^3.4.1",
5050
"@react-aria/menu": "^3.17.0",
51-
"@react-aria/toast": "3.0.0-beta.19",
5251
"@react-aria/toolbar": "3.0.0-beta.12",
5352
"@react-aria/utils": "^3.27.0",
5453
"@react-aria/virtualizer": "^4.1.1",

packages/react-aria-components/src/Toast.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import {AriaToastProps, AriaToastRegionProps, useToast, useToastRegion} from '@react-aria/toast';
13+
import {AriaToastProps, AriaToastRegionProps, mergeProps, useFocusRing, useToast, useToastRegion} from 'react-aria';
1414
import {ButtonContext} from './Button';
1515
import {ContextValue, DEFAULT_SLOT, Provider, RenderProps, StyleRenderProps, useContextProps, useRenderProps} from './utils';
1616
import {createPortal} from 'react-dom';
1717
import {forwardRefType} from '@react-types/shared';
18-
import {mergeProps, useFocusRing} from 'react-aria';
1918
import {QueuedToast, ToastQueue, ToastState, useToastQueue} from '@react-stately/toast';
2019
import React, {createContext, ForwardedRef, forwardRef, HTMLAttributes, JSX, ReactElement, useContext} from 'react';
2120
import {TextContext} from './Text';

yarn.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29030,7 +29030,6 @@ __metadata:
2903029030
"@react-aria/interactions": "npm:^3.23.0"
2903129031
"@react-aria/live-announcer": "npm:^3.4.1"
2903229032
"@react-aria/menu": "npm:^3.17.0"
29033-
"@react-aria/toast": "npm:3.0.0-beta.19"
2903429033
"@react-aria/toolbar": "npm:3.0.0-beta.12"
2903529034
"@react-aria/utils": "npm:^3.27.0"
2903629035
"@react-aria/virtualizer": "npm:^4.1.1"

0 commit comments

Comments
 (0)