# All files / src/movement utils.ts

99.25% Statements 268/270
98.11% Branches 52/53
90% Functions 9/10
99.25% Lines 268/270

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
    2711x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    218x
    183x
    1x
    1x
    1x
    1x
    1x
    1x
    404x
    404x
    404x
    404x
    2x
    2x
    402x
    404x
    210x
    210x
    192x
    192x
    192x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    18x
    18x
    18x
    18x
    5x
    5x
    13x
    13x
    29x
    26x
    26x
    3x
    3x
    3x
    13x
    13x
    1x
    1x
    1x
    1x
    1x
    1x
    61x
    61x
    61x
    61x
    61x
    2x
    2x
    59x
    59x
    59x
    59x
    73x
    61x
    15x
    15x
    59x
    61x
    1x
    1x
    58x
    58x
    58x
    1x
    1x
    1x
    1x
    1x
    1x
    64x
    64x
    64x
    64x
    64x
    2x
    2x
    62x
    62x
    62x
    62x
    70x
    64x
    9x
    9x
    62x
    64x
    1x
    1x
    61x
    61x
    61x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    51x
    51x
    51x
    51x
    51x
    51x
    51x
    51x
    51x
    51x
    4x
    4x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    10x
    51x
    10x
    10x
    10x
    10x
    10x
    10x
    10x
    10x
    47x
    47x
    47x
    47x
    47x
    1x
    1x
    1x
    1x
    1x
    1x
    83x
    83x
    83x
    83x
    4x
    4x
    79x
    79x
    79x
    79x
    79x
    79x
    79x
    79x
    32x
    32x
    79x
    79x
    79x
    79x
    79x
    83x
    83x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    15x
    15x
    15x
    6x
    6x
    6x
    6x
    6x
    6x
    9x
    9x
    9x
    9x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    73x
    73x
    73x
    73x
    17x
    17x
    16x
    16x
    16x
    73x
    73x
    1x
    1x
    1x
    1x
    1x
    1x

     import { type KeyboardEvent } from "react";
    import { loop } from "../utils/loop.js";
    import { type FocusableIndexOptions, type TabIndexBehavior } from "./types.js";

    /**
     * @remarks \@since 6.0.0
     * @internal
     */
    export const isElementDisabled = (element: HTMLElement): boolean =>
      element.getAttribute("disabled") !== null ||
      element.getAttribute("aria-disabled") === "true";

    /**
     * @remarks \@since 5.0.0
     * @internal
     */
    export const isNotFocusable = (
      element: HTMLElement | undefined,
      includeDisabled: boolean
    ): boolean => {
      if (!element) {
        return true;
      }

      if (includeDisabled) {
        return false;
      }

      return isElementDisabled(element);
    };

    /**
     * @remarks \@since 6.0.0
     * @internal
     */
    export interface VirtualFocusableIndexOptions {
      focusables: readonly HTMLElement[];
      includeDisabled: boolean;
      activeDescendantId: string;
    }

    /**
     * @remarks \@since 6.0.0
     * @internal
     */
    export const getVirtualFocusDefaultIndex = (
      options: VirtualFocusableIndexOptions
    ): number => {
      const { focusables, includeDisabled, activeDescendantId } = options;
      if (!focusables.length || (!activeDescendantId && includeDisabled)) {
        return 0;
      }

      const activeIndex = focusables.findIndex((element) => {
        if (activeDescendantId) {
          return element.id === activeDescendantId;
        }

        return !isElementDisabled(element);
      });
      return Math.max(0, activeIndex);
    };

    /**
     * @remarks \@since 5.0.0
     * @internal
     */
    export const getFirstFocusableIndex = (
      options: FocusableIndexOptions
    ): number => {
      const { focusables, includeDisabled } = options;

      if (!focusables.length) {
        return -1;
      }

      let firstIndex = 0;
      while (
        firstIndex < focusables.length - 1 &&
        isNotFocusable(focusables[firstIndex], includeDisabled)
      ) {
        firstIndex += 1;
      }

      if (isNotFocusable(focusables[firstIndex], includeDisabled)) {
        return -1;
      }

      return firstIndex;
    };

    /**
     * @remarks \@since 5.0.0
     * @internal
     */
    export const getLastFocusableIndex = (
      options: FocusableIndexOptions
    ): number => {
      const { focusables, includeDisabled } = options;

      if (!focusables.length) {
        return -1;
      }

      let lastIndex = focusables.length - 1;
      while (
        lastIndex > 0 &&
        isNotFocusable(focusables[lastIndex], includeDisabled)
      ) {
        lastIndex -= 1;
      }

      if (isNotFocusable(focusables[lastIndex], includeDisabled)) {
        return -1;
      }

      return lastIndex;
    };

    /**
     * @remarks \@since 5.0.0
     * @internal
     */
    interface NextFocusableIndexOptions extends FocusableIndexOptions {
      loopable: boolean;
      increment: boolean;
      currentFocusIndex: number;
    }

    /**
     * @remarks \@since 5.0.0
     * @internal
     */
    export const getNextFocusableIndex = (
      options: NextFocusableIndexOptions
    ): number => {
      const {
        loopable,
        increment,
        focusables,
        includeDisabled,
        currentFocusIndex,
      } = options;
      if (!focusables.length) {
        return currentFocusIndex;
      }

      const min = getFirstFocusableIndex({ focusables, includeDisabled });
      const max = getLastFocusableIndex({ focusables, includeDisabled });
      let nextIndex = loop({
        min,
        max,
        value: currentFocusIndex,
        minmax: !loopable,
        increment,
      });
      while (
        isNotFocusable(focusables[nextIndex], includeDisabled) &&
        (loopable || nextIndex !== (increment ? max : min))
      ) {
        nextIndex = loop({
          min,
          max,
          value: nextIndex,
          minmax: !loopable,
          increment,
        });
      }

      // Since the `min` and `max` values are "safely" set, I don't need to verify
      // the nextIndex is still focusable
      return nextIndex;
    };

    /**
     * @remarks \@since 5.0.0
     * @internal
     */
    export function getSearchText(
      element: HTMLElement,
      searchable: boolean
    ): string {
      if (!searchable) {
        return "";
      }

      const cloned = element.cloneNode(true) as HTMLElement;
      cloned
        .querySelectorAll(
          // Note: do not include DISPLAY_NONE_CLASS since it is presentational only
          ".rmd-icon--font,[aria-hidden=true],[hidden],[role=presentation]"
        )
        .forEach((element) => {
          element.parentNode?.removeChild(element);
        });

      // Note: It would be good to use `cloned.innerText` (maybe?) at some point,
      // but it returns `undefined` in jsdom. It also does cause a reflow, so maybe
      // this is fine?
      // https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent#differences_from_innertext
      return (cloned.textContent || "")[0].toUpperCase();
    }

    /**
     * @remarks \@since 6.0.0
     * @internal
     */
    export interface RecalculateOptions {
      focusables: readonly HTMLElement[];
      includeDisabled: boolean;
      tabIndexBehavior: TabIndexBehavior | undefined;
      activeDescendantId: string;
    }

    /**
     * This was added to help with specific widgets that cause focus index to change
     * between renders (i.e. expanding all tree items on the same level with `*`).
     * There might be a better way to handle this in the future.
     *
     * @remarks \@since 6.0.0
     * @internal
     */
    export function recalculateFocusIndex(options: RecalculateOptions): number {
      const { focusables, includeDisabled, tabIndexBehavior, activeDescendantId } =
        options;
      if (tabIndexBehavior === "virtual") {
        return getVirtualFocusDefaultIndex({
          focusables,
          includeDisabled,
          activeDescendantId,
        });
      }

      const { activeElement } = document;
      return focusables.findIndex((element) => element === activeElement);
    }

    /**
     * Checks if a keyboard event can trigger a search through focusable elements
     * by:
     *
     * - checking if the key is a single letter that is not the space key
     * - checking that the alt, ctrl, and meta keys are not being held
     *
     * The shift key **is allowed** because holding shift means "search from the
     * beginning" instead of "search from current location".
     *
     * @remarks \@since 6.0.0
     * @internal
     */
    export function isSearchableEvent(event: KeyboardEvent): boolean {
      const { key, altKey, ctrlKey, metaKey } = event;

      return (
        key.length === 1 &&
        // can't search with space since it is generally a click event
        key !== " " &&
        !altKey &&
        !ctrlKey &&
        !metaKey
      );
    }

    /**
     * @remarks \@since 6.0.0
     * @internal
     */
    export function isTypeEvent(event: KeyboardEvent): boolean {
      return isSearchableEvent(event) || ["Backspace", " "].includes(event.key);
    }