# All files / src/movement utils.ts

100% Statements 262/262
98.11% Branches 52/53
100% Functions 9/9
100% Lines 262/262

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
    2631x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    182x
    147x
    1x
    1x
    1x
    1x
    1x
    1x
    371x
    371x
    371x
    371x
    2x
    2x
    369x
    371x
    210x
    210x
    159x
    159x
    159x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    17x
    17x
    17x
    17x
    4x
    4x
    13x
    13x
    29x
    26x
    26x
    3x
    3x
    3x
    13x
    13x
    1x
    1x
    1x
    1x
    1x
    1x
    55x
    55x
    55x
    55x
    55x
    2x
    2x
    53x
    53x
    53x
    53x
    67x
    55x
    15x
    15x
    53x
    55x
    1x
    1x
    52x
    52x
    52x
    1x
    1x
    1x
    1x
    1x
    1x
    58x
    58x
    58x
    58x
    58x
    2x
    2x
    56x
    56x
    56x
    56x
    64x
    58x
    9x
    9x
    56x
    58x
    1x
    1x
    55x
    55x
    55x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    4x
    4x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    42x
    10x
    46x
    10x
    10x
    10x
    10x
    10x
    10x
    10x
    10x
    42x
    42x
    42x
    42x
    42x
    1x
    1x
    1x
    1x
    1x
    1x
    79x
    79x
    79x
    79x
    4x
    4x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    75x
    32x
    32x
    75x
    75x
    75x
    75x
    75x
    79x
    79x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    14x
    14x
    14x
    5x
    5x
    5x
    5x
    5x
    5x
    9x
    9x
    9x
    9x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    64x
    64x
    64x
    64x
    16x
    16x
    15x
    15x
    15x
    64x
    64x
     import type { KeyboardEvent } from "react";
    import { loop } from "../utils/loop.js";
    import type { FocusableIndexOptions, 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
      );
    }