Skip to content

Commit 8db56ad

Browse files
committed
Add option spring config and over-drag with resistance (#5 and #7)
1 parent ac87bc6 commit 8db56ad

File tree

3 files changed

+64
-47
lines changed

3 files changed

+64
-47
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ class Example extends React.Component {
6464
| renderHeader | no | | Method for rendering non-scrollable header of bottom sheet. |
6565
| enabledGestureInteraction | no | `true` | Defines if bottom sheet could be scrollable by gesture. |
6666
| enabledManualSnapping | no | `true` | If `false` blocks snapping using `snapTo` method. |
67-
| enabledInnerScrolling | no | `true` | Defines whether it's possible to scroll inner content of bottom sheet.
68-
| callbackNode | no | | `reanimated` node which holds position of bottom sheet, where `0` it the highest snap point and `1` is the lowest.
67+
| enabledInnerScrolling | no | `true` | Defines whether it's possible to scroll inner content of bottom sheet. |
68+
| callbackNode | no | | `reanimated` node which holds position of bottom sheet, where `0` it the highest snap point and `1` is the lowest. |
69+
| overdragResistanceFactor | no | 0 | `Defines how violently sheet has to stopped while overdragging. 0 means no overdrag |
70+
| springConfig | no | `{ }` | Overrides config for spring animation |
6971

7072

7173
## Methods

index.js

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const {
3838
} = magic
3939

4040

41-
const { set, cond, onChange, block, eq, greaterOrEq, not, defined, max, add, and, Value, spring, or, divide, greaterThan, sub, event, diff, multiply, clockRunning, startClock, stopClock, decay, Clock, lessThan } = Animated
41+
const { set, cond, onChange, block, eq, greaterOrEq, sqrt, not, defined, max, add, and, Value, spring, or, divide, greaterThan, sub, event, diff, multiply, clockRunning, startClock, stopClock, decay, Clock, lessThan } = Animated
4242

4343

4444
function runDecay(clock, value, velocity, wasStartedFromBegin) {
@@ -109,45 +109,14 @@ function withDecaying(drag, state, decayClock, velocity, prevent) {
109109
])
110110
}
111111

112-
function runSpring(clock, value, velocity, dest, wasRun = 0, isManuallySet = 0) {
113-
const state = {
114-
finished: new Value(0),
115-
velocity: new Value(0),
116-
position: new Value(0),
117-
time: new Value(0),
118-
}
119-
120-
const config = {
121-
damping,
122-
mass,
123-
stiffness,
124-
overshootClamping,
125-
restSpeedThreshold,
126-
restDisplacementThreshold,
127-
toValue: new Value(0),
128-
}
129-
130-
return [
131-
cond(clockRunning(clock), 0, [
132-
set(state.finished, 0),
133-
set(state.velocity, velocity),
134-
set(state.position, value),
135-
set(config.toValue, dest),
136-
cond(and(wasRun, not(isManuallySet)), 0, startClock(clock)),
137-
cond(defined(wasRun), set(wasRun, 1)),
138-
]),
139-
spring(clock, state, config),
140-
cond(state.finished, stopClock(clock)),
141-
state.position,
142-
]
143-
}
144-
145112
export default class BottomSheetBehavior extends Component {
146113
static defaultProps = {
114+
overdragResistanceFactor: 0,
147115
initialSnap: 0,
148116
enabledImperativeSnapping: true,
149117
enabledGestureInteraction: true,
150-
enabledInnerScrolling: true
118+
enabledInnerScrolling: true,
119+
springConfig: {},
151120
};
152121

153122
decayClock = new Clock();
@@ -197,7 +166,7 @@ export default class BottomSheetBehavior extends Component {
197166
[
198167
cond(this.isManuallySetValue, stopClock(masterClock)),
199168
set(masterOffseted,
200-
runSpring(masterClock, masterOffseted, this.masterVelocity,
169+
this.runSpring(masterClock, masterOffseted, this.masterVelocity,
201170
cond(this.isManuallySetValue, this.manuallySetValue, this.snapPoint),
202171
wasRun, this.isManuallySetValue)
203172
),
@@ -210,15 +179,21 @@ export default class BottomSheetBehavior extends Component {
210179
set(this.preventDecaying, 1),
211180
set(masterOffseted, add(masterOffseted, sub(this.dragMasterY, prevMasterDrag))),
212181
set(prevMasterDrag, this.dragMasterY),
182+
set(wasRun, 0), // not sure about this move for cond-began
213183
cond(eq(this.panMasterState, State.BEGAN),
214-
[
215-
stopClock(this.masterClockForOverscroll),
216-
set(wasRun, 0),
217-
]
184+
stopClock(this.masterClockForOverscroll),
218185
),
219186
]
220187
),
221-
max(masterOffseted, snapPoints[0])
188+
cond(greaterThan(masterOffseted, snapPoints[0]), masterOffseted,
189+
max(
190+
multiply(
191+
sub(snapPoints[0], sqrt(add(1, sub(snapPoints[0], masterOffseted)))),
192+
props.overdragResistanceFactor
193+
),
194+
masterOffseted
195+
)
196+
)
222197
])
223198

224199
this.Y = this.withEnhancedLimits(
@@ -231,6 +206,40 @@ export default class BottomSheetBehavior extends Component {
231206
masterOffseted)
232207
}
233208

209+
runSpring(clock, value, velocity, dest, wasRun = 0, isManuallySet = 0) {
210+
const state = {
211+
finished: new Value(0),
212+
velocity: new Value(0),
213+
position: new Value(0),
214+
time: new Value(0),
215+
}
216+
217+
const config = {
218+
damping,
219+
mass,
220+
stiffness,
221+
overshootClamping,
222+
restSpeedThreshold,
223+
restDisplacementThreshold,
224+
toValue: new Value(0),
225+
...this.props.springConfig
226+
}
227+
228+
return [
229+
cond(clockRunning(clock), 0, [
230+
set(state.finished, 0),
231+
set(state.velocity, velocity),
232+
set(state.position, value),
233+
set(config.toValue, dest),
234+
cond(and(wasRun, not(isManuallySet)), 0, startClock(clock)),
235+
cond(defined(wasRun), set(wasRun, 1)),
236+
]),
237+
spring(clock, state, config),
238+
cond(state.finished, stopClock(clock)),
239+
state.position,
240+
]
241+
}
242+
234243
handleMasterPan = event([{
235244
nativeEvent: ({
236245
translationY: this.dragMasterY,
@@ -292,7 +301,7 @@ export default class BottomSheetBehavior extends Component {
292301
cond(and(eq(this.panState, State.END), not(wasEndedMasterAfterInner), not(eq(this.panMasterState, State.ACTIVE)), not(eq(this.panMasterState, State.BEGAN)), or(clockRunning(this.masterClockForOverscroll), not(wasRunMaster))), [
293302
// cond(justEndedIfEnded, set(this.masterVelocity, diff(val))),
294303
set(this.masterVelocity, cond(justEndedIfEnded, diff(val), this.velocity)),
295-
set(masterOffseted, runSpring(this.masterClockForOverscroll, masterOffseted, diff(val), this.snapPoint, wasRunMaster)),
304+
set(masterOffseted, this.runSpring(this.masterClockForOverscroll, masterOffseted, diff(val), this.snapPoint, wasRunMaster)),
296305
set(this.masterVelocity, 0)
297306
]),
298307
// cond(eq(this.panState, State.END), set(wasEndedMasterAfterInner, 0)),
@@ -463,8 +472,8 @@ export default class BottomSheetBehavior extends Component {
463472
<Animated.Code
464473
exec={onChange(this.tapState, cond(eq(this.tapState, State.BEGAN), stopClock(this.decayClock)))}/>
465474
{this.props.callbackNode &&
466-
<Animated.Code
467-
exec={onChange(this.translateMaster, set(this.props.callbackNode, divide(this.translateMaster, this.state.snapPoints[this.state.snapPoints.length - 1])))}/>}
475+
<Animated.Code
476+
exec={onChange(this.translateMaster, set(this.props.callbackNode, divide(this.translateMaster, this.state.snapPoints[this.state.snapPoints.length - 1])))}/>}
468477
</View>
469478
</Animated.View>
470479
</React.Fragment>

reanimated-bottom-sheet.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ declare module 'reanimated-bottom-sheet' {
2525
enabledInnerScrolling?: boolean;
2626

2727
/** Reanimated node which holds position of bottom sheet, where 1 it the highest snap point and 0 is the lowest. */
28-
callbackNode?: Animated.Node<any>;
28+
callbackNode?: Animated.Value<number>;
29+
30+
/** Defines how violently sheet has to stopped while overdragging. 0 means no overdrag. Defaults to 0. */
31+
overdragResistanceFactor?: number;
32+
33+
/** Overrides config for spring animation */
34+
springConfig?: object;
2935
}
3036

3137
export default class BottomSheet extends React.Component<BottomSheetProps> {

0 commit comments

Comments
 (0)