Skip to content

Commit 6766c4e

Browse files
committed
minor improvements to scrolling & gestures
1 parent 0074a9c commit 6766c4e

File tree

4 files changed

+70
-61
lines changed

4 files changed

+70
-61
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-actions-sheet",
3-
"version": "0.8.21",
3+
"version": "0.8.22",
44
"description": "A Cross Platform(Android & iOS) ActionSheet with a robust and flexible api, native performance and zero dependency code for react native. Create anything you want inside ActionSheet.",
55
"main": "./dist/index.js",
66
"repository": {

src/hooks/use-scroll-handlers.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ import {ActionSheetRef} from '../index';
1515
* with it to enable vertical scrolling. For horizontal ScrollViews, you should not use this hook.
1616
* @param id Id for the handler. Could be any string value.
1717
* @param ref ref of the ActionSheet in which the ScrollView is present.
18+
* @param minGesutureBoundary The minimum area of scrollView from top, where swipe gestures are allowed always.
1819
* @returns
1920
*/
2021
export function useScrollHandlers<T>(
2122
id: string,
2223
ref: RefObject<ActionSheetRef>,
24+
minGesutureBoundary: number = 50,
2325
) {
24-
//const [enabled,setEnabled] = useState(false);
2526
const scrollRef = useRef<T>(null);
2627
const scrollLayout = useRef<LayoutRectangle>();
2728
const scrollOffset = useRef(0);
@@ -37,10 +38,6 @@ export function useScrollHandlers<T>(
3738
};
3839

3940
const disableScrolling = React.useCallback(() => {
40-
//@ts-ignore
41-
// scrollRef.current?.setNativeProps?.({
42-
// scrollEnabled: false,
43-
// });
4441
if (Platform.OS === 'web') {
4542
//@ts-ignore
4643
scrollRef.current.style.touchAction = 'none';
@@ -50,10 +47,6 @@ export function useScrollHandlers<T>(
5047
}, [scrollRef]);
5148

5249
const enableScrolling = React.useCallback(() => {
53-
//@ts-ignore
54-
// scrollRef.current?.setNativeProps?.({
55-
// scrollEnabled: true,
56-
// });
5750
if (Platform.OS === 'web') {
5851
//@ts-ignore
5952
scrollRef.current.style.overflowY = 'scroll';
@@ -69,7 +62,10 @@ export function useScrollHandlers<T>(
6962
}, [id, ref, disableScrolling, enableScrolling]);
7063

7164
const onLayout = (event: LayoutChangeEvent) => {
72-
scrollLayout.current = event.nativeEvent.layout;
65+
scrollLayout.current = {
66+
...event.nativeEvent.layout,
67+
y: event.nativeEvent.layout.y || minGesutureBoundary,
68+
};
7369
subscription.current = ref.current?.ev.subscribe(
7470
'onoffsetchange',
7571
(offset: number) => {
@@ -91,7 +87,10 @@ export function useScrollHandlers<T>(
9187
);
9288
ref.current?.modifyGesturesForLayout(
9389
id,
94-
scrollLayout.current,
90+
{
91+
...scrollLayout.current,
92+
y: scrollLayout.current.y || minGesutureBoundary,
93+
},
9594
scrollOffset.current,
9695
);
9796
};

src/index.tsx

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from 'react-native';
2727
import EventManager, {actionSheetEventManager} from './eventmanager';
2828
import {
29+
Route,
2930
RouterContext,
3031
RouterParamsContext,
3132
useRouter,
@@ -294,7 +295,7 @@ export default forwardRef<ActionSheetRef, ActionSheetProps>(
294295

295296
const getCurrentPosition = React.useCallback(() => {
296297
//@ts-ignore
297-
return animations.translateY._value <= minTranslateValue.current
298+
return animations.translateY._value <= minTranslateValue.current + 5
298299
? 0
299300
: //@ts-ignore
300301
(animations.translateY._value as number);
@@ -579,20 +580,23 @@ export default forwardRef<ActionSheetRef, ActionSheetProps>(
579580
let gestures = true;
580581
for (let _id in gestureBoundaries.current) {
581582
const gestureBoundary = gestureBoundaries.current[_id];
582-
if (getCurrentPosition() > 0 || !gestureBoundary) {
583+
if (getCurrentPosition() > 3 || !gestureBoundary) {
583584
gestures = true;
584585
break;
585586
}
586587
const scrollOffset = gestureBoundary?.scrollOffset || 0;
587588
if (
588-
event.nativeEvent.pageY < gestureBoundary?.y ||
589+
event.nativeEvent.locationY < gestureBoundary?.y ||
589590
(gesture.vy > 0 && scrollOffset <= 0) ||
590591
getCurrentPosition() !== 0
591592
) {
592-
if (event.nativeEvent.pageY > gestureBoundary?.y)
593-
gestures = false;
594-
595-
if (Platform.OS === 'web') {
593+
if (
594+
!props.enableGesturesInScrollView &&
595+
Platform.OS !== 'web' &&
596+
event.nativeEvent.locationY > gestureBoundary?.y
597+
) {
598+
return false;
599+
} else {
596600
gestures = true;
597601
}
598602
} else {
@@ -614,29 +618,27 @@ export default forwardRef<ActionSheetRef, ActionSheetProps>(
614618
onStartShouldSetPanResponder: (event, _gesture) => {
615619
if (sheetId && !isRenderedOnTop(sheetId, currentContext))
616620
return false;
617-
618-
if (Platform.OS === 'web') {
619-
let gestures = true;
620-
for (let _id in gestureBoundaries.current) {
621-
const gestureBoundary = gestureBoundaries.current[_id];
622-
if (getCurrentPosition() > 3 || !gestureBoundary) {
623-
gestures = true;
624-
}
625-
const scrollOffset = gestureBoundary?.scrollOffset || 0;
626-
if (
627-
event.nativeEvent.pageY < gestureBoundary?.y ||
628-
scrollOffset <= 0 ||
629-
getCurrentPosition() !== 0
630-
) {
631-
gestures = true;
621+
let gestures = true;
622+
for (let _id in gestureBoundaries.current) {
623+
const gestureBoundary = gestureBoundaries.current[_id];
624+
if (getCurrentPosition() > 3 || !gestureBoundary) {
625+
gestures = true;
626+
}
627+
const scrollOffset = gestureBoundary?.scrollOffset || 0;
628+
if (
629+
event.nativeEvent.locationY < gestureBoundary?.y ||
630+
(scrollOffset <= 0 && getCurrentPosition() !== 0)
631+
) {
632+
if (Platform.OS !== 'web') {
633+
return false;
632634
} else {
633-
gestures = false;
635+
gestures = true;
634636
}
637+
} else {
638+
gestures = false;
635639
}
636-
637-
return gestures;
638640
}
639-
return true;
641+
return gestures;
640642
},
641643
onPanResponderMove: (_event, gesture) => {
642644
const value = initialValue.current + gesture.dy;
@@ -688,6 +690,7 @@ export default forwardRef<ActionSheetRef, ActionSheetProps>(
688690
sheetId,
689691
currentContext,
690692
getCurrentPosition,
693+
props.enableGesturesInScrollView,
691694
overdrawFactor,
692695
overdrawSize,
693696
animations.translateY,
@@ -950,6 +953,30 @@ export default forwardRef<ActionSheetRef, ActionSheetProps>(
950953
],
951954
);
952955

956+
const renderRoute = useCallback(
957+
(route: Route) => {
958+
const RouteComponent = route.component as any;
959+
return (
960+
<Animated.View
961+
key={route.name}
962+
style={{
963+
display:
964+
route.name !== router.currentRoute?.name ? 'none' : 'flex',
965+
opacity: animations.routeOpacity,
966+
}}>
967+
<RouterParamsContext.Provider value={route?.params}>
968+
<RouteComponent
969+
router={router}
970+
params={route?.params}
971+
payload={payloadRef.current}
972+
/>
973+
</RouterParamsContext.Provider>
974+
</Animated.View>
975+
);
976+
},
977+
[animations.routeOpacity, router],
978+
);
979+
953980
const getPaddingBottom = () => {
954981
if (!props.useBottomSafeAreaPadding && !props.containerStyle) return 0;
955982

@@ -1147,29 +1174,7 @@ export default forwardRef<ActionSheetRef, ActionSheetProps>(
11471174

11481175
{router?.hasRoutes() ? (
11491176
<RouterContext.Provider value={router}>
1150-
{router?.stack.map(route => {
1151-
const RouteComponent = route.component as any;
1152-
return (
1153-
<Animated.View
1154-
key={route.name}
1155-
style={{
1156-
display:
1157-
route.name !== router.currentRoute?.name
1158-
? 'none'
1159-
: 'flex',
1160-
opacity: animations.routeOpacity,
1161-
}}>
1162-
<RouterParamsContext.Provider
1163-
value={route?.params}>
1164-
<RouteComponent
1165-
router={router}
1166-
params={route?.params}
1167-
payload={payloadRef.current}
1168-
/>
1169-
</RouterParamsContext.Provider>
1170-
</Animated.View>
1171-
);
1172-
})}
1177+
{router?.stack.map(renderRoute)}
11731178
</RouterContext.Provider>
11741179
) : (
11751180
props?.children

src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,9 @@ export type ActionSheetProps = {
296296
* the sheet regardless of the route in stack.
297297
*/
298298
enableRouterBackNavigation?: boolean;
299+
/**
300+
* Enable swipe gestures inside ScrollView/FlatList. Use this with caution. It
301+
* might be a little buggy and conflict with ScrollView touch events.
302+
*/
303+
enableGesturesInScrollView?: boolean;
299304
};

0 commit comments

Comments
 (0)