All files / packages/theme-selector/src/dropdown events.ts

100% Statements 62/62
100% Branches 29/29
100% Functions 10/10
100% Lines 62/62

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176                      4x             5x                       27x 27x   27x     4x 4x 4x     16x 16x 12x 12x 12x   4x 4x   16x     6x 6x 3x 3x 3x   3x 3x   6x                           10x 10x   10x   2x 2x 2x     2x 2x 2x     1x 1x 1x       2x 2x 2x     1x 1x 1x     1x 1x 1x                           45x 45x 45x     45x     3x 3x           45x     8x 2x             45x     26x 1x             45x   27x         45x 225x   10x          
// SPDX-License-Identifier: MIT
/**
 * Dropdown event handlers
 */
 
import type { DropdownElements, DropdownState, DropdownStateManager } from './state.js';
 
/**
 * Calculates the next index when navigating down through menu items.
 */
function getNextIndex(currentIndex: number, totalItems: number): number {
  return currentIndex < totalItems - 1 ? currentIndex + 1 : 0;
}
 
/**
 * Calculates the previous index when navigating up through menu items.
 */
function getPrevIndex(currentIndex: number, totalItems: number): number {
  return currentIndex > 0 ? currentIndex - 1 : totalItems - 1;
}
 
/**
 * Handles keyboard navigation on the trigger button.
 */
function handleTriggerKeydown(
  e: KeyboardEvent,
  dropdown: HTMLElement,
  state: DropdownState,
  stateManager: DropdownStateManager
): void {
  const { focusMenuItem, toggleDropdown, updateAriaExpanded } = stateManager;
  const totalItems = state.menuItems.length;
 
  switch (e.key) {
    case 'Enter':
    case ' ':
      e.preventDefault();
      toggleDropdown(!dropdown.classList.contains('is-active'));
      break;
 
    case 'ArrowDown':
      e.preventDefault();
      if (!dropdown.classList.contains('is-active')) {
        dropdown.classList.add('is-active');
        updateAriaExpanded(true);
        focusMenuItem(0);
      } else {
        const nextIndex = state.currentIndex < 0 ? 0 : getNextIndex(state.currentIndex, totalItems);
        focusMenuItem(nextIndex);
      }
      break;
 
    case 'ArrowUp':
      e.preventDefault();
      if (!dropdown.classList.contains('is-active')) {
        dropdown.classList.add('is-active');
        updateAriaExpanded(true);
        focusMenuItem(totalItems - 1);
      } else {
        const startIndex = state.currentIndex < 0 ? totalItems - 1 : state.currentIndex;
        focusMenuItem(getPrevIndex(startIndex, totalItems));
      }
      break;
  }
}
 
/**
 * Handles keyboard navigation on menu items.
 */
function handleMenuItemKeydown(
  e: KeyboardEvent,
  index: number,
  item: HTMLElement,
  state: DropdownState,
  stateManager: DropdownStateManager
): void {
  const { focusMenuItem, closeDropdown } = stateManager;
  const totalItems = state.menuItems.length;
 
  switch (e.key) {
    case 'ArrowDown':
      e.preventDefault();
      focusMenuItem(getNextIndex(index, totalItems));
      break;
 
    case 'ArrowUp':
      e.preventDefault();
      focusMenuItem(getPrevIndex(index, totalItems));
      break;
 
    case 'Escape':
      e.preventDefault();
      closeDropdown();
      break;
 
    case 'Enter':
    case ' ':
      e.preventDefault();
      item.click();
      break;
 
    case 'Home':
      e.preventDefault();
      focusMenuItem(0);
      break;
 
    case 'End':
      e.preventDefault();
      focusMenuItem(totalItems - 1);
      break;
  }
}
 
/**
 * Wires up all event handlers for the dropdown.
 */
export function wireDropdownEventHandlers(
  documentObj: Document,
  elements: DropdownElements,
  state: DropdownState,
  stateManager: DropdownStateManager,
  abortController: AbortController
): void {
  const { trigger, dropdown } = elements;
  const { closeDropdown, toggleDropdown } = stateManager;
  const signal = abortController.signal;
 
  // Trigger click
  trigger.addEventListener(
    'click',
    (e) => {
      e.preventDefault();
      toggleDropdown();
    },
    { signal }
  );
 
  // Outside click
  documentObj.addEventListener(
    'click',
    (e: MouseEvent) => {
      if (!dropdown.contains(e.target as Node)) {
        closeDropdown({ restoreFocus: false });
      }
    },
    { signal }
  );
 
  // Escape key (document-level)
  documentObj.addEventListener(
    'keydown',
    (e: KeyboardEvent) => {
      if (e.key === 'Escape' && dropdown.classList.contains('is-active')) {
        closeDropdown({ restoreFocus: true });
      }
    },
    { signal }
  );
 
  // Trigger keyboard navigation
  trigger.addEventListener(
    'keydown',
    (e: KeyboardEvent) => handleTriggerKeydown(e, dropdown, state, stateManager),
    { signal }
  );
 
  // Menu item keyboard navigation
  for (const [index, item] of state.menuItems.entries()) {
    item.addEventListener(
      'keydown',
      (e: KeyboardEvent) => handleMenuItemKeydown(e, index, item, state, stateManager),
      { signal }
    );
  }
}