Skip to content

Commit b028ad3

Browse files
authored
Constrain dragged item to list edges (#211)
* v2.3.3 * constrain drag to list boundaries * add min * rename prop * rename prop * update README
1 parent 879bcfd commit b028ad3

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ All props are spread onto underlying [FlatList](https://facebook.github.io/react
4848
| `activationDistance` | `number` | Distance a finger must travel before the gesture handler activates. Useful when using a draggable list within a TabNavigator so that the list does not capture navigator gestures. |
4949
| `layoutInvalidationKey` | `string` | Changing this value forces a remeasure of all item layouts. Useful if item size/ordering updates after initial mount. |
5050
| `onScrollOffsetChange` | `(offset: number) => void` | Called with scroll offset. Stand-in for `onScroll`. |
51+
| `dragItemOverflow` | `boolean` | If true, dragged item follows finger beyond list boundary. |
5152

5253
## Example
5354

src/index.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const {
4646
stopClock,
4747
spring,
4848
defined,
49+
min,
4950
max,
5051
debug
5152
} = Animated;
@@ -67,7 +68,8 @@ const defaultProps = {
6768
autoscrollSpeed: 100,
6869
animationConfig: defaultAnimationConfig as Animated.SpringConfig,
6970
scrollEnabled: true,
70-
activationDistance: 0
71+
activationDistance: 0,
72+
dragItemOverflow: false
7173
};
7274

7375
type DefaultProps = Readonly<typeof defaultProps>;
@@ -110,6 +112,7 @@ type Props<T> = Modify<
110112
debug?: boolean;
111113
layoutInvalidationKey?: string;
112114
onScrollOffsetChange?: (scrollOffset: number) => void;
115+
dragItemOverflow?: boolean;
113116
} & Partial<DefaultProps>
114117
>;
115118

@@ -177,7 +180,16 @@ class DraggableFlatList<T> extends React.Component<Props<T>, State> {
177180
add(this.scrollOffset, this.containerSize, scrollPositionTolerance),
178181
this.scrollViewSize
179182
);
180-
hoverAnim = sub(this.touchAbsolute, this.touchCellOffset);
183+
184+
hoverAnimUnconstrained = sub(this.touchAbsolute, this.touchCellOffset);
185+
hoverAnimConstrained = min(
186+
sub(this.containerSize, this.activeCellSize),
187+
max(0, this.hoverAnimUnconstrained)
188+
);
189+
190+
hoverAnim = this.props.dragItemOverflow
191+
? this.hoverAnimUnconstrained
192+
: this.hoverAnimConstrained;
181193
hoverMid = add(this.hoverAnim, divide(this.activeCellSize, 2));
182194
hoverOffset = add(this.hoverAnim, this.scrollOffset);
183195

0 commit comments

Comments
 (0)