Skip to content

Commit 12ea702

Browse files
committed
Small refactor
1 parent b222458 commit 12ea702

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

src/components/panningViews/panView.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
Frame,
1919
PanDismissThreshold,
2020
getTranslation,
21-
getDismissVelocity
21+
getDismissVelocity,
22+
DEFAULT_THRESHOLD
2223
} from './panningUtil';
2324
export {PanningDirections, TranslationLock, PanDismissThreshold};
2425

@@ -67,12 +68,6 @@ interface Props extends PanViewProps {
6768
children?: React.ReactNode | React.ReactNode[];
6869
}
6970

70-
const DEFAULT_THRESHOLD: Required<PanDismissThreshold> = {
71-
velocity: 750,
72-
x: Constants.screenWidth / 4,
73-
y: Constants.screenHeight / 4
74-
};
75-
7671
const RETURN_ANIMATION_SPRING_CONFIG = {velocity: 300, damping: 15, stiffness: 300, mass: 0.8};
7772

7873
const PanView = (props: Props) => {

src/components/panningViews/panningUtil.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,21 @@ function getVelocityDirectionClamp(event: PanGestureHandlerEventExtra, direction
142142
return {x, y};
143143
}
144144

145+
function checkThresholds(event: PanGestureHandlerEventExtra,
146+
directions: PanningDirections[],
147+
velocity: number,
148+
threshold: Required<PanDismissThreshold>) {
149+
const velocityPassedThreshold = velocity > threshold.velocity;
150+
const xPassedThreshold =
151+
(directions.includes(PanningDirections.RIGHT) && event.translationX > threshold.x) ||
152+
(directions.includes(PanningDirections.LEFT) && -event.translationX > threshold.x);
153+
const yPassedThreshold =
154+
(directions.includes(PanningDirections.DOWN) && event.translationY > threshold.y) ||
155+
(directions.includes(PanningDirections.UP) && -event.translationY > threshold.y);
156+
157+
return {velocityPassedThreshold, xPassedThreshold, yPassedThreshold};
158+
}
159+
145160
/**
146161
* Will return undefined if should not dismiss
147162
*/
@@ -157,15 +172,12 @@ export function getDismissVelocity(event: PanGestureHandlerEventExtra,
157172
x: threshold?.x || DEFAULT_THRESHOLD.x,
158173
y: threshold?.y || DEFAULT_THRESHOLD.y
159174
};
160-
const allowedVelocity = getVelocityDirectionClamp(event, directions);
161-
const velocity = Math.sqrt(Math.pow(allowedVelocity.x, 2) + Math.pow(allowedVelocity.y, 2));
162-
const velocityPassedThreshold = velocity > _threshold.velocity;
163-
const xPassedThreshold =
164-
(directions.includes(PanningDirections.RIGHT) && event.translationX > _threshold.x) ||
165-
(directions.includes(PanningDirections.LEFT) && -event.translationX > _threshold.x);
166-
const yPassedThreshold =
167-
(directions.includes(PanningDirections.DOWN) && event.translationY > _threshold.y) ||
168-
(directions.includes(PanningDirections.UP) && -event.translationY > _threshold.y);
175+
const clampedVelocity = getVelocityDirectionClamp(event, directions);
176+
const velocity = Math.sqrt(Math.pow(clampedVelocity.x, 2) + Math.pow(clampedVelocity.y, 2));
177+
const {velocityPassedThreshold, xPassedThreshold, yPassedThreshold} = checkThresholds(event,
178+
directions,
179+
velocity,
180+
_threshold);
169181
if (velocityPassedThreshold || xPassedThreshold || yPassedThreshold) {
170182
let velocity: Partial<Frame> = {};
171183
if (velocityPassedThreshold) {

0 commit comments

Comments
 (0)