# All files TabList.tsx

100% Statements 224/224
100% Branches 8/8
100% Functions 1/1
100% Lines 224/224

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
    2251x
    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
    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
    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
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    20x
    20x
    20x
    12x
    12x
    8x
    8x
    8x
    3x
    8x
    8x
    8x
    8x
    8x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    61x
    1x
     "use client";
    import type { HTMLAttributes } from "react";
    import { forwardRef, useEffect, useRef, useState } from "react";
    import { useAppSize } from "../media-queries/AppSizeProvider.js";
    import { KeyboardMovementProvider } from "../movement/useKeyboardMovementProvider.js";
    import type { BaseTabListScrollButtonProps } from "./TabListScrollButton.js";
    import { TabListScrollButton } from "./TabListScrollButton.js";
    import type { TabsAlignment } from "./tabListStyles.js";
    import { tabList } from "./tabListStyles.js";
    import { useTabList } from "./useTabList.js";
    import type { GetTabListScrollToOptions } from "./utils.js";

    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    import type { useTabs } from "./useTabs.js";

    /**
     * @remarks \@since 6.0.0
     */
    export interface TabListProps extends HTMLAttributes<HTMLDivElement> {
      activeIndex: number;
      setActiveIndex(nextActiveIndex: number): void;

      /**
       * @defaultValue `"left"`
       */
      align?: TabsAlignment;

      /**
       * @defaultValue `false`
       */
      padded?: boolean;

      /**
       * @defaultValue `false`
       */
      vertical?: boolean;

      /**
       * Set this to `true` to show a scrollbar when the number of tabs cause
       * overflow.
       *
       * @see {@link scrollButtons}
       * @defaultValue `false`
       */
      scrollbar?: boolean;

      /**
       * Set this to `true` to disable the active tab indicator from animating
       * when a new tab is selected.
       *
       * @defaultValue `false`
       */
      disableTransition?: boolean;

      /**
       * This should be equal to the `$tabs-transition-duration` variable.
       *
       * @defaultValue `150`
       */
      transitionDuration?: number;

      /**
       * @defaultValue `"manual"`
       */
      activationMode?: "manual" | "automatic";

      /**
       * Set this to `true` to render buttons that can scroll forwards or backwards
       * within the tab list if there is overflow **on desktop**. If you want to
       * display the scroll buttons on mobile as well, set this to `"allow-phone"` .
       *
       * @defaultValue `false`
       */
      scrollButtons?: boolean | "allow-phone";

      /**
       * A convenience prop for the {@link BaseTabListScrollButtonProps.getScrollToOptions}
       * on {@link forwardScrollButtonProps} and {@link backwardScrollButtonProps}.
       */
      getScrollToOptions?: GetTabListScrollToOptions;

      /**
       * Any additional props that should be passed to the scroll forward button.
       *
       * @example
       * ```tsx
       * forwardScrollButtonProps={{
       *   "aria-label": "Scroll right",
       *   theme: "primary",
       *   themeType: "contained",
       *   className: styles.buttonContainer,
       *   buttonProps: {
       *     className: styles.button,
       *   }
       * }}
       * ```
       */
      forwardScrollButtonProps?: BaseTabListScrollButtonProps;

      /**
       * Any additional props that should be passed to the scroll backward button.
       *
       * @example
       * ```tsx
       * forwardScrollButtonProps={{
       *   "aria-label": "Scroll left",
       *   theme: "primary",
       *   themeType: "contained",
       *   className: styles.buttonContainer,
       *   buttonProps: {
       *     className: styles.button,
       *   }
       * }}
       * ```
       */
      backwardScrollButtonProps?: BaseTabListScrollButtonProps;
    }

    /**
     * **Client Component**
     *
     * @see {@link useTabs} for example usage.
     *
     * @remarks \@since 6.0.0
     */
    export const TabList = forwardRef<HTMLDivElement, TabListProps>(
      function TabList(props, ref) {
        const {
          style,
          onClick,
          onFocus,
          onKeyDown,
          className,
          children,
          activeIndex,
          setActiveIndex,
          activationMode = "manual",
          align = "left",
          padded = false,
          vertical = false,
          scrollbar = false,
          scrollButtons = false,
          disableTransition = false,
          transitionDuration = 150,
          getScrollToOptions,
          forwardScrollButtonProps,
          backwardScrollButtonProps,
          ...remaining
        } = props;

        const { isPhone } = useAppSize();
        const showScrollButtons =
          !!scrollButtons && (scrollButtons === "allow-phone" || !isPhone);

        const { elementProps, movementContext, backwardProps, forwardProps } =
          useTabList({
            ref,
            style,
            onClick,
            onFocus,
            onKeyDown,
            vertical,
            activeIndex,
            setActiveIndex,
            activationMode,
            scrollButtons: showScrollButtons,
            disableTransition,
          });

        const prevActiveIndex = useRef(activeIndex);
        const [animate, setAnimate] = useState(false);
        useEffect(() => {
          const isSameIndex = activeIndex === prevActiveIndex.current;
          prevActiveIndex.current = activeIndex;
          if (disableTransition || isSameIndex) {
            return;
          }

          setAnimate(true);
          const timeout = window.setTimeout(() => {
            setAnimate(false);
          }, transitionDuration);

          return () => {
            window.clearTimeout(timeout);
          };
        }, [activeIndex, disableTransition, transitionDuration]);

        return (
          <KeyboardMovementProvider value={movementContext}>
            <div
              {...remaining}
              {...elementProps}
              role="tablist"
              className={tabList({
                align,
                padded,
                animate: !disableTransition && animate,
                vertical,
                scrollbar,
                className,
                indicator: !disableTransition,
              })}
            >
              {showScrollButtons && (
                <TabListScrollButton
                  getScrollToOptions={getScrollToOptions}
                  {...backwardScrollButtonProps}
                  {...backwardProps}
                />
              )}
              {children}
              {showScrollButtons && (
                <TabListScrollButton
                  getScrollToOptions={getScrollToOptions}
                  {...forwardScrollButtonProps}
                  {...forwardProps}
                />
              )}
            </div>
          </KeyboardMovementProvider>
        );
      }
    );