# All files / hoverMode useHoverMode.ts

93.4% Statements 184/197
80% Branches 20/25
50% Functions 1/2
93.4% Lines 184/197

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
    1981x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    132x
    326x
    194x
    194x
    326x
    326x
    326x
    58x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    50x

    50x
    50x

    50x
    50x
    50x
    50x
    50x
    326x
    326x
    326x
    50x
    50x
    50x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    31x
    31x
    3x
    3x
    28x
    28x
    31x
    28x
    31x

    28x
    28x
    28x
    28x
    27x
    27x
    28x
    28x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    13x

    13x
    13x
    13x
    13x
    13x
    13x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
    326x
     "use client";
    import type { MouseEvent } from "react";
    import { useCallback, useEffect, useRef, useState } from "react";
    import type { UseStateInitializer, UseStateSetter } from "../types.js";
    import type { SimpleHoverModeContext } from "./useHoverModeProvider.js";

    /**
     * @remarks \@since 6.0.0
     */
    export interface HoverModeConfigurationOptions extends SimpleHoverModeContext {
      /** @defaultValue `false` */
      disabled?: boolean;

      /**
       * This can be used to override the `HoverModeContext`'s hover time.
       */
      hoverTimeout?: number;

      /**
       * This can be used to override the `HoverModeContext`'s leave time.
       */
      leaveTimeout?: number;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface ControlledHoverModeOptions
      extends HoverModeConfigurationOptions {
      setVisible: UseStateSetter<boolean>;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface ControlledHoverModeImplementation {
      startShowFlow(id?: string | MouseEvent): void;
      startHideFlow(): void;
      clearVisibilityTimeout(): void;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface UncontrolledHoverModeOptions
      extends HoverModeConfigurationOptions {
      defaultVisible?: UseStateInitializer<boolean>;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface UncontrolledHoverModeImplementation
      extends ControlledHoverModeImplementation {
      visible: boolean;
      setVisible: UseStateSetter<boolean>;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface HoverModeImplementation
      extends ControlledHoverModeImplementation {
      visible?: boolean;
      setVisible?: UseStateSetter<boolean>;
    }

    /**
     * @remarks
     * \@since 2.8.0
     * \@since 5.0.0 This hook no longer returns `handlers` or
     * `stickyHandlers` and does not hide when an element on the page is clicked.
     * \@since 6.0.0 Requires passing the custom hover mode context to
     * work.
     */
    export function useHoverMode(
      options: ControlledHoverModeOptions
    ): ControlledHoverModeImplementation;
    export function useHoverMode(
      options: UncontrolledHoverModeOptions
    ): UncontrolledHoverModeImplementation;
    export function useHoverMode(
      options: ControlledHoverModeOptions | UncontrolledHoverModeOptions
    ): HoverModeImplementation {
      const {
        disabled,
        hoverTimeout: hoverTime,
        hoverTimeoutRef,
        leaveTimeout: leaveTime,
        leaveTimeoutRef,
        enableHoverMode,
        disableHoverMode,
        startDisableTimer,
        clearDisableTimer,
        setVisible: propSetVisible,
        defaultVisible = false,
      } = options as ControlledHoverModeOptions & UncontrolledHoverModeOptions;

      const state = useState(defaultVisible);
      let visible: boolean | undefined;
      let setVisible: UseStateSetter<boolean>;
      if (typeof propSetVisible !== "undefined") {
        setVisible = propSetVisible;
      } else {
        [visible, setVisible] = state;
      }

      const visibilityTimeout = useRef<number | undefined>();
      const clearVisibilityTimeout = useCallback(() => {
        window.clearTimeout(visibilityTimeout.current);
      }, []);

      // if the element is near the viewport edge, the mouseleave event might not
      // trigger correctly. for these cases, just clear any timeouts to be safe.
      // do not hide the visibility so that you can still inspect things in the
      // devtools
      useEffect(() => {
        if (disabled) {
          return;
        }

        const handler = (): void => {
          window.clearTimeout(visibilityTimeout.current);

          // might need to play with this more or make it configurable. if the mouse
          // leaves the window, you're _normally_ not interacting with the app
          // anymore and state should reset.
          disableHoverMode();
        };

        document.addEventListener("mouseleave", handler);
        return () => {
          document.removeEventListener("mouseleave", handler);
        };
      }, [disableHoverMode, disabled]);

      useEffect(() => {
        return () => {
          window.clearTimeout(visibilityTimeout.current);
        };
      }, []);

      return {
        visible,
        setVisible: setVisible === propSetVisible ? undefined : setVisible,
        startShowFlow: useCallback(
          (eventOrId) => {
            const hoverTimeout = hoverTime ?? hoverTimeoutRef.current;
            if (disabled || typeof hoverTimeout === "undefined") {
              return;
            }

            let id: string;
            if (typeof eventOrId === "string" || typeof eventOrId === "undefined") {
              id = eventOrId || "";
            } else {
              id = eventOrId.currentTarget.id;
            }

            clearDisableTimer();
            clearVisibilityTimeout();
            visibilityTimeout.current = window.setTimeout(() => {
              enableHoverMode(id);
              setVisible(true);
            }, hoverTimeout);
          },
          [
            clearDisableTimer,
            clearVisibilityTimeout,
            disabled,
            enableHoverMode,
            hoverTime,
            hoverTimeoutRef,
            setVisible,
          ]
        ),
        startHideFlow: useCallback(() => {
          if (disabled) {
            return;
          }

          startDisableTimer();
          clearVisibilityTimeout();
          visibilityTimeout.current = window.setTimeout(() => {
            setVisible(false);
          }, leaveTime ?? leaveTimeoutRef.current);
        }, [
          clearVisibilityTimeout,
          disabled,
          leaveTime,
          leaveTimeoutRef,
          setVisible,
          startDisableTimer,
        ]),
        clearVisibilityTimeout,
      };
    }