MCPcopy Create free account
hub / github.com/react-dnd/react-dnd / hover

Function hover

examples/04 Sortable/Simple/Card.js:25–68  ·  view source on GitHub ↗
(props, monitor, component)

Source from the content-addressed store, hash-verified

23
24const cardTarget = {
25 hover(props, monitor, component) {
26 const dragIndex = monitor.getItem().index;
27 const hoverIndex = props.index;
28
29 // Don't replace items with themselves
30 if (dragIndex === hoverIndex) {
31 return;
32 }
33
34 // Determine rectangle on screen
35 const hoverBoundingRect = findDOMNode(component).getBoundingClientRect();
36
37 // Get vertical middle
38 const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
39
40 // Determine mouse position
41 const clientOffset = monitor.getClientOffset();
42
43 // Get pixels to the top
44 const hoverClientY = clientOffset.y - hoverBoundingRect.top;
45
46 // Only perform the move when the mouse has crossed half of the items height
47 // When dragging downwards, only move when the cursor is below 50%
48 // When dragging upwards, only move when the cursor is above 50%
49
50 // Dragging downwards
51 if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
52 return;
53 }
54
55 // Dragging upwards
56 if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
57 return;
58 }
59
60 // Time to actually perform the action
61 props.moveCard(dragIndex, hoverIndex);
62
63 // Note: we're mutating the monitor item here!
64 // Generally it's better to avoid mutations,
65 // but it's good here for the sake of performance
66 // to avoid expensive index searches.
67 monitor.getItem().index = hoverIndex;
68 },
69};
70
71@DropTarget(ItemTypes.CARD, cardTarget, connect => ({

Callers

nothing calls this directly

Calls 3

getItemMethod · 0.45
getClientOffsetMethod · 0.45
moveCardMethod · 0.45

Tested by

no test coverage detected