All files / src/browser/ui back-to-top.ts

0% Statements 0/23
0% Branches 0/12
0% Functions 0/6
0% Lines 0/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                                                                                                           
/**
 * @module UI/BackToTop
 * @description WCAG-compliant scroll-to-top button with reduced motion support.
 
 *
 * @intelligence WCAG-compliant navigation intelligence — scroll-to-top accessibility component with reduced motion support (prefers-reduced-motion media query). Ensures inclusive navigation for intelligence analysts working through long-form dashboard content.
 *
 * @business Accessibility compliance requirement — WCAG 2.1 AA is mandatory for government clients (B2G) and required by EU accessibility directives. Back-to-top is a basic UX pattern that reduces friction for all users, improving engagement metrics across all customer segments.
 *
 * @marketing Inclusive design demonstration — accessibility features are marketable to government agencies, educational institutions, and organizations with diversity mandates. WCAG compliance is a checkbox requirement in many RFP processes.
 * */
 
export function initBackToTop(): void {
  const btn = document.getElementById('back-to-top');
  if (!btn) return;
 
  const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
 
  const toggleVisibility = () => {
    const scrolled = window.scrollY > 300;
    btn.classList.toggle('visible', scrolled);
    btn.setAttribute('aria-hidden', String(!scrolled));
    btn.tabIndex = scrolled ? 0 : -1;
  };
 
  let ticking = false;
  window.addEventListener('scroll', () => {
    if (!ticking) {
      window.requestAnimationFrame(() => {
        toggleVisibility();
        ticking = false;
      });
      ticking = true;
    }
  }, { passive: true });
 
  btn.addEventListener('click', () => {
    window.scrollTo({
      top: 0,
      behavior: prefersReducedMotion ? 'instant' : 'smooth',
    });
  });
 
  btn.addEventListener('keydown', (e: KeyboardEvent) => {
    if (e.key === 'Enter' || e.key === ' ') {
      e.preventDefault();
      btn.click();
    }
  });
 
  // Initial state
  toggleVisibility();
}