Compare commits

..

1 Commits

Author SHA1 Message Date
Alexander Whitestone
1f744e846d fix: replace blocking alert() with inline toast for safety plan save (#73)
All checks were successful
Sanity Checks / sanity-test (pull_request) Successful in 6s
Smoke Test / smoke (pull_request) Successful in 16s
Safety plan save used browser alert() for success and error feedback.
These are blocking, jarring, and inaccessible.

Changes:
- Added toast notification CSS (.toast-notification with .visible,
  .success, .error states)
- Added toast HTML element with role=status, aria-live=polite,
  aria-atomic=true for screen reader announcements
- Added showToast(message, type) function with 3s auto-dismiss
- Replaced alert('Safety plan saved locally.') with
  showToast('Safety plan saved.', 'success')
- Replaced alert('Error saving plan.') with
  showToast('Error saving plan. Please try again.', 'error')

Toast appears at bottom center of screen, slides up with CSS
transition, auto-dismisses after 3 seconds.

Tests: tests/test_safety_plan_toast.py (8 tests, all pass)

Closes #73
2026-04-14 22:05:26 -04:00
3 changed files with 130 additions and 67 deletions

View File

@@ -531,6 +531,36 @@ html, body {
.btn-secondary:hover { color: #e6edf3; border-color: #8b949e; }
/* Toast notification (replaces blocking alert()) */
.toast-notification {
position: fixed;
bottom: 24px;
left: 50%;
transform: translateX(-50%) translateY(100px);
padding: 12px 24px;
border-radius: 8px;
font-size: 0.9rem;
font-weight: 500;
z-index: 10001;
opacity: 0;
transition: transform 0.3s ease, opacity 0.3s ease;
pointer-events: none;
max-width: 90vw;
text-align: center;
}
.toast-notification.visible {
transform: translateX(-50%) translateY(0);
opacity: 1;
}
.toast-notification.success {
background: #238636;
color: #fff;
}
.toast-notification.error {
background: #da3633;
color: #fff;
}
/* ===== FOOTER ===== */
#footer {
flex-shrink: 0;
@@ -744,6 +774,9 @@ html, body {
</div>
</div>
<!-- Toast notification (accessible, non-blocking feedback) -->
<div id="toast-notification" class="toast-notification" role="status" aria-live="polite" aria-atomic="true"></div>
<script>
(function() {
'use strict';
@@ -1029,12 +1062,12 @@ Sovereignty and service always.`;
overlayDismissBtn.textContent = 'Continue to chat (' + countdown + 's)';
// Disable background interaction via inert attribute
var mainApp = document.getElementById('app');
var mainApp = document.querySelector('.app');
if (mainApp) mainApp.setAttribute('inert', '');
// Also hide from assistive tech
var chatSection = document.getElementById('chat-area');
var chatSection = document.getElementById('chat');
if (chatSection) chatSection.setAttribute('aria-hidden', 'true');
var footerEl = document.getElementById('footer');
var footerEl = document.querySelector('footer');
if (footerEl) footerEl.setAttribute('aria-hidden', 'true');
if (overlayTimer) clearInterval(overlayTimer);
@@ -1050,10 +1083,7 @@ Sovereignty and service always.`;
}
}, 1000);
var overlayCallLink = crisisOverlay.querySelector('.overlay-call');
if (overlayCallLink && typeof overlayCallLink.focus === 'function') {
overlayCallLink.focus();
}
overlayDismissBtn.focus();
}
// Register focus trap on document (always listening, gated by class check)
@@ -1068,11 +1098,11 @@ Sovereignty and service always.`;
}
// Re-enable background interaction
var mainApp = document.getElementById('app');
var mainApp = document.querySelector('.app');
if (mainApp) mainApp.removeAttribute('inert');
var chatSection = document.getElementById('chat-area');
var chatSection = document.getElementById('chat');
if (chatSection) chatSection.removeAttribute('aria-hidden');
var footerEl = document.getElementById('footer');
var footerEl = document.querySelector('footer');
if (footerEl) footerEl.removeAttribute('aria-hidden');
// Restore focus to the element that had it before the overlay opened
@@ -1186,6 +1216,24 @@ Sovereignty and service always.`;
} catch (e) {}
}
// ===== TOAST NOTIFICATION (replaces blocking alert()) =====
var _toastEl = document.getElementById('toast-notification');
var _toastTimer = null;
function showToast(message, type) {
type = type || 'success';
_toastEl.textContent = message;
_toastEl.className = 'toast-notification ' + type;
// Force reflow before adding visible class
void _toastEl.offsetHeight;
_toastEl.classList.add('visible');
if (_toastTimer) clearTimeout(_toastTimer);
_toastTimer = setTimeout(function() {
_toastEl.classList.remove('visible');
}, 3000);
}
closeSafetyPlan.addEventListener('click', function() {
safetyPlanModal.classList.remove('active');
_restoreSafetyPlanFocus();
@@ -1208,9 +1256,9 @@ Sovereignty and service always.`;
localStorage.setItem('timmy_safety_plan', JSON.stringify(plan));
safetyPlanModal.classList.remove('active');
_restoreSafetyPlanFocus();
alert('Safety plan saved locally.');
showToast('Safety plan saved.', 'success');
} catch (e) {
alert('Error saving plan.');
showToast('Error saving plan. Please try again.', 'error');
}
});

View File

@@ -1,55 +0,0 @@
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()

View File

@@ -0,0 +1,70 @@
import pathlib
import re
import unittest
ROOT = pathlib.Path(__file__).resolve().parents[1]
INDEX_HTML = ROOT / 'index.html'
class TestSafetyPlanToast(unittest.TestCase):
"""Verify safety plan save feedback uses non-blocking toast instead of alert()."""
@classmethod
def setUpClass(cls):
cls.html = INDEX_HTML.read_text()
def test_no_alert_calls_in_safety_plan_save(self):
"""Safety plan save should not use blocking alert() dialogs."""
# Find the save handler section
save_section = re.search(
r'saveSafetyPlan\.addEventListener.*?\}\);',
self.html, re.DOTALL
)
self.assertIsNotNone(save_section, 'Expected safety plan save handler to exist.')
section = save_section.group(0)
# Should not contain alert( calls
self.assertNotIn('alert(', section,
'Safety plan save handler should not use alert() — use showToast() instead.')
def test_toast_notification_element_exists(self):
"""Toast notification element should exist in the DOM."""
self.assertIn('id="toast-notification"', self.html,
'Expected toast-notification element in HTML.')
def test_toast_has_accessibility_attributes(self):
"""Toast should have aria-live for screen reader announcements."""
self.assertIn('aria-live="polite"', self.html,
'Toast should have aria-live="polite" for accessibility.')
self.assertIn('aria-atomic="true"', self.html,
'Toast should have aria-atomic="true" for complete announcement.')
def test_toast_css_exists(self):
"""Toast CSS styles should be defined."""
self.assertIn('.toast-notification', self.html,
'Expected .toast-notification CSS class.')
self.assertIn('.toast-notification.visible', self.html,
'Expected .toast-notification.visible CSS class.')
self.assertIn('.toast-notification.success', self.html,
'Expected .toast-notification.success CSS class.')
self.assertIn('.toast-notification.error', self.html,
'Expected .toast-notification.error CSS class.')
def test_showToast_function_exists(self):
"""showToast function should be defined."""
self.assertRegex(self.html, r'function\s+showToast\s*\(',
'Expected showToast function to be defined.')
def test_success_message_uses_toast(self):
"""Success feedback should use showToast with success type."""
self.assertIn("showToast('Safety plan saved.", self.html,
'Expected success message to use showToast.')
def test_error_message_uses_toast(self):
"""Error feedback should use showToast with error type."""
self.assertIn("showToast('Error saving plan.", self.html,
'Expected error message to use showToast.')
def test_toast_auto_dismisses(self):
"""Toast should auto-dismiss after timeout."""
self.assertRegex(self.html, r'setTimeout\s*\(\s*function',
'Expected setTimeout for toast auto-dismiss.')