Skip to content

Commit 78508cc

Browse files
sebqqosdnk
authored andcommitted
feat: add bottom clamp option (#108)
1 parent ca3a1a8 commit 78508cc

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class Example extends React.Component {
6767
| enabledContentGestureInteraction | no | `true` | Defines if bottom sheet content could be scrollable by gesture. |
6868
| enabledContentTapInteraction | no | `true` | Defines whether bottom sheet content could be tapped. |
6969
| enabledManualSnapping | no | `true` | If `false` blocks snapping using `snapTo` method. |
70+
| enabledBottomClamp | no | `false` | If `true` block movement is clamped from bottom to minimal snappoint. |
7071
| enabledInnerScrolling | no | `true` | Defines whether it's possible to scroll inner content of bottom sheet. |
7172
| callbackNode | no | | `reanimated` node which holds position of bottom sheet, where `0` it the highest snap point and `1` is the lowest. |
7273
| contentPosition | no | | `reanimated` node which holds position of bottom sheet's content (in dp) |

src/index.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ type Props = {
4141
*/
4242
enabledContentTapInteraction?: boolean
4343

44+
/**
45+
* When true, clamp bottom position to first snapPoint.
46+
*/
47+
enabledBottomClamp?: boolean
48+
4449
/**
4550
* If false blocks snapping using snapTo method. Defaults to true.
4651
*/
@@ -286,6 +291,7 @@ export default class BottomSheetBehavior extends React.Component<Props, State> {
286291
initialSnap: 0,
287292
enabledImperativeSnapping: true,
288293
enabledGestureInteraction: true,
294+
enabledBottomClamp: false,
289295
enabledHeaderGestureInteraction: true,
290296
enabledContentGestureInteraction: true,
291297
enabledContentTapInteraction: true,
@@ -317,6 +323,7 @@ export default class BottomSheetBehavior extends React.Component<Props, State> {
317323
private tapRef: React.RefObject<TapGestureHandler>
318324
private snapPoint: Animated.Node<number>
319325
private Y: Animated.Node<number>
326+
private clampingValue: Animated.Value<number> = new Value(0)
320327
private onOpenStartValue: Animated.Value<number> = new Value(0)
321328
private onOpenEndValue: Animated.Value<number> = new Value(0)
322329
private onCloseStartValue: Animated.Value<number> = new Value(1)
@@ -358,6 +365,10 @@ export default class BottomSheetBehavior extends React.Component<Props, State> {
358365
// current snap point desired
359366
this.snapPoint = currentSnapPoint()
360367

368+
if (props.enabledBottomClamp) {
369+
this.clampingValue.setValue(snapPoints[snapPoints.length - 1])
370+
}
371+
361372
const masterClock = new Clock()
362373
const prevMasterDrag = new Value(0)
363374
const wasRun: Animated.Value<number> = new Value(0)
@@ -406,7 +417,14 @@ export default class BottomSheetBehavior extends React.Component<Props, State> {
406417
),
407418
cond(
408419
greaterThan(masterOffseted, snapPoints[0]),
409-
masterOffseted,
420+
cond(
421+
and(
422+
props.enabledBottomClamp ? 1 : 0,
423+
greaterThan(masterOffseted, this.clampingValue)
424+
),
425+
this.clampingValue,
426+
masterOffseted
427+
),
410428
max(
411429
multiply(
412430
sub(
@@ -432,6 +450,13 @@ export default class BottomSheetBehavior extends React.Component<Props, State> {
432450
)
433451
}
434452

453+
componentDidUpdate(_prevProps: Props, prevState: State) {
454+
const { snapPoints } = this.state
455+
if (this.props.enabledBottomClamp && snapPoints !== prevState.snapPoints) {
456+
this.clampingValue.setValue(snapPoints[snapPoints.length - 1])
457+
}
458+
}
459+
435460
private runSpring(
436461
clock: Animated.Clock,
437462
value: Animated.Value<number>,

0 commit comments

Comments
 (0)