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

Function onDragStart

packages/react-aria/src/dnd/useDrag.ts:125–233  ·  view source on GitHub ↗
(e: DragEvent)

Source from the content-addressed store, hash-verified

123 let modalityOnPointerDown = useRef<string>(null);
124
125 let onDragStart = (e: DragEvent) => {
126 if (e.defaultPrevented) {
127 return;
128 }
129
130 // Prevent the drag event from propagating to any parent draggables
131 e.stopPropagation();
132
133 // If this drag was initiated by a mobile screen reader (e.g. VoiceOver or TalkBack), enter virtual dragging mode.
134 if (modalityOnPointerDown.current === 'virtual') {
135 e.preventDefault();
136 startDragging(getEventTarget(e) as HTMLElement);
137 modalityOnPointerDown.current = null;
138 return;
139 }
140
141 if (typeof options.onDragStart === 'function') {
142 options.onDragStart({
143 type: 'dragstart',
144 x: e.clientX,
145 y: e.clientY
146 });
147 }
148
149 let items = options.getItems();
150 // Clear existing data (e.g. selected text on the page would be included in some browsers)
151 e.dataTransfer.clearData?.();
152 writeToDataTransfer(e.dataTransfer, items);
153
154 let allowed = DROP_OPERATION.all;
155 if (typeof options.getAllowedDropOperations === 'function') {
156 let allowedOperations = options.getAllowedDropOperations();
157 allowed = DROP_OPERATION.none;
158 for (let operation of allowedOperations) {
159 allowed |= DROP_OPERATION[operation] || DROP_OPERATION.none;
160 }
161 }
162
163 setGlobalAllowedDropOperations(allowed);
164 let effectAllowed = EFFECT_ALLOWED[allowed] || 'none';
165 e.dataTransfer.effectAllowed = effectAllowed === 'cancel' ? 'none' : effectAllowed;
166
167 // If there is a preview option, use it to render a custom preview image that will
168 // appear under the pointer while dragging. If not, the element itself is dragged by the browser.
169 if (typeof options.preview?.current === 'function') {
170 options.preview.current(items, (node, userX, userY) => {
171 if (!node) {
172 return;
173 }
174 // Compute the offset that the preview will appear under the mouse.
175 // If possible, this is based on the point the user clicked on the target.
176 // If the preview is much smaller, then just use the center point of the preview.
177 let size = node.getBoundingClientRect();
178 let rect = e.currentTarget.getBoundingClientRect();
179 let defaultX = e.clientX - rect.x;
180 let defaultY = e.clientY - rect.y;
181 if (defaultX > size.width || defaultY > size.height) {
182 defaultX = size.width / 2;

Callers 1

startDragFunction · 0.50

Calls 9

getEventTargetFunction · 0.90
writeToDataTransferFunction · 0.90
getItemsMethod · 0.80
clearDataMethod · 0.80
setDragImageMethod · 0.80
startDraggingFunction · 0.70
setDraggingFunction · 0.70
currentMethod · 0.45

Tested by

no test coverage detected