All files / use-key-press/src utils.ts

93.5% Statements 72/77
88.37% Branches 76/86
100% Functions 14/14
93.5% Lines 72/77

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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340                          31x     1x             49x           49x       49x 49x                   1x                                         1x                                                     1x                                 47x 47x 47x 68x 68x     4x 2x     64x     47x                       51x   51x 5x 2x       3x 2x   1x 1x         46x 51x                               47x             47x   47x 66x 66x 19x 2x     2x     17x   19x     47x     47x                     32x 1x   31x                                     39x 37x           6x     2x                     33x 1x     32x                                   34x 4x     30x 32x     34x 32x 35x                   7x 1x   6x     6x 6x 4x       2x                 5x                             34x 27x   7x 2x   5x 3x   2x    
import type { RefObject } from "react";
import type {
  KeyPressEventTarget,
  KeyPressMatchBy,
  KeyPressTarget,
  ParsedShortcut,
} from "./types";
 
/**
 * Detects whether the current runtime is a browser with DOM access.
 * Returns `false` during server-side rendering.
 */
export function isKeyPressSupported(): boolean {
  return typeof window !== "undefined" && typeof document !== "undefined";
}
 
const APPLE_PLATFORM_RE = /Mac|iPhone|iPod|iPad/i;
 
/**
 * Detects Apple platforms so the `"mod"` alias can resolve to the Command
 * (Meta) key on macOS/iOS and to Control everywhere else. SSR-safe.
 */
export function isApplePlatform(): boolean {
  Iif (typeof navigator === "undefined") {
    return false;
  }
  // `navigator.platform` is deprecated but remains the most reliable synchronous
  // signal; fall back to the user-agent string when it is unavailable.
  const platform =
    (navigator as Navigator & { userAgentData?: { platform?: string } })
      .userAgentData?.platform ||
    navigator.platform ||
    "";
  const userAgent = navigator.userAgent || "";
  return APPLE_PLATFORM_RE.test(platform) || APPLE_PLATFORM_RE.test(userAgent);
}
 
/**
 * Maps modifier aliases to their canonical modifier name. `"mod"` is resolved
 * later against the current platform.
 */
const MODIFIER_TOKENS: Record<
  string,
  "ctrl" | "shift" | "alt" | "meta" | "mod"
> = {
  ctrl: "ctrl",
  control: "ctrl",
  ctl: "ctrl",
  shift: "shift",
  alt: "alt",
  option: "alt",
  opt: "alt",
  meta: "meta",
  cmd: "meta",
  command: "meta",
  win: "meta",
  windows: "meta",
  super: "meta",
  mod: "mod",
};
 
/**
 * Friendly aliases mapped to canonical `event.key` values (used when
 * `matchBy` is `"key"`).
 */
const KEY_ALIASES: Record<string, string> = {
  esc: "Escape",
  escape: "Escape",
  space: " ",
  spacebar: " ",
  up: "ArrowUp",
  down: "ArrowDown",
  left: "ArrowLeft",
  right: "ArrowRight",
  arrowup: "ArrowUp",
  arrowdown: "ArrowDown",
  arrowleft: "ArrowLeft",
  arrowright: "ArrowRight",
  enter: "Enter",
  return: "Enter",
  del: "Delete",
  delete: "Delete",
  ins: "Insert",
  tab: "Tab",
  backspace: "Backspace",
  plus: "+",
};
 
/**
 * Friendly aliases mapped to lower-cased `event.code` values (used when
 * `matchBy` is `"code"`).
 */
const CODE_ALIASES: Record<string, string> = {
  space: "space",
  esc: "escape",
  escape: "escape",
  up: "arrowup",
  down: "arrowdown",
  left: "arrowleft",
  right: "arrowright",
  enter: "enter",
  return: "enter",
};
 
/**
 * Splits a shortcut string on `+` while treating the literal `+` key
 * correctly (e.g. `"ctrl++"` → `["ctrl", "+"]`, `"+"` → `["+"]`).
 */
function splitShortcut(shortcut: string): string[] {
  const parts = shortcut.trim().split("+");
  const tokens: string[] = [];
  for (const part of parts) {
    const token = part.trim();
    if (token === "") {
      // An empty segment means the `+` character was itself a key, not a
      // separator. Collapse consecutive empties so we only add one.
      if (tokens[tokens.length - 1] !== "+") {
        tokens.push("+");
      }
    } else {
      tokens.push(token);
    }
  }
  return tokens;
}
 
/**
 * Normalizes a non-modifier key token to the form it will be compared against
 * at match time.
 */
export function normalizeKeyToken(
  token: string,
  matchBy: KeyPressMatchBy,
  caseSensitive: boolean
): string {
  const lower = token.toLowerCase();
 
  if (matchBy === "code") {
    if (lower in CODE_ALIASES) {
      return CODE_ALIASES[lower];
    }
    // Allow bare letters/digits to map to their physical code, e.g.
    // "s" -> "keys", "1" -> "digit1".
    if (/^[a-z]$/.test(lower)) {
      return `key${lower}`;
    }
    Eif (/^[0-9]$/.test(lower)) {
      return `digit${lower}`;
    }
    return lower;
  }
 
  const canonical = lower in KEY_ALIASES ? KEY_ALIASES[lower] : token;
  return caseSensitive ? canonical : canonical.toLowerCase();
}
 
/**
 * Parses a single shortcut binding such as `"mod+shift+k"` into a
 * {@link ParsedShortcut}. The last non-modifier token wins as the key.
 *
 * @param shortcut - The shortcut string.
 * @param matchBy - Whether to match by logical key or physical code.
 * @param caseSensitive - Whether letter matching is case-sensitive (key mode only).
 */
export function parseShortcut(
  shortcut: string,
  matchBy: KeyPressMatchBy = "key",
  caseSensitive = false
): ParsedShortcut {
  const parsed: ParsedShortcut = {
    ctrl: false,
    shift: false,
    alt: false,
    meta: false,
    key: null,
  };
  const apple = isApplePlatform();
 
  for (const token of splitShortcut(shortcut)) {
    const modifier = MODIFIER_TOKENS[token.toLowerCase()];
    if (modifier) {
      if (modifier === "mod") {
        Iif (apple) {
          parsed.meta = true;
        } else {
          parsed.ctrl = true;
        }
      } else {
        parsed[modifier] = true;
      }
      continue;
    }
    // Non-modifier token: the last one specified is the key to match.
    parsed.key = normalizeKeyToken(token, matchBy, caseSensitive);
  }
 
  return parsed;
}
 
/**
 * Extracts the comparable key string from a keyboard event.
 */
function getComparableEventKey(
  event: KeyboardEvent,
  matchBy: KeyPressMatchBy,
  caseSensitive: boolean
): string {
  if (matchBy === "code") {
    return event.code.toLowerCase();
  }
  return caseSensitive ? event.key : event.key.toLowerCase();
}
 
/**
 * Determines whether a keyboard event satisfies a parsed shortcut.
 *
 * @param event - The keyboard event.
 * @param parsed - The parsed shortcut to test against.
 * @param exactModifiers - When `true`, modifier state must match exactly.
 * @param matchBy - Whether to compare `event.key` or `event.code`.
 * @param caseSensitive - Whether letter matching is case-sensitive (key mode only).
 */
export function matchesShortcut(
  event: KeyboardEvent,
  parsed: ParsedShortcut,
  exactModifiers: boolean,
  matchBy: KeyPressMatchBy,
  caseSensitive: boolean
): boolean {
  if (exactModifiers) {
    if (
      event.ctrlKey !== parsed.ctrl ||
      event.shiftKey !== parsed.shift ||
      event.altKey !== parsed.alt ||
      event.metaKey !== parsed.meta
    ) {
      return false;
    }
  } else {
    Iif (
      (parsed.ctrl && !event.ctrlKey) ||
      (parsed.shift && !event.shiftKey) ||
      (parsed.alt && !event.altKey) ||
      (parsed.meta && !event.metaKey)
    ) {
      return false;
    }
  }
 
  // Modifier-only binding (e.g. "shift"): matched purely on modifier state.
  if (parsed.key === null) {
    return true;
  }
 
  return getComparableEventKey(event, matchBy, caseSensitive) === parsed.key;
}
 
/**
 * Builds a predicate from a {@link KeyPressTarget}. String and string-array
 * targets are pre-parsed once; a function target is used directly.
 *
 * @param target - The key(s) or predicate to match.
 * @param matchBy - Whether to compare `event.key` or `event.code`.
 * @param caseSensitive - Whether letter matching is case-sensitive (key mode only).
 * @param exactModifiers - Whether modifier state must match exactly.
 */
export function createMatcher(
  target: KeyPressTarget,
  matchBy: KeyPressMatchBy,
  caseSensitive: boolean,
  exactModifiers: boolean
): (event: KeyboardEvent) => boolean {
  if (typeof target === "function") {
    return target;
  }
 
  const shortcuts = (Array.isArray(target) ? target : [target]).map((binding) =>
    parseShortcut(binding, matchBy, caseSensitive)
  );
 
  return (event: KeyboardEvent) =>
    shortcuts.some((parsed) =>
      matchesShortcut(event, parsed, exactModifiers, matchBy, caseSensitive)
    );
}
 
/**
 * Returns `true` if the event originated from an editable element — an
 * `<input>`, `<textarea>`, `<select>`, or `contenteditable` node. Used to
 * suppress shortcuts while the user is typing.
 */
export function isEditableElement(target: EventTarget | null): boolean {
  if (target === null || typeof HTMLElement === "undefined") {
    return false;
  }
  Iif (!(target instanceof HTMLElement)) {
    return false;
  }
  const tagName = target.tagName;
  if (tagName === "INPUT" || tagName === "TEXTAREA" || tagName === "SELECT") {
    return true;
  }
  // `isContentEditable` may be `undefined` in some environments (e.g. jsdom);
  // coerce to a strict boolean.
  return target.isContentEditable === true;
}
 
/**
 * Type guard for React `RefObject`s.
 */
function isRefObject(
  target: KeyPressEventTarget
): target is RefObject<HTMLElement | null> {
  return (
    target !== null &&
    target !== undefined &&
    typeof target === "object" &&
    "current" in target
  );
}
 
/**
 * Resolves a {@link KeyPressEventTarget} to a concrete event target, defaulting
 * to `window` when omitted and returning `null` when detached or unresolved.
 */
export function resolveTarget(
  target: KeyPressEventTarget
): Window | Document | HTMLElement | null {
  if (target === undefined) {
    return typeof window !== "undefined" ? window : null;
  }
  if (target === null) {
    return null;
  }
  if (isRefObject(target)) {
    return target.current;
  }
  return target;
}