| 43 | * brief, temporary notifications of actions, errors, or other events in an application. |
| 44 | */ |
| 45 | export function useToast<T>( |
| 46 | props: AriaToastProps<T>, |
| 47 | state: ToastState<T>, |
| 48 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 49 | ref: RefObject<FocusableElement | null> |
| 50 | ): ToastAria { |
| 51 | let {key, timer, timeout} = props.toast; |
| 52 | |
| 53 | useEffect(() => { |
| 54 | if (timer == null || timeout == null) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | timer.reset(timeout); |
| 59 | return () => { |
| 60 | timer.pause(); |
| 61 | }; |
| 62 | }, [timer, timeout]); |
| 63 | |
| 64 | let titleId = useId(); |
| 65 | let descriptionId = useSlotId(); |
| 66 | let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/toast'); |
| 67 | |
| 68 | // This is required for NVDA announcements, without it NVDA will NOT announce the toast when it appears. |
| 69 | // Originally was tied to animationStart/End via https://github.com/adobe/react-spectrum/pull/6223/commits/e22e319df64958e822ab7cd9685e96818cae9ba5 |
| 70 | // but toasts don't always have animations. |
| 71 | let [isVisible, setIsVisible] = useState(false); |
| 72 | useLayoutEffect(() => { |
| 73 | setIsVisible(true); |
| 74 | }, []); |
| 75 | |
| 76 | let toastProps = filterDOMProps(props, {labelable: true}); |
| 77 | |
| 78 | return { |
| 79 | toastProps: { |
| 80 | ...toastProps, |
| 81 | role: 'alertdialog', |
| 82 | 'aria-modal': 'false', |
| 83 | 'aria-labelledby': props['aria-labelledby'] || titleId, |
| 84 | 'aria-describedby': props['aria-describedby'] || descriptionId, |
| 85 | tabIndex: 0 |
| 86 | }, |
| 87 | contentProps: { |
| 88 | role: 'alert', |
| 89 | 'aria-atomic': 'true', |
| 90 | 'aria-hidden': isVisible ? undefined : 'true' |
| 91 | }, |
| 92 | titleProps: { |
| 93 | id: titleId |
| 94 | }, |
| 95 | descriptionProps: { |
| 96 | id: descriptionId |
| 97 | }, |
| 98 | closeButtonProps: { |
| 99 | 'aria-label': stringFormatter.format('close'), |
| 100 | onPress: () => state.close(key) |
| 101 | } |
| 102 | }; |