Skip to content

Toast notifications now announced properly by screen reader #3391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions client/modules/IDE/actions/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ export function saveProject(
dispatch(projectSaveSuccess());
if (!autosave) {
if (state.ide.justOpenedProject && state.preferences.autosave) {
dispatch(showToast(5500));
dispatch(showToast(12000));
dispatch(setToastText('Toast.SketchSaved'));
setTimeout(
() => dispatch(setToastText('Toast.AutosaveEnabled')),
1500
60000
);
dispatch(resetJustOpenedProject());
} else {
dispatch(showToast(1500));
dispatch(showToast(6000));
dispatch(setToastText('Toast.SketchSaved'));
}
}
Expand Down Expand Up @@ -224,15 +224,15 @@ export function saveProject(
dispatch(projectSaveSuccess());
if (!autosave) {
if (state.preferences.autosave) {
dispatch(showToast(5500));
dispatch(showToast(12000));
dispatch(setToastText('Toast.SketchSaved'));
setTimeout(
() => dispatch(setToastText('Toast.AutosaveEnabled')),
1500
6000
);
dispatch(resetJustOpenedProject());
} else {
dispatch(showToast(1500));
dispatch(showToast(6000));
dispatch(setToastText('Toast.SketchSaved'));
}
}
Expand Down
6 changes: 3 additions & 3 deletions client/modules/IDE/actions/toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export function hideToast() {
* Temporary fix until #2206 is merged.
* Supports legacy two-action syntax:
* dispatch(setToastText('Toast.SketchFailedSave'));
* dispatch(showToast(1500));
* dispatch(showToast(6000));
* And also supports proposed single-action syntax with message and optional timeout.
* dispatch(showToast('Toast.SketchFailedSave'));
* dispatch(showToast('Toast.SketchSaved', 5500));
* dispatch(showToast('Toast.SketchSaved', 6000));
*/
export function showToast(textOrTime, timeout = 1500) {
export function showToast(textOrTime, timeout = 6000) {
return (dispatch) => {
let time = timeout;
if (typeof textOrTime === 'string') {
Expand Down
43 changes: 32 additions & 11 deletions client/modules/IDE/components/Toast.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useTranslation } from 'react-i18next';
import { hideToast } from '../actions/toast';
Expand All @@ -9,19 +9,40 @@ export default function Toast() {
const { text, isVisible } = useSelector((state) => state.toast);
const dispatch = useDispatch();
const { t } = useTranslation();

useEffect(() => {
if (isVisible) {
const liveRegion = document.getElementById('toast-live-region');
if (liveRegion) {
liveRegion.textContent = t(text); // Update live region globally
}
}
}, [isVisible, text, t]);

if (!isVisible) {
return null;
}

return (
<section className="toast" role="status" aria-live="polite">
<p>{t(text)}</p>
<button
className="toast__close"
onClick={() => dispatch(hideToast())}
aria-label="Close Alert"
>
<ExitIcon focusable="false" aria-hidden="true" />
</button>
</section>
<>
{/* Global ARIA Live Region */}
<div
id="toast-live-region"
aria-live="polite"
style={{ position: 'absolute', left: '-9999px' }}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want to preserve the original setup here, such as referencing toast__close rather than adding the styling here.

/>

{/* Toast UI */}
<section className="toast" role="status">
<p>{t(text)}</p>
<button
className="toast__close"
onClick={() => dispatch(hideToast())}
aria-label="Close Alert"
>
<ExitIcon focusable="false" aria-hidden="true" />
</button>
</section>
</>
);
}