Compare commits

..

1 Commits

Author SHA1 Message Date
b4c4919361 fix: overlay initial focus targets enabled element (closes #90, refs #69)
All checks were successful
Sanity Checks / sanity-test (pull_request) Successful in 16s
Smoke Test / smoke (pull_request) Successful in 19s
2026-04-14 23:25:28 -04:00
3 changed files with 96 additions and 93 deletions

View File

@@ -475,26 +475,6 @@ html, body {
margin-bottom: 24px;
}
.modal-status {
min-height: 22px;
margin: 0 0 16px;
font-size: 0.9rem;
line-height: 1.45;
color: #8b949e;
}
.modal-status.is-visible {
display: block;
}
.modal-status.success {
color: #3fb950;
}
.modal-status.error {
color: #ff7b72;
}
.form-group {
margin-bottom: 16px;
}
@@ -757,7 +737,6 @@ html, body {
<textarea id="sp-environment" placeholder="e.g., Giving my car keys to a friend, locking away meds..."></textarea>
</div>
</div>
<div id="safety-plan-status" class="modal-status" role="status" aria-live="polite" aria-atomic="true"></div>
<div class="modal-footer">
<button class="btn btn-secondary" id="cancel-safety-plan">Cancel</button>
<button class="btn btn-primary" id="save-safety-plan">Save Plan</button>
@@ -839,7 +818,6 @@ Sovereignty and service always.`;
var closeSafetyPlan = document.getElementById('close-safety-plan');
var cancelSafetyPlan = document.getElementById('cancel-safety-plan');
var saveSafetyPlan = document.getElementById('save-safety-plan');
var safetyPlanStatus = document.getElementById('safety-plan-status');
var clearChatBtn = document.getElementById('clear-chat-btn');
// ===== STATE =====
@@ -1072,11 +1050,43 @@ Sovereignty and service always.`;
}
}, 1000);
overlayDismissBtn.focus();
// Focus the Call 988 link — always enabled, most important action
var callLink = crisisOverlay.querySelector('.overlay-call');
if (callLink) {
callLink.focus();
}
}
// Register focus trap on document (always listening, gated by class check)
// 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)
document.addEventListener('keydown', trapFocusInOverlay);
document.addEventListener('keydown', trapCrisisOverlayEscape);
overlayDismissBtn.addEventListener('click', function() {
if (!overlayDismissBtn.disabled) {
@@ -1205,24 +1215,12 @@ Sovereignty and service always.`;
} catch (e) {}
}
function setSafetyPlanStatus(message, type) {
safetyPlanStatus.textContent = message;
safetyPlanStatus.className = 'modal-status is-visible ' + (type || '');
}
function clearSafetyPlanStatus() {
safetyPlanStatus.textContent = '';
safetyPlanStatus.className = 'modal-status';
}
closeSafetyPlan.addEventListener('click', function() {
clearSafetyPlanStatus();
safetyPlanModal.classList.remove('active');
_restoreSafetyPlanFocus();
});
cancelSafetyPlan.addEventListener('click', function() {
clearSafetyPlanStatus();
safetyPlanModal.classList.remove('active');
_restoreSafetyPlanFocus();
});
@@ -1237,9 +1235,11 @@ Sovereignty and service always.`;
};
try {
localStorage.setItem('timmy_safety_plan', JSON.stringify(plan));
setSafetyPlanStatus('Safety plan saved locally.', 'success');
safetyPlanModal.classList.remove('active');
_restoreSafetyPlanFocus();
alert('Safety plan saved locally.');
} catch (e) {
setSafetyPlanStatus('Error saving plan.', 'error');
alert('Error saving plan.');
}
});
@@ -1317,7 +1317,6 @@ Sovereignty and service always.`;
// Wire open buttons to activate focus trap
safetyPlanBtn.addEventListener('click', function() {
clearSafetyPlanStatus();
loadSafetyPlan();
safetyPlanModal.classList.add('active');
_activateSafetyPlanFocusTrap(safetyPlanBtn);
@@ -1326,8 +1325,6 @@ Sovereignty and service always.`;
// Crisis panel safety plan button (if crisis panel is visible)
if (crisisSafetyPlanBtn) {
crisisSafetyPlanBtn.addEventListener('click', function() {
clearSafetyPlanStatus();
clearSafetyPlanStatus();
loadSafetyPlan();
safetyPlanModal.classList.add('active');
_activateSafetyPlanFocusTrap(crisisSafetyPlanBtn);

View File

@@ -52,6 +52,64 @@ 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

@@ -1,52 +0,0 @@
import pathlib
import re
import unittest
ROOT = pathlib.Path(__file__).resolve().parents[1]
INDEX_HTML = ROOT / 'index.html'
class TestSafetyPlanSaveFeedback(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.html = INDEX_HTML.read_text()
def test_modal_has_inline_status_live_region(self):
self.assertRegex(
self.html,
r'<div[^>]+id="safety-plan-status"[^>]+role="status"[^>]+aria-live="polite"[^>]*>',
'Expected an inline polite live region for safety plan save feedback.',
)
def test_save_feedback_does_not_use_blocking_alerts(self):
self.assertNotIn(
"alert('Safety plan saved locally.')",
self.html,
'Expected success feedback to stop using blocking alert().',
)
self.assertNotIn(
"alert('Error saving plan.')",
self.html,
'Expected error feedback to stop using blocking alert().',
)
def test_save_logic_updates_inline_status_for_success_and_error(self):
self.assertRegex(
self.html,
r'function\s+setSafetyPlanStatus\s*\(',
'Expected a helper to update inline save feedback.',
)
self.assertRegex(
self.html,
r"setSafetyPlanStatus\('Safety plan saved locally\.'\s*,\s*'success'\)",
'Expected success path to update inline status.',
)
self.assertRegex(
self.html,
r"setSafetyPlanStatus\('Error saving plan\.'\s*,\s*'error'\)",
'Expected error path to update inline status.',
)
if __name__ == '__main__':
unittest.main()