Files
breadcrumb-the-shire/web/js/components/app-flash-auto-dismiss.js
fs d9f07dcd63 fix: web/ quick wins — CSS cleanup, ARIA tabs, and file header comments
CSS:
- Remove duplicate li::before block in app-search.css
- Fix typo: search-reuslt → search-result in app-search.css
- Remove commented-out CSS rules in app-flash.css
- Add descriptive header comment to all 27 CSS component files

JS:
- Complete WAI-ARIA Tabs pattern: generate IDs, add aria-controls on
  tab buttons and aria-labelledby on tab panels (app-tabs.js)
- Add JSDoc header comments to ~33 JS files (core + components)
- Add explanatory block comment to app-boot.js (why classic IIFE)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 22:21:37 +01:00

57 lines
1.4 KiB
JavaScript

/**
* Auto-dismisses flash notices after their data-flash-timeout expires.
*/
export function initFlashAutoDismiss(options = {}) {
const {
selector = '.flash-stack .notice[data-flash-timeout]',
defaultTimeout = 0
} = options;
const notices = document.querySelectorAll(selector);
if (!notices.length) {
return;
}
const postForm = async (form) => {
const action = form.getAttribute('action');
if (!action) {return null;}
const body = new URLSearchParams(new FormData(form));
return fetch(action, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-Requested-With': 'fetch'
},
body
});
};
notices.forEach((notice) => {
const timeout = Number.parseInt(
notice.dataset.flashTimeout || `${defaultTimeout}`,
10
);
if (!timeout || timeout <= 0) {
return;
}
notice.style.setProperty('--flash-timeout', `${timeout}ms`);
notice.classList.add('flash-timed');
window.setTimeout(async () => {
const form = notice.querySelector('form');
if (form) {
const response = await postForm(form);
if (!response || !response.ok) {
return;
}
}
notice.remove();
}, timeout);
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => initFlashAutoDismiss());
} else {
initFlashAutoDismiss();
}