Skip to content
This repository was archived by the owner on Aug 18, 2025. It is now read-only.

Commit c8c8660

Browse files
authored
Adding onBeforeCapture Responder (#1605)
1 parent c943a8a commit c8c8660

24 files changed

Lines changed: 700 additions & 143 deletions

.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ module.exports = {
177177
// Having issues with this rule not working correctly
178178
'react/default-props-match-prop-types': 'off',
179179

180+
// Allowing functions to be passed as props
181+
'react/jsx-no-bind': 'off',
182+
180183
// Require // @flow at the top of files
181184
'flowtype/require-valid-file-annotation': [
182185
'error',

.size-snapshot.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
{
22
"dist/react-beautiful-dnd.js": {
3-
"bundled": 380407,
4-
"minified": 139714,
5-
"gzipped": 42033
3+
"bundled": 381298,
4+
"minified": 140093,
5+
"gzipped": 42104
66
},
77
"dist/react-beautiful-dnd.min.js": {
8-
"bundled": 322021,
9-
"minified": 115167,
10-
"gzipped": 34123
8+
"bundled": 322912,
9+
"minified": 115528,
10+
"gzipped": 34201
1111
},
1212
"dist/react-beautiful-dnd.esm.js": {
13-
"bundled": 241645,
14-
"minified": 126072,
15-
"gzipped": 32900,
13+
"bundled": 242417,
14+
"minified": 126431,
15+
"gzipped": 32987,
1616
"treeshaked": {
1717
"rollup": {
18-
"code": 21306,
18+
"code": 21303,
1919
"import_statements": 788
2020
},
2121
"webpack": {
22-
"code": 24963
22+
"code": 24960
2323
}
2424
}
2525
}

docs/guides/responders.md

Lines changed: 102 additions & 121 deletions
Large diffs are not rendered by default.

docs/guides/types.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ type DraggableId = Id;
1818
```js
1919
type Responders = {|
2020
// optional
21+
onBeforeCapture?: OnBeforeCaptureResponder,
2122
onBeforeDragStart?: OnBeforeDragStartResponder,
2223
onDragStart?: OnDragStartResponder,
2324
onDragUpdate?: OnDragUpdateResponder,
2425
// required
2526
onDragEnd: OnDragEndResponder,
2627
|};
2728

29+
type OnBeforeCaptureResponder = (before: BeforeCapture) => mixed;
2830
type OnBeforeDragStartResponder = (start: DragStart) => mixed;
2931
type OnDragStartResponder = (
3032
start: DragStart,
@@ -39,11 +41,16 @@ type OnDragEndResponder = (
3941
provided: ResponderProvided,
4042
) => mixed;
4143

44+
type BeforeCapture = {|
45+
draggableId: DraggableId,
46+
mode: MovementMode,
47+
|};
48+
4249
type DraggableRubric = {|
4350
draggableId: DraggableId,
4451
type: TypeId,
4552
source: DraggableLocation,
46-
|}
53+
|};
4754

4855
type DragStart = {|
4956
...DraggableRubric,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-beautiful-dnd",
3-
"version": "12.0.0",
3+
"version": "12.1.0-alpha.0",
44
"description": "Beautiful and accessible drag and drop for lists with React",
55
"author": "Alex Reardon <areardon@atlassian.com>",
66
"keywords": [

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ export type {
1919
DroppableId,
2020
DraggableRubric,
2121
MovementMode,
22+
BeforeCapture,
2223
DragStart,
2324
DragUpdate,
2425
DropResult,
2526
Direction,
2627
ResponderProvided,
2728
Announce,
2829
DraggableLocation,
30+
OnBeforeCaptureResponder,
2931
OnBeforeDragStartResponder,
3032
OnDragStartResponder,
3133
OnDragUpdateResponder,

src/state/action-creators.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ import type {
1212
Published,
1313
} from '../types';
1414

15+
export type BeforeInitialCaptureArgs = {|
16+
draggableId: DraggableId,
17+
movementMode: MovementMode,
18+
|};
19+
20+
export type BeforeInitialCaptureAction = {|
21+
type: 'BEFORE_INITIAL_CAPTURE',
22+
payload: BeforeInitialCaptureArgs,
23+
|};
24+
25+
export const beforeInitialCapture = (
26+
args: BeforeInitialCaptureArgs,
27+
): BeforeInitialCaptureAction => ({
28+
type: 'BEFORE_INITIAL_CAPTURE',
29+
payload: args,
30+
});
31+
1532
export type LiftArgs = {|
1633
// lifting with DraggableId rather than descriptor
1734
// as the descriptor might change after a drop is flushed
@@ -286,6 +303,7 @@ export const dropAnimationFinished = (): DropAnimationFinishedAction => ({
286303
});
287304

288305
export type Action =
306+
| BeforeInitialCaptureAction
289307
| LiftAction
290308
| InitialPublishAction
291309
| WhileDraggingPublishAction

src/state/middleware/lift.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { invariant } from '../../invariant';
33
import type { DimensionMarshal } from '../dimension-marshal/dimension-marshal-types';
44
import type { State, ScrollOptions, LiftRequest } from '../../types';
55
import type { MiddlewareStore, Action, Dispatch } from '../store-types';
6-
import { completeDrop, initialPublish, flush } from '../action-creators';
6+
import {
7+
completeDrop,
8+
initialPublish,
9+
flush,
10+
beforeInitialCapture,
11+
} from '../action-creators';
712
import validateDimensions from './util/validate-dimensions';
813

914
export default (marshal: DimensionMarshal) => ({
@@ -30,6 +35,11 @@ export default (marshal: DimensionMarshal) => ({
3035
// Removing any placeholders before we capture any starting dimensions
3136
dispatch(flush());
3237

38+
// Let consumers know we are just about to publish
39+
// We are only publishing a small amount of information as
40+
// things might change as a result of the onBeforeCapture callback
41+
dispatch(beforeInitialCapture({ draggableId: id, movementMode }));
42+
3343
// will communicate with the marshal to start requesting dimensions
3444
const scrollOptions: ScrollOptions = {
3545
shouldPublishImmediately: movementMode === 'SNAP',

src/state/middleware/responders/publisher.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ import type {
1111
Responders,
1212
ResponderProvided,
1313
Critical,
14+
BeforeCapture,
1415
DragImpact,
1516
DraggableLocation,
1617
Combine,
1718
DragStart,
1819
Announce,
1920
DragUpdate,
2021
MovementMode,
22+
DraggableId,
23+
OnBeforeCaptureResponder,
2124
OnBeforeDragStartResponder,
2225
OnDragStartResponder,
2326
OnDragUpdateResponder,
@@ -83,6 +86,24 @@ export default (getResponders: () => Responders, announce: Announce) => {
8386
const asyncMarshal: AsyncMarshal = getAsyncMarshal();
8487
let dragging: ?WhileDragging = null;
8588

89+
const beforeCapture = (draggableId: DraggableId, mode: MovementMode) => {
90+
invariant(
91+
!dragging,
92+
'Cannot fire onBeforeCapture as a drag start has already been published',
93+
);
94+
withTimings('onBeforeCapture', () => {
95+
// No use of screen reader for this responder
96+
const fn: ?OnBeforeCaptureResponder = getResponders().onBeforeCapture;
97+
if (fn) {
98+
const before: BeforeCapture = {
99+
draggableId,
100+
mode,
101+
};
102+
fn(before);
103+
}
104+
});
105+
};
106+
86107
const beforeStart = (critical: Critical, mode: MovementMode) => {
87108
invariant(
88109
!dragging,
@@ -221,6 +242,7 @@ export default (getResponders: () => Responders, announce: Announce) => {
221242
};
222243

223244
return {
245+
beforeCapture,
224246
beforeStart,
225247
start,
226248
update,

src/state/middleware/responders/responders-middleware.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ export default (
2626
return (store: MiddlewareStore) => (next: Dispatch) => (
2727
action: Action,
2828
): any => {
29+
if (action.type === 'BEFORE_INITIAL_CAPTURE') {
30+
publisher.beforeCapture(
31+
action.payload.draggableId,
32+
action.payload.movementMode,
33+
);
34+
return;
35+
}
36+
2937
if (action.type === 'INITIAL_PUBLISH') {
3038
const critical: Critical = action.payload.critical;
3139
publisher.beforeStart(critical, action.payload.movementMode);

0 commit comments

Comments
 (0)