All files / src/gestures use-hover-gesture.ts

93.75% Statements 15/16
85% Branches 17/20
100% Functions 3/3
100% Lines 15/15

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 4733x   33x 33x     33x             68x 18x         18x 18x       33x 34x 34x 34x 34x   34x               34x                
import { isMouseEvent } from "./utils/event-type"
import { EventInfo } from "../events/types"
import { AnimationType } from "../render/utils/types"
import { usePointerEvent } from "../events/use-pointer-event"
import { VisualElement } from "../render/types"
import { FeatureProps } from "../motion/features/types"
import { isDragActive } from "./drag/utils/lock"
 
function createHoverEvent(
    visualElement: VisualElement,
    isActive: boolean,
    callback?: (event: MouseEvent, info: EventInfo) => void
) {
    return (event: MouseEvent, info: EventInfo) => {
        Iif (!isMouseEvent(event) || isDragActive()) return
 
        /**
         * Ensure we trigger animations before firing event callback
         */
        visualElement.animationState?.setActive(AnimationType.Hover, isActive)
        callback?.(event, info)
    }
}
 
export function useHoverGesture({
    onHoverStart,
    onHoverEnd,
    whileHover,
    visualElement,
}: FeatureProps) {
    usePointerEvent(
        visualElement,
        "pointerenter",
        onHoverStart || whileHover
            ? createHoverEvent(visualElement, true, onHoverStart)
            : undefined
    )
 
    usePointerEvent(
        visualElement,
        "pointerleave",
        onHoverEnd || whileHover
            ? createHoverEvent(visualElement, false, onHoverEnd)
            : undefined
    )
}