components/banner/banner.ts
29 lines and 6 functions in DrDustinEdwards/capsomer, at commit d3966cc on 2026-10-24. This file on GitHub.
Functions
| Function | Lines | Complexity | Covered | CRAP | Mutation |
|---|---|---|---|---|---|
| dismissBanner | 5 to 13 | 5 | No data: no coverage report in this map | No data: no coverage report in this map | No data: mutation testing has not run on this repo yet |
| enhance | 17 to 31 | 2 | No data: no coverage report in this map | No data: no coverage report in this map | No data: mutation testing has not run on this repo yet |
| onClick | 21 to 23 | 2 | No data: no coverage report in this map | No data: no coverage report in this map | No data: mutation testing has not run on this repo yet |
| <anonymous:25> | 25 to 28 | 1 | No data: no coverage report in this map | No data: no coverage report in this map | No data: mutation testing has not run on this repo yet |
| <anonymous:30> | 30 to 30 | 1 | No data: no coverage report in this map | No data: no coverage report in this map | No data: mutation testing has not run on this repo yet |
| <anonymous:30> | 30 to 30 | 1 | No data: no coverage report in this map | No data: no coverage report in this map | No data: mutation testing has not run on this repo yet |
Imports
It imports no other module of this repo. No module of this repo imports it.
Source
// A banner's Dismiss button. The rest of a banner is CSS; its Try again is the app's own.
// Hides a banner and puts focus somewhere sensible if it was inside: the page's main
// region, made focusable for the purpose, so focus is never left on nothing.
export function dismissBanner(banner: HTMLElement): void {
const hadFocus = banner.contains(document.activeElement);
banner.hidden = true;
if (!hadFocus) return;
const main = banner.closest("main") ?? document.querySelector("main");
if (!main) return;
if (!main.hasAttribute("tabindex")) main.setAttribute("tabindex", "-1");
main.focus();
}
// Attaches to every [data-cap="banner"] under root not yet attached. Returns a function
// that detaches.
export function enhance(root: ParentNode = document): () => void {
const undo: Array<() => void> = [];
for (const banner of root.querySelectorAll<HTMLElement>("[data-cap='banner']:not([data-cap-ready])")) {
banner.dataset.capReady = "";
const onClick = (e: MouseEvent) => {
if ((e.target as Element).closest("[data-cap-part='dismiss']")) dismissBanner(banner);
};
banner.addEventListener("click", onClick);
undo.push(() => {
banner.removeEventListener("click", onClick);
delete banner.dataset.capReady;
});
}
return () => undo.forEach((f) => f());
}