fix(screen-reader): add safeguards for adding alerts

Adds checks for the alert's text and DOM element before adding screen
reader alerts.
This commit is contained in:
Arthurk12 2024-04-11 14:34:41 -03:00 committed by prlanzarin
parent fcbfcb1bbc
commit 6cb0e914ab

View File

@ -9,7 +9,10 @@ const ScreenReaderAlert = ({ olderAlert }) => {
if (olderAlert) setTimeout(() => removeAlert(olderAlert.id), ARIA_ALERT_EXT_TIMEOUT);
}, [olderAlert?.id]);
return olderAlert ? createPortal(olderAlert.text, document.getElementById('aria-polite-alert')) : null;
const ariaAlertsElement = document.getElementById('aria-polite-alert');
const shouldAddAlert = olderAlert && olderAlert.text && ariaAlertsElement !== null;
return shouldAddAlert ? createPortal(olderAlert.text, ariaAlertsElement) : null;
};
export default ScreenReaderAlert;