# All files Tab.tsx

100% Statements 179/179
100% Branches 6/6
100% Functions 1/1
100% Lines 179/179

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
    1801x
    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
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    135x
    135x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
    136x
     "use client";
    import {
      type AnchorHTMLAttributes,
      type ButtonHTMLAttributes,
      type ReactElement,
      type ReactNode,
    } from "react";
    import { useElementInteraction } from "../interaction/useElementInteraction.js";
    import { useHigherContrastChildren } from "../interaction/useHigherContrastChildren.js";
    import { type CustomLinkComponent } from "../link/Link.js";
    import { useKeyboardMovementContext } from "../movement/useKeyboardMovementProvider.js";
    import { useEnsuredId } from "../useEnsuredId.js";
    import { tab } from "./tabStyles.js";

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

    /**
     * @remarks \@since 6.0.0
     */
    export interface BaseTabProps {
      /**
       * Set this to `true` if the tab is currently active.
       *
       * This is normally provided by the {@link useTabs} hook.
       */
      active: boolean;

      /**
       * Set this to `true` if the {@link TabListProps.disableTransition} prop has
       * also been set to `true` to disable an active indicator below the tab when
       * {@link active} is `true`.
       *
       * @defaultValue `false`
       */
      activeIndicator?: boolean;

      /**
       * Set this to `true` when rendering the tabs vertically and
       * {@link activeIndicator} has been enabled.
       *
       * @defaultValue !false
       */
      verticalActiveIndicator?: boolean;

      /**
       * An optional icon to render with the with the {@link children}. The default
       * behavior will render this icon before the children.
       *
       * @see {@link iconAfter}
       * @see {@link stacked}
       */
      icon?: ReactNode;

      /**
       * Set this to `true` to render the {@link icon} after the {@link children}.
       *
       * @defaultValue `false`
       */
      iconAfter?: boolean;

      /**
       * Set this to `true` to render the {@link icon} and {@link children} stacked
       * instead of horizontally.
       *
       * @defaultValue `false`
       */
      stacked?: boolean;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface TabButtonProps
      extends BaseTabProps,
        ButtonHTMLAttributes<HTMLButtonElement> {
      as?: "button";
    }

    export interface TabLinkProps
      extends BaseTabProps,
        AnchorHTMLAttributes<HTMLAnchorElement> {
      as: CustomLinkComponent;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export type TabProps = TabButtonProps | TabLinkProps;

    /**
     * **Client Component**
     *
     * This component should usually be used with the `TabsList` component and
     * `useTabs` hook.
     *
     * @see {@link useTabs}
     *
     * @remarks \@since 6.0.0
     */
    export function Tab(props: TabProps): ReactElement {
      const {
        id: propId,
        as: Component = "button",
        active,
        activeIndicator,
        verticalActiveIndicator,
        icon,
        iconAfter,
        stacked,
        className,
        children: propChildren,
        onBlur,
        onClick,
        onKeyDown,
        onKeyUp,
        onMouseDown,
        onMouseUp,
        onMouseLeave,
        onDragStart,
        onTouchStart,
        onTouchEnd,
        onTouchMove,
        ...remaining
      } = props as TabButtonProps;
      const { disabled } = props as TabButtonProps;

      const id = useEnsuredId(propId, "tab");
      const { activeDescendantId } = useKeyboardMovementContext();
      const { ripples, handlers } = useElementInteraction({
        onBlur,
        onClick,
        onKeyDown,
        onKeyUp,
        onMouseDown,
        onMouseUp,
        onMouseLeave,
        onDragStart,
        onTouchStart,
        onTouchEnd,
        onTouchMove,
        disabled,
      });

      const isLink = Component !== "button";
      const children = useHigherContrastChildren(propChildren);
      let buttonOnlyProps: Record<string, unknown> | undefined;
      if (!isLink) {
        buttonOnlyProps = { type: "button" };
      }

      return (
        <Component
          {...remaining}
          {...buttonOnlyProps}
          {...handlers}
          aria-selected={active}
          id={id}
          role="tab"
          tabIndex={id === activeDescendantId ? 0 : -1}
          className={tab({
            className,
            active,
            isLink,
            stacked: !!icon && stacked,
            disabled,
            reversed: !!icon && iconAfter,
            activeIndicator,
            verticalActiveIndicator,
          })}
        >
          {icon}
          {children}
          {ripples}
        </Component>
      );
    }