# All files / tabs TabListScrollButton.tsx

98.44% Statements 127/129
70% Branches 7/10
100% Functions 3/3
98.44% Lines 127/129

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
    1301x
    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
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    40x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    92x
    92x
    92x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    14x
    14x
    14x

    14x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    14x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
     "use client";
    import type { HTMLAttributes } from "react";
    import { forwardRef, useCallback, useRef, useState } from "react";
    import type { ButtonProps } from "../button/Button.js";
    import { Button } from "../button/Button.js";
    import type { ButtonClassNameThemeOptions } from "../button/buttonStyles.js";
    import { useIcon } from "../icon/IconProvider.js";
    import { PropsWithRef } from "../types.js";
    import { useDir } from "../typography/WritingDirection.js";
    import { useIntersectionObserver } from "../useIntersectionObserver.js";
    import { applyRef } from "../utils/applyRef.js";
    import {
      tabListScrollButton,
      tabListScrollButtonContainer,
    } from "./tabListScrollButtonStyles.js";
    import type { GetTabListScrollToOptions } from "./utils.js";
    import { getTabListScrollToOptions } from "./utils.js";

    /**
     * @internal
     * @remarks \@since 6.0.0
     */
    export interface BaseTabListScrollButtonProps
      extends HTMLAttributes<HTMLDivElement>,
        ButtonClassNameThemeOptions {
      buttonProps?: PropsWithRef<ButtonProps, HTMLButtonElement>;

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

      /** @defaultValue {@link getTabListScrollToOptions} */
      getScrollToOptions?: GetTabListScrollToOptions;
    }

    /**
     * @internal
     * @remarks \@since 6.0.0
     */
    export interface TabListScrollButtonProps extends BaseTabListScrollButtonProps {
      type: "back" | "forward";
      /** @defaultValue `false` */
      vertical?: boolean;
    }

    /**
     * **Client Component**
     *
     * @internal
     * @remarks \@since 6.0.0
     */
    export const TabListScrollButton = forwardRef<
      HTMLDivElement,
      TabListScrollButtonProps
    >(function TabListScrollButton(props, ref) {
      const {
        "aria-label": ariaLabel,
        className,
        buttonProps,
        type,
        theme,
        themeType,
        buttonType = "icon",
        disabled: propDisabled,
        children: propChildren,
        getScrollToOptions = getTabListScrollToOptions,
        vertical = false,
        disableTransition = false,
        ...remaining
      } = props;

      const forward = type === "forward";
      const iconButton = buttonType === "icon";
      const icon = useIcon(type);
      const children = propChildren || icon;

      const root = useRef<HTMLElement | null>(null);
      const isRTL = useDir().dir === "rtl";
      const [disabled, setDisabled] = useState(!forward);
      const nodeRef = useIntersectionObserver({
        root,
        onUpdate: useCallback(([entry]) => {
          setDisabled(entry.intersectionRatio === 1);
        }, []),
      });

      return (
        <>
          {!forward && <span ref={nodeRef} />}
          <div
            {...remaining}
            ref={(instance) => {
              applyRef(instance, ref);
              root.current = instance?.parentElement || null;
            }}
            className={tabListScrollButtonContainer({ forward, className })}
          >
            <Button
              aria-label={ariaLabel || (iconButton ? type : undefined)}
              theme={theme}
              themeType={themeType}
              buttonType={buttonType}
              disabled={propDisabled || disabled}
              {...buttonProps}
              className={tabListScrollButton(buttonProps)}
              onClick={(event) => {
                buttonProps?.onClick?.(event);
                const container = root.current;
                if (!container) {
                  return;
                }

                container.scrollTo(
                  getScrollToOptions({
                    isRTL,
                    animate: !disableTransition,
                    vertical,
                    container,
                    increment: forward,
                  })
                );
              }}
            >
              {children}
            </Button>
          </div>
          {forward && <span ref={nodeRef} />}
        </>
      );
    });