All files / src/gestures/drag use-drag.ts

100% Statements 11/11
100% Branches 2/2
100% Functions 4/4
100% Lines 10/10

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 2833x 33x 33x               33x 296x   148x 74x         148x 35x         148x    
import { useEffect } from "react"
import { VisualElementDragControls } from "./VisualElementDragControls"
import { useConstant } from "../../utils/use-constant"
import { FeatureProps } from "../../motion/features/types"
 
/**
 * A hook that allows an element to be dragged.
 *
 * @internal
 */
export function useDrag(props: FeatureProps) {
    const { dragControls: groupDragControls, visualElement } = props
 
    const dragControls = useConstant(
        () => new VisualElementDragControls(visualElement)
    )
 
    // If we've been provided a DragControls for manual control over the drag gesture,
    // subscribe this component to it on mount.
    useEffect(
        () => groupDragControls && groupDragControls.subscribe(dragControls),
        [dragControls, groupDragControls]
    )
 
    // Apply the event listeners to the element
    useEffect(() => dragControls.addListeners(), [dragControls])
}