# All files Tooltip.tsx

100% Statements 158/158
100% Branches 1/1
100% Functions 1/1
100% Lines 158/158

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
    1591x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    257x
    1x
     "use client";
    import { forwardRef, type HTMLAttributes } from "react";
    import { type TextOverflow } from "../cssUtils.js";
    import { Portal } from "../portal/Portal.js";
    import { type SimplePosition } from "../positioning/types.js";
    import {
      type CSSTransitionComponentProps,
      type TransitionActions,
    } from "../transition/types.js";
    import { useCSSTransition } from "../transition/useCSSTransition.js";
    import { useEnsuredId } from "../useEnsuredId.js";
    import {
      DEFAULT_TOOLTIP_CLASSNAMES,
      DEFAULT_TOOLTIP_POSITION,
      DEFAULT_TOOLTIP_TIMEOUT,
    } from "./constants.js";
    import { tooltip } from "./tooltipStyles.js";

    /**
     * The base props for the `Tooltip` component. This can be extended when
     * creating custom tooltip implementations.
     *
     * @remarks
     * \@since 2.8.0 Supports the `RenderConditionalPortalProps`
     * \@since 6.0.0 No longer supports the `RenderConditionalPortalProps`.
     */
    export interface TooltipProps
      extends HTMLAttributes<HTMLSpanElement>,
        CSSTransitionComponentProps,
        TransitionActions {
      visible: boolean;

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

      /**
       * @defaultValue `DEFAULT_TOOLTIP_POSITION`
       * @see {@link DEFAULT_TOOLTIP_POSITION}
       */
      position?: SimplePosition;

      /**
       * @see {@link CSSTransitionComponentProps.temporary}
       * @defaultValue `true`
       */
      temporary?: boolean;

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

      /**
       * Set this to `"nowrap"` for tooltips that are positioned near the edge of
       * the viewport that have a position of `"above"` or `"below"` so that the
       * tooltip no longer aligns to the center of the tooltipped element.
       *
       * Set this to `"ellipsis"` if the tooltip should only show a single line of
       * text and ellipsis once it has reached the max tooltip width.
       *
       * @defaultValue `"allow"`
       */
      textOverflow?: TextOverflow;
    }

    /**
     * **Client Component**
     *
     * This is the base tooltip component that can only be used to render a tooltip
     * with an animation when the visibility changes. If this component is used, you
     * will need to manually add all the event listeners and triggers to change the
     * `visible` prop.
     *
     * @example
     * Simple Usage
     * ```tsx
     * import { Button, useTooltip, Tooltip } from "@react-md/core";
     *
     * function Example() {
     *   const { elementProps, tooltipProps } = useTooltip();
     *
     *   return (
     *     <>
     *       <Button {...elementProps}>Button</Button>
     *       <Tooltip {...tooltipProps}>
     *         Tooltip Content
     *       </Tooltip>
     *     </>
     *   );
     * }
     * ```
     */
    export const Tooltip = forwardRef<HTMLSpanElement, TooltipProps>(
      function Tooltip(props, nodeRef) {
        const {
          id: propId,
          dense,
          visible,
          children,
          appear,
          enter,
          exit,
          onEnter,
          onEntering,
          onEntered,
          onExit,
          onExiting,
          onExited,
          timeout = DEFAULT_TOOLTIP_TIMEOUT,
          classNames = DEFAULT_TOOLTIP_CLASSNAMES,
          className,
          position = DEFAULT_TOOLTIP_POSITION,
          temporary = true,
          exitedHidden = !temporary,
          textOverflow,
          disablePortal: propDisablePortal,
          ...remaining
        } = props;
        const id = useEnsuredId(propId, "tooltip");

        const { rendered, elementProps, disablePortal } = useCSSTransition({
          nodeRef,
          appear,
          enter,
          exit,
          transitionIn: visible,
          timeout,
          classNames,
          className: tooltip({
            dense,
            position,
            className,
            textOverflow,
          }),
          onEnter,
          onEntering,
          onEntered,
          onExit,
          onExiting,
          onExited,
          temporary,
          exitedHidden,
          disablePortal: propDisablePortal,
        });

        return (
          <Portal disabled={disablePortal}>
            {rendered && (
              <span {...remaining} {...elementProps} id={id} role="tooltip">
                {children}
              </span>
            )}
          </Portal>
        );
      }
    );