# All files useTabList.ts

94.47% Statements 205/217
84% Branches 21/25
100% Functions 4/4
94.47% Lines 205/217

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
    2181x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    19x
    19x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    12x
    12x
    12x
    12x
    12x
    12x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    11x
    11x
    11x
    11x
    11x
    11x
    11x

    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    3x
    11x
    3x
    3x
    8x
    8x
    8x
    11x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    19x
    19x

    19x
    19x
    19x
    19x
    19x
    5x
    5x
    19x
    56x
    56x
    56x
    15x
    15x
    2x
    15x

    15x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    18x
    18x
    18x
    18x
    18x
    18x
    18x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
    56x
     "use client";
    import type {
      CSSProperties,
      FocusEventHandler,
      KeyboardEventHandler,
      MouseEventHandler,
      Ref,
      RefObject,
    } from "react";
    import { useCallback, useEffect, useRef, useState } from "react";
    import type {
      KeyboardMovementContext,
      KeyboardMovementProps,
    } from "../movement/types.js";
    import { useKeyboardMovementProvider } from "../movement/useKeyboardMovementProvider.js";
    import { useDir } from "../typography/WritingDirectionProvider.js";
    import { useEnsuredRef } from "../useEnsuredRef.js";
    import { useResizeObserver } from "../useResizeObserver.js";
    import { getTabRoleOnly, scrollTabIntoView } from "./utils.js";

    declare module "react" {
      interface CSSProperties {
        "--rmd-tab-size"?: string;
        "--rmd-tab-offset"?: string | number;
        "--rmd-tab-indicator-background"?: string;
      }
    }

    const TAB_SIZE_VAR = "--rmd-tab-size";
    const TAB_OFFSET_VAR = "--rmd-tab-offset";

    const noop = (): void => {
      // do nothing
    };

    export type TabWidthVar = typeof TAB_SIZE_VAR;
    export type TabOffsetVar = typeof TAB_OFFSET_VAR;

    export type IndicatorCSSProperties = CSSProperties &
      Record<TabWidthVar | TabOffsetVar, string>;

    export interface TabListHookOptions {
      ref: Ref<HTMLDivElement> | undefined;
      style: CSSProperties | undefined;
      activeIndex: number;
      setActiveIndex(nextActiveIndex: number): void;
      scrollButtons: boolean;
      activationMode: "manual" | "automatic";
      vertical: boolean;
      onClick: MouseEventHandler<HTMLDivElement> | undefined;
      onFocus: FocusEventHandler<HTMLDivElement> | undefined;
      onKeyDown: KeyboardEventHandler<HTMLDivElement> | undefined;
      disableTransition: boolean;
    }

    export interface TabListHookReturnValue {
      elementProps: KeyboardMovementProps<HTMLDivElement> & {
        "aria-orientation": "horizontal" | "vertical";
        style: CSSProperties;
        ref: Ref<HTMLDivElement>;
        onClick: MouseEventHandler<HTMLDivElement>;
      };
      movementContext: KeyboardMovementContext;
      backwardProps: {
        ref: RefObject<HTMLDivElement>;
        type: "back";
        vertical: boolean;
      };
      forwardProps: {
        ref: RefObject<HTMLDivElement>;
        type: "forward";
        vertical: boolean;
      };
    }

    /**
     * @internal
     */
    export function useTabList(
      options: TabListHookOptions
    ): TabListHookReturnValue {
      const {
        ref: propRef,
        style,
        activeIndex,
        scrollButtons,
        onClick = noop,
        onFocus,
        onKeyDown,
        activationMode,
        vertical,
        setActiveIndex,
        disableTransition,
      } = options;

      const isRTL = useDir().dir === "rtl";

      const [indicatorStyles, setIndicatorStyles] =
        useState<IndicatorCSSProperties>(() => {
          const tabWidth = `${100 / 3}%`;
          return {
            [TAB_SIZE_VAR]: tabWidth,
            [TAB_OFFSET_VAR]: "0px",
          };
        });

      const [nodeRef, ref] = useEnsuredRef(propRef);
      const tabListRef = useResizeObserver({
        ref,
        disabled: disableTransition,
        onUpdate: useCallback(
          (entry) => {
            // this is kind of hacky -- the styles should update when switching between
            // RTL, but the RTL state isn't required for any styles. Just reference it
            // so that the hooks eslint rule doesn't show a warning...
            isRTL;

            const activeTab = getTabRoleOnly(entry.target)[activeIndex];
            if (!activeTab) {
              return;
            }

            const size = vertical ? activeTab.offsetHeight : activeTab.offsetWidth;
            const offset = vertical ? activeTab.offsetTop : activeTab.offsetLeft;
            const cssVars: IndicatorCSSProperties = {
              [TAB_SIZE_VAR]: `${size}px`,
              [TAB_OFFSET_VAR]: `${offset}px`,
            };

            setIndicatorStyles((prevStyles) => {
              if (
                prevStyles &&
                prevStyles[TAB_SIZE_VAR] === cssVars[TAB_SIZE_VAR] &&
                prevStyles[TAB_OFFSET_VAR] === cssVars[TAB_OFFSET_VAR]
              ) {
                return prevStyles;
              }

              return cssVars;
            });
          },
          [activeIndex, isRTL, vertical]
        ),
      });
      const forwardRef = useRef<HTMLDivElement>(null);
      const backwardRef = useRef<HTMLDivElement>(null);
      const { movementProps, movementContext } = useKeyboardMovementProvider({
        onClick(event) {
          onClick(event);
          if (event.isPropagationStopped() || !(event.target instanceof Element)) {
            return;
          }

          const clickedTab = event.target.closest("[role='tab']");
          const tabs = getTabRoleOnly(event.currentTarget);
          const i = tabs.findIndex((tab) => tab === clickedTab);
          if (i !== -1) {
            setActiveIndex(i);
          }
        },
        onFocus,
        onKeyDown,
        onFocusChange(event) {
          const { index } = event;
          if (activationMode === "automatic") {
            setActiveIndex(index);
          } else if (scrollButtons) {
            scrollTabIntoView({
              activeIndex: index,
              backward: backwardRef.current,
              container: nodeRef.current,
              forward: forwardRef.current,
              vertical,
            });
          }
        },
        loopable: true,
        searchable: true,
        horizontal: !vertical,
        includeDisabled: true,
        tabIndexBehavior: "roving",
        getFocusableElements: getTabRoleOnly,
      });

      useEffect(() => {
        scrollTabIntoView({
          activeIndex,
          backward: backwardRef.current,
          container: nodeRef.current,
          forward: forwardRef.current,
          vertical,
        });
      }, [activeIndex, nodeRef, vertical]);

      return {
        elementProps: {
          "aria-orientation": vertical ? "vertical" : "horizontal",
          ref: tabListRef,
          style: {
            ...style,
            ...(disableTransition ? undefined : indicatorStyles),
          },
          ...movementProps,
        },
        backwardProps: {
          ref: backwardRef,
          type: "back",
          vertical,
        },
        forwardProps: {
          ref: forwardRef,
          type: "forward",
          vertical,
        },
        movementContext,
      };
    }