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 <tukon479@gmail.com>
This commit is contained in:
Innei
2025-04-14 23:50:07 +08:00
parent 00e625b052
commit ee5c66305a
7 changed files with 71 additions and 81 deletions
@@ -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<BottomTabContextType>(
() => ({
currentIndexAtom: tabIndexAtom,
loadedableIndexAtom: atom(new Set<number>()),
tabScreensAtom: atom<TabScreenProps[]>([]),
tabHeightAtom: atom(0),
}),
[tabIndexAtom],
)
return (
<BottomTabBarBackgroundContext.Provider value={useMemo(() => ({ opacity }), [opacity])}>
<SetBottomTabBarVisibleContext.Provider value={setTabBarVisible}>
<BottomTabBarVisibleContext.Provider value={tabBarVisible}>
<BottomTabHeightProvider>{children}</BottomTabHeightProvider>
</BottomTabBarVisibleContext.Provider>
</SetBottomTabBarVisibleContext.Provider>
</BottomTabBarBackgroundContext.Provider>
<BottomTabContext.Provider value={ctxValue}>
<BottomTabBarBackgroundContext.Provider value={useMemo(() => ({ opacity }), [opacity])}>
<SetBottomTabBarVisibleContext.Provider value={setTabBarVisible}>
<BottomTabBarVisibleContext.Provider value={tabBarVisible}>
<BottomTabHeightProvider>{children}</BottomTabHeightProvider>
</BottomTabBarVisibleContext.Provider>
</SetBottomTabBarVisibleContext.Provider>
</BottomTabBarBackgroundContext.Provider>
</BottomTabContext.Provider>
)
}
@@ -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 = <T = unknown>(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])
}
@@ -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<TabRootProps & PropsWithChildren> = ({
children,
initialTabIndex = 0,
}) => {
const [tabIndexAtom] = useState(() => atom(initialTabIndex))
const [tabIndex, setTabIndex] = useAtom(tabIndexAtom)
const ctxValue = useMemo<BottomTabContextType>(
() => ({
currentIndexAtom: tabIndexAtom,
loadedableIndexAtom: atom(new Set<number>()),
tabScreensAtom: atom<TabScreenProps[]>([]),
tabHeightAtom: atom(0),
}),
[tabIndexAtom],
)
export const TabRoot: FC<PropsWithChildren> = ({ 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<TabRootProps & PropsWithChildren> = ({
})
}, [children])
return (
<BottomTabContext.Provider value={ctxValue}>
<TabBarRoot
style={StyleSheet.absoluteFill}
onTabIndexChange={useCallback(
(e) => {
setTabIndex(e.nativeEvent.index)
},
[setTabIndex],
)}
selectedIndex={tabIndex}
>
{MapChildren}
</TabBarRoot>
</BottomTabContext.Provider>
<TabBarRoot
style={StyleSheet.absoluteFill}
onTabIndexChange={useCallback(
(e) => {
setTabIndex(e.nativeEvent.index)
},
[setTabIndex],
)}
selectedIndex={tabIndex}
>
{MapChildren}
</TabBarRoot>
)
}
@@ -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<TabRootProps & PropsWithChildren> = ({
children,
initialTabIndex = 0,
}) => {
const [tabIndexAtom] = useState(() => atom(initialTabIndex))
const ctxValue = useMemo<BottomTabContextType>(
() => ({
currentIndexAtom: tabIndexAtom,
loadedableIndexAtom: atom(new Set<number>()),
tabScreensAtom: atom<TabScreenProps[]>([]),
tabHeightAtom: atom(0),
}),
[tabIndexAtom],
)
export const TabRoot: FC<PropsWithChildren> = ({ children }) => {
const MapChildren = useMemo(() => {
let cnt = 0
return React.Children.map(children, (child) => {
@@ -40,9 +17,5 @@ export const TabRoot: FC<TabRootProps & PropsWithChildren> = ({
return child
})
}, [children])
return (
<BottomTabContext.Provider value={ctxValue}>
<View style={StyleSheet.absoluteFill}>{MapChildren}</View>
</BottomTabContext.Provider>
)
return <View style={StyleSheet.absoluteFill}>{MapChildren}</View>
}
@@ -1,4 +1,4 @@
import { createContext } from "react"
export const PagerListVisibleContext = createContext(false)
export const PagerListVisibleContext = createContext(true)
export const PagerListWillVisibleContext = createContext(false)
@@ -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 <EntryList />
}
@@ -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 (
<EntryListContext.Provider value={useMemo(() => ({ type: "subscriptions" }), [])}>
<RootSiblingParent>