@draftbit/core 49.0.1-10a8bd.2 → 49.0.1-2cdf19.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,902 +0,0 @@
1
- /**
2
- * TODO: Replace with https://github.com/gorhom/react-native-bottom-sheet. (@gorhom/bottom-sheet)
3
- * @gorhom/bottom-sheet v5 (which is not ready yet) will support web, allowing us to transition to that better supported library
4
- *
5
- * We are unable to upgrade to reanimated v3 until this is removed since it relies on reanimated v1 APIs
6
- */
7
-
8
- /**
9
- * Slightly modfied version as taken from https://github.com/rgommezz/react-native-scroll-bottom-sheet
10
- * Main previously breaking change:
11
- * const node = this.props.innerRef.current?.getNode() ==> const node = this.props.innerRef.current as any
12
- */
13
-
14
- /**
15
- * Copyright (c) 2020 Raul Gomez Acuna
16
- *
17
- * This source code is licensed under the MIT license found in the
18
- * LICENSE file in the root directory of this source tree.
19
- *
20
- */
21
-
22
- import React, { Component, RefObject } from "react";
23
- import {
24
- Dimensions,
25
- FlatList,
26
- FlatListProps,
27
- Platform,
28
- ScrollView,
29
- ScrollViewProps,
30
- SectionList,
31
- SectionListProps,
32
- StyleSheet,
33
- View,
34
- ViewStyle,
35
- } from "react-native";
36
- import Animated, {
37
- abs,
38
- add,
39
- and,
40
- call,
41
- Clock,
42
- clockRunning,
43
- cond,
44
- Easing as EasingDeprecated,
45
- // @ts-ignore: this property is only present in Reanimated 2
46
- EasingNode,
47
- eq,
48
- event,
49
- Extrapolate,
50
- greaterOrEq,
51
- greaterThan,
52
- multiply,
53
- not,
54
- onChange,
55
- or,
56
- set,
57
- startClock,
58
- stopClock,
59
- sub,
60
- spring,
61
- timing,
62
- Value,
63
- } from "react-native-reanimated";
64
- import {
65
- NativeViewGestureHandler,
66
- PanGestureHandler,
67
- PanGestureHandlerProperties,
68
- State as GestureState,
69
- TapGestureHandler,
70
- } from "react-native-gesture-handler";
71
- import { Assign } from "utility-types";
72
-
73
- const {
74
- //@ts-ignore
75
- interpolate: interpolateDeprecated,
76
- // @ts-ignore: this property is only present in Reanimated 2
77
- interpolateNode,
78
- } = Animated;
79
-
80
- const interpolate: typeof interpolateDeprecated =
81
- interpolateNode ?? interpolateDeprecated;
82
- //@ts-ignore
83
- const Easing: typeof EasingDeprecated = EasingNode ?? EasingDeprecated;
84
-
85
- const FlatListComponentType = "FlatList" as const;
86
- const ScrollViewComponentType = "ScrollView" as const;
87
- const SectionListComponentType = "SectionList" as const;
88
- const TimingAnimationType = "timing" as const;
89
- const SpringAnimationType = "spring" as const;
90
-
91
- const DEFAULT_SPRING_PARAMS = {
92
- damping: 50,
93
- mass: 0.3,
94
- stiffness: 121.6,
95
- overshootClamping: true,
96
- restSpeedThreshold: 0.3,
97
- restDisplacementThreshold: 0.3,
98
- };
99
-
100
- const { height: windowHeight } = Dimensions.get("window");
101
- const IOS_NORMAL_DECELERATION_RATE = 0.998;
102
- const ANDROID_NORMAL_DECELERATION_RATE = 0.985;
103
- const DEFAULT_ANIMATION_DURATION = 250;
104
- const DEFAULT_EASING = Easing.inOut(Easing.linear);
105
- const imperativeScrollOptions = {
106
- [FlatListComponentType]: {
107
- method: "scrollToIndex",
108
- args: {
109
- index: 0,
110
- viewPosition: 0,
111
- viewOffset: 1000,
112
- animated: true,
113
- },
114
- },
115
- [ScrollViewComponentType]: {
116
- method: "scrollTo",
117
- args: {
118
- x: 0,
119
- y: 0,
120
- animated: true,
121
- },
122
- },
123
- [SectionListComponentType]: {
124
- method: "scrollToLocation",
125
- args: {
126
- itemIndex: 0,
127
- sectionIndex: 0,
128
- viewPosition: 0,
129
- viewOffset: 1000,
130
- animated: true,
131
- },
132
- },
133
- };
134
-
135
- type AnimatedScrollableComponent = FlatList | ScrollView | SectionList;
136
-
137
- type FlatListOption<T> = Assign<
138
- { componentType: typeof FlatListComponentType },
139
- FlatListProps<T>
140
- >;
141
- type ScrollViewOption = Assign<
142
- { componentType: typeof ScrollViewComponentType },
143
- ScrollViewProps
144
- >;
145
- type SectionListOption<T> = Assign<
146
- { componentType: typeof SectionListComponentType },
147
- SectionListProps<T>
148
- >;
149
-
150
- interface TimingParams {
151
- clock: Animated.Clock;
152
- from: Animated.Node<number>;
153
- to: Animated.Node<number>;
154
- position: Animated.Value<number>;
155
- finished: Animated.Value<number>;
156
- frameTime: Animated.Value<number>;
157
- velocity: Animated.Node<number>;
158
- }
159
-
160
- type CommonProps = {
161
- /**
162
- * Array of numbers that indicate the different resting positions of the bottom sheet (in dp or %), starting from the top.
163
- * If a percentage is used, that would translate to the relative amount of the total window height.
164
- * For instance, if 50% is used, that'd be windowHeight * 0.5. If you wanna take into account safe areas during
165
- * the calculation, such as status bars and notches, please use 'topInset' prop
166
- */
167
- snapPoints: Array<string | number>;
168
- /**
169
- * Index that references the initial resting position of the drawer, starting from the top
170
- */
171
- initialSnapIndex: number;
172
- /**
173
- * Render prop for the handle
174
- */
175
- renderHandle: () => React.ReactNode;
176
- /**
177
- * Callback that is executed right after the drawer settles on one of the snapping points.
178
- * The new index is provided on the callback
179
- * @param index
180
- */
181
- onSettle?: (index: number) => void;
182
- /**
183
- * Animated value that tracks the position of the drawer, being:
184
- * 0 => closed
185
- * 1 => fully opened
186
- */
187
- animatedPosition?: Animated.Value<number>;
188
- /**
189
- * This value is useful if you want to take into consideration safe area insets
190
- * when applying percentages for snapping points. We recommend using react-native-safe-area-context
191
- * library for that.
192
- * @see https://github.com/th3rdwave/react-native-safe-area-context#usage, insets.top
193
- */
194
- topInset: number;
195
- /**
196
- * Reference to FlatList, ScrollView or SectionList in order to execute its imperative methods.
197
- */
198
- innerRef: RefObject<FlatList | ScrollView | SectionList>;
199
- /*
200
- * Style to be applied to the container.
201
- */
202
- containerStyle?: Animated.AnimateStyle<ViewStyle>;
203
- /*
204
- * Factor of resistance when the gesture is released. A value of 0 offers maximum
205
- * acceleration, whereas 1 acts as the opposite. Defaults to 0.95
206
- */
207
- friction: number;
208
- /*
209
- * Allow drawer to be dragged beyond lowest snap point
210
- */
211
- enableOverScroll: boolean;
212
- };
213
-
214
- type TimingAnimationProps = {
215
- animationType: typeof TimingAnimationType;
216
- /**
217
- * Configuration for the timing reanimated function
218
- */
219
- animationConfig?: Partial<Animated.TimingConfig>;
220
- };
221
-
222
- type SpringAnimationProps = {
223
- animationType: typeof SpringAnimationType;
224
- /**
225
- * Configuration for the spring reanimated function
226
- */
227
- animationConfig?: Partial<Animated.SpringConfig>;
228
- };
229
-
230
- type Props<T> = CommonProps &
231
- (FlatListOption<T> | ScrollViewOption | SectionListOption<T>) &
232
- (TimingAnimationProps | SpringAnimationProps);
233
-
234
- export class ScrollBottomSheet<T extends any> extends Component<Props<T>> {
235
- static defaultProps = {
236
- topInset: 0,
237
- friction: 0.95,
238
- animationType: "timing",
239
- innerRef: React.createRef<AnimatedScrollableComponent>(),
240
- enableOverScroll: false,
241
- };
242
-
243
- /**
244
- * Gesture Handler references
245
- */
246
- private masterDrawer = React.createRef<TapGestureHandler>();
247
- private drawerHandleRef = React.createRef<PanGestureHandler>();
248
- private drawerContentRef = React.createRef<PanGestureHandler>();
249
- private scrollComponentRef = React.createRef<NativeViewGestureHandler>();
250
-
251
- /**
252
- * ScrollView prop
253
- */
254
- private onScrollBeginDrag: ScrollViewProps["onScrollBeginDrag"];
255
- /**
256
- * Pan gesture handler events for drawer handle and content
257
- */
258
- private onHandleGestureEvent: PanGestureHandlerProperties["onGestureEvent"];
259
- private onDrawerGestureEvent: PanGestureHandlerProperties["onGestureEvent"];
260
- /**
261
- * Main Animated Value that drives the top position of the UI drawer at any point in time
262
- */
263
- private translateY: Animated.Node<number>;
264
- /**
265
- * Animated value that keeps track of the position: 0 => closed, 1 => opened
266
- */
267
- private position: Animated.Node<number>;
268
- /**
269
- * Flag to indicate imperative snapping
270
- */
271
- private isManuallySetValue: Animated.Value<number> = new Value(0);
272
- /**
273
- * Manual snapping amount
274
- */
275
- private manualYOffset: Animated.Value<number> = new Value(0);
276
- /**
277
- * Keeps track of the current index
278
- */
279
- private nextSnapIndex: Animated.Value<number>;
280
- /**
281
- * Deceleration rate of the scroll component. This is used only on Android to
282
- * compensate the unexpected glide it gets sometimes.
283
- */
284
- private decelerationRate: Animated.Value<number>;
285
- private prevSnapIndex = -1;
286
- private dragY = new Value(0);
287
- private prevDragY = new Value(0);
288
- private tempDestSnapPoint = new Value(0);
289
- private isAndroid = new Value(Number(Platform.OS === "android"));
290
- private animationClock = new Clock();
291
- private animationPosition = new Value(0);
292
- private animationFinished = new Value(0);
293
- private animationFrameTime = new Value(0);
294
- private velocityY = new Value(0);
295
- private lastStartScrollY: Animated.Value<number> = new Value(0);
296
- private prevTranslateYOffset: Animated.Value<number>;
297
- private translationY: Animated.Value<number>;
298
- private destSnapPoint = new Value(0);
299
-
300
- private lastSnap: Animated.Value<number>;
301
- private dragWithHandle = new Value(0);
302
- private scrollUpAndPullDown = new Value(0);
303
- private didGestureFinish: Animated.Node<0 | 1>;
304
- private didScrollUpAndPullDown: Animated.Node<number>;
305
- private setTranslationY: Animated.Node<number>;
306
- private extraOffset: Animated.Node<number>;
307
- private calculateNextSnapPoint: (
308
- i?: number
309
- ) => number | Animated.Node<number>;
310
-
311
- private scrollComponent: React.ComponentType<
312
- FlatListProps<T> | ScrollViewProps | SectionListProps<T>
313
- >;
314
-
315
- convertPercentageToDp = (str: string) =>
316
- (Number(str.split("%")[0]) * (windowHeight - this.props.topInset)) / 100;
317
-
318
- constructor(props: Props<T>) {
319
- super(props);
320
- const { initialSnapIndex, animationType } = props;
321
-
322
- const animationDriver = animationType === "timing" ? 0 : 1;
323
- const animationDuration =
324
- (props.animationType === "timing" && props.animationConfig?.duration) ||
325
- DEFAULT_ANIMATION_DURATION;
326
-
327
- const ScrollComponent = this.getScrollComponent();
328
- // @ts-ignore
329
- this.scrollComponent = Animated.createAnimatedComponent(ScrollComponent);
330
-
331
- const snapPoints = this.getNormalisedSnapPoints();
332
- const openPosition = snapPoints[0];
333
- const closedPosition = this.props.enableOverScroll
334
- ? windowHeight
335
- : snapPoints[snapPoints.length - 1];
336
- const initialSnap = snapPoints[initialSnapIndex];
337
- this.nextSnapIndex = new Value(initialSnapIndex);
338
-
339
- const initialDecelerationRate = Platform.select({
340
- android:
341
- props.initialSnapIndex === 0 ? ANDROID_NORMAL_DECELERATION_RATE : 0,
342
- ios: IOS_NORMAL_DECELERATION_RATE,
343
- });
344
- this.decelerationRate = new Value(initialDecelerationRate);
345
-
346
- //@ts-ignore
347
- const handleGestureState = new Value<GestureState>(-1);
348
- //@ts-ignore
349
- const handleOldGestureState = new Value<GestureState>(-1);
350
- //@ts-ignore
351
- const drawerGestureState = new Value<GestureState>(-1);
352
- //@ts-ignore
353
- const drawerOldGestureState = new Value<GestureState>(-1);
354
-
355
- const lastSnapInRange = new Value(1);
356
- this.prevTranslateYOffset = new Value(initialSnap);
357
- this.translationY = new Value(initialSnap);
358
-
359
- this.lastSnap = new Value(initialSnap);
360
-
361
- this.onHandleGestureEvent = event([
362
- {
363
- nativeEvent: {
364
- translationY: this.dragY,
365
- oldState: handleOldGestureState,
366
- state: handleGestureState,
367
- velocityY: this.velocityY,
368
- },
369
- },
370
- ]);
371
- this.onDrawerGestureEvent = event([
372
- {
373
- nativeEvent: {
374
- translationY: this.dragY,
375
- oldState: drawerOldGestureState,
376
- state: drawerGestureState,
377
- velocityY: this.velocityY,
378
- },
379
- },
380
- ]);
381
- this.onScrollBeginDrag = event([
382
- {
383
- nativeEvent: {
384
- contentOffset: { y: this.lastStartScrollY },
385
- },
386
- },
387
- ]);
388
-
389
- const didHandleGestureBegin = eq(handleGestureState, GestureState.ACTIVE);
390
-
391
- const isAnimationInterrupted = and(
392
- or(
393
- eq(handleGestureState, GestureState.BEGAN),
394
- eq(drawerGestureState, GestureState.BEGAN),
395
- and(
396
- eq(this.isAndroid, 0),
397
- eq(animationDriver, 1),
398
- or(
399
- eq(drawerGestureState, GestureState.ACTIVE),
400
- eq(handleGestureState, GestureState.ACTIVE)
401
- )
402
- )
403
- ),
404
- clockRunning(this.animationClock)
405
- );
406
-
407
- this.didGestureFinish = or(
408
- and(
409
- eq(handleOldGestureState, GestureState.ACTIVE),
410
- eq(handleGestureState, GestureState.END)
411
- ),
412
- and(
413
- eq(drawerOldGestureState, GestureState.ACTIVE),
414
- eq(drawerGestureState, GestureState.END)
415
- )
416
- );
417
-
418
- // Function that determines if the last snap point is in the range {snapPoints}
419
- // In the case of interruptions in the middle of an animation, we'll get
420
- // lastSnap values outside the range
421
- const isLastSnapPointInRange = (i: number = 0): Animated.Node<number> =>
422
- i === snapPoints.length
423
- ? lastSnapInRange
424
- : cond(
425
- eq(this.lastSnap, snapPoints[i]),
426
- [set(lastSnapInRange, 1)],
427
- isLastSnapPointInRange(i + 1)
428
- );
429
-
430
- const scrollY = [
431
- set(lastSnapInRange, 0),
432
- isLastSnapPointInRange(),
433
- cond(
434
- or(
435
- didHandleGestureBegin,
436
- and(
437
- this.isManuallySetValue,
438
- not(eq(this.manualYOffset, snapPoints[0]))
439
- )
440
- ),
441
- [set(this.dragWithHandle, 1), 0]
442
- ),
443
- cond(
444
- // This is to account for a continuous scroll on the drawer from a snap point
445
- // Different than top, bringing the drawer to the top position, so that if we
446
- // change scroll direction without releasing the gesture, it doesn't pull down the drawer again
447
- and(
448
- eq(this.dragWithHandle, 1),
449
- greaterThan(snapPoints[0], add(this.lastSnap, this.dragY)),
450
- and(not(eq(this.lastSnap, snapPoints[0])), lastSnapInRange)
451
- ),
452
- [
453
- set(this.lastSnap, snapPoints[0]),
454
- set(this.dragWithHandle, 0),
455
- this.lastStartScrollY,
456
- ],
457
- cond(eq(this.dragWithHandle, 1), 0, this.lastStartScrollY)
458
- ),
459
- ];
460
-
461
- this.didScrollUpAndPullDown = cond(
462
- and(
463
- greaterOrEq(this.dragY, this.lastStartScrollY),
464
- greaterThan(this.lastStartScrollY, 0)
465
- ),
466
- set(this.scrollUpAndPullDown, 1)
467
- );
468
-
469
- this.setTranslationY = cond(
470
- and(
471
- not(this.dragWithHandle),
472
- not(greaterOrEq(this.dragY, this.lastStartScrollY))
473
- ),
474
- set(this.translationY, sub(this.dragY, this.lastStartScrollY)),
475
- set(this.translationY, this.dragY)
476
- );
477
-
478
- this.extraOffset = cond(
479
- eq(this.scrollUpAndPullDown, 1),
480
- this.lastStartScrollY,
481
- 0
482
- );
483
- const endOffsetY = add(
484
- this.lastSnap,
485
- this.translationY,
486
- multiply(1 - props.friction, this.velocityY)
487
- );
488
-
489
- this.calculateNextSnapPoint = (i = 0): Animated.Node<number> | number =>
490
- i === snapPoints.length
491
- ? this.tempDestSnapPoint
492
- : cond(
493
- greaterThan(
494
- abs(sub(this.tempDestSnapPoint, endOffsetY)),
495
- abs(sub(add(snapPoints[i], this.extraOffset), endOffsetY))
496
- ),
497
- [
498
- set(this.tempDestSnapPoint, add(snapPoints[i], this.extraOffset)),
499
- set(this.nextSnapIndex, i),
500
- this.calculateNextSnapPoint(i + 1),
501
- ],
502
- this.calculateNextSnapPoint(i + 1)
503
- );
504
-
505
- const runAnimation = ({
506
- clock,
507
- from,
508
- to,
509
- position,
510
- finished,
511
- velocity,
512
- frameTime,
513
- }: TimingParams) => {
514
- const state = {
515
- finished,
516
- velocity: new Value(0),
517
- position,
518
- time: new Value(0),
519
- frameTime,
520
- };
521
-
522
- const timingConfig = {
523
- duration: animationDuration,
524
- easing:
525
- (props.animationType === "timing" && props.animationConfig?.easing) ||
526
- DEFAULT_EASING,
527
- toValue: new Value(0),
528
- };
529
-
530
- const springConfig = {
531
- ...DEFAULT_SPRING_PARAMS,
532
- ...((props.animationType === "spring" && props.animationConfig) || {}),
533
- toValue: new Value(0),
534
- };
535
-
536
- return [
537
- cond(and(not(clockRunning(clock)), not(eq(finished, 1))), [
538
- // If the clock isn't running, we reset all the animation params and start the clock
539
- set(state.finished, 0),
540
- set(state.velocity, velocity),
541
- set(state.time, 0),
542
- set(state.position, from),
543
- set(state.frameTime, 0),
544
- set(timingConfig.toValue, to),
545
- set(springConfig.toValue, to),
546
- startClock(clock),
547
- ]),
548
- // We run the step here that is going to update position
549
- cond(
550
- eq(animationDriver, 0),
551
- //@ts-ignore
552
- timing(clock, state, timingConfig),
553
- spring(clock, state, springConfig)
554
- ),
555
- cond(
556
- state.finished,
557
- [
558
- call([this.nextSnapIndex], ([value]) => {
559
- if (value !== this.prevSnapIndex) {
560
- this.props.onSettle?.(value);
561
- }
562
- this.prevSnapIndex = value;
563
- }),
564
- // Resetting appropriate values
565
- set(drawerOldGestureState, GestureState.END),
566
- set(handleOldGestureState, GestureState.END),
567
- set(this.prevTranslateYOffset, state.position),
568
- cond(eq(this.scrollUpAndPullDown, 1), [
569
- set(
570
- this.prevTranslateYOffset,
571
- sub(this.prevTranslateYOffset, this.lastStartScrollY)
572
- ),
573
- set(this.lastStartScrollY, 0),
574
- set(this.scrollUpAndPullDown, 0),
575
- ]),
576
- cond(eq(this.destSnapPoint, snapPoints[0]), [
577
- set(this.dragWithHandle, 0),
578
- ]),
579
- set(this.isManuallySetValue, 0),
580
- set(this.manualYOffset, 0),
581
- stopClock(clock),
582
- this.prevTranslateYOffset,
583
- ],
584
- // We made the block return the updated position,
585
- state.position
586
- ),
587
- ];
588
- };
589
-
590
- const translateYOffset = cond(
591
- isAnimationInterrupted,
592
- [
593
- // set(prevTranslateYOffset, animationPosition) should only run if we are
594
- // interrupting an animation when the drawer is currently in a different
595
- // position than the top
596
- cond(
597
- or(
598
- this.dragWithHandle,
599
- greaterOrEq(abs(this.prevDragY), this.lastStartScrollY)
600
- ),
601
- set(this.prevTranslateYOffset, this.animationPosition)
602
- ),
603
- set(this.animationFinished, 1),
604
- set(this.translationY, 0),
605
- // Resetting appropriate values
606
- set(drawerOldGestureState, GestureState.END),
607
- set(handleOldGestureState, GestureState.END),
608
- // By forcing that frameTime exceeds duration, it has the effect of stopping the animation
609
- set(this.animationFrameTime, add(animationDuration, 1000)),
610
- set(this.velocityY, 0),
611
- stopClock(this.animationClock),
612
- this.prevTranslateYOffset,
613
- ],
614
- cond(
615
- or(
616
- this.didGestureFinish,
617
- this.isManuallySetValue,
618
- clockRunning(this.animationClock)
619
- ),
620
- [
621
- runAnimation({
622
- clock: this.animationClock,
623
- from: cond(
624
- this.isManuallySetValue,
625
- this.prevTranslateYOffset,
626
- add(this.prevTranslateYOffset, this.translationY)
627
- ),
628
- to: this.destSnapPoint,
629
- position: this.animationPosition,
630
- finished: this.animationFinished,
631
- frameTime: this.animationFrameTime,
632
- velocity: this.velocityY,
633
- }),
634
- ],
635
- [
636
- set(this.animationFrameTime, 0),
637
- set(this.animationFinished, 0),
638
- // @ts-ignore
639
- this.prevTranslateYOffset,
640
- ]
641
- )
642
- );
643
-
644
- this.translateY = interpolate(
645
- add(translateYOffset, this.dragY, multiply(scrollY, -1)),
646
- {
647
- inputRange: [openPosition, closedPosition],
648
- outputRange: [openPosition, closedPosition],
649
- extrapolate: Extrapolate.CLAMP,
650
- }
651
- );
652
-
653
- this.position = interpolate(this.translateY, {
654
- inputRange: [openPosition, snapPoints[snapPoints.length - 1]],
655
- outputRange: [1, 0],
656
- extrapolate: Extrapolate.CLAMP,
657
- });
658
- }
659
-
660
- private getNormalisedSnapPoints = () => {
661
- return this.props.snapPoints.map((p) => {
662
- if (typeof p === "string") {
663
- return this.convertPercentageToDp(p);
664
- } else if (typeof p === "number") {
665
- return p;
666
- }
667
-
668
- throw new Error(
669
- `Invalid type for value ${p}: ${typeof p}. It should be either a percentage string or a number`
670
- );
671
- });
672
- };
673
-
674
- private getScrollComponent = () => {
675
- switch (this.props.componentType) {
676
- case "FlatList":
677
- return FlatList;
678
- case "ScrollView":
679
- return ScrollView;
680
- case "SectionList":
681
- return SectionList;
682
- default:
683
- throw new Error(
684
- "Component type not supported: it should be one of `FlatList`, `ScrollView` or `SectionList`"
685
- );
686
- }
687
- };
688
-
689
- snapTo = (index: number) => {
690
- const snapPoints = this.getNormalisedSnapPoints();
691
- this.isManuallySetValue.setValue(1);
692
- this.manualYOffset.setValue(snapPoints[index]);
693
- this.nextSnapIndex.setValue(index);
694
- };
695
-
696
- render() {
697
- const { renderHandle, initialSnapIndex, containerStyle, ...rest } =
698
- this.props;
699
- const AnimatedScrollableComponent = this.scrollComponent;
700
- const normalisedSnapPoints = this.getNormalisedSnapPoints();
701
- const initialSnap = normalisedSnapPoints[initialSnapIndex];
702
-
703
- const Content = (
704
- <Animated.View
705
- style={[
706
- StyleSheet.absoluteFillObject,
707
- containerStyle,
708
- // @ts-ignore
709
- {
710
- transform: [{ translateY: this.translateY }],
711
- },
712
- ]}
713
- >
714
- <PanGestureHandler
715
- ref={this.drawerHandleRef}
716
- shouldCancelWhenOutside={false}
717
- simultaneousHandlers={this.masterDrawer}
718
- onGestureEvent={this.onHandleGestureEvent}
719
- onHandlerStateChange={this.onHandleGestureEvent}
720
- >
721
- <Animated.View>{renderHandle()}</Animated.View>
722
- </PanGestureHandler>
723
- <PanGestureHandler
724
- ref={this.drawerContentRef}
725
- simultaneousHandlers={[this.scrollComponentRef, this.masterDrawer]}
726
- shouldCancelWhenOutside={false}
727
- onGestureEvent={this.onDrawerGestureEvent}
728
- onHandlerStateChange={this.onDrawerGestureEvent}
729
- >
730
- <Animated.View style={styles.container}>
731
- <NativeViewGestureHandler
732
- ref={this.scrollComponentRef}
733
- waitFor={this.masterDrawer}
734
- simultaneousHandlers={this.drawerContentRef}
735
- >
736
- <AnimatedScrollableComponent
737
- overScrollMode="never"
738
- bounces={false}
739
- {...rest}
740
- ref={this.props.innerRef}
741
- // @ts-ignore
742
- decelerationRate={this.decelerationRate}
743
- onScrollBeginDrag={this.onScrollBeginDrag}
744
- scrollEventThrottle={1}
745
- contentContainerStyle={[
746
- rest.contentContainerStyle,
747
- { paddingBottom: this.getNormalisedSnapPoints()[0] },
748
- ]}
749
- />
750
- </NativeViewGestureHandler>
751
- </Animated.View>
752
- </PanGestureHandler>
753
- {this.props.animatedPosition && (
754
- <Animated.Code
755
- exec={onChange(
756
- this.position,
757
- set(this.props.animatedPosition, this.position)
758
- )}
759
- />
760
- )}
761
- <Animated.Code
762
- exec={onChange(
763
- this.dragY,
764
- cond(not(eq(this.dragY, 0)), set(this.prevDragY, this.dragY))
765
- )}
766
- />
767
- <Animated.Code
768
- exec={onChange(
769
- this.didGestureFinish,
770
- cond(this.didGestureFinish, [
771
- this.didScrollUpAndPullDown,
772
- this.setTranslationY,
773
- set(
774
- this.tempDestSnapPoint,
775
- add(normalisedSnapPoints[0], this.extraOffset)
776
- ),
777
- set(this.nextSnapIndex, 0),
778
- set(this.destSnapPoint, this.calculateNextSnapPoint()),
779
- cond(
780
- and(
781
- greaterThan(this.dragY, this.lastStartScrollY),
782
- this.isAndroid,
783
- not(this.dragWithHandle)
784
- ),
785
- call([], () => {
786
- // This prevents the scroll glide from happening on Android when pulling down with inertia.
787
- // It's not perfect, but does the job for now
788
- const { method, args } =
789
- imperativeScrollOptions[this.props.componentType];
790
- // @ts-ignore
791
- const node = this.props.innerRef.current as any;
792
-
793
- if (
794
- node &&
795
- node[method] &&
796
- ((this.props.componentType === "FlatList" &&
797
- (this.props?.data?.length || 0) > 0) ||
798
- (this.props.componentType === "SectionList" &&
799
- this.props.sections.length > 0) ||
800
- this.props.componentType === "ScrollView")
801
- ) {
802
- node[method](args);
803
- }
804
- })
805
- ),
806
- set(this.dragY, 0),
807
- set(this.velocityY, 0),
808
- set(
809
- this.lastSnap,
810
- sub(
811
- this.destSnapPoint,
812
- cond(
813
- eq(this.scrollUpAndPullDown, 1),
814
- this.lastStartScrollY,
815
- 0
816
- )
817
- )
818
- ),
819
- call([this.lastSnap], ([value]) => {
820
- // This is the TapGHandler trick
821
- // @ts-ignore
822
- this.masterDrawer?.current?.setNativeProps({
823
- maxDeltaY: value - this.getNormalisedSnapPoints()[0],
824
- });
825
- }),
826
- set(
827
- this.decelerationRate,
828
- cond(
829
- eq(this.isAndroid, 1),
830
- cond(
831
- eq(this.lastSnap, normalisedSnapPoints[0]),
832
- ANDROID_NORMAL_DECELERATION_RATE,
833
- 0
834
- ),
835
- IOS_NORMAL_DECELERATION_RATE
836
- )
837
- ),
838
- ])
839
- )}
840
- />
841
- <Animated.Code
842
- exec={onChange(this.isManuallySetValue, [
843
- cond(
844
- this.isManuallySetValue,
845
- [
846
- set(this.destSnapPoint, this.manualYOffset),
847
- set(this.animationFinished, 0),
848
- set(this.lastSnap, this.manualYOffset),
849
- call([this.lastSnap], ([value]) => {
850
- // This is the TapGHandler trick
851
- // @ts-ignore
852
- this.masterDrawer?.current?.setNativeProps({
853
- maxDeltaY: value - this.getNormalisedSnapPoints()[0],
854
- });
855
- }),
856
- ],
857
- [set(this.nextSnapIndex, 0)]
858
- ),
859
- ])}
860
- />
861
- </Animated.View>
862
- );
863
-
864
- // On Android, having an intermediary view with pointerEvents="box-none", breaks the
865
- // waitFor logic
866
- if (Platform.OS === "android") {
867
- return (
868
- <TapGestureHandler
869
- maxDurationMs={100000}
870
- ref={this.masterDrawer}
871
- maxDeltaY={initialSnap - this.getNormalisedSnapPoints()[0]}
872
- shouldCancelWhenOutside={false}
873
- >
874
- {Content}
875
- </TapGestureHandler>
876
- );
877
- }
878
-
879
- // On iOS, We need to wrap the content on a view with PointerEvents box-none
880
- // So that we can start scrolling automatically when reaching the top without
881
- // Stopping the gesture
882
- return (
883
- <TapGestureHandler
884
- maxDurationMs={100000}
885
- ref={this.masterDrawer}
886
- maxDeltaY={initialSnap - this.getNormalisedSnapPoints()[0]}
887
- >
888
- <View style={StyleSheet.absoluteFillObject} pointerEvents="box-none">
889
- {Content}
890
- </View>
891
- </TapGestureHandler>
892
- );
893
- }
894
- }
895
-
896
- export default ScrollBottomSheet;
897
-
898
- const styles = StyleSheet.create({
899
- container: {
900
- flex: 1,
901
- },
902
- });