Skip to content
This repository was archived by the owner on Nov 27, 2022. It is now read-only.

Commit 6198d42

Browse files
authored
feat: add springVelocityScale effect (#839)
Motivation In this PR I add springVelocityScale parameter as a config of Pager. Toss defines how much velocity of gesture impacts on an initial velocity of Pager. Found it very useful in bottom sheet. However, while writing this code I figured out interesting bug in Pager which led to fact that initial velocity of spring was always zero. It happened bc previously velocity was set only if clock was not running. However few lines above we made similar check and if clock was not running... then we were starting it, so as the result there was no chance that in current state clock was not running. I fixed it by moving starting clock few lines below. Test plan You may add springVelocityScale ={4} param to ScrollableTabBarExample
1 parent dd8a4f3 commit 6198d42

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,11 @@ Configuration object for the spring animation which occurs after swiping. Suppor
341341
- `restSpeedThreshold` (`number`)
342342
- `restDisplacementThreshold` (`number`)
343343

344-
##### `initialLayout`
344+
##### `springVelocityScale`
345+
346+
Number for determining how meaningful is gesture velocity for calculating initial velocity of spring animation. Defaults to `0`.
347+
348+
##### `initialLayout
345349

346350
Object containing the initial height and width of the screens. Passing this will improve the initial rendering performance. For most apps, this is a good default:
347351

src/Pager.tsx

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ const SPRING_CONFIG = {
9191
restSpeedThreshold: 0.01,
9292
};
9393

94+
const SPRING_VELOCITY_SCALE = 1;
95+
9496
const TIMING_CONFIG = {
9597
duration: 200,
9698
easing: Easing.out(Easing.cubic),
@@ -99,13 +101,15 @@ const TIMING_CONFIG = {
99101
export default class Pager<T extends Route> extends React.Component<Props<T>> {
100102
static defaultProps = {
101103
swipeVelocityImpact: SWIPE_VELOCITY_IMPACT,
104+
springVelocityScale: SPRING_VELOCITY_SCALE,
102105
};
103106

104107
componentDidUpdate(prevProps: Props<T>) {
105108
const {
106109
navigationState,
107110
layout,
108111
swipeVelocityImpact,
112+
springVelocityScale,
109113
springConfig,
110114
timingConfig,
111115
} = this.props;
@@ -138,12 +142,20 @@ export default class Pager<T extends Route> extends React.Component<Props<T>> {
138142

139143
if (prevProps.swipeVelocityImpact !== swipeVelocityImpact) {
140144
this.swipeVelocityImpact.setValue(
141-
swipeVelocityImpact != null
145+
swipeVelocityImpact !== undefined
142146
? swipeVelocityImpact
143147
: SWIPE_VELOCITY_IMPACT
144148
);
145149
}
146150

151+
if (prevProps.springVelocityScale !== springVelocityScale) {
152+
this.springVelocityScale.setValue(
153+
springVelocityScale !== undefined
154+
? springVelocityScale
155+
: SPRING_VELOCITY_SCALE
156+
);
157+
}
158+
147159
if (prevProps.springConfig !== springConfig) {
148160
this.springConfig.damping.setValue(
149161
springConfig.damping !== undefined
@@ -228,7 +240,15 @@ export default class Pager<T extends Route> extends React.Component<Props<T>> {
228240

229241
// Determines how relevant is a velocity while calculating next position while swiping
230242
private swipeVelocityImpact = new Value(
231-
this.props.swipeVelocityImpact || SWIPE_VELOCITY_IMPACT
243+
this.props.swipeVelocityImpact !== undefined
244+
? this.props.swipeVelocityImpact
245+
: SWIPE_VELOCITY_IMPACT
246+
);
247+
248+
private springVelocityScale = new Value(
249+
this.props.springVelocityScale !== undefined
250+
? this.props.springVelocityScale
251+
: SPRING_VELOCITY_SCALE
232252
);
233253

234254
// The position value represent the position of the pager on a scale of 0 - routes.length-1
@@ -381,7 +401,6 @@ export default class Pager<T extends Route> extends React.Component<Props<T>> {
381401
set(state.time, 0),
382402
set(state.finished, FALSE),
383403
set(this.index, index),
384-
startClock(this.clock),
385404
]),
386405
cond(
387406
this.isSwipeGesture,
@@ -390,8 +409,14 @@ export default class Pager<T extends Route> extends React.Component<Props<T>> {
390409
cond(
391410
not(clockRunning(this.clock)),
392411
I18nManager.isRTL
393-
? set(this.initialVelocityForSpring, multiply(-1, this.velocityX))
394-
: set(this.initialVelocityForSpring, this.velocityX)
412+
? set(
413+
this.initialVelocityForSpring,
414+
multiply(-1, this.velocityX, this.springVelocityScale)
415+
)
416+
: set(
417+
this.initialVelocityForSpring,
418+
multiply(this.velocityX, this.springVelocityScale)
419+
)
395420
),
396421
spring(
397422
this.clock,
@@ -406,6 +431,7 @@ export default class Pager<T extends Route> extends React.Component<Props<T>> {
406431
{ ...TIMING_CONFIG, ...this.timingConfig, toValue }
407432
)
408433
),
434+
cond(not(clockRunning(this.clock)), startClock(this.clock)),
409435
cond(state.finished, [
410436
// Reset values
411437
set(this.isSwipeGesture, FALSE),

src/TabView.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export default class TabView<T extends Route> extends React.Component<
117117
sceneContainerStyle,
118118
style,
119119
gestureHandlerProps,
120+
springVelocityScale,
120121
} = this.props;
121122
const { layout } = this.state;
122123

@@ -133,6 +134,7 @@ export default class TabView<T extends Route> extends React.Component<
133134
onSwipeStart={onSwipeStart}
134135
onSwipeEnd={onSwipeEnd}
135136
onIndexChange={this.jumpToIndex}
137+
springVelocityScale={springVelocityScale}
136138
removeClippedSubviews={removeClippedSubviews}
137139
gestureHandlerProps={gestureHandlerProps}
138140
>

src/types.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export type PagerCommonProps = {
4242
swipeVelocityImpact?: number;
4343
onSwipeStart?: () => void;
4444
onSwipeEnd?: () => void;
45+
springVelocityScale?: number;
4546
springConfig: {
4647
damping?: number;
4748
mass?: number;

0 commit comments

Comments
 (0)