refactor(mobile): reorganize navigation and layout components

This commit restructures the mobile app's navigation and layout components, moving them from scattered locations into a more organized structure. Key changes include:

- Created new layout and navigation-related directories
- Moved context, hooks, and view components to more logical locations
- Improved imports and component organization
- Added constants for UI-related values
- Simplified and standardized navigation and tab bar components

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-02-26 21:00:51 +08:00
parent e481486921
commit 09ed50a4d7
48 changed files with 777 additions and 270 deletions
@@ -1,215 +0,0 @@
import { Header, useHeaderHeight } from "@react-navigation/elements"
import type { NativeStackNavigationOptions } from "@react-navigation/native-stack"
import { router, Stack, useNavigation } from "expo-router"
import type { FC, PropsWithChildren } from "react"
import { createContext, useContext, useEffect, useMemo, useRef, useState } from "react"
import type { ScrollView, ScrollViewProps } from "react-native"
import {
Animated as RNAnimated,
StyleSheet,
TouchableOpacity,
useAnimatedValue,
View,
} from "react-native"
import Animated, { useAnimatedStyle, useSharedValue, withTiming } from "react-native-reanimated"
import type { ReanimatedScrollEvent } from "react-native-reanimated/lib/typescript/hook/commonTypes"
import { useSafeAreaInsets } from "react-native-safe-area-context"
import { useColor } from "react-native-uikit-colors"
import {
AttachNavigationScrollViewContext,
SetAttachNavigationScrollViewContext,
} from "@/src/components/ui/tabbar/contexts/AttachNavigationScrollViewContext"
import { useBottomTabBarHeight } from "@/src/components/ui/tabbar/hooks"
import { useDefaultHeaderHeight } from "@/src/hooks/useDefaultHeaderHeight"
import { MingcuteLeftLineIcon } from "@/src/icons/mingcute_left_line"
import { AnimatedScrollView } from "./AnimatedComponents"
import { ThemedBlurView } from "./ThemedBlurView"
type SafeNavigationScrollViewProps = Omit<ScrollViewProps, "onScroll"> & {
withHeaderBlur?: boolean
onScroll?: (e: ReanimatedScrollEvent) => void
// For scroll view content adjustment behavior
withTopInset?: boolean
withBottomInset?: boolean
} & PropsWithChildren
export const NavigationContext = createContext<{
scrollY: RNAnimated.Value
} | null>(null)
export const SafeNavigationScrollView: FC<SafeNavigationScrollViewProps> = ({
children,
withHeaderBlur = true,
onScroll,
withBottomInset = false,
withTopInset = false,
...props
}) => {
const insets = useSafeAreaInsets()
const tabBarHeight = useBottomTabBarHeight()
const headerHeight = useHeaderHeight()
const scrollY = useAnimatedValue(0)
const scrollViewRef = useRef<ScrollView>(null)
const setAttachNavigationScrollViewRef = useContext(SetAttachNavigationScrollViewContext)
useEffect(() => {
if (setAttachNavigationScrollViewRef) {
setAttachNavigationScrollViewRef(scrollViewRef)
}
}, [setAttachNavigationScrollViewRef, scrollViewRef])
return (
<NavigationContext.Provider value={useMemo(() => ({ scrollY }), [scrollY])}>
{withHeaderBlur && <NavigationBlurEffectHeader />}
<AnimatedScrollView
ref={scrollViewRef}
onScroll={RNAnimated.event([{ nativeEvent: { contentOffset: { y: scrollY } } }], {
useNativeDriver: true,
})}
{...props}
>
<View style={{ height: headerHeight - (withTopInset ? insets.top : 0) }} />
<AttachNavigationScrollViewContext.Provider value={scrollViewRef}>
<View>{children}</View>
</AttachNavigationScrollViewContext.Provider>
<View style={{ height: tabBarHeight - (withBottomInset ? insets.bottom : 0) }} />
</AnimatedScrollView>
</NavigationContext.Provider>
)
}
export const NavigationBlurEffectHeader = ({
blurThreshold = 0,
headerHideableBottom,
...props
}: NativeStackNavigationOptions & {
blurThreshold?: number
headerHideableBottom?: () => React.ReactNode
}) => {
const label = useColor("label")
const canBack = useNavigation().canGoBack()
const { scrollY } = useContext(NavigationContext)!
const border = useColor("opaqueSeparator")
const opacityAnimated = useSharedValue(0)
const originalDefaultHeaderHeight = useDefaultHeaderHeight()
const lastScrollY = useRef(0)
const largeDefaultHeaderHeightRef = useRef(0)
const largeHeaderHeight = useSharedValue(originalDefaultHeaderHeight)
const [hideableBottomRef, setHideableBottomRef] = useState<View | undefined>()
useEffect(() => {
hideableBottomRef?.measure((x, y, width, height) => {
const largeHeight = height + originalDefaultHeaderHeight
largeDefaultHeaderHeightRef.current = largeHeight
largeHeaderHeight.value = largeHeight
})
}, [hideableBottomRef, largeHeaderHeight, originalDefaultHeaderHeight])
useEffect(() => {
const id = scrollY.addListener(({ value }) => {
opacityAnimated.value = Math.max(0, Math.min(1, (value + blurThreshold) / 10))
if (!headerHideableBottom) {
return
}
const largeDefaultHeaderHeight = largeDefaultHeaderHeightRef.current
if (value <= 100) {
largeHeaderHeight.value = withTiming(largeDefaultHeaderHeight)
} else if (value > lastScrollY.current) {
largeHeaderHeight.value = withTiming(originalDefaultHeaderHeight)
} else {
largeHeaderHeight.value = withTiming(largeDefaultHeaderHeight)
}
lastScrollY.current = value
})
return () => {
scrollY.removeListener(id)
}
}, [
blurThreshold,
headerHideableBottom,
largeHeaderHeight,
opacityAnimated,
originalDefaultHeaderHeight,
scrollY,
])
const style = useAnimatedStyle(() => ({
opacity: opacityAnimated.value,
...StyleSheet.absoluteFillObject,
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: border,
}))
const hideableBottom = headerHideableBottom?.()
const { headerLeft, ...rest } = props
return (
<Stack.Screen
options={{
headerBackground: () => (
<Animated.View style={style}>
<ThemedBlurView className="flex-1" />
</Animated.View>
),
headerTransparent: true,
headerLeft:
headerLeft ??
(canBack
? () => (
<TouchableOpacity hitSlop={10} onPress={() => router.back()}>
<MingcuteLeftLineIcon height={20} width={20} color={label} />
</TouchableOpacity>
)
: undefined),
header: headerHideableBottom
? ({ options }) => {
return (
<Animated.View style={{ height: largeHeaderHeight }} className="overflow-hidden">
<View pointerEvents="box-none" style={StyleSheet.absoluteFill}>
{options.headerBackground?.()}
</View>
<Header title={options.title ?? ""} {...options} headerBackground={() => null} />
{!!hideableBottom && (
<View
ref={setHideableBottomRef as any}
onLayout={(e) => {
const { height } = e.nativeEvent.layout
largeDefaultHeaderHeightRef.current = height + originalDefaultHeaderHeight
largeHeaderHeight.value = largeDefaultHeaderHeightRef.current
}}
>
{hideableBottom}
</View>
)}
</Animated.View>
)
}
: undefined,
...rest,
}}
/>
)
}
@@ -0,0 +1,303 @@
import { getDefaultHeaderHeight, HeaderTitle } from "@react-navigation/elements"
import { router, useNavigation } from "expo-router"
import type { FC, PropsWithChildren, ReactNode } from "react"
import { createElement, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react"
import type { LayoutChangeEvent } from "react-native"
import { StyleSheet, TouchableOpacity, useWindowDimensions, View } from "react-native"
import type { AnimatedProps } from "react-native-reanimated"
import Animated, {
useAnimatedRef,
useAnimatedStyle,
useSharedValue,
withTiming,
} from "react-native-reanimated"
import type { DefaultStyle } from "react-native-reanimated/lib/typescript/hook/commonTypes"
import { useSafeAreaFrame, useSafeAreaInsets } from "react-native-safe-area-context"
import type { ViewProps } from "react-native-svg/lib/typescript/fabric/utils"
import { useColor } from "react-native-uikit-colors"
import { MingcuteLeftLineIcon } from "@/src/icons/mingcute_left_line"
import { ThemedBlurView } from "../../common/ThemedBlurView"
import { PortalHost } from "../../ui/portal"
import { NavigationContext } from "../views/NavigationContext"
import { SetNavigationHeaderHeightContext } from "../views/NavigationHeaderContext"
export interface NavigationHeaderRawProps {
headerLeft?: FC<{
canGoBack: boolean
}>
headerTitle?: FC<React.ComponentProps<typeof HeaderTitle>> | ReactNode
headerRight?: FC<{
canGoBack: boolean
}>
title?: string
modal?: boolean
hideableBottom?: ReactNode
hideableBottomHeight?: number
}
const useHideableBottom = (
enable: boolean,
originalDefaultHeaderHeight: number,
hideableBottomHeight?: number,
) => {
const lastScrollY = useRef(0)
const largeDefaultHeaderHeightRef = useRef(
originalDefaultHeaderHeight + (hideableBottomHeight || 0),
)
const largeHeaderHeight = useSharedValue(originalDefaultHeaderHeight)
const [hideableBottomRef, setHideableBottomRef] = useState<View | undefined>()
useEffect(() => {
hideableBottomRef?.measure((x, y, width, height) => {
const largeHeight = height + originalDefaultHeaderHeight
largeDefaultHeaderHeightRef.current = largeHeight
largeHeaderHeight.value = largeHeight
})
}, [hideableBottomRef, largeHeaderHeight, originalDefaultHeaderHeight])
const { scrollY } = useContext(NavigationContext)!
useEffect(() => {
const id = scrollY.addListener(({ value }) => {
if (!enable) {
return
}
const largeDefaultHeaderHeight = largeDefaultHeaderHeightRef.current
if (value <= 100) {
largeHeaderHeight.value = withTiming(largeDefaultHeaderHeight)
} else if (value > lastScrollY.current) {
largeHeaderHeight.value = withTiming(originalDefaultHeaderHeight)
} else {
largeHeaderHeight.value = withTiming(largeDefaultHeaderHeight)
}
lastScrollY.current = value
})
return () => {
scrollY.removeListener(id)
}
}, [enable, largeHeaderHeight, originalDefaultHeaderHeight, scrollY])
const layoutHeightOnceRef = useRef(false)
const onLayout = useCallback(
(e: LayoutChangeEvent) => {
if (typeof hideableBottomHeight === "number") {
return
}
const { height } = e.nativeEvent.layout
if (!height) return
if (layoutHeightOnceRef.current) {
return
}
layoutHeightOnceRef.current = true
largeDefaultHeaderHeightRef.current = height + originalDefaultHeaderHeight
largeHeaderHeight.value = largeDefaultHeaderHeightRef.current
},
[hideableBottomHeight, largeHeaderHeight, originalDefaultHeaderHeight],
)
return {
hideableBottomRef,
setHideableBottomRef,
largeHeaderHeight,
largeDefaultHeaderHeightRef,
onLayout,
}
}
interface NavigationHeaderProps
extends Omit<AnimatedProps<ViewProps>, "children">,
NavigationHeaderRawProps,
PropsWithChildren {}
const blurThreshold = 0
const titlebarPaddingHorizontal = 8
const titleMarginHorizontal = 16
export const NavigationHeader = ({
style,
children,
headerLeft,
headerRight,
title,
headerTitle: customHeaderTitle,
modal = false,
hideableBottom,
hideableBottomHeight,
...rest
}: NavigationHeaderProps) => {
const insets = useSafeAreaInsets()
const frame = useSafeAreaFrame()
const defaultHeight = useMemo(
() => getDefaultHeaderHeight(frame, modal, insets.top),
[frame, insets.top, modal],
)
const border = useColor("opaqueSeparator")
const opacityAnimated = useSharedValue(0)
const { scrollY } = useContext(NavigationContext)!
const setHeaderHeight = useContext(SetNavigationHeaderHeightContext)
useEffect(() => {
const id = scrollY.addListener(({ value }) => {
opacityAnimated.value = Math.max(0, Math.min(1, (value + blurThreshold) / 10))
})
return () => {
scrollY.removeListener(id)
}
}, [opacityAnimated, scrollY])
const blurStyle = useAnimatedStyle(() => ({
opacity: opacityAnimated.value,
...StyleSheet.absoluteFillObject,
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: border,
}))
const { setHideableBottomRef, largeHeaderHeight, onLayout } = useHideableBottom(
!!hideableBottom,
defaultHeight,
hideableBottomHeight,
)
const rootHeightStyle = useAnimatedStyle(() => {
const styles = {
paddingTop: insets.top,
minHeight: defaultHeight,
position: "relative",
overflow: "hidden",
} satisfies DefaultStyle
if (hideableBottom) {
;(styles as DefaultStyle).height = largeHeaderHeight.value
}
return styles
})
const navigation = useNavigation()
const canBack = navigation.canGoBack()
useEffect(() => {
if (title) {
navigation.setOptions({ title })
}
}, [navigation, title])
const HeaderLeft = headerLeft ?? DefaultHeaderLeft
const renderTitle = customHeaderTitle ?? HeaderTitle
const headerTitle =
typeof renderTitle !== "function"
? renderTitle
: createElement(renderTitle, {
children: title,
})
const RightButton = headerRight ?? Noop
const animatedRef = useAnimatedRef<Animated.View>()
useEffect(() => {
animatedRef.current?.measure((x, y, width, height) => {
setHeaderHeight?.(height)
})
}, [animatedRef, setHeaderHeight])
const [headerLeftWidth, setHeaderLeftWidth] = useState(0)
const titleBarWidth = useWindowDimensions().width
const [titleWidth, setTitleWidth] = useState(0)
const titleShouldCenterTransformX = useMemo(() => {
const halfTitleWidth = titleWidth / 2
const currentTitleCenterX =
titlebarPaddingHorizontal + headerLeftWidth + titleMarginHorizontal + halfTitleWidth
const centerX = titleBarWidth / 2
const transformX = centerX - currentTitleCenterX
return transformX
}, [titleBarWidth, titleWidth, headerLeftWidth])
return (
<Animated.View
ref={animatedRef}
pointerEvents={"box-none"}
{...rest}
style={[rootHeightStyle, style]}
onLayout={useCallback(
(e: LayoutChangeEvent) => {
setHeaderHeight?.(e.nativeEvent.layout.height)
},
[setHeaderHeight],
)}
>
<Animated.View style={blurStyle} pointerEvents={"none"}>
<ThemedBlurView className="flex-1" />
</Animated.View>
{/* Grid */}
<View
className="relative flex-row items-stretch"
style={{
marginLeft: insets.left,
marginRight: insets.right,
paddingHorizontal: titlebarPaddingHorizontal,
}}
pointerEvents={"box-none"}
>
<PortalHost>
{/* Left */}
<View
className="min-w-6 flex-row items-center justify-start"
pointerEvents={"box-none"}
onLayout={useCallback((e: LayoutChangeEvent) => {
setHeaderLeftWidth(e.nativeEvent.layout.width)
}, [])}
>
<HeaderLeft canGoBack={canBack} />
</View>
{/* Center */}
<View
onLayout={useCallback((e: LayoutChangeEvent) => {
setTitleWidth(e.nativeEvent.layout.width)
}, [])}
className="flex-1 items-center justify-center"
pointerEvents={"box-none"}
style={{
marginHorizontal: titleMarginHorizontal,
transform: [{ translateX: titleShouldCenterTransformX }],
}}
>
{headerTitle}
</View>
{/* Right */}
<View className="min-w-6 flex-row items-center justify-end" pointerEvents={"box-none"}>
<RightButton canGoBack={canBack} />
</View>
</PortalHost>
</View>
{!!hideableBottom && (
<View ref={setHideableBottomRef as any} onLayout={onLayout}>
{hideableBottom}
</View>
)}
</Animated.View>
)
}
const DefaultHeaderLeft = ({ canGoBack }: { canGoBack: boolean }) => {
const label = useColor("label")
if (!canGoBack) return null
return (
<TouchableOpacity hitSlop={10} onPress={() => router.back()}>
<MingcuteLeftLineIcon height={20} width={20} color={label} />
</TouchableOpacity>
)
}
const Noop = () => null
@@ -0,0 +1,11 @@
import { useContext } from "react"
import { NavigationHeaderHeightContext } from "../views/NavigationHeaderContext"
export const useNavigationHeaderHeight = () => {
const headerHeight = useContext(NavigationHeaderHeightContext)
if (!headerHeight) {
throw new Error("NavigationHeaderHeightContext is not found")
}
return headerHeight
}
@@ -1,6 +1,6 @@
import { useState } from "react"
import { BottomTabBarHeightContext } from "@/src/components/ui/tabbar/contexts/BottomTabBarHeightContext"
import { BottomTabBarHeightContext } from "@/src/components/layouts/tabbar/contexts/BottomTabBarHeightContext"
import { SetBottomTabBarHeightContext } from "./contexts/BottomTabBarHeightContext"
@@ -13,14 +13,14 @@ import Animated, {
} from "react-native-reanimated"
import { useSafeAreaInsets } from "react-native-safe-area-context"
import { SetBottomTabBarHeightContext } from "@/src/components/ui/tabbar/contexts/BottomTabBarHeightContext"
import { SetBottomTabBarHeightContext } from "@/src/components/layouts/tabbar/contexts/BottomTabBarHeightContext"
import { quickSpringPreset, softSpringPreset } from "@/src/constants/spring"
import { PlayerTabBar } from "@/src/modules/player/PlayerTabBar"
import { accentColor, useColor } from "@/src/theme/colors"
import { ThemedBlurView } from "../../common/ThemedBlurView"
import { Grid } from "../../ui/grid"
import { ScaleFadeRotateIn } from "../../ux/animations/ScaleFadeRotate"
import { Grid } from "../grid"
import { BottomTabBarBackgroundContext } from "./contexts/BottomTabBarBackgroundContext"
import { BottomTabBarVisibleContext } from "./contexts/BottomTabBarVisibleContext"
@@ -0,0 +1,6 @@
import { createContext } from "react"
import type { Animated } from "react-native"
export const NavigationContext = createContext<{
scrollY: Animated.Value
} | null>(null)
@@ -0,0 +1,7 @@
import type { Dispatch, SetStateAction } from "react"
import { createContext } from "react"
export const NavigationHeaderHeightContext = createContext<number>(null!)
export const SetNavigationHeaderHeightContext = createContext<Dispatch<SetStateAction<number>>>(
null!,
)
@@ -0,0 +1,150 @@
import { useTypeScriptHappyCallback } from "@follow/hooks"
import { getDefaultHeaderHeight } from "@react-navigation/elements"
import type { NativeStackNavigationOptions } from "@react-navigation/native-stack"
import { router, Stack, useNavigation } from "expo-router"
import type { FC, PropsWithChildren } from "react"
import { useContext, useEffect, useMemo, useRef, useState } from "react"
import type { ScrollView, ScrollViewProps } from "react-native"
import { Animated as RNAnimated, TouchableOpacity, useAnimatedValue, View } from "react-native"
import type { ReanimatedScrollEvent } from "react-native-reanimated/lib/typescript/hook/commonTypes"
import { useSafeAreaFrame, useSafeAreaInsets } from "react-native-safe-area-context"
import { useColor } from "react-native-uikit-colors"
import {
AttachNavigationScrollViewContext,
SetAttachNavigationScrollViewContext,
} from "@/src/components/layouts/tabbar/contexts/AttachNavigationScrollViewContext"
import { useBottomTabBarHeight } from "@/src/components/layouts/tabbar/hooks"
import { MingcuteLeftLineIcon } from "@/src/icons/mingcute_left_line"
import { AnimatedScrollView } from "../../common/AnimatedComponents"
import { NavigationHeader } from "../header/NavigationHeader"
import { NavigationContext } from "./NavigationContext"
import {
NavigationHeaderHeightContext,
SetNavigationHeaderHeightContext,
} from "./NavigationHeaderContext"
type SafeNavigationScrollViewProps = Omit<ScrollViewProps, "onScroll"> & {
withHeaderBlur?: boolean
onScroll?: (e: ReanimatedScrollEvent) => void
// For scroll view content adjustment behavior
withTopInset?: boolean
withBottomInset?: boolean
} & PropsWithChildren
export const SafeNavigationScrollView: FC<SafeNavigationScrollViewProps> = ({
children,
withHeaderBlur = true,
onScroll,
withBottomInset = false,
withTopInset = false,
...props
}) => {
const insets = useSafeAreaInsets()
const tabBarHeight = useBottomTabBarHeight()
const scrollY = useAnimatedValue(0)
const scrollViewRef = useRef<ScrollView>(null)
const setAttachNavigationScrollViewRef = useContext(SetAttachNavigationScrollViewContext)
useEffect(() => {
if (setAttachNavigationScrollViewRef) {
setAttachNavigationScrollViewRef(scrollViewRef)
}
}, [setAttachNavigationScrollViewRef, scrollViewRef])
const frame = useSafeAreaFrame()
const [headerHeight, setHeaderHeight] = useState(() =>
getDefaultHeaderHeight(frame, false, insets.top),
)
return (
<NavigationContext.Provider value={useMemo(() => ({ scrollY }), [scrollY])}>
<NavigationHeaderHeightContext.Provider value={headerHeight}>
<SetNavigationHeaderHeightContext.Provider value={setHeaderHeight}>
{withHeaderBlur && <NavigationBlurEffectHeader />}
<AnimatedScrollView
ref={scrollViewRef}
onScroll={RNAnimated.event([{ nativeEvent: { contentOffset: { y: scrollY } } }], {
useNativeDriver: true,
})}
{...props}
>
<View style={{ height: headerHeight - (withTopInset ? insets.top : 0) }} />
<AttachNavigationScrollViewContext.Provider value={scrollViewRef}>
<View>{children}</View>
</AttachNavigationScrollViewContext.Provider>
<View style={{ height: tabBarHeight - (withBottomInset ? insets.bottom : 0) }} />
</AnimatedScrollView>
</SetNavigationHeaderHeightContext.Provider>
</NavigationHeaderHeightContext.Provider>
</NavigationContext.Provider>
)
}
export const NavigationBlurEffectHeader = ({
headerHideableBottom,
headerHideableBottomHeight,
...props
}: NativeStackNavigationOptions & {
blurThreshold?: number
headerHideableBottomHeight?: number
headerHideableBottom?: () => React.ReactNode
}) => {
const label = useColor("label")
const canBack = useNavigation().canGoBack()
const navigationContext = useContext(NavigationContext)!
const setHeaderHeight = useContext(SetNavigationHeaderHeightContext)
const hideableBottom = headerHideableBottom?.()
const { headerLeft, ...rest } = props
return (
<Stack.Screen
options={{
headerTransparent: true,
headerShown: true,
headerLeft:
headerLeft ??
(canBack
? () => (
<TouchableOpacity hitSlop={10} onPress={() => router.back()}>
<MingcuteLeftLineIcon height={20} width={20} color={label} />
</TouchableOpacity>
)
: undefined),
header: useTypeScriptHappyCallback(
({ options }) => (
<NavigationContext.Provider value={navigationContext}>
<SetNavigationHeaderHeightContext.Provider value={setHeaderHeight}>
<NavigationHeader
modal={options.presentation === "modal" || options.presentation === "formSheet"}
title={options.title}
headerRight={options.headerRight}
headerLeft={options.headerLeft}
hideableBottom={hideableBottom}
hideableBottomHeight={headerHideableBottomHeight}
// @ts-expect-error
headerTitle={options.headerTitle}
/>
</SetNavigationHeaderHeightContext.Provider>
</NavigationContext.Provider>
),
[hideableBottom, navigationContext],
),
...rest,
}}
/>
)
}
@@ -0,0 +1,47 @@
import * as React from "react"
import { useEffect, useMemo, useRef } from "react"
import PortalContext from "./PortalContext"
import type { IPortalMethods, PortalConsumerProps } from "./type"
const PortalConsumer: React.FC<PortalConsumerProps> = (props) => {
const id = React.useId()
const key = useRef<string>(id)
const once = useRef<boolean>(false)
useEffect(
() => () => {
const { manager } = props
manager?.unmount?.(key.current)
},
[],
)
useEffect(() => {
const { manager, children } = props
if (!once.current) {
once.current = true
key.current = manager?.mount?.(children) ?? ""
} else {
manager?.update?.(key.current, children)
}
}, [props])
return null
}
export function Portal({ children }: React.PropsWithChildren) {
return useMemo(
() => (
<PortalContext.Consumer>
{(manager: IPortalMethods | null): React.ReactNode => (
<PortalConsumer manager={manager}>{children}</PortalConsumer>
)}
</PortalContext.Consumer>
),
[children],
)
}
export { PortalConsumerProps, PortalProps } from "./type"
@@ -0,0 +1,5 @@
import * as React from "react"
import type { IPortalMethods } from "./type"
export default React.createContext<IPortalMethods | null>(null)
@@ -0,0 +1,87 @@
import * as React from "react"
import { useCallback, useMemo, useRef } from "react"
import PortalContext from "./PortalContext"
import PortalManager from "./PortalManager"
import type { IPortalManager, Operation, PortalKey } from "./type"
interface PortalHostProps {
children: React.ReactNode
}
export function PortalHost(props: PortalHostProps) {
const id = React.useId()
const nextKey = useRef(`${id}-0`)
const queue = useRef<Operation[]>([])
const manager = useRef<IPortalManager>()
const mount = useCallback((children: React.ReactNode, _key?: PortalKey) => {
let key = _key
if (!key) {
key = nextKey.current
nextKey.current = `${id}-${Number.parseInt(key) + 1}`
}
if (manager.current) {
manager.current.mount(key, children)
} else {
queue.current.push({ type: "mount", key, children })
}
return key
}, [])
const update = useCallback((key: PortalKey, children: React.ReactNode) => {
if (manager.current) {
manager.current.update(key, children)
} else {
const op: Operation = { type: "mount", key, children }
const index = queue.current.findIndex(
(o) => o.type === "mount" || (o.type === "update" && o.key === key),
)
if (index !== -1) {
queue.current[index] = op
} else {
queue.current.push(op)
}
}
}, [])
const unmount = useCallback((key: PortalKey) => {
if (manager.current) {
manager.current.unmount(key)
} else {
queue.current.push({ type: "unmount", key })
}
}, [])
const { children } = props
const ctxValue = useMemo(
() => ({
mount,
update,
unmount,
}),
[mount, update, unmount],
)
return useMemo(
() => (
<>
<PortalContext.Provider value={ctxValue}>
<>
{/* placeholder */}
{children}
{/* portal container */}
<PortalManager ref={manager} />
</>
</PortalContext.Provider>
</>
),
[ctxValue, children],
)
}
PortalHost.displayName = "Portal.host"
@@ -0,0 +1,46 @@
import * as React from "react"
import { useCallback, useImperativeHandle, useMemo, useState } from "react"
import { StyleSheet, View } from "react-native"
export default React.forwardRef((_, ref: any) => {
const [portals, setPortals] = useState<{ key: number; children: React.ReactNode }[]>([])
const mount = useCallback((key: number, children: React.ReactNode) => {
setPortals((oldPortals) => [...oldPortals, { key, children }])
}, [])
const update = useCallback((key: number, children: React.ReactNode) => {
setPortals((oldPortals) =>
oldPortals.map((item) => {
if (item.key === key) {
return { ...item, children }
}
return { ...item }
}),
)
}, [])
const unmount = useCallback(
(key: number) => setPortals((oldPortals) => oldPortals.filter((item) => item.key !== key)),
[],
)
useImperativeHandle(ref, () => ({
mount,
update,
unmount,
}))
return useMemo(
() => (
<>
{portals.map(({ key, children }) => (
<View key={key} style={StyleSheet.absoluteFill} pointerEvents="box-none">
{children}
</View>
))}
</>
),
[portals],
)
})
@@ -0,0 +1,2 @@
export { Portal } from "./Portal"
export { PortalHost } from "./PortalHost"
@@ -0,0 +1,44 @@
import type * as React from "react"
export type PortalKey = string
export interface IPortalManager {
mount: (key: PortalKey, children: React.ReactNode) => void
update: (key: PortalKey, children: React.ReactNode) => void
unmount: (key: PortalKey) => void
}
export interface IPortalMethods {
mount: (children: React.ReactNode) => PortalKey
update: (key: PortalKey, children: React.ReactNode) => void
unmount: (key: PortalKey) => void
}
export type Operation =
| {
type: "mount"
key: PortalKey
children: React.ReactNode
}
| {
type: "update"
key: PortalKey
children: React.ReactNode
}
| {
type: "unmount"
key: PortalKey
}
export interface IStaticContainerProps {
shouldUpdate: boolean
children: React.ReactNode
}
export interface PortalProps {
children?: React.ReactNode
}
export interface PortalConsumerProps {
manager: IPortalMethods | null
children: React.ReactNode
}
+1
View File
@@ -0,0 +1 @@
export const TIMELINE_VIEW_SELECTOR_HEIGHT = 58
@@ -23,7 +23,7 @@ import { AnimatedScrollView } from "@/src/components/common/AnimatedComponents"
import {
useBottomTabBarHeight,
useRegisterNavigationScrollView,
} from "@/src/components/ui/tabbar/hooks"
} from "@/src/components/layouts/tabbar/hooks"
import type { TabComponent } from "@/src/components/ui/tabview/TabView"
import { apiClient } from "@/src/lib/api-fetch"
@@ -1,4 +1,5 @@
import { useAtom } from "jotai"
import { useState } from "react"
import { Clipboard, Share, TouchableOpacity, View } from "react-native"
import type { SharedValue } from "react-native-reanimated"
import Animated, { interpolate, useAnimatedStyle } from "react-native-reanimated"
@@ -31,6 +32,7 @@ interface HeaderRightActionsProps {
titleOpacityShareValue: SharedValue<number>
isHeaderTitleVisible: boolean
}
const HeaderRightActionsImpl = ({
entryId,
titleOpacityShareValue,
@@ -93,15 +95,23 @@ const HeaderRightActionsImpl = ({
})
}
const [extraActionContainerWidth, setExtraActionContainerWidth] = useState(0)
return (
<View className="relative flex-row gap-4">
{!isHeaderTitleVisible && (
<View style={{ width: extraActionContainerWidth }} pointerEvents="none" />
)}
<Animated.View
onLayout={(e) => {
setExtraActionContainerWidth(e.nativeEvent.layout.width)
}}
style={useAnimatedStyle(() => {
return {
opacity: interpolate(titleOpacityShareValue.value, [0, 1], [1, 0]),
}
})}
className="absolute right-[32px] flex-row gap-2"
className="absolute right-[32px] z-10 flex-row gap-2"
>
{!!subscription && (
<ActionBarItem
@@ -3,14 +3,13 @@ import { useCallback, useContext, useEffect, useState } from "react"
import { Text, View } from "react-native"
import Animated, { useSharedValue, withTiming } from "react-native-reanimated"
import {
NavigationBlurEffectHeader,
NavigationContext,
} from "@/src/components/common/SafeNavigationScrollView"
import { NavigationContext } from "@/src/components/layouts/views/NavigationContext"
import { NavigationBlurEffectHeader } from "@/src/components/layouts/views/SafeNavigationScrollView"
import { UserAvatar } from "@/src/components/ui/avatar/UserAvatar"
import { FeedIcon } from "@/src/components/ui/icon/feed-icon"
import { Portal } from "@/src/components/ui/portal/Portal"
import { EntryContentContext, useEntryContentContext } from "@/src/modules/entry-content/ctx"
import { EntryContentHeaderRightActions } from "@/src/modules/entry-content/HeaderActions"
import { EntryContentHeaderRightActions } from "@/src/modules/entry-content/EntryContentHeaderRightActions"
import { useEntry } from "@/src/store/entry/hooks"
import { useFeed } from "@/src/store/feed/hooks"
@@ -60,13 +59,17 @@ export const EntryTitle = ({ title, entryId }: { title: string; entryId: string
[ctxValue, entryId, opacityAnimatedValue, isHeaderTitleVisible],
)}
headerTitle={() => (
<Animated.Text
className={"text-label text-[17px] font-semibold"}
numberOfLines={1}
style={{ opacity: opacityAnimatedValue }}
>
{title}
</Animated.Text>
<Portal>
<View className="absolute inset-x-12 inset-y-0 flex-row items-center justify-center">
<Animated.Text
className={"text-label text-center text-[17px] font-semibold"}
numberOfLines={1}
style={{ opacity: opacityAnimatedValue }}
>
{title}
</Animated.Text>
</View>
</Portal>
)}
/>
<View
@@ -4,7 +4,7 @@ import type { RefObject } from "react"
import { useContext, useEffect, useRef } from "react"
import type { ScrollView } from "react-native"
import { SetAttachNavigationScrollViewContext } from "@/src/components/ui/tabbar/contexts/AttachNavigationScrollViewContext"
import { SetAttachNavigationScrollViewContext } from "@/src/components/layouts/tabbar/contexts/AttachNavigationScrollViewContext"
import { EntryListContentGrid } from "@/src/modules/entry-list/EntryListContentGrid"
import { EntryListContentArticle } from "./EntryListContentArticle"
@@ -17,10 +17,10 @@ import {
import { useSharedValue } from "react-native-reanimated"
import { useSafeAreaInsets } from "react-native-safe-area-context"
import { BottomTabBarHeightContext } from "@/src/components/layouts/tabbar/contexts/BottomTabBarHeightContext"
import { AccordionItem } from "@/src/components/ui/accordion/AccordionItem"
import { FeedIcon } from "@/src/components/ui/icon/feed-icon"
import { ItemPressable } from "@/src/components/ui/pressable/ItemPressable"
import { BottomTabBarHeightContext } from "@/src/components/ui/tabbar/contexts/BottomTabBarHeightContext"
import { MingcuteRightLine } from "@/src/icons/mingcute_right_line"
import { useFeed, usePrefetchFeed } from "@/src/store/feed/hooks"
import { useList } from "@/src/store/list/hooks"
@@ -11,8 +11,8 @@ import { RefreshControl } from "react-native"
import { useSafeAreaInsets } from "react-native-safe-area-context"
import { useColor } from "react-native-uikit-colors"
import { NavigationContext } from "@/src/components/common/SafeNavigationScrollView"
import { useBottomTabBarHeight } from "@/src/components/ui/tabbar/hooks"
import { useBottomTabBarHeight } from "@/src/components/layouts/tabbar/hooks"
import { NavigationContext } from "@/src/components/layouts/views/NavigationContext"
import { useHeaderHeight } from "@/src/modules/screen/hooks/useHeaderHeight"
import { usePrefetchSubscription } from "@/src/store/subscription/hooks"
import { usePrefetchUnread } from "@/src/store/unread/hooks"
@@ -3,11 +3,10 @@ import { useMemo } from "react"
import { Share, useAnimatedValue, View } from "react-native"
import { useColor } from "react-native-uikit-colors"
import {
NavigationBlurEffectHeader,
NavigationContext,
} from "@/src/components/common/SafeNavigationScrollView"
import { NavigationContext } from "@/src/components/layouts/views/NavigationContext"
import { NavigationBlurEffectHeader } from "@/src/components/layouts/views/SafeNavigationScrollView"
import { UIBarButton } from "@/src/components/ui/button/UIBarButton"
import { TIMELINE_VIEW_SELECTOR_HEIGHT } from "@/src/constants/ui"
import { Share3CuteReIcon } from "@/src/icons/share_3_cute_re"
import { getWebUrl } from "@/src/lib/env"
import {
@@ -56,6 +55,7 @@ export function TimelineSelectorProvider({ children }: { children: React.ReactNo
return
}, [isFeed, isTimeline, isSubscriptions, params])}
headerHideableBottom={isTimeline || isSubscriptions ? TimelineViewSelector : undefined}
headerHideableBottomHeight={TIMELINE_VIEW_SELECTOR_HEIGHT}
/>
{children}
</NavigationContext.Provider>
@@ -10,6 +10,7 @@ import Animated, {
import { ReAnimatedTouchableOpacity } from "@/src/components/common/AnimatedComponents"
import { gentleSpringPreset } from "@/src/constants/spring"
import { TIMELINE_VIEW_SELECTOR_HEIGHT } from "@/src/constants/ui"
import type { ViewDefinition } from "@/src/constants/views"
import { selectTimeline, useSelectedFeed } from "@/src/modules/screen/atoms"
import { useViewWithSubscription } from "@/src/store/subscription/hooks"
@@ -25,7 +26,10 @@ export function TimelineViewSelector() {
const activeViews = useViewWithSubscription()
return (
<View className="flex items-center justify-between py-2">
<View
className="flex items-center justify-between py-2"
style={{ height: TIMELINE_VIEW_SELECTOR_HEIGHT }}
>
<ScrollView
horizontal
scrollsToTop={false}
+3 -5
View File
@@ -18,16 +18,14 @@ import { AddFeedDialog } from "../dialogs/AddFeedDialog"
import { MarkAllAsReadDialog } from "../dialogs/MarkAllAsReadDialog"
const ActionGroup = ({ children, className }: PropsWithChildren<{ className?: string }>) => {
return (
<View className={cn("flex flex-row items-center gap-2 px-[12px]", className)}>{children}</View>
)
return <View className={cn("flex flex-row items-center gap-2", className)}>{children}</View>
}
export function HomeLeftAction() {
const user = useWhoami()
if (!user) return null
return (
<ActionGroup>
<ActionGroup className="ml-2">
<TouchableOpacity onPress={() => router.push("/profile")}>
<UserAvatar image={user.image} name={user.name!} size={28} className="rounded-full" />
</TouchableOpacity>
@@ -37,7 +35,7 @@ export function HomeLeftAction() {
export function HomeSharedRightAction(props: PropsWithChildren) {
return (
<ActionGroup className="-mr-2">
<ActionGroup>
{props.children}
<UIBarButton
@@ -6,13 +6,13 @@ import type { ScrollView } from "react-native"
import { Alert, View } from "react-native"
import { useSafeAreaInsets } from "react-native-safe-area-context"
import { SetBottomTabBarVisibleContext } from "@/src/components/layouts/tabbar/contexts/BottomTabBarVisibleContext"
import { useBottomTabBarHeight } from "@/src/components/layouts/tabbar/hooks"
import {
GroupedInsetListCard,
GroupedInsetListNavigationLink,
GroupedInsetListNavigationLinkIcon,
} from "@/src/components/ui/grouped/GroupedList"
import { SetBottomTabBarVisibleContext } from "@/src/components/ui/tabbar/contexts/BottomTabBarVisibleContext"
import { useBottomTabBarHeight } from "@/src/components/ui/tabbar/hooks"
import { getDbPath } from "@/src/database"
import { BellRingingCuteFiIcon } from "@/src/icons/bell_ringing_cute_fi"
import { CertificateCuteFiIcon } from "@/src/icons/certificate_cute_fi"
@@ -5,7 +5,7 @@ import { Linking, Text, View } from "react-native"
import {
NavigationBlurEffectHeader,
SafeNavigationScrollView,
} from "@/src/components/common/SafeNavigationScrollView"
} from "@/src/components/layouts/views/SafeNavigationScrollView"
import {
GroupedInsetListBaseCell,
GroupedInsetListCard,
@@ -4,7 +4,7 @@ import { Text, TouchableOpacity, View } from "react-native"
import {
NavigationBlurEffectHeader,
SafeNavigationScrollView,
} from "@/src/components/common/SafeNavigationScrollView"
} from "@/src/components/layouts/views/SafeNavigationScrollView"
import {
GroupedInsetListBaseCell,
GroupedInsetListCard,
@@ -5,7 +5,7 @@ import { setUISetting, useUISettingKey } from "@/src/atoms/settings/ui"
import {
NavigationBlurEffectHeader,
SafeNavigationScrollView,
} from "@/src/components/common/SafeNavigationScrollView"
} from "@/src/components/layouts/views/SafeNavigationScrollView"
import { Select } from "@/src/components/ui/form/Select"
import {
GroupedInsetListCard,
@@ -7,7 +7,7 @@ import { setDataSetting, useDataSettingKey } from "@/src/atoms/settings/data"
import {
NavigationBlurEffectHeader,
SafeNavigationScrollView,
} from "@/src/components/common/SafeNavigationScrollView"
} from "@/src/components/layouts/views/SafeNavigationScrollView"
import {
GroupedInsetListActionCell,
GroupedInsetListCard,
@@ -5,7 +5,7 @@ import { setGeneralSetting, useGeneralSettingKey } from "@/src/atoms/settings/ge
import {
NavigationBlurEffectHeader,
SafeNavigationScrollView,
} from "@/src/components/common/SafeNavigationScrollView"
} from "@/src/components/layouts/views/SafeNavigationScrollView"
import { Select } from "@/src/components/ui/form/Select"
import {
GroupedInsetListBaseCell,
@@ -9,7 +9,7 @@ import { Balance } from "@/src/components/common/Balance"
import {
NavigationBlurEffectHeader,
SafeNavigationScrollView,
} from "@/src/components/common/SafeNavigationScrollView"
} from "@/src/components/layouts/views/SafeNavigationScrollView"
import {
GroupedInformationCell,
GroupedInsetListCard,
@@ -9,7 +9,7 @@ import { ModalHeaderSubmitButton } from "@/src/components/common/ModalSharedComp
import {
NavigationBlurEffectHeader,
SafeNavigationScrollView,
} from "@/src/components/common/SafeNavigationScrollView"
} from "@/src/components/layouts/views/SafeNavigationScrollView"
import {
GroupedInsetListBaseCell,
GroupedInsetListCard,
@@ -3,7 +3,7 @@ import { router } from "expo-router"
import {
NavigationBlurEffectHeader,
SafeNavigationScrollView,
} from "@/src/components/common/SafeNavigationScrollView"
} from "@/src/components/layouts/views/SafeNavigationScrollView"
import {
GroupedInsetListCard,
GroupedInsetListNavigationLink,
@@ -4,8 +4,8 @@ import { useMemo, useState } from "react"
import { Text } from "react-native"
import { useEventCallback } from "usehooks-ts"
import { useRegisterNavigationScrollView } from "@/src/components/layouts/tabbar/hooks"
import { ItemPressable } from "@/src/components/ui/pressable/ItemPressable"
import { useRegisterNavigationScrollView } from "@/src/components/ui/tabbar/hooks"
import { StarCuteFiIcon } from "@/src/icons/star_cute_fi"
import { closeDrawer, selectFeed } from "@/src/modules/screen/atoms"
import { TimelineSelectorList } from "@/src/modules/screen/TimelineSelectorList"
+2 -4
View File
@@ -13,10 +13,8 @@ import type { OtpInputRef } from "react-native-otp-entry"
import { OtpInput } from "react-native-otp-entry"
import { useColor } from "react-native-uikit-colors"
import {
NavigationBlurEffectHeader,
NavigationContext,
} from "@/src/components/common/SafeNavigationScrollView"
import { NavigationContext } from "@/src/components/layouts/views/NavigationContext"
import { NavigationBlurEffectHeader } from "@/src/components/layouts/views/SafeNavigationScrollView"
import { MingcuteLeftLineIcon } from "@/src/icons/mingcute_left_line"
import { twoFactor } from "@/src/lib/auth"
import { queryClient } from "@/src/lib/query-client"
+1 -1
View File
@@ -12,7 +12,7 @@ import {
ModalHeaderCloseButton,
ModalHeaderSubmitButton,
} from "@/src/components/common/ModalSharedComponents"
import { SafeModalScrollView } from "@/src/components/common/SafeModalScrollView"
import { SafeModalScrollView } from "@/src/components/layouts/views/SafeModalScrollView"
import { FormProvider, useFormContext } from "@/src/components/ui/form/FormProvider"
import { FormLabel } from "@/src/components/ui/form/Label"
import { NumberField, TextField } from "@/src/components/ui/form/TextField"
@@ -19,7 +19,7 @@ import {
ModalHeaderCloseButton,
ModalHeaderSubmitButton,
} from "@/src/components/common/ModalSharedComponents"
import { SafeModalScrollView } from "@/src/components/common/SafeModalScrollView"
import { SafeModalScrollView } from "@/src/components/layouts/views/SafeModalScrollView"
import { FormProvider, useFormContext } from "@/src/components/ui/form/FormProvider"
import { Select } from "@/src/components/ui/form/Select"
import { TextField } from "@/src/components/ui/form/TextField"
@@ -3,7 +3,7 @@ import { Easing, Pressable, View } from "react-native"
import { Gesture, GestureDetector } from "react-native-gesture-handler"
import { runOnJS } from "react-native-reanimated"
import { BottomTabs } from "@/src/components/ui/tabbar/BottomTabs"
import { BottomTabs } from "@/src/components/layouts/tabbar/BottomTabs"
import { BlackBoard2CuteFiIcon } from "@/src/icons/black_board_2_cute_fi"
import { BlackBoard2CuteReIcon } from "@/src/icons/black_board_2_cute_re"
import { Home5CuteFiIcon } from "@/src/icons/home_5_cute_fi"
@@ -11,12 +11,12 @@ import { useEventCallback } from "usehooks-ts"
import { ReAnimatedScrollView } from "@/src/components/common/AnimatedComponents"
import { BlurEffect } from "@/src/components/common/BlurEffect"
import { BottomTabBarBackgroundContext } from "@/src/components/ui/tabbar/contexts/BottomTabBarBackgroundContext"
import { SetBottomTabBarVisibleContext } from "@/src/components/ui/tabbar/contexts/BottomTabBarVisibleContext"
import { BottomTabBarBackgroundContext } from "@/src/components/layouts/tabbar/contexts/BottomTabBarBackgroundContext"
import { SetBottomTabBarVisibleContext } from "@/src/components/layouts/tabbar/contexts/BottomTabBarVisibleContext"
import {
useBottomTabBarHeight,
useRegisterNavigationScrollView,
} from "@/src/components/ui/tabbar/hooks"
} from "@/src/components/layouts/tabbar/hooks"
import { SettingRoutes } from "@/src/modules/settings/routes"
import { SettingsList } from "@/src/modules/settings/SettingsList"
import { UserHeaderBanner } from "@/src/modules/settings/UserHeaderBanner"
@@ -7,9 +7,9 @@ import { Pressable, Text, View } from "react-native"
import Animated, { FadeIn, FadeOut } from "react-native-reanimated"
import { useSafeAreaInsets } from "react-native-safe-area-context"
import { SafeNavigationScrollView } from "@/src/components/common/SafeNavigationScrollView"
import { BottomTabBarHeightContext } from "@/src/components/layouts/tabbar/contexts/BottomTabBarHeightContext"
import { SafeNavigationScrollView } from "@/src/components/layouts/views/SafeNavigationScrollView"
import { EntryContentWebView } from "@/src/components/native/webview/EntryContentWebView"
import { BottomTabBarHeightContext } from "@/src/components/ui/tabbar/contexts/BottomTabBarHeightContext"
import { openLink } from "@/src/lib/native"
import { EntryContentContext } from "@/src/modules/entry-content/ctx"
import { EntryAISummary } from "@/src/modules/entry-content/EntryAISummary"
@@ -3,7 +3,7 @@ import { useLocalSearchParams } from "expo-router"
import { useMemo } from "react"
import { useSafeAreaInsets } from "react-native-safe-area-context"
import { BottomTabBarHeightContext } from "@/src/components/ui/tabbar/contexts/BottomTabBarHeightContext"
import { BottomTabBarHeightContext } from "@/src/components/layouts/tabbar/contexts/BottomTabBarHeightContext"
import { EntryListSelector } from "@/src/modules/entry-list/EntryListSelector"
import { EntryListContext, useSelectedView } from "@/src/modules/screen/atoms"
import { TimelineSelectorProvider } from "@/src/modules/screen/TimelineSelectorProvider"