-
-
Notifications
You must be signed in to change notification settings - Fork 799
/
Copy pathBottomSheet.tsx
1945 lines (1801 loc) · 58.2 KB
/
BottomSheet.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import invariant from 'invariant';
import React, {
useMemo,
useCallback,
forwardRef,
useImperativeHandle,
memo,
useEffect,
} from 'react';
import { Platform } from 'react-native';
import { State } from 'react-native-gesture-handler';
import Animated, {
useAnimatedReaction,
useSharedValue,
useAnimatedStyle,
useDerivedValue,
runOnJS,
interpolate,
Extrapolation,
runOnUI,
cancelAnimation,
useWorkletCallback,
type WithSpringConfig,
type WithTimingConfig,
} from 'react-native-reanimated';
// import BottomSheetDebugView from '../bottomSheetDebugView';
import {
ANIMATION_SOURCE,
ANIMATION_STATE,
KEYBOARD_BEHAVIOR,
KEYBOARD_BLUR_BEHAVIOR,
KEYBOARD_INPUT_MODE,
KEYBOARD_STATE,
SCROLLABLE_STATE,
SHEET_STATE,
SNAP_POINT_TYPE,
} from '../../constants';
import {
BottomSheetInternalProvider,
BottomSheetProvider,
} from '../../contexts';
import {
useAnimatedSnapPoints,
useKeyboard,
usePropsValidator,
useReactiveSharedValue,
useScrollable,
} from '../../hooks';
import type { BottomSheetMethods, Insets } from '../../types';
import {
animate,
getKeyboardAnimationConfigs,
normalizeSnapPoint,
print,
} from '../../utilities';
import BottomSheetBackdropContainer from '../bottomSheetBackdropContainer';
import BottomSheetBackgroundContainer from '../bottomSheetBackgroundContainer';
import BottomSheetContainer from '../bottomSheetContainer';
import BottomSheetDraggableView from '../bottomSheetDraggableView';
import BottomSheetFooterContainer from '../bottomSheetFooterContainer/BottomSheetFooterContainer';
import BottomSheetGestureHandlersProvider from '../bottomSheetGestureHandlersProvider';
import BottomSheetHandleContainer from '../bottomSheetHandleContainer';
import {
DEFAULT_ACCESSIBILITY_LABEL,
DEFAULT_ACCESSIBILITY_ROLE,
DEFAULT_ACCESSIBLE,
DEFAULT_ANIMATE_ON_MOUNT,
DEFAULT_DYNAMIC_SIZING,
DEFAULT_ENABLE_CONTENT_PANNING_GESTURE,
DEFAULT_ENABLE_OVER_DRAG,
DEFAULT_ENABLE_PAN_DOWN_TO_CLOSE,
DEFAULT_KEYBOARD_BEHAVIOR,
DEFAULT_KEYBOARD_BLUR_BEHAVIOR,
DEFAULT_KEYBOARD_INPUT_MODE,
DEFAULT_OVER_DRAG_RESISTANCE_FACTOR,
INITIAL_CONTAINER_HEIGHT,
INITIAL_CONTAINER_OFFSET,
INITIAL_HANDLE_HEIGHT,
INITIAL_POSITION,
INITIAL_SNAP_POINT,
INITIAL_VALUE,
} from './constants';
import { styles } from './styles';
import type { AnimateToPositionType, BottomSheetProps } from './types';
Animated.addWhitelistedUIProps({
decelerationRate: true,
});
type BottomSheet = BottomSheetMethods;
const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
function BottomSheet(props, ref) {
//#region extract props
const {
// animations configurations
animationConfigs: _providedAnimationConfigs,
// configurations
index: _providedIndex = 0,
snapPoints: _providedSnapPoints,
animateOnMount = DEFAULT_ANIMATE_ON_MOUNT,
enableContentPanningGesture = DEFAULT_ENABLE_CONTENT_PANNING_GESTURE,
enableHandlePanningGesture,
enableOverDrag = DEFAULT_ENABLE_OVER_DRAG,
enablePanDownToClose = DEFAULT_ENABLE_PAN_DOWN_TO_CLOSE,
enableDynamicSizing = DEFAULT_DYNAMIC_SIZING,
overDragResistanceFactor = DEFAULT_OVER_DRAG_RESISTANCE_FACTOR,
// styles
style: _providedStyle,
containerStyle: _providedContainerStyle,
backgroundStyle: _providedBackgroundStyle,
handleStyle: _providedHandleStyle,
handleIndicatorStyle: _providedHandleIndicatorStyle,
// hooks
gestureEventsHandlersHook,
// keyboard
keyboardBehavior = DEFAULT_KEYBOARD_BEHAVIOR,
keyboardBlurBehavior = DEFAULT_KEYBOARD_BLUR_BEHAVIOR,
android_keyboardInputMode = DEFAULT_KEYBOARD_INPUT_MODE,
// layout
containerHeight: _providedContainerHeight,
containerOffset: _providedContainerOffset,
topInset = 0,
bottomInset = 0,
maxDynamicContentSize,
// animated callback shared values
animatedPosition: _providedAnimatedPosition,
animatedIndex: _providedAnimatedIndex,
// gestures
simultaneousHandlers: _providedSimultaneousHandlers,
waitFor: _providedWaitFor,
activeOffsetX: _providedActiveOffsetX,
activeOffsetY: _providedActiveOffsetY,
failOffsetX: _providedFailOffsetX,
failOffsetY: _providedFailOffsetY,
// callbacks
onChange: _providedOnChange,
onClose: _providedOnClose,
onAnimate: _providedOnAnimate,
// private
$modal = false,
detached = false,
// components
handleComponent,
backdropComponent,
backgroundComponent,
footerComponent,
children,
// accessibility
accessible: _providedAccessible = DEFAULT_ACCESSIBLE,
accessibilityLabel:
_providedAccessibilityLabel = DEFAULT_ACCESSIBILITY_LABEL,
accessibilityRole:
_providedAccessibilityRole = DEFAULT_ACCESSIBILITY_ROLE,
} = props;
//#endregion
//#region validate props
if (__DEV__) {
// biome-ignore lint/correctness/useHookAtTopLevel: used in development only.
usePropsValidator({
index: _providedIndex,
snapPoints: _providedSnapPoints,
enableDynamicSizing,
topInset,
bottomInset,
});
}
//#endregion
//#region layout variables
/**
* This variable is consider an internal variable,
* that will be used conditionally in `animatedContainerHeight`
*/
const _animatedContainerHeight = useReactiveSharedValue(
_providedContainerHeight ?? INITIAL_CONTAINER_HEIGHT
);
/**
* This is a conditional variable, where if the `BottomSheet` is used
* in a modal, then it will subset vertical insets (top+bottom) from
* provided container height.
*/
const animatedContainerHeight = useDerivedValue(() => {
const verticalInset = topInset + bottomInset;
return $modal
? _animatedContainerHeight.value - verticalInset
: _animatedContainerHeight.value;
}, [topInset, bottomInset, $modal, _animatedContainerHeight.value]);
const animatedContainerOffset = useReactiveSharedValue(
_providedContainerOffset ?? INITIAL_CONTAINER_OFFSET
) as Animated.SharedValue<Insets>;
const animatedHandleHeight = useReactiveSharedValue<number>(
INITIAL_HANDLE_HEIGHT
);
const animatedFooterHeight = useSharedValue(0);
const animatedContentHeight = useSharedValue(INITIAL_CONTAINER_HEIGHT);
const [animatedSnapPoints, animatedDynamicSnapPointIndex] =
useAnimatedSnapPoints(
_providedSnapPoints,
animatedContainerHeight,
animatedContentHeight,
animatedHandleHeight,
enableDynamicSizing,
maxDynamicContentSize
);
const animatedHighestSnapPoint = useDerivedValue(
() => animatedSnapPoints.value[animatedSnapPoints.value.length - 1],
[animatedSnapPoints.value]
);
const animatedClosedPosition = useDerivedValue(() => {
let closedPosition = animatedContainerHeight.value;
if ($modal || detached) {
closedPosition = animatedContainerHeight.value + bottomInset;
}
return closedPosition;
}, [animatedContainerHeight.value, $modal, detached, bottomInset]);
const animatedSheetHeight = useDerivedValue(
() => animatedContainerHeight.value - animatedHighestSnapPoint.value,
[animatedContainerHeight.value, animatedHighestSnapPoint.value]
);
const animatedCurrentIndex = useReactiveSharedValue(
animateOnMount ? -1 : _providedIndex
);
const animatedPosition = useSharedValue(INITIAL_POSITION);
const animatedNextPosition = useSharedValue(INITIAL_VALUE);
const animatedNextPositionIndex = useSharedValue(0);
// conditional
const isAnimatedOnMount = useSharedValue(false);
const isContentHeightFixed = useSharedValue(false);
const isLayoutCalculated = useDerivedValue(() => {
let isContainerHeightCalculated = false;
//container height was provided.
if (
_providedContainerHeight !== null ||
_providedContainerHeight !== undefined
) {
isContainerHeightCalculated = true;
}
// container height did set.
if (animatedContainerHeight.value !== INITIAL_CONTAINER_HEIGHT) {
isContainerHeightCalculated = true;
}
let isHandleHeightCalculated = false;
// handle component is null.
if (handleComponent === null) {
animatedHandleHeight.value = 0;
isHandleHeightCalculated = true;
}
// handle height did set.
if (animatedHandleHeight.value !== INITIAL_HANDLE_HEIGHT) {
isHandleHeightCalculated = true;
}
let isSnapPointsNormalized = false;
// the first snap point did normalized
if (animatedSnapPoints.value[0] !== INITIAL_SNAP_POINT) {
isSnapPointsNormalized = true;
}
return (
isContainerHeightCalculated &&
isHandleHeightCalculated &&
isSnapPointsNormalized
);
}, [
_providedContainerHeight,
animatedContainerHeight.value,
animatedHandleHeight,
animatedSnapPoints.value,
handleComponent,
]);
const isInTemporaryPosition = useSharedValue(false);
const isForcedClosing = useSharedValue(false);
const animatedContainerHeightDidChange = useSharedValue(false);
// gesture
const animatedContentGestureState = useSharedValue<State>(
State.UNDETERMINED
);
const animatedHandleGestureState = useSharedValue<State>(
State.UNDETERMINED
);
//#endregion
//#region hooks variables
// scrollable variables
const {
animatedScrollableType,
animatedScrollableContentOffsetY,
animatedScrollableOverrideState,
isScrollableRefreshable,
setScrollableRef,
removeScrollableRef,
} = useScrollable();
// keyboard
const {
state: animatedKeyboardState,
height: animatedKeyboardHeight,
animationDuration: keyboardAnimationDuration,
animationEasing: keyboardAnimationEasing,
shouldHandleKeyboardEvents,
} = useKeyboard();
const animatedKeyboardHeightInContainer = useSharedValue(0);
//#endregion
//#region state/dynamic variables
// states
const animatedAnimationState = useSharedValue(ANIMATION_STATE.UNDETERMINED);
const animatedAnimationSource = useSharedValue<ANIMATION_SOURCE>(
ANIMATION_SOURCE.MOUNT
);
const animatedSheetState = useDerivedValue(() => {
// closed position = position >= container height
if (animatedPosition.value >= animatedClosedPosition.value) {
return SHEET_STATE.CLOSED;
}
// extended position = container height - sheet height
const extendedPosition =
animatedContainerHeight.value - animatedSheetHeight.value;
if (animatedPosition.value === extendedPosition) {
return SHEET_STATE.EXTENDED;
}
// extended position with keyboard =
// container height - (sheet height + keyboard height in root container)
const keyboardHeightInContainer = animatedKeyboardHeightInContainer.value;
const extendedPositionWithKeyboard = Math.max(
0,
animatedContainerHeight.value -
(animatedSheetHeight.value + keyboardHeightInContainer)
);
// detect if keyboard is open and the sheet is in temporary position
if (
keyboardBehavior === KEYBOARD_BEHAVIOR.interactive &&
isInTemporaryPosition.value &&
animatedPosition.value === extendedPositionWithKeyboard
) {
return SHEET_STATE.EXTENDED;
}
// fill parent = 0
if (animatedPosition.value === 0) {
return SHEET_STATE.FILL_PARENT;
}
// detect if position is below extended point
if (animatedPosition.value < extendedPosition) {
return SHEET_STATE.OVER_EXTENDED;
}
return SHEET_STATE.OPENED;
}, [
animatedClosedPosition,
animatedContainerHeight,
animatedKeyboardHeightInContainer,
animatedPosition,
animatedSheetHeight,
isInTemporaryPosition,
keyboardBehavior,
]);
const animatedScrollableState = useDerivedValue(() => {
/**
* if user had disabled content panning gesture, then we unlock
* the scrollable state.
*/
if (!enableContentPanningGesture) {
return SCROLLABLE_STATE.UNLOCKED;
}
/**
* if scrollable override state is set, then we just return its value.
*/
if (
animatedScrollableOverrideState.value !== SCROLLABLE_STATE.UNDETERMINED
) {
return animatedScrollableOverrideState.value;
}
/**
* if sheet state is fill parent, then unlock scrolling
*/
if (animatedSheetState.value === SHEET_STATE.FILL_PARENT) {
return SCROLLABLE_STATE.UNLOCKED;
}
/**
* if sheet state is extended, then unlock scrolling
*/
if (animatedSheetState.value === SHEET_STATE.EXTENDED) {
return SCROLLABLE_STATE.UNLOCKED;
}
/**
* if keyboard is shown and sheet is animating
* then we do not lock the scrolling to not lose
* current scrollable scroll position.
*/
if (
animatedKeyboardState.value === KEYBOARD_STATE.SHOWN &&
animatedAnimationState.value === ANIMATION_STATE.RUNNING
) {
return SCROLLABLE_STATE.UNLOCKED;
}
return SCROLLABLE_STATE.LOCKED;
}, [
enableContentPanningGesture,
animatedAnimationState.value,
animatedKeyboardState.value,
animatedScrollableOverrideState.value,
animatedSheetState.value,
]);
// dynamic
const animatedContentHeightMax = useDerivedValue(() => {
const keyboardHeightInContainer = animatedKeyboardHeightInContainer.value;
const handleHeight = Math.max(0, animatedHandleHeight.value);
let contentHeight = animatedSheetHeight.value - handleHeight;
if (
keyboardBehavior === KEYBOARD_BEHAVIOR.extend &&
animatedKeyboardState.value === KEYBOARD_STATE.SHOWN
) {
contentHeight = contentHeight - keyboardHeightInContainer;
} else if (
keyboardBehavior === KEYBOARD_BEHAVIOR.fillParent &&
isInTemporaryPosition.value
) {
if (animatedKeyboardState.value === KEYBOARD_STATE.SHOWN) {
contentHeight =
animatedContainerHeight.value -
handleHeight -
keyboardHeightInContainer;
} else {
contentHeight = animatedContainerHeight.value - handleHeight;
}
} else if (
keyboardBehavior === KEYBOARD_BEHAVIOR.interactive &&
isInTemporaryPosition.value
) {
const contentWithKeyboardHeight =
contentHeight + keyboardHeightInContainer;
if (animatedKeyboardState.value === KEYBOARD_STATE.SHOWN) {
if (
keyboardHeightInContainer + animatedSheetHeight.value >
animatedContainerHeight.value
) {
contentHeight =
animatedContainerHeight.value -
keyboardHeightInContainer -
handleHeight;
}
} else if (
contentWithKeyboardHeight + handleHeight >
animatedContainerHeight.value
) {
contentHeight = animatedContainerHeight.value - handleHeight;
} else {
contentHeight = contentWithKeyboardHeight;
}
}
/**
* before the container is measured, `contentHeight` value will be below zero,
* which will lead to freeze the scrollable.
*
* @link (https://github.com/gorhom/react-native-bottom-sheet/issues/470)
*/
return Math.max(contentHeight, 0);
}, [
animatedContainerHeight,
animatedHandleHeight,
animatedKeyboardHeightInContainer,
animatedKeyboardState,
animatedSheetHeight,
isInTemporaryPosition,
keyboardBehavior,
]);
const animatedIndex = useDerivedValue(() => {
const adjustedSnapPoints = animatedSnapPoints.value.slice().reverse();
const adjustedSnapPointsIndexes = animatedSnapPoints.value
.slice()
.map((_, index: number) => index)
.reverse();
/**
* we add the close state index `-1`
*/
adjustedSnapPoints.push(animatedContainerHeight.value);
adjustedSnapPointsIndexes.push(-1);
const currentIndex = isLayoutCalculated.value
? interpolate(
animatedPosition.value,
adjustedSnapPoints,
adjustedSnapPointsIndexes,
Extrapolation.CLAMP
)
: -1;
/**
* if the sheet is currently running an animation by the keyboard opening,
* then we clamp the index on android with resize keyboard mode.
*/
if (
android_keyboardInputMode === KEYBOARD_INPUT_MODE.adjustResize &&
animatedAnimationSource.value === ANIMATION_SOURCE.KEYBOARD &&
animatedAnimationState.value === ANIMATION_STATE.RUNNING &&
isInTemporaryPosition.value
) {
return Math.max(animatedCurrentIndex.value, currentIndex);
}
/**
* if the sheet is currently running an animation by snap point change - usually caused
* by dynamic content height -, then we return the next position index.
*/
if (
animatedAnimationSource.value === ANIMATION_SOURCE.SNAP_POINT_CHANGE &&
animatedAnimationState.value === ANIMATION_STATE.RUNNING
) {
return animatedNextPositionIndex.value;
}
return currentIndex;
}, [
android_keyboardInputMode,
animatedAnimationSource.value,
animatedAnimationState.value,
animatedContainerHeight.value,
animatedCurrentIndex.value,
animatedNextPositionIndex.value,
animatedPosition.value,
animatedSnapPoints.value,
isInTemporaryPosition.value,
isLayoutCalculated.value,
]);
//#endregion
//#region private methods
// biome-ignore lint/correctness/useExhaustiveDependencies(BottomSheet.name): used for debug only
const handleOnChange = useCallback(
function handleOnChange(index: number, position: number) {
if (__DEV__) {
print({
component: BottomSheet.name,
method: handleOnChange.name,
category: 'callback',
params: {
index,
animatedCurrentIndex: animatedCurrentIndex.value,
},
});
}
if (!_providedOnChange) {
return;
}
_providedOnChange(
index,
position,
index === animatedDynamicSnapPointIndex.value
? SNAP_POINT_TYPE.DYNAMIC
: SNAP_POINT_TYPE.PROVIDED
);
},
[_providedOnChange, animatedCurrentIndex, animatedDynamicSnapPointIndex]
);
// biome-ignore lint/correctness/useExhaustiveDependencies(BottomSheet.name): used for debug only
const handleOnAnimate = useCallback(
function handleOnAnimate(targetIndex: number) {
if (__DEV__) {
print({
component: BottomSheet.name,
method: handleOnAnimate.name,
category: 'callback',
params: {
toIndex: targetIndex,
fromIndex: animatedCurrentIndex.value,
},
});
}
if (!_providedOnAnimate) {
return;
}
if (targetIndex !== animatedCurrentIndex.value) {
_providedOnAnimate(animatedCurrentIndex.value, targetIndex);
}
},
[_providedOnAnimate, animatedCurrentIndex]
);
//#endregion
//#region animation
const stopAnimation = useWorkletCallback(() => {
cancelAnimation(animatedPosition);
animatedAnimationSource.value = ANIMATION_SOURCE.NONE;
animatedAnimationState.value = ANIMATION_STATE.STOPPED;
}, [animatedPosition, animatedAnimationState, animatedAnimationSource]);
const animateToPositionCompleted = useWorkletCallback(
function animateToPositionCompleted(isFinished?: boolean) {
if (!isFinished) {
return;
}
if (__DEV__) {
runOnJS(print)({
component: BottomSheet.name,
method: animateToPositionCompleted.name,
params: {
animatedCurrentIndex: animatedCurrentIndex.value,
animatedNextPosition: animatedNextPosition.value,
animatedNextPositionIndex: animatedNextPositionIndex.value,
},
});
}
if (animatedAnimationSource.value === ANIMATION_SOURCE.MOUNT) {
isAnimatedOnMount.value = true;
}
isForcedClosing.value = false;
// reset values
animatedAnimationSource.value = ANIMATION_SOURCE.NONE;
animatedAnimationState.value = ANIMATION_STATE.STOPPED;
animatedNextPosition.value = INITIAL_VALUE;
animatedNextPositionIndex.value = INITIAL_VALUE;
animatedContainerHeightDidChange.value = false;
}
);
const animateToPosition: AnimateToPositionType = useWorkletCallback(
function animateToPosition(
position: number,
source: ANIMATION_SOURCE,
velocity = 0,
configs?: WithTimingConfig | WithSpringConfig
) {
if (__DEV__) {
runOnJS(print)({
component: BottomSheet.name,
method: animateToPosition.name,
params: {
currentPosition: animatedPosition.value,
nextPosition: position,
},
});
}
if (
position === animatedPosition.value ||
position === undefined ||
(animatedAnimationState.value === ANIMATION_STATE.RUNNING &&
position === animatedNextPosition.value)
) {
return;
}
// stop animation if it is running
if (animatedAnimationState.value === ANIMATION_STATE.RUNNING) {
stopAnimation();
}
/**
* set animation state to running, and source
*/
animatedAnimationState.value = ANIMATION_STATE.RUNNING;
animatedAnimationSource.value = source;
/**
* store next position
*/
animatedNextPosition.value = position;
/**
* offset the position if keyboard is shown
*/
let offset = 0;
if (animatedKeyboardState.value === KEYBOARD_STATE.SHOWN) {
offset = animatedKeyboardHeightInContainer.value;
}
animatedNextPositionIndex.value = animatedSnapPoints.value.indexOf(
position + offset
);
/**
* fire `onAnimate` callback
*/
runOnJS(handleOnAnimate)(animatedNextPositionIndex.value);
/**
* start animation
*/
animatedPosition.value = animate({
point: position,
configs: configs || _providedAnimationConfigs,
velocity,
onComplete: animateToPositionCompleted,
});
},
[handleOnAnimate, _providedAnimationConfigs]
);
/**
* Set to position without animation.
*
* @param targetPosition position to be set.
*/
const setToPosition = useWorkletCallback(function setToPosition(
targetPosition: number
) {
if (
targetPosition === animatedPosition.value ||
targetPosition === undefined ||
(animatedAnimationState.value === ANIMATION_STATE.RUNNING &&
targetPosition === animatedNextPosition.value)
) {
return;
}
if (__DEV__) {
runOnJS(print)({
component: BottomSheet.name,
method: setToPosition.name,
params: {
currentPosition: animatedPosition.value,
targetPosition,
},
});
}
/**
* store next position
*/
animatedNextPosition.value = targetPosition;
animatedNextPositionIndex.value =
animatedSnapPoints.value.indexOf(targetPosition);
stopAnimation();
// set values
animatedPosition.value = targetPosition;
animatedContainerHeightDidChange.value = false;
}, []);
//#endregion
//#region private methods
/**
* Calculate and evaluate the current position based on multiple
* local states.
*/
const getEvaluatedPosition = useWorkletCallback(
function getEvaluatedPosition(source: ANIMATION_SOURCE) {
'worklet';
const currentIndex = animatedCurrentIndex.value;
const snapPoints = animatedSnapPoints.value;
const keyboardState = animatedKeyboardState.value;
const highestSnapPoint = animatedHighestSnapPoint.value;
/**
* if the keyboard blur behavior is restore and keyboard is hidden,
* then we return the previous snap point.
*/
if (
source === ANIMATION_SOURCE.KEYBOARD &&
keyboardBlurBehavior === KEYBOARD_BLUR_BEHAVIOR.restore &&
keyboardState === KEYBOARD_STATE.HIDDEN &&
animatedContentGestureState.value !== State.ACTIVE &&
animatedHandleGestureState.value !== State.ACTIVE
) {
isInTemporaryPosition.value = false;
const nextPosition = snapPoints[currentIndex];
return nextPosition;
}
/**
* if the keyboard appearance behavior is extend and keyboard is shown,
* then we return the heights snap point.
*/
if (
keyboardBehavior === KEYBOARD_BEHAVIOR.extend &&
keyboardState === KEYBOARD_STATE.SHOWN
) {
return highestSnapPoint;
}
/**
* if the keyboard appearance behavior is fill parent and keyboard is shown,
* then we return 0 ( full screen ).
*/
if (
keyboardBehavior === KEYBOARD_BEHAVIOR.fillParent &&
keyboardState === KEYBOARD_STATE.SHOWN
) {
isInTemporaryPosition.value = true;
return 0;
}
/**
* if the keyboard appearance behavior is interactive and keyboard is shown,
* then we return the heights points minus the keyboard in container height.
*/
if (
keyboardBehavior === KEYBOARD_BEHAVIOR.interactive &&
keyboardState === KEYBOARD_STATE.SHOWN &&
// ensure that this logic does not run on android
// with resize input mode
!(
Platform.OS === 'android' &&
android_keyboardInputMode === 'adjustResize'
)
) {
isInTemporaryPosition.value = true;
const keyboardHeightInContainer =
animatedKeyboardHeightInContainer.value;
return Math.max(0, highestSnapPoint - keyboardHeightInContainer);
}
/**
* if the bottom sheet is in temporary position, then we return
* the current position.
*/
if (isInTemporaryPosition.value) {
return animatedPosition.value;
}
/**
* if the bottom sheet did not animate on mount,
* then we return the provided index or the closed position.
*/
if (!isAnimatedOnMount.value) {
return _providedIndex === -1
? animatedClosedPosition.value
: snapPoints[_providedIndex];
}
/**
* return the current index position.
*/
return snapPoints[currentIndex];
},
[
animatedContentGestureState,
animatedCurrentIndex,
animatedHandleGestureState,
animatedHighestSnapPoint,
animatedKeyboardHeightInContainer,
animatedKeyboardState,
animatedPosition,
animatedSnapPoints,
isInTemporaryPosition,
isAnimatedOnMount,
keyboardBehavior,
keyboardBlurBehavior,
_providedIndex,
]
);
/**
* Evaluate the bottom sheet position based based on a event source and other local states.
*/
const evaluatePosition = useWorkletCallback(
function evaluatePosition(
source: ANIMATION_SOURCE,
animationConfigs?: WithSpringConfig | WithTimingConfig
) {
/**
* if a force closing is running and source not from user, then we early exit
*/
if (isForcedClosing.value && source !== ANIMATION_SOURCE.USER) {
return;
}
/**
* when evaluating the position while layout is not calculated, then we early exit till it is.
*/
if (!isLayoutCalculated.value) {
return;
}
const proposedPosition = getEvaluatedPosition(source);
/**
* when evaluating the position while the mount animation not been handled,
* then we evaluate on mount use cases.
*/
if (!isAnimatedOnMount.value) {
/**
* if animate on mount is set to true, then we animate to the propose position,
* else, we set the position with out animation.
*/
if (animateOnMount) {
animateToPosition(
proposedPosition,
ANIMATION_SOURCE.MOUNT,
undefined,
animationConfigs
);
} else {
setToPosition(proposedPosition);
isAnimatedOnMount.value = true;
}
return;
}
/**
* when evaluating the position while the bottom sheet is animating.
*/
if (animatedAnimationState.value === ANIMATION_STATE.RUNNING) {
/**
* when evaluating the position while the bottom sheet is
* closing, then we force closing the bottom sheet with no animation.
*/
if (
animatedNextPositionIndex.value === -1 &&
!isInTemporaryPosition.value
) {
setToPosition(animatedClosedPosition.value);
return;
}
/**
* when evaluating the position while it's animating to
* a position other than the current position, then we
* restart the animation.
*/
if (animatedNextPositionIndex.value !== animatedCurrentIndex.value) {
animateToPosition(
animatedSnapPoints.value[animatedNextPositionIndex.value],
source,
undefined,
animationConfigs
);
return;
}
}
/**
* when evaluating the position while the bottom sheet is in closed
* position and not animating, we re-set the position to closed position.
*/
if (
animatedAnimationState.value !== ANIMATION_STATE.RUNNING &&
animatedCurrentIndex.value === -1
) {
setToPosition(animatedClosedPosition.value);
return;
}
/**
* when evaluating the position after the container resize, then we
* force the bottom sheet to the proposed position with no
* animation.
*/
if (animatedContainerHeightDidChange.value) {
setToPosition(proposedPosition);
return;
}
/**
* we fall back to the proposed position.
*/
animateToPosition(
proposedPosition,
source,
undefined,
animationConfigs
);
},
[getEvaluatedPosition, animateToPosition, setToPosition]
);
//#endregion
//#region public methods
// biome-ignore lint/correctness/useExhaustiveDependencies(BottomSheet.name): used for debug only
const handleSnapToIndex = useCallback(
function handleSnapToIndex(
index: number,
animationConfigs?: WithSpringConfig | WithTimingConfig
) {
const snapPoints = animatedSnapPoints.value;