# All files / src/interaction utils.ts

31.38% Statements 43/137
100% Branches 0/0
0% Functions 0/5
31.38% Lines 43/137

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
    1381x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

     import type { KeyboardEvent, MouseEvent, TouchEvent } from "react";
    import { findSizingContainer } from "../positioning/utils.js";
    import type {
      ElementInteractionState,
      RippleState,
      RippleStyle,
    } from "./types.js";

    /** @internal */
    function calcHypotenuse(a: number, b: number): number {
      return Math.sqrt(a * a + b * b);
    }

    /**
     * Gets the current radius for a ripple based on the x and y page dimensions
     * as well as the size of the element.
     *
     * This is really just in a separate file so I can easily mock this and write
     * tests.
     *
     * @internal
     */
    function getRadius(
      x: number,
      y: number,
      offsetWidth: number,
      offsetHeight: number
    ): number {
      return Math.max(
        calcHypotenuse(x, y),
        calcHypotenuse(offsetWidth - x, y),
        calcHypotenuse(offsetWidth - x, offsetHeight - y),
        calcHypotenuse(x, offsetHeight - y)
      );
    }

    /** @internal */
    export function getRippleStyle(
      event:
        | MouseEvent<HTMLElement>
        | TouchEvent<HTMLElement>
        | KeyboardEvent<HTMLElement>,
      programmatic: boolean
    ): RippleStyle {
      const element =
        findSizingContainer(event.currentTarget) || event.currentTarget;
      const rect = element.getBoundingClientRect();
      const { left, top, height, width } = rect;

      let x: number;
      let y: number;
      if ("key" in event || programmatic) {
        x = width / 2;
        y = height / 2;
      } else {
        let pageX: number;
        let pageY: number;
        if ("touches" in event) {
          ({ pageX, pageY } = event.touches[0]);
        } else {
          ({ pageX, pageY } = event);
        }

        x = pageX - (left + window.pageXOffset);
        y = pageY - (top + window.pageYOffset);
      }

      const radius = getRadius(x, y, width, height);
      const size = radius * 2;

      return {
        left: x - radius,
        top: y - radius,
        height: size,
        width: size,
      };
    }

    /**
     * This is used to set the `exiting` state after a normal "touch" duration.
     * 300ms seemed like a good threshold since it is the majority of the scaling
     * duration (450ms)
     */
    const ENTER_DELAY = 300;

    /** @internal */
    export function releaseRipple(
      state: ElementInteractionState
    ): ElementInteractionState {
      // find the first non-exiting ripple which should now be released
      const i = state.ripples.findIndex((ripple) => !ripple.exiting);
      if (i === -1) {
        return state;
      }

      const ripples = state.ripples.slice();
      const ripple = ripples[i];
      ripples[i] = {
        ...ripple,
        exiting: ripple.entered || Date.now() - ripple.startTime > ENTER_DELAY,
      };
      return { pressed: false, ripples };
    }

    /** @internal */
    interface UpdateRipplesStateOptions {
      type: "entered" | "exited";
      state: ElementInteractionState;
      ripple: RippleState;
      holding: boolean;
    }

    /** @internal */
    export function updateRipplesState(
      options: UpdateRipplesStateOptions
    ): ElementInteractionState {
      const { type, ripple, state, holding } = options;

      const rippleIndex = state.ripples.findIndex((r) => r === ripple);
      if (rippleIndex === -1) {
        return state;
      }

      const ripples = state.ripples.slice();
      if (type === "exited") {
        ripples.splice(rippleIndex, 1);
      } else {
        const exiting = !holding || Date.now() - ripple.startTime > ENTER_DELAY;
        ripples[rippleIndex] = {
          ...ripple,
          exiting,
          entered: true,
        };
      }

      return { ...state, ripples };
    }