2024-04-06 06:30:58 +08:00
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
import { createPortal } from 'react-dom';
|
|
|
|
import { shiftAlert } from './service';
|
2024-08-10 01:58:44 +08:00
|
|
|
import { ScreenReaderAlert as ScreenReaderAlertResponse } from './queue';
|
2024-04-06 06:30:58 +08:00
|
|
|
|
|
|
|
const ARIA_ALERT_EXT_TIMEOUT = 15000;
|
|
|
|
|
|
|
|
interface Props {
|
2024-08-10 01:58:44 +08:00
|
|
|
olderAlert: ScreenReaderAlertResponse | undefined;
|
2024-04-06 06:30:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const ScreenReaderAlert: React.FC<Props> = ({ olderAlert }) => {
|
|
|
|
useEffect(() => {
|
|
|
|
if (olderAlert) setTimeout(() => shiftAlert(), ARIA_ALERT_EXT_TIMEOUT);
|
|
|
|
}, [olderAlert?.id]);
|
|
|
|
|
2024-04-12 01:34:41 +08:00
|
|
|
const ariaAlertsElement = document.getElementById('aria-polite-alert');
|
|
|
|
|
|
|
|
return (olderAlert && olderAlert.text && ariaAlertsElement !== null)
|
2024-04-06 06:30:58 +08:00
|
|
|
? createPortal(olderAlert.text, document.getElementById('aria-polite-alert') as HTMLElement)
|
|
|
|
: null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ScreenReaderAlert;
|