MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useDrag1D

Function useDrag1D

packages/react-aria/src/utils/useDrag1D.ts:44–207  ·  view source on GitHub ↗
(props: UseDrag1DProps)

Source from the content-addressed store, hash-verified

42// It can also handle either a vertical or horizontal movement, but not both at the same time
43
44export function useDrag1D(props: UseDrag1DProps): HTMLAttributes<HTMLElement> {
45 console.warn(
46 'useDrag1D is deprecated, please use `useMove` instead https://react-spectrum.adobe.com/react-aria/useMove.html'
47 );
48 let {
49 containerRef,
50 reverse,
51 orientation,
52 onHover,
53 onDrag,
54 onPositionChange,
55 onIncrement,
56 onDecrement,
57 onIncrementToMax,
58 onDecrementToMin,
59 onCollapseToggle
60 } = props;
61 let getPosition = e => (orientation === 'horizontal' ? e.clientX : e.clientY);
62 let getNextOffset = (e: MouseEvent) => {
63 let containerOffset = getOffset(containerRef.current, reverse, orientation);
64 let mouseOffset = getPosition(e);
65 let nextOffset = reverse ? containerOffset - mouseOffset : mouseOffset - containerOffset;
66 return nextOffset;
67 };
68 let dragging = useRef(false);
69 let prevPosition = useRef(0);
70
71 // Keep track of the current handlers in a ref so that the events can access them.
72 let handlers = useRef({onPositionChange, onDrag});
73 // oxlint-disable-next-line react/react-compiler
74 handlers.current.onDrag = onDrag;
75 // oxlint-disable-next-line react/react-compiler
76 handlers.current.onPositionChange = onPositionChange;
77
78 let onMouseDragged = (e: MouseEvent) => {
79 e.preventDefault();
80 let nextOffset = getNextOffset(e);
81 if (!dragging.current) {
82 dragging.current = true;
83 if (handlers.current.onDrag) {
84 handlers.current.onDrag(true);
85 }
86 if (handlers.current.onPositionChange) {
87 handlers.current.onPositionChange(nextOffset);
88 }
89 }
90 if (prevPosition.current === nextOffset) {
91 return;
92 }
93 prevPosition.current = nextOffset;
94 if (onPositionChange) {
95 onPositionChange(nextOffset);
96 }
97 };
98
99 let onMouseUp = (e: MouseEvent) => {
100 let target = getEventTarget(e) as HTMLElement;
101 dragging.current = false;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected