Skip to content

Commit

Permalink
fix: useScrollEnabled alway false in the bottom tab
Browse files Browse the repository at this point in the history
  • Loading branch information
linhvovan29546 committed Feb 11, 2025
1 parent bbcfd4e commit 3895981
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/components/ScrollView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {ScrollView as RNScrollView} from 'react-native';
import type {ScrollViewProps} from 'react-native';
import useScrollEnabled from '@hooks/useScrollEnabled';

function ScrollView({children, scrollIndicatorInsets, ...props}: ScrollViewProps, ref: ForwardedRef<RNScrollView>) {
const scrollEnabled = useScrollEnabled();
function ScrollView({children, scrollIndicatorInsets, scrollEnabled: RNScrollEnabled, ...props}: ScrollViewProps, ref: ForwardedRef<RNScrollView>) {
const scrollEnabled = useScrollEnabled(RNScrollEnabled);

return (
<RNScrollView
Expand Down
32 changes: 28 additions & 4 deletions src/hooks/useScrollEnabled/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
import {useIsFocused} from '@react-navigation/native';
import usePrevious from '@hooks/usePrevious';
import type {EventArg, NavigationContainerEventMap} from '@react-navigation/native';
import {useEffect, useState} from 'react';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import Navigation, {navigationRef} from '@libs/Navigation/Navigation';
import SCREENS from '@src/SCREENS';
import type UseScrollEnabled from './types';

const useScrollEnabled: UseScrollEnabled = () => {
const useScrollEnabled: UseScrollEnabled = (RNScrollEnabled?: boolean) => {
const isFocused = useIsFocused();
const prevIsFocused = usePrevious(isFocused);
return !(prevIsFocused && !isFocused);
const [isScreenFocused, setIsScreenFocused] = useState(false);
useEffect(() => {
const listener = (event: EventArg<'state', false, NavigationContainerEventMap['state']['data']>) => {
const routName = Navigation.getRouteNameFromStateEvent(event);
if (routName === SCREENS.SEARCH.CENTRAL_PANE || routName === SCREENS.SETTINGS_CENTRAL_PANE || routName === SCREENS.HOME) {
setIsScreenFocused(true);
return;
}
setIsScreenFocused(false);
};
navigationRef.addListener('state', listener);
return () => navigationRef.removeListener('state', listener);
}, []);
const {shouldUseNarrowLayout} = useResponsiveLayout();

// On the Search, Settings, Home screen, isFocused returns false, but it is actually focused
if (shouldUseNarrowLayout) {
return isFocused || isScreenFocused;
}

// On desktop screen sizes, isFocused always returns false when useIsFocused is called inside the bottom tab. So, we need to manually set scrollEnabled for ScrollView in this case
return isFocused || RNScrollEnabled;
};
export default useScrollEnabled;
2 changes: 1 addition & 1 deletion src/hooks/useScrollEnabled/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type UseScrollEnabled = () => boolean | undefined;
type UseScrollEnabled = (scrollEnabled?: boolean) => boolean | undefined;

export default UseScrollEnabled;
1 change: 1 addition & 0 deletions src/pages/Search/SearchTypeMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ function SearchTypeMenu({queryJSON, searchName}: SearchTypeMenuProps) {
<ScrollView
onScroll={onScroll}
ref={scrollViewRef}
scrollEnabled
>
<View style={[styles.pb4, styles.mh3, styles.mt3]}>
{typeMenuItems.map((item, index) => {
Expand Down
1 change: 1 addition & 0 deletions src/pages/settings/InitialSettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr
scrollEventThrottle={16}
contentContainerStyle={[styles.w100]}
showsVerticalScrollIndicator={false}
scrollEnabled
>
{accountMenuItems}
{workspaceMenuItems}
Expand Down

0 comments on commit 3895981

Please sign in to comment.