# All files / src/list ListItemLink.tsx

100% Statements 177/177
50% Branches 1/2
100% Functions 1/1
100% Lines 177/177

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
    1781x
    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
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    4x
    1x
     "use client";
    import {
      forwardRef,
      type AnchorHTMLAttributes,
      type HTMLAttributes,
    } from "react";
    import { useElementInteraction } from "../interaction/useElementInteraction.js";
    import { useHigherContrastChildren } from "../interaction/useHigherContrastChildren.js";
    import { type CustomLinkComponent } from "../link/Link.js";
    import { type PropsWithRef } from "../types.js";
    import { ListItemChildren } from "./ListItemChildren.js";
    import { getListItemHeight } from "./getListItemHeight.js";
    import { listItem, type ListItemClassNameOptions } from "./listItemStyles.js";
    import { type ListItemChildrenProps } from "./types.js";

    export interface ListItemLinkProps
      extends AnchorHTMLAttributes<HTMLAnchorElement>,
        ListItemClassNameOptions,
        ListItemChildrenProps {
      /** @defaultValue `"a"` */
      as?: CustomLinkComponent;

      /**
       * This should only be used if the {@link as} {@link CustomLinkComponent}
       * accepts a `to` prop instead of {@link href}.
       */
      to?: string;

      /**
       * The link's href. Either this or the {@link to} prop **should** be provided.
       */
      href?: string;

      /**
       * Any additional props to provide the wrapping `<li>` element such as
       * `style`, `className`, and `ref`.
       */
      liProps?: PropsWithRef<HTMLAttributes<HTMLLIElement>, HTMLLIElement>;

      /**
       * @defaultValue `disabled ? -1 : undefined`
       */
      tabIndex?: number;
    }

    /**
     * **Client Component**
     *
     * The `ListItemLink` should be used to render links within the `List` component.
     */
    export const ListItemLink = forwardRef<HTMLAnchorElement, ListItemLinkProps>(
      function ListItemLink(props, ref) {
        const {
          as: Link = "a",
          to,
          href,
          className,
          liProps,
          textProps,
          textClassName,
          secondaryTextClassName,
          primaryText,
          secondaryText,
          secondaryTextProps,
          disableTextChildren = false,
          height: propHeight = "auto",
          leftAddon,
          leftAddonType = "icon",
          leftAddonPosition = "middle",
          leftAddonClassName,
          leftAddonForceWrap,
          rightAddon,
          rightAddonType = "icon",
          rightAddonPosition = "middle",
          rightAddonClassName,
          rightAddonForceWrap,
          disableLeftAddonCenteredMedia = false,
          disableRightAddonCenteredMedia = false,
          multiline = false,
          disabled = false,
          disabledOpacity = false,
          onBlur,
          onClick,
          onKeyDown,
          onKeyUp,
          onMouseDown,
          onMouseUp,
          onMouseLeave,
          onDragStart,
          onTouchStart,
          onTouchEnd,
          onTouchMove,
          role,
          tabIndex = disabled || role === "menuitem" ? -1 : undefined,
          children: propChildren,
          ...remaining
        } = props;

        const { pressedClassName, ripples, handlers } = useElementInteraction({
          onBlur,
          onClick,
          onKeyDown,
          onKeyUp,
          onMouseDown,
          onMouseUp,
          onMouseLeave,
          onDragStart,
          onTouchStart,
          onTouchEnd,
          onTouchMove,
          disabled,
        });
        const children = useHigherContrastChildren(
          propChildren,
          !disableTextChildren
        );

        const height = getListItemHeight({
          height: propHeight,
          leftAddon,
          leftAddonType,
          rightAddon,
          rightAddonType,
          secondaryText,
        });

        return (
          <li {...liProps} role="none">
            <Link
              ref={ref}
              {...remaining}
              {...handlers}
              role={role}
              className={listItem({
                className,
                link: true,
                height,
                disabled,
                disabledOpacity,
                multiline,
                pressedClassName,
              })}
              // TODO: Figure out a better type for the CustomLinkComponent to fix this
              to={to as string}
              href={href as string}
              tabIndex={tabIndex}
            >
              <ListItemChildren
                multiline={multiline}
                textClassName={textClassName}
                secondaryTextClassName={secondaryTextClassName}
                disableTextChildren={disableTextChildren}
                primaryText={primaryText}
                textProps={textProps}
                secondaryText={secondaryText}
                secondaryTextProps={secondaryTextProps}
                leftAddon={leftAddon}
                leftAddonType={leftAddonType}
                leftAddonPosition={leftAddonPosition}
                leftAddonClassName={leftAddonClassName}
                leftAddonForceWrap={leftAddonForceWrap}
                rightAddon={rightAddon}
                rightAddonType={rightAddonType}
                rightAddonPosition={rightAddonPosition}
                rightAddonClassName={rightAddonClassName}
                rightAddonForceWrap={rightAddonForceWrap}
                disableLeftAddonCenteredMedia={disableLeftAddonCenteredMedia}
                disableRightAddonCenteredMedia={disableRightAddonCenteredMedia}
              >
                {children}
              </ListItemChildren>
              {ripples}
            </Link>
          </li>
        );
      }
    );