Skip to content

Commit

Permalink
feat: add velocity props to improve dismiss behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremybarbet committed Apr 14, 2020
1 parent cb63738 commit 9c6b85a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
12 changes: 11 additions & 1 deletion docs/PROPSMETHODS.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,22 @@ A number that determines the momentum of the scroll required.

### `threshold`

Number of pixels that the user must drag the modal before snapping to another position.
Number of pixels that the user must pass to be able to close the modal.

| Type | Required | Default |
| -------- | -------- | ------- |
| number | No | `150` |

### `velocity`

Number of pixels the user has to pan down fast to close the modal (e.g. When the user is reaching the top of the ScrollView and is immediately panning down to dismiss Modalize. If it reaches the velocity threshold then it instantly closes the modal). The highest the number it is, the more the user will need to pan down and make an important gesture to dismiss the modal.

?> If `velocity` is defined, then the `threshold` props is not used. If you want to use the `threshold` method instead of the `velocity` defines it to `velocity={undefined}`.

| Type | Required | Default |
| ------------------ | -------- | ------- |
| number | undefined | No | `2800` |

### `adjustToContentHeight`

Shrink the modal to your content's height.
Expand Down
15 changes: 9 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export class Modalize<FlatListItem = any, SectionListItem = any> extends React.C
timing: { duration: 280, easing: Easing.ease },
},
dragToss: 0.05,
threshold: 150,
threshold: 120,
velocity: 2800,
};

private snaps: number[] = [];
Expand Down Expand Up @@ -482,11 +483,17 @@ export class Modalize<FlatListItem = any, SectionListItem = any> extends React.C
onPositionChange,
panGestureAnimatedValue,
threshold,
velocity,
} = this.props;
const { timing } = closeAnimationConfig!;
const { lastSnap, modalHeight, overlay } = this.state;
const { velocityY, translationY } = nativeEvent;
const enableBounces = this.beginScrollYValue > 0 || translationY < 0;
const thresholdProps =
translationY > (adjustToContentHeight ? (modalHeight || 0) / 3 : threshold);
const closeThreshold = velocity
? this.beginScrollYValue <= 20 && velocityY >= velocity
: thresholdProps && this.beginScrollYValue === 0;

this.setState({ enableBounces });

Expand Down Expand Up @@ -514,11 +521,7 @@ export class Modalize<FlatListItem = any, SectionListItem = any> extends React.C
}
}
});
} else if (
translationY > (adjustToContentHeight ? (modalHeight || 0) / 3 : threshold) &&
this.beginScrollYValue === 0 &&
!alwaysOpen
) {
} else if (closeThreshold && !alwaysOpen) {
this.willCloseModalize = true;
this.close();
}
Expand Down
10 changes: 8 additions & 2 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,17 @@ export interface IProps<FlatListItem = any, SectionListItem = any> {
dragToss: number;

/**
* Number of pixels that the user must drag the modal before snapping to another position.
* @default 150
* Number of pixels that the user must pass to be able to close the modal.
* @default 120
*/
threshold: number;

/**
* Number of pixels the user has to pan down fast to close the modal.
* @default 2800
*/
velocity: number | undefined;

/**
* Shrink the modal to your content's height.
* @default false
Expand Down

0 comments on commit 9c6b85a

Please sign in to comment.