Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(a11y): use persistent live regions for toast notifications (#3666)

LearningCircuite3f3770c3d27
diff --git a/src/local_deep_research/web/static/js/services/ui.js b/src/local_deep_research/web/static/js/services/ui.js
index 4e118a095d..fd59b2c6c6 100644
--- a/src/local_deep_research/web/static/js/services/ui.js
+++ b/src/local_deep_research/web/static/js/services/ui.js
@@ -154,6 +154,55 @@ function getCSSVariable(varName, fallback) {
* @param {string} type - Message type: 'success', 'error', 'info', 'warning'
* @param {number} duration - How long to show the message in ms
*/
+/**
+ * Get or lazily create the two persistent notification banners.
+ *
+ * Live regions need to exist in the DOM before their text content
+ * changes, otherwise NVDA/VoiceOver will not reliably announce the
+ * message. The previous implementation removed and re-created the
+ * banner on every `showMessage` call, which made announcements
+ * inconsistent across browsers and screen readers.
+ *
+ * Two regions are used so that errors/warnings get assertive
+ * announcement (interrupts current speech) while success/info get
+ * polite (waits for the user to be idle).
+ */
+function _ensureNotificationBanners() {
+ let polite = document.getElementById('notification-banner-polite');
+ let assertive = document.getElementById(
+ 'notification-banner-assertive',
+ );
+
+ const buildBanner = (id, role, liveness) => {
+ const el = document.createElement('div');
+ el.id = id;
+ el.className = 'ldr-notification-banner';
+ el.setAttribute('role', role);
+ el.setAttribute('aria-live', liveness);
+ el.setAttribute('aria-atomic', 'true');
+ // Stable internal structure: <i class="fas …"></i><span></span>
+ // The span's textContent is what gets announced; mutating it
+ // (rather than the element itself) is what makes the live
+ // region work reliably.
+ el.appendChild(document.createElement('i'));
+ el.appendChild(document.createElement('span'));
+ document.body.appendChild(el);
+ return el;
+ };
+
+ if (!polite) {
+ polite = buildBanner('notification-banner-polite', 'status', 'polite');
+ }
+ if (!assertive) {
+ assertive = buildBanner(
+ 'notification-banner-assertive',
+ 'alert',
+ 'assertive',
+ );
+ }
+ return { polite, assertive };
+}
+
function showMessage(message, type = 'success', duration = UI_CONFIG.NOTIFICATION_DURATION_MS) {
// Get theme colors from CSS variables with fallbacks
let accentColor, iconClass;
@@ -177,44 +226,36 @@ function showMessage(message, type = 'success', duration = UI_CONFIG.NOTIFICATIO
break;
}
- // Create or get notification banner
- let banner = document.getElementById('notification-banner');
- if (banner) {
- banner.remove();
- }
- banner = document.createElement('div');
- banner.id = 'notification-banner';
- banner.className = 'ldr-notification-banner';
-
- // Set ARIA attributes BEFORE content injection so screen readers announce it
- if (type === 'error' || type === 'warning') {
- banner.setAttribute('role', 'alert');
- } else {
- banner.setAttribute('role', 'status');
- }
- banner.setAttribute('aria-atomic', 'true');
+ const { polite, assertive } = _ensureNotificationBanners();
+ const banner =
+ type === 'error' || type === 'warning' ? assertive : polite;
+ const otherBanner = banner === polite ? assertive : polite;
- document.body.appendChild(banner);
+ // Hide the other banner so it doesn't overlap visually.
+ otherBanner.style.transform = 'translateY(-100%)';
// Apply dynamic accent color for border (static styles handled by CSS class)
banner.style.borderBottom = `3px solid ${accentColor}`;
- // Escape message before including in HTML template
- const escapedMessage = (window.escapeHtml || escapeHtmlFallback)(message);
-
- // Set content with colored icon
- safeSetHTML(banner, `
- <i class="fas ${iconClass}" style="color: ${accentColor}; font-size: 1.1rem;"></i>
- <span>${escapedMessage}</span>
- `, 'ui');
+ // Mutate the existing live region's children rather than rebuilding
+ // the host element. Updating textContent on the inner span is what
+ // triggers the screen reader announcement.
+ const icon = banner.firstElementChild;
+ const textSpan = banner.lastElementChild;
+ icon.className = `fas ${iconClass}`;
+ icon.style.color = accentColor;
+ icon.style.fontSize = '1.1rem';
+ textSpan.textContent = message;
// Slide in
requestAnimationFrame(() => {
banner.style.transform = 'translateY(0)';
});
- // Slide out after duration
- setTimeout(() => {
+ // Slide out after duration; clear any previous timer so a rapid
+ // second call doesn't slide this one out early.
+ clearTimeout(banner._hideTimer);
+ banner._hideTimer = setTimeout(() => {
banner.style.transform = 'translateY(-100%)';
}, duration);
}
diff --git a/tests/js/services/ui.test.js b/tests/js/services/ui.test.js
index 0012b06568..8e3d315e40 100644
--- a/tests/js/services/ui.test.js
+++ b/tests/js/services/ui.test.js
@@ -221,41 +221,95 @@ describe('ui service', () => {
describe('showMessage', () => {
afterEach(() => {
- const banner = document.getElementById('notification-banner');
- if (banner) banner.remove();
+ // Persistent banners stay in the DOM by design; clean up
+ // between tests so each test starts fresh.
+ document
+ .getElementById('notification-banner-polite')
+ ?.remove();
+ document
+ .getElementById('notification-banner-assertive')
+ ?.remove();
});
- it('creates a notification banner', () => {
+ it('shows the polite banner for success messages', () => {
ui.showMessage('Saved!', 'success');
- const banner = document.getElementById('notification-banner');
- expect(banner).not.toBeNull();
- expect(banner.textContent).toContain('Saved!');
+ const polite = document.getElementById(
+ 'notification-banner-polite',
+ );
+ expect(polite).not.toBeNull();
+ expect(polite.textContent).toContain('Saved!');
+ expect(polite.getAttribute('role')).toBe('status');
+ expect(polite.getAttribute('aria-live')).toBe('polite');
});
- it('sets role="alert" for error/warning types', () => {
+ it('shows the assertive banner for error messages', () => {
ui.showMessage('Error occurred', 'error');
- const banner = document.getElementById('notification-banner');
- expect(banner.getAttribute('role')).toBe('alert');
+ const assertive = document.getElementById(
+ 'notification-banner-assertive',
+ );
+ expect(assertive).not.toBeNull();
+ expect(assertive.textContent).toContain('Error occurred');
+ expect(assertive.getAttribute('role')).toBe('alert');
+ expect(assertive.getAttribute('aria-live')).toBe('assertive');
});
- it('sets role="status" for success/info types', () => {
- ui.showMessage('Done', 'success');
- const banner = document.getElementById('notification-banner');
- expect(banner.getAttribute('role')).toBe('status');
+ it('uses the assertive banner for warnings', () => {
+ ui.showMessage('Heads up', 'warning');
+ const assertive = document.getElementById(
+ 'notification-banner-assertive',
+ );
+ expect(assertive.textContent).toContain('Heads up');
});
- it('escapes HTML in message', () => {
+ it('uses the polite banner for info messages', () => {
+ ui.showMessage('FYI', 'info');
+ const polite = document.getElementById(
+ 'notification-banner-polite',
+ );
+ expect(polite.textContent).toContain('FYI');
+ });
+
+ it('escapes HTML by using textContent', () => {
ui.showMessage('<img src=x onerror=alert(1)>', 'info');
- const banner = document.getElementById('notification-banner');
- expect(banner.innerHTML).not.toContain('<img');
+ const polite = document.getElementById(
+ 'notification-banner-polite',
+ );
+ // textContent is set, not innerHTML, so the markup is text.
+ expect(polite.textContent).toContain(
+ '<img src=x onerror=alert(1)>',
+ );
+ expect(polite.querySelector('img')).toBeNull();
});
- it('replaces previous banner', () => {
+ it('reuses the same banner element across calls', () => {
ui.showMessage('First', 'success');
+ const firstNode = document.getElementById(
+ 'notification-banner-polite',
+ );
ui.showMessage('Second', 'info');
- const banners = document.querySelectorAll('#notification-banner');
- expect(banners.length).toBe(1);
- expect(banners[0].textContent).toContain('Second');
+ const secondNode = document.getElementById(
+ 'notification-banner-polite',
+ );
+ // Same DOM node — live regions must persist for the
+ // screen reader to announce subsequent updates.
+ expect(secondNode).toBe(firstNode);
+ expect(secondNode.textContent).toContain('Second');
+ expect(secondNode.textContent).not.toContain('First');
+ });
+
+ it('switches between banners when type changes', () => {
+ ui.showMessage('Saved', 'success');
+ ui.showMessage('Boom', 'error');
+ const polite = document.getElementById(
+ 'notification-banner-polite',
+ );
+ const assertive = document.getElementById(
+ 'notification-banner-assertive',
+ );
+ expect(assertive.textContent).toContain('Boom');
+ // Polite is hidden via transform but its prior text
+ // is still in the DOM; the visible banner is assertive.
+ expect(polite.style.transform).toContain('-100%');
});
});
});