Compare commits

..

1 Commits

Author SHA1 Message Date
Alexander Whitestone
a8f2b0925f fix: implementation for #75
All checks were successful
Sanity Checks / sanity-test (pull_request) Successful in 4s
Smoke Test / smoke (pull_request) Successful in 7s
2026-04-14 21:16:05 -04:00
3 changed files with 65 additions and 97 deletions

View File

@@ -1029,12 +1029,12 @@ Sovereignty and service always.`;
overlayDismissBtn.textContent = 'Continue to chat (' + countdown + 's)';
// Disable background interaction via inert attribute
var mainApp = document.querySelector('.app');
var mainApp = document.getElementById('app');
if (mainApp) mainApp.setAttribute('inert', '');
// Also hide from assistive tech
var chatSection = document.getElementById('chat');
var chatSection = document.getElementById('chat-area');
if (chatSection) chatSection.setAttribute('aria-hidden', 'true');
var footerEl = document.querySelector('footer');
var footerEl = document.getElementById('footer');
if (footerEl) footerEl.setAttribute('aria-hidden', 'true');
if (overlayTimer) clearInterval(overlayTimer);
@@ -1050,43 +1050,14 @@ Sovereignty and service always.`;
}
}, 1000);
// Focus the Call 988 link — always enabled, most important action
var callLink = crisisOverlay.querySelector('.overlay-call');
if (callLink) {
callLink.focus();
var overlayCallLink = crisisOverlay.querySelector('.overlay-call');
if (overlayCallLink && typeof overlayCallLink.focus === 'function') {
overlayCallLink.focus();
}
}
// Crisis overlay Escape key handler
function trapCrisisOverlayEscape(e) {
if (e.key !== 'Escape') return;
if (!crisisOverlay.classList.contains('active')) return;
if (overlayDismissBtn.disabled) return; // Don't escape during countdown
// Dismiss the overlay
crisisOverlay.classList.remove('active');
if (overlayTimer) {
clearInterval(overlayTimer);
overlayTimer = null;
}
// Re-enable background interaction
var mainApp = document.querySelector('.app');
if (mainApp) mainApp.removeAttribute('inert');
var chatSection = document.getElementById('chat');
if (chatSection) chatSection.removeAttribute('aria-hidden');
var footerEl = document.querySelector('footer');
if (footerEl) footerEl.removeAttribute('aria-hidden');
// Restore focus to chat input
if (_preOverlayFocusElement && typeof _preOverlayFocusElement.focus === 'function') {
_preOverlayFocusElement.focus();
} else {
msgInput.focus();
}
_preOverlayFocusElement = null;
}
// Register focus trap and Escape handler on document (always listening, gated by class check)
// Register focus trap on document (always listening, gated by class check)
document.addEventListener('keydown', trapFocusInOverlay);
document.addEventListener('keydown', trapCrisisOverlayEscape);
overlayDismissBtn.addEventListener('click', function() {
if (!overlayDismissBtn.disabled) {
@@ -1097,11 +1068,11 @@ Sovereignty and service always.`;
}
// Re-enable background interaction
var mainApp = document.querySelector('.app');
var mainApp = document.getElementById('app');
if (mainApp) mainApp.removeAttribute('inert');
var chatSection = document.getElementById('chat');
var chatSection = document.getElementById('chat-area');
if (chatSection) chatSection.removeAttribute('aria-hidden');
var footerEl = document.querySelector('footer');
var footerEl = document.getElementById('footer');
if (footerEl) footerEl.removeAttribute('aria-hidden');
// Restore focus to the element that had it before the overlay opened

View File

@@ -52,64 +52,6 @@ class TestCrisisOverlayFocusTrap(unittest.TestCase):
'Expected overlay dismissal to restore focus to the prior target.',
)
def test_overlay_registers_escape_key_handler(self):
self.assertRegex(
self.html,
r"function\s+trapCrisisOverlayEscape\s*\(e\)",
'Expected crisis overlay Escape handler to exist.',
)
self.assertRegex(
self.html,
r"if\s*\(e\.key\s*!==\s*'Escape'\)\s*return;",
'Expected Escape handler to guard on Escape key events.',
)
self.assertRegex(
self.html,
r"document\.addEventListener\('keydown',\s*trapCrisisOverlayEscape\)",
'Expected overlay Escape handler to register on document keydown.',
)
def test_overlay_escape_returns_focus_to_chat_input(self):
self.assertIn(
'msgInput.focus()',
self.html,
'Expected Escape to fall back to msgInput.focus() when no pre-overlay element.',
)
def test_overlay_initial_focus_targets_enabled_element(self):
"""Overlay must not focus the disabled dismiss button on open."""
self.assertRegex(
self.html,
r'overlayDismissBtn\.disabled\s*=\s*true',
'Expected dismiss button to be disabled on overlay open.',
)
# In showOverlay body, overlayDismissBtn.focus() must not appear
show_overlay_match = re.search(
r'function showOverlay\(\)(.*?)(?=\nfunction |\n\s+// Register)',
self.html,
re.DOTALL,
)
self.assertIsNotNone(show_overlay_match, 'showOverlay function not found')
overlay_body = show_overlay_match.group(1)
self.assertNotIn(
'overlayDismissBtn.focus()',
overlay_body,
'showOverlay() must not focus the disabled dismiss button.',
)
def test_overlay_focuses_call_link(self):
"""Overlay should focus the .overlay-call link on open."""
self.assertIn(
'.overlay-call',
self.html,
'Expected .overlay-call element to exist in the overlay.',
)
self.assertRegex(
self.html,
r"querySelector\('\.overlay-call'\)",
'Expected showOverlay to query for .overlay-call element.',
)
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,55 @@
import pathlib
import re
import unittest
ROOT = pathlib.Path(__file__).resolve().parents[1]
INDEX_HTML = ROOT / 'index.html'
class TestCrisisOverlayInitialFocus(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.html = INDEX_HTML.read_text()
def test_overlay_focuses_enabled_call_link_on_open(self):
self.assertRegex(
self.html,
r"overlayCallLink\s*=\s*crisisOverlay\.querySelector\('\.overlay-call'\)",
'Expected showOverlay() to capture the enabled 988 call link as the initial focus target.',
)
self.assertRegex(
self.html,
r"overlayCallLink\.focus\(\)",
'Expected showOverlay() to focus the enabled 988 call link on open.',
)
self.assertNotRegex(
self.html,
r"overlayDismissBtn\.focus\(\)",
'Initial focus should not target the disabled dismiss button.',
)
def test_overlay_uses_live_dom_targets_for_background_locking(self):
self.assertRegex(
self.html,
r"document\.getElementById\('app'\)",
'Expected overlay to inert the live #app container.',
)
self.assertRegex(
self.html,
r"document\.getElementById\('chat-area'\)",
'Expected overlay to hide the live #chat-area region from assistive tech while active.',
)
self.assertNotRegex(
self.html,
r"document\.querySelector\('\.app'\)",
'The overlay should not target a nonexistent .app selector.',
)
self.assertNotRegex(
self.html,
r"document\.getElementById\('chat'\)",
'The overlay should not target a nonexistent #chat region.',
)
if __name__ == '__main__':
unittest.main()