From ee5c66305aa5376d686433e9ff1c823ef9b5b439 Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 14 Apr 2025 23:50:07 +0800 Subject: [PATCH] feat(tabbar): enhance BottomTabProvider with initialTabIndex and context integration - Added initialTabIndex prop to BottomTabProvider for customizable tab index. - Integrated BottomTabContext to manage tab state and improve context handling. - Introduced useResetTabOpacityWhenFocused hook to reset tab opacity based on focus state. - Updated TabRoot components to utilize context for tab index management. - Adjusted PagerListVisibleContext default value for better visibility handling. Signed-off-by: Innei --- .../layouts/tabbar/BottomTabProvider.tsx | 41 +++++++++++--- .../src/components/layouts/tabbar/hooks.ts | 15 ++++- .../lib/navigation/bottom-tab/TabRoot.ios.tsx | 55 ++++++------------- .../src/lib/navigation/bottom-tab/TabRoot.tsx | 33 +---------- .../src/modules/screen/PagerListContext.ts | 2 +- .../src/screens/(stack)/(tabs)/index.tsx | 3 +- .../screens/(stack)/(tabs)/subscriptions.tsx | 3 +- 7 files changed, 71 insertions(+), 81 deletions(-) diff --git a/apps/mobile/src/components/layouts/tabbar/BottomTabProvider.tsx b/apps/mobile/src/components/layouts/tabbar/BottomTabProvider.tsx index 484c710cb..ae5d79f4b 100644 --- a/apps/mobile/src/components/layouts/tabbar/BottomTabProvider.tsx +++ b/apps/mobile/src/components/layouts/tabbar/BottomTabProvider.tsx @@ -1,6 +1,11 @@ +import { atom } from "jotai" import { useMemo, useState } from "react" import { useSharedValue } from "react-native-reanimated" +import type { BottomTabContextType } from "@/src/lib/navigation/bottom-tab/BottomTabContext" +import { BottomTabContext } from "@/src/lib/navigation/bottom-tab/BottomTabContext" +import type { TabScreenProps } from "@/src/lib/navigation/bottom-tab/types" + import { BottomTabHeightProvider } from "./BottomTabHeightProvider" import { BottomTabBarBackgroundContext } from "./contexts/BottomTabBarBackgroundContext" import { @@ -8,17 +13,37 @@ import { SetBottomTabBarVisibleContext, } from "./contexts/BottomTabBarVisibleContext" -export const BottomTabProvider = ({ children }: { children: React.ReactNode }) => { +export const BottomTabProvider = ({ + children, + initialTabIndex = 0, +}: { + children: React.ReactNode + initialTabIndex?: number +}) => { const opacity = useSharedValue(1) const [tabBarVisible, setTabBarVisible] = useState(true) + const [tabIndexAtom] = useState(() => atom(initialTabIndex)) + + const ctxValue = useMemo( + () => ({ + currentIndexAtom: tabIndexAtom, + loadedableIndexAtom: atom(new Set()), + tabScreensAtom: atom([]), + tabHeightAtom: atom(0), + }), + [tabIndexAtom], + ) + return ( - ({ opacity }), [opacity])}> - - - {children} - - - + + ({ opacity }), [opacity])}> + + + {children} + + + + ) } diff --git a/apps/mobile/src/components/layouts/tabbar/hooks.ts b/apps/mobile/src/components/layouts/tabbar/hooks.ts index 79020196f..3404cd076 100644 --- a/apps/mobile/src/components/layouts/tabbar/hooks.ts +++ b/apps/mobile/src/components/layouts/tabbar/hooks.ts @@ -1,5 +1,5 @@ import type { RefObject } from "react" -import { useCallback, useContext, useEffect, useRef } from "react" +import { useCallback, useContext, useEffect, useLayoutEffect, useRef } from "react" import type { FlatList, ScrollView } from "react-native" import { findNodeHandle, Platform } from "react-native" @@ -8,8 +8,9 @@ import { SetAttachNavigationScrollViewContext, useAttachNavigationScrollView, } from "@/src/lib/navigation/AttachNavigationScrollViewContext" -import { useScreenIsAppeared } from "@/src/lib/navigation/bottom-tab/hooks" +import { useScreenIsAppeared, useTabScreenIsFocused } from "@/src/lib/navigation/bottom-tab/hooks" +import { BottomTabBarBackgroundContext } from "./contexts/BottomTabBarBackgroundContext" import { BottomTabBarHeightContext } from "./contexts/BottomTabBarHeightContext" export const useBottomTabBarHeight = () => { @@ -75,3 +76,13 @@ export const useRegisterNavigationScrollView = (active = true) => { }, [setAttachNavigationScrollViewRef, scrollViewRef, active, tabScreenIsFocused]) return scrollViewRef } + +export const useResetTabOpacityWhenFocused = () => { + const { opacity } = useContext(BottomTabBarBackgroundContext) + const tabScreenIsFocus = useTabScreenIsFocused() + useLayoutEffect(() => { + if (tabScreenIsFocus) { + opacity.value = 1 + } + }, [tabScreenIsFocus, opacity]) +} diff --git a/apps/mobile/src/lib/navigation/bottom-tab/TabRoot.ios.tsx b/apps/mobile/src/lib/navigation/bottom-tab/TabRoot.ios.tsx index 099dd8f8b..49109a475 100644 --- a/apps/mobile/src/lib/navigation/bottom-tab/TabRoot.ios.tsx +++ b/apps/mobile/src/lib/navigation/bottom-tab/TabRoot.ios.tsx @@ -1,19 +1,13 @@ import { requireNativeView } from "expo" -import { atom, useAtom } from "jotai" +import { useAtom } from "jotai" import type { FC, PropsWithChildren } from "react" import * as React from "react" -import { useCallback, useMemo, useState } from "react" +import { useCallback, useMemo } from "react" import type { NativeSyntheticEvent, ViewProps } from "react-native" import { StyleSheet } from "react-native" -import type { BottomTabContextType } from "./BottomTabContext" import { BottomTabContext } from "./BottomTabContext" import { TabScreen } from "./TabScreen.ios" -import type { TabScreenProps } from "./types" - -interface TabRootProps { - initialTabIndex?: number -} const TabBarRoot = requireNativeView< { @@ -22,22 +16,9 @@ const TabBarRoot = requireNativeView< } & ViewProps >("TabBarRoot") -export const TabRoot: FC = ({ - children, - initialTabIndex = 0, -}) => { - const [tabIndexAtom] = useState(() => atom(initialTabIndex)) - const [tabIndex, setTabIndex] = useAtom(tabIndexAtom) - - const ctxValue = useMemo( - () => ({ - currentIndexAtom: tabIndexAtom, - loadedableIndexAtom: atom(new Set()), - tabScreensAtom: atom([]), - tabHeightAtom: atom(0), - }), - [tabIndexAtom], - ) +export const TabRoot: FC = ({ children }) => { + const { currentIndexAtom } = React.useContext(BottomTabContext) + const [tabIndex, setTabIndex] = useAtom(currentIndexAtom) const MapChildren = useMemo(() => { let cnt = 0 @@ -51,19 +32,17 @@ export const TabRoot: FC = ({ }) }, [children]) return ( - - { - setTabIndex(e.nativeEvent.index) - }, - [setTabIndex], - )} - selectedIndex={tabIndex} - > - {MapChildren} - - + { + setTabIndex(e.nativeEvent.index) + }, + [setTabIndex], + )} + selectedIndex={tabIndex} + > + {MapChildren} + ) } diff --git a/apps/mobile/src/lib/navigation/bottom-tab/TabRoot.tsx b/apps/mobile/src/lib/navigation/bottom-tab/TabRoot.tsx index 6be3ddae4..eb99934f5 100644 --- a/apps/mobile/src/lib/navigation/bottom-tab/TabRoot.tsx +++ b/apps/mobile/src/lib/navigation/bottom-tab/TabRoot.tsx @@ -1,34 +1,11 @@ -import { atom } from "jotai" import type { FC, PropsWithChildren } from "react" import * as React from "react" -import { useMemo, useState } from "react" +import { useMemo } from "react" import { StyleSheet, View } from "react-native" -import type { BottomTabContextType } from "./BottomTabContext" -import { BottomTabContext } from "./BottomTabContext" import { TabScreen } from "./TabScreen" -import type { TabScreenProps } from "./types" - -interface TabRootProps { - initialTabIndex?: number -} - -export const TabRoot: FC = ({ - children, - initialTabIndex = 0, -}) => { - const [tabIndexAtom] = useState(() => atom(initialTabIndex)) - - const ctxValue = useMemo( - () => ({ - currentIndexAtom: tabIndexAtom, - loadedableIndexAtom: atom(new Set()), - tabScreensAtom: atom([]), - tabHeightAtom: atom(0), - }), - [tabIndexAtom], - ) +export const TabRoot: FC = ({ children }) => { const MapChildren = useMemo(() => { let cnt = 0 return React.Children.map(children, (child) => { @@ -40,9 +17,5 @@ export const TabRoot: FC = ({ return child }) }, [children]) - return ( - - {MapChildren} - - ) + return {MapChildren} } diff --git a/apps/mobile/src/modules/screen/PagerListContext.ts b/apps/mobile/src/modules/screen/PagerListContext.ts index 48b0773fb..e02890910 100644 --- a/apps/mobile/src/modules/screen/PagerListContext.ts +++ b/apps/mobile/src/modules/screen/PagerListContext.ts @@ -1,4 +1,4 @@ import { createContext } from "react" -export const PagerListVisibleContext = createContext(false) +export const PagerListVisibleContext = createContext(true) export const PagerListWillVisibleContext = createContext(false) diff --git a/apps/mobile/src/screens/(stack)/(tabs)/index.tsx b/apps/mobile/src/screens/(stack)/(tabs)/index.tsx index f5668dc01..bf13451de 100644 --- a/apps/mobile/src/screens/(stack)/(tabs)/index.tsx +++ b/apps/mobile/src/screens/(stack)/(tabs)/index.tsx @@ -1,3 +1,4 @@ +import { useResetTabOpacityWhenFocused } from "@/src/components/layouts/tabbar/hooks" import { usePrepareEntryRenderWebView } from "@/src/components/native/webview/hooks" import { Home5CuteFiIcon } from "@/src/icons/home_5_cute_fi" import { Home5CuteReIcon } from "@/src/icons/home_5_cute_re" @@ -6,7 +7,7 @@ import { EntryList } from "@/src/modules/entry-list" export const IndexTabScreen: TabScreenComponent = () => { usePrepareEntryRenderWebView() - + useResetTabOpacityWhenFocused() return } diff --git a/apps/mobile/src/screens/(stack)/(tabs)/subscriptions.tsx b/apps/mobile/src/screens/(stack)/(tabs)/subscriptions.tsx index 637a8b005..db049f416 100644 --- a/apps/mobile/src/screens/(stack)/(tabs)/subscriptions.tsx +++ b/apps/mobile/src/screens/(stack)/(tabs)/subscriptions.tsx @@ -6,6 +6,7 @@ import { useColor } from "react-native-uikit-colors" import { ErrorBoundary } from "@/src/components/common/ErrorBoundary" import { NoLoginInfo } from "@/src/components/common/NoLoginInfo" import { ListErrorView } from "@/src/components/errors/ListErrorView" +import { useResetTabOpacityWhenFocused } from "@/src/components/layouts/tabbar/hooks" import { BlackBoard2CuteFiIcon } from "@/src/icons/black_board_2_cute_fi" import { BlackBoard2CuteReIcon } from "@/src/icons/black_board_2_cute_re" import type { TabScreenComponent } from "@/src/lib/navigation/bottom-tab/types" @@ -18,7 +19,7 @@ import { useWhoami } from "@/src/store/user/hooks" export default function Subscriptions() { const whoami = useWhoami() const systemGroupedBackground = useColor("systemGroupedBackground") - + useResetTabOpacityWhenFocused() return ( ({ type: "subscriptions" }), [])}>