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

Function dragAndDrop

packages/@react-spectrum/s2/test/utils/dragAndDrop.ts:93–153  ·  view source on GitHub ↗
(
  source: Element,
  target: Element,
  options?: DragAndDropOptions
)

Source from the content-addressed store, hash-verified

91 * dispatching the full DragEvent lifecycle with synthetic events instead.
92 */
93export async function dragAndDrop(
94 source: Element,
95 target: Element,
96 options?: DragAndDropOptions
97): Promise<void> {
98 if (server.browser !== 'chromium') {
99 await userEvent.dragAndDrop(source, target, options);
100 // Reset WebKit's pointer state after the native drag before the next test.
101 await server.commands.mouseUp();
102 return;
103 }
104
105 let {dataTransfer, proxy} = createWritableDataTransfer();
106
107 let sourcePos = resolveClientPosition(source, options?.sourcePosition);
108 let targetPos = resolveClientPosition(target, options?.targetPosition);
109
110 // Patch DragEvent.prototype.dataTransfer to return our proxy whenever the
111 // event carries our real dataTransfer. This is the only reliable way to make
112 // effectAllowed/dropEffect writable in Chromium for synthetic events.
113 let origDesc = Object.getOwnPropertyDescriptor(DragEvent.prototype, 'dataTransfer')!;
114 Object.defineProperty(DragEvent.prototype, 'dataTransfer', {
115 get() {
116 let real = origDesc.get!.call(this);
117 return real === dataTransfer ? proxy : real;
118 },
119 configurable: true
120 });
121
122 try {
123 // Dispatch dragstart so useDrag populates the DataTransfer with items and sets effectAllowed.
124 source.dispatchEvent(
125 new DragEvent('dragstart', {dataTransfer, bubbles: true, cancelable: true, ...sourcePos})
126 );
127
128 // Allow React state updates from the dragstart handler to flush.
129 await new Promise(resolve => requestAnimationFrame(resolve));
130
131 target.dispatchEvent(
132 new DragEvent('dragenter', {dataTransfer, bubbles: true, cancelable: true, ...targetPos})
133 );
134 target.dispatchEvent(
135 new DragEvent('dragover', {dataTransfer, bubbles: true, cancelable: true, ...targetPos})
136 );
137
138 // In a real browser drag, the drop event only fires when dropEffect is not 'none'.
139 // useDrop sets dropEffect during dragenter/dragover based on getDropOperation.
140 if (proxy.dropEffect !== 'none') {
141 target.dispatchEvent(
142 new DragEvent('drop', {dataTransfer, bubbles: true, cancelable: true, ...targetPos})
143 );
144 }
145
146 source.dispatchEvent(
147 new DragEvent('dragend', {dataTransfer, bubbles: true, cancelable: true, ...targetPos})
148 );
149 } finally {
150 // Restore the original dataTransfer getter.

Callers 1

Calls 2

resolveClientPositionFunction · 0.85

Tested by

no test coverage detected