All files / src/events use-pointer-event.ts

93.75% Statements 15/16
87.5% Branches 7/8
100% Functions 3/3
93.75% Lines 15/16

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  34x 34x 34x                                 34x                     34x               421x 32x 389x 16x 373x 373x           34x           191x               34x           230x              
import { RefObject } from "react"
import { useDomEvent, addDomEvent } from "./use-dom-event"
import { wrapHandler, EventListenerWithPointInfo } from "./event-info"
import {
    supportsPointerEvents,
    supportsTouchEvents,
    supportsMouseEvents,
} from "./utils"
 
interface PointerNameMap {
    pointerdown: string
    pointermove: string
    pointerup: string
    pointercancel: string
    pointerover?: string
    pointerout?: string
    pointerenter?: string
    pointerleave?: string
}
 
const mouseEventNames: PointerNameMap = {
    pointerdown: "mousedown",
    pointermove: "mousemove",
    pointerup: "mouseup",
    pointercancel: "mousecancel",
    pointerover: "mouseover",
    pointerout: "mouseout",
    pointerenter: "mouseenter",
    pointerleave: "mouseleave",
}
 
const touchEventNames: PointerNameMap = {
    pointerdown: "touchstart",
    pointermove: "touchmove",
    pointerup: "touchend",
    pointercancel: "touchcancel",
}
 
function getPointerEventName(name: string): string {
    if (supportsPointerEvents()) {
        return name
    } else if (supportsTouchEvents()) {
        return touchEventNames[name]
    } else Eif (supportsMouseEvents()) {
        return mouseEventNames[name]
    }
 
    return name
}
 
export function addPointerEvent(
    target: EventTarget,
    eventName: string,
    handler: EventListenerWithPointInfo,
    options?: AddEventListenerOptions
) {
    return addDomEvent(
        target,
        getPointerEventName(eventName),
        wrapHandler(handler, eventName === "pointerdown"),
        options
    )
}
 
export function usePointerEvent(
    ref: RefObject<Element>,
    eventName: string,
    handler?: EventListenerWithPointInfo | undefined,
    options?: AddEventListenerOptions
) {
    return useDomEvent(
        ref,
        getPointerEventName(eventName),
        handler && wrapHandler(handler, eventName === "pointerdown"),
        options
    )
}