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

Commit f5df221

Browse files
authored
feat: export extra animation options for Pager (#747)
Sometimes I suppose a user might want to define own behavior of timing of spring and I want to make it possible with this PR
1 parent 3c219a4 commit f5df221

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,22 @@ Callback which is called when the swipe gesture starts, i.e. the user touches th
283283

284284
Callback which is called when the swipe gesture ends, i.e. the user lifts their finger from the screen after the swipe gesture.
285285

286+
##### `timingConfig`
287+
288+
Configuration object for the timing animation which occurs when tapping on tabs. Supported properties are:
289+
290+
- `duration` (`number`)
291+
292+
##### `springConfig`
293+
294+
Configuration object for the spring animation which occurs after swiping. Supported properties are:
295+
296+
- `damping` (`number`)
297+
- `mass` (`number`)
298+
- `stiffness` (`number`)
299+
- `restSpeedThreshold` (`number`)
300+
- `restDisplacementThreshold` (`number`)
301+
286302
##### `initialLayout`
287303

288304
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:

src/Pager.js

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,48 @@ export default class Pager<T: Route> extends React.Component<Props<T>> {
150150
) {
151151
this._swipeVelocityThreshold.setValue(this.props.swipeVelocityThreshold);
152152
}
153+
154+
if (prevProps.springConfig !== this.props.springConfig) {
155+
const { springConfig } = this.props;
156+
157+
this._springConfig.damping.setValue(
158+
springConfig.damping !== undefined
159+
? springConfig.damping
160+
: SPRING_CONFIG.damping
161+
);
162+
163+
this._springConfig.mass.setValue(
164+
springConfig.mass !== undefined ? springConfig.mass : SPRING_CONFIG.mass
165+
);
166+
167+
this._springConfig.stiffness.setValue(
168+
springConfig.stiffness !== undefined
169+
? springConfig.stiffness
170+
: SPRING_CONFIG.stiffness
171+
);
172+
173+
this._springConfig.restSpeedThreshold.setValue(
174+
springConfig.restSpeedThreshold !== undefined
175+
? springConfig.restSpeedThreshold
176+
: SPRING_CONFIG.restSpeedThreshold
177+
);
178+
179+
this._springConfig.restDisplacementThreshold.setValue(
180+
springConfig.restDisplacementThreshold !== undefined
181+
? springConfig.restDisplacementThreshold
182+
: SPRING_CONFIG.restDisplacementThreshold
183+
);
184+
}
185+
186+
if (prevProps.timingConfig !== this.props.timingConfig) {
187+
const { timingConfig } = this.props;
188+
189+
this._timingConfig.duration.setValue(
190+
timingConfig.duration !== undefined
191+
? timingConfig.duration
192+
: TIMING_CONFIG.duration
193+
);
194+
}
153195
}
154196

155197
// Clock used for tab transition animations
@@ -190,6 +232,43 @@ export default class Pager<T: Route> extends React.Component<Props<T>> {
190232
_swipeDistanceThreshold = new Value(this.props.swipeDistanceThreshold || 180);
191233
_swipeVelocityThreshold = new Value(this.props.swipeVelocityThreshold);
192234

235+
// Animation configuration
236+
_springConfig = {
237+
damping: new Value(
238+
this.props.springConfig.damping !== undefined
239+
? this.props.springConfig.damping
240+
: SPRING_CONFIG.damping
241+
),
242+
mass: new Value(
243+
this.props.springConfig.mass !== undefined
244+
? this.props.springConfig.mass
245+
: SPRING_CONFIG.mass
246+
),
247+
stiffness: new Value(
248+
this.props.springConfig.stiffness !== undefined
249+
? this.props.springConfig.stiffness
250+
: SPRING_CONFIG.stiffness
251+
),
252+
restSpeedThreshold: new Value(
253+
this.props.springConfig.restSpeedThreshold !== undefined
254+
? this.props.springConfig.restSpeedThreshold
255+
: SPRING_CONFIG.restSpeedThreshold
256+
),
257+
restDisplacementThreshold: new Value(
258+
this.props.springConfig.restDisplacementThreshold !== undefined
259+
? this.props.springConfig.restDisplacementThreshold
260+
: SPRING_CONFIG.restDisplacementThreshold
261+
),
262+
};
263+
264+
_timingConfig = {
265+
duration: new Value(
266+
this.props.timingConfig.duration !== undefined
267+
? this.props.timingConfig.duration
268+
: TIMING_CONFIG.duration
269+
),
270+
};
271+
193272
// The reason for using this value instead of simply passing `this._velocity`
194273
// into a spring animation is that we need to reverse it if we're using RTL mode.
195274
// Also, it's not possible to pass multiplied value there, because
@@ -310,14 +389,14 @@ export default class Pager<T: Route> extends React.Component<Props<T>> {
310389
spring(
311390
this._clock,
312391
{ ...state, velocity: this._initialVelocityForSpring },
313-
{ ...SPRING_CONFIG, toValue }
392+
{ ...SPRING_CONFIG, ...this._springConfig, toValue }
314393
),
315394
],
316395
// Otherwise use a timing animation for faster switching
317396
timing(
318397
this._clock,
319398
{ ...state, frameTime },
320-
{ ...TIMING_CONFIG, toValue }
399+
{ ...TIMING_CONFIG, ...this._timingConfig, toValue }
321400
)
322401
),
323402
cond(state.finished, [

src/TabView.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export default class TabView<T: Route> extends React.Component<
5353
swipeEnabled: true,
5454
lazy: false,
5555
removeClippedSubviews: false,
56+
springConfig: {},
57+
timingConfig: {},
5658
};
5759

5860
state = {
@@ -94,6 +96,8 @@ export default class TabView<T: Route> extends React.Component<
9496
swipeEnabled,
9597
swipeDistanceThreshold,
9698
swipeVelocityThreshold,
99+
timingConfig,
100+
springConfig,
97101
tabBarPosition,
98102
renderTabBar,
99103
renderScene,
@@ -112,6 +116,8 @@ export default class TabView<T: Route> extends React.Component<
112116
swipeEnabled={swipeEnabled}
113117
swipeDistanceThreshold={swipeDistanceThreshold}
114118
swipeVelocityThreshold={swipeVelocityThreshold}
119+
timingConfig={timingConfig}
120+
springConfig={springConfig}
115121
onSwipeStart={onSwipeStart}
116122
onSwipeEnd={onSwipeEnd}
117123
onIndexChange={this._jumpToIndex}

src/types.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,14 @@ export type PagerCommonProps = {|
4141
swipeVelocityThreshold?: number,
4242
onSwipeStart?: () => mixed,
4343
onSwipeEnd?: () => mixed,
44+
springConfig: {|
45+
damping?: number,
46+
mass?: number,
47+
stiffness?: number,
48+
restSpeedThreshold?: number,
49+
restDisplacementThreshold?: number,
50+
|},
51+
timingConfig: {|
52+
duration?: number,
53+
|},
4454
|};

0 commit comments

Comments
 (0)