diff --git a/apps/mobile/src/components/common/SafeNavigationScrollView.tsx b/apps/mobile/src/components/common/SafeNavigationScrollView.tsx deleted file mode 100644 index f7b3ca2c0..000000000 --- a/apps/mobile/src/components/common/SafeNavigationScrollView.tsx +++ /dev/null @@ -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 & { - 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 = ({ - 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(null) - - const setAttachNavigationScrollViewRef = useContext(SetAttachNavigationScrollViewContext) - useEffect(() => { - if (setAttachNavigationScrollViewRef) { - setAttachNavigationScrollViewRef(scrollViewRef) - } - }, [setAttachNavigationScrollViewRef, scrollViewRef]) - - return ( - ({ scrollY }), [scrollY])}> - {withHeaderBlur && } - - - - {children} - - - - - ) -} - -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() - - 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 ( - ( - - - - ), - headerTransparent: true, - - headerLeft: - headerLeft ?? - (canBack - ? () => ( - router.back()}> - - - ) - : undefined), - - header: headerHideableBottom - ? ({ options }) => { - return ( - - - {options.headerBackground?.()} - - -
null} /> - {!!hideableBottom && ( - { - const { height } = e.nativeEvent.layout - - largeDefaultHeaderHeightRef.current = height + originalDefaultHeaderHeight - largeHeaderHeight.value = largeDefaultHeaderHeightRef.current - }} - > - {hideableBottom} - - )} - - ) - } - : undefined, - - ...rest, - }} - /> - ) -} diff --git a/apps/mobile/src/components/layouts/header/NavigationHeader.tsx b/apps/mobile/src/components/layouts/header/NavigationHeader.tsx new file mode 100644 index 000000000..d96b40df1 --- /dev/null +++ b/apps/mobile/src/components/layouts/header/NavigationHeader.tsx @@ -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> | 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() + + 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, "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() + 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 ( + { + setHeaderHeight?.(e.nativeEvent.layout.height) + }, + [setHeaderHeight], + )} + > + + + + + {/* Grid */} + + + + {/* Left */} + { + setHeaderLeftWidth(e.nativeEvent.layout.width) + }, [])} + > + + + {/* Center */} + { + setTitleWidth(e.nativeEvent.layout.width) + }, [])} + className="flex-1 items-center justify-center" + pointerEvents={"box-none"} + style={{ + marginHorizontal: titleMarginHorizontal, + transform: [{ translateX: titleShouldCenterTransformX }], + }} + > + {headerTitle} + + {/* Right */} + + + + + + + {!!hideableBottom && ( + + {hideableBottom} + + )} + + ) +} + +const DefaultHeaderLeft = ({ canGoBack }: { canGoBack: boolean }) => { + const label = useColor("label") + if (!canGoBack) return null + return ( + router.back()}> + + + ) +} + +const Noop = () => null diff --git a/apps/mobile/src/components/layouts/header/hooks.ts b/apps/mobile/src/components/layouts/header/hooks.ts new file mode 100644 index 000000000..e393d2835 --- /dev/null +++ b/apps/mobile/src/components/layouts/header/hooks.ts @@ -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 +} diff --git a/apps/mobile/src/components/ui/tabbar/BottomTabHeightProvider.tsx b/apps/mobile/src/components/layouts/tabbar/BottomTabHeightProvider.tsx similarity index 81% rename from apps/mobile/src/components/ui/tabbar/BottomTabHeightProvider.tsx rename to apps/mobile/src/components/layouts/tabbar/BottomTabHeightProvider.tsx index b05f619fd..b720c2040 100644 --- a/apps/mobile/src/components/ui/tabbar/BottomTabHeightProvider.tsx +++ b/apps/mobile/src/components/layouts/tabbar/BottomTabHeightProvider.tsx @@ -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" diff --git a/apps/mobile/src/components/ui/tabbar/BottomTabs.tsx b/apps/mobile/src/components/layouts/tabbar/BottomTabs.tsx similarity index 100% rename from apps/mobile/src/components/ui/tabbar/BottomTabs.tsx rename to apps/mobile/src/components/layouts/tabbar/BottomTabs.tsx diff --git a/apps/mobile/src/components/ui/tabbar/Tabbar.tsx b/apps/mobile/src/components/layouts/tabbar/Tabbar.tsx similarity index 98% rename from apps/mobile/src/components/ui/tabbar/Tabbar.tsx rename to apps/mobile/src/components/layouts/tabbar/Tabbar.tsx index ad76994df..99e9dc63c 100644 --- a/apps/mobile/src/components/ui/tabbar/Tabbar.tsx +++ b/apps/mobile/src/components/layouts/tabbar/Tabbar.tsx @@ -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" diff --git a/apps/mobile/src/components/ui/tabbar/contexts/AttachNavigationScrollViewContext.tsx b/apps/mobile/src/components/layouts/tabbar/contexts/AttachNavigationScrollViewContext.tsx similarity index 100% rename from apps/mobile/src/components/ui/tabbar/contexts/AttachNavigationScrollViewContext.tsx rename to apps/mobile/src/components/layouts/tabbar/contexts/AttachNavigationScrollViewContext.tsx diff --git a/apps/mobile/src/components/ui/tabbar/contexts/BottomTabBarBackgroundContext.tsx b/apps/mobile/src/components/layouts/tabbar/contexts/BottomTabBarBackgroundContext.tsx similarity index 100% rename from apps/mobile/src/components/ui/tabbar/contexts/BottomTabBarBackgroundContext.tsx rename to apps/mobile/src/components/layouts/tabbar/contexts/BottomTabBarBackgroundContext.tsx diff --git a/apps/mobile/src/components/ui/tabbar/contexts/BottomTabBarHeightContext.tsx b/apps/mobile/src/components/layouts/tabbar/contexts/BottomTabBarHeightContext.tsx similarity index 100% rename from apps/mobile/src/components/ui/tabbar/contexts/BottomTabBarHeightContext.tsx rename to apps/mobile/src/components/layouts/tabbar/contexts/BottomTabBarHeightContext.tsx diff --git a/apps/mobile/src/components/ui/tabbar/contexts/BottomTabBarVisibleContext.tsx b/apps/mobile/src/components/layouts/tabbar/contexts/BottomTabBarVisibleContext.tsx similarity index 100% rename from apps/mobile/src/components/ui/tabbar/contexts/BottomTabBarVisibleContext.tsx rename to apps/mobile/src/components/layouts/tabbar/contexts/BottomTabBarVisibleContext.tsx diff --git a/apps/mobile/src/components/ui/tabbar/hooks.ts b/apps/mobile/src/components/layouts/tabbar/hooks.ts similarity index 100% rename from apps/mobile/src/components/ui/tabbar/hooks.ts rename to apps/mobile/src/components/layouts/tabbar/hooks.ts diff --git a/apps/mobile/src/components/layouts/views/NavigationContext.tsx b/apps/mobile/src/components/layouts/views/NavigationContext.tsx new file mode 100644 index 000000000..a4f29ead9 --- /dev/null +++ b/apps/mobile/src/components/layouts/views/NavigationContext.tsx @@ -0,0 +1,6 @@ +import { createContext } from "react" +import type { Animated } from "react-native" + +export const NavigationContext = createContext<{ + scrollY: Animated.Value +} | null>(null) diff --git a/apps/mobile/src/components/layouts/views/NavigationHeaderContext.tsx b/apps/mobile/src/components/layouts/views/NavigationHeaderContext.tsx new file mode 100644 index 000000000..d84b02a26 --- /dev/null +++ b/apps/mobile/src/components/layouts/views/NavigationHeaderContext.tsx @@ -0,0 +1,7 @@ +import type { Dispatch, SetStateAction } from "react" +import { createContext } from "react" + +export const NavigationHeaderHeightContext = createContext(null!) +export const SetNavigationHeaderHeightContext = createContext>>( + null!, +) diff --git a/apps/mobile/src/components/common/SafeModalScrollView.tsx b/apps/mobile/src/components/layouts/views/SafeModalScrollView.tsx similarity index 100% rename from apps/mobile/src/components/common/SafeModalScrollView.tsx rename to apps/mobile/src/components/layouts/views/SafeModalScrollView.tsx diff --git a/apps/mobile/src/components/layouts/views/SafeNavigationScrollView.tsx b/apps/mobile/src/components/layouts/views/SafeNavigationScrollView.tsx new file mode 100644 index 000000000..945ea098d --- /dev/null +++ b/apps/mobile/src/components/layouts/views/SafeNavigationScrollView.tsx @@ -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 & { + withHeaderBlur?: boolean + onScroll?: (e: ReanimatedScrollEvent) => void + + // For scroll view content adjustment behavior + withTopInset?: boolean + withBottomInset?: boolean +} & PropsWithChildren + +export const SafeNavigationScrollView: FC = ({ + children, + + withHeaderBlur = true, + onScroll, + + withBottomInset = false, + withTopInset = false, + + ...props +}) => { + const insets = useSafeAreaInsets() + const tabBarHeight = useBottomTabBarHeight() + + const scrollY = useAnimatedValue(0) + const scrollViewRef = useRef(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 ( + ({ scrollY }), [scrollY])}> + + + {withHeaderBlur && } + + + + {children} + + + + + + + ) +} + +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 ( + ( + router.back()}> + + + ) + : undefined), + + header: useTypeScriptHappyCallback( + ({ options }) => ( + + + + + + ), + [hideableBottom, navigationContext], + ), + + ...rest, + }} + /> + ) +} diff --git a/apps/mobile/src/components/ui/portal/Portal.tsx b/apps/mobile/src/components/ui/portal/Portal.tsx new file mode 100644 index 000000000..e82525081 --- /dev/null +++ b/apps/mobile/src/components/ui/portal/Portal.tsx @@ -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 = (props) => { + const id = React.useId() + const key = useRef(id) + const once = useRef(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( + () => ( + + {(manager: IPortalMethods | null): React.ReactNode => ( + {children} + )} + + ), + [children], + ) +} + +export { PortalConsumerProps, PortalProps } from "./type" diff --git a/apps/mobile/src/components/ui/portal/PortalContext.tsx b/apps/mobile/src/components/ui/portal/PortalContext.tsx new file mode 100644 index 000000000..1a7b607e6 --- /dev/null +++ b/apps/mobile/src/components/ui/portal/PortalContext.tsx @@ -0,0 +1,5 @@ +import * as React from "react" + +import type { IPortalMethods } from "./type" + +export default React.createContext(null) diff --git a/apps/mobile/src/components/ui/portal/PortalHost.tsx b/apps/mobile/src/components/ui/portal/PortalHost.tsx new file mode 100644 index 000000000..f6d5e2a0c --- /dev/null +++ b/apps/mobile/src/components/ui/portal/PortalHost.tsx @@ -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([]) + const manager = useRef() + + 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( + () => ( + <> + + <> + {/* placeholder */} + {children} + {/* portal container */} + + + + + ), + [ctxValue, children], + ) +} + +PortalHost.displayName = "Portal.host" diff --git a/apps/mobile/src/components/ui/portal/PortalManager.tsx b/apps/mobile/src/components/ui/portal/PortalManager.tsx new file mode 100644 index 000000000..bfd6cc40b --- /dev/null +++ b/apps/mobile/src/components/ui/portal/PortalManager.tsx @@ -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 }) => ( + + {children} + + ))} + + ), + [portals], + ) +}) diff --git a/apps/mobile/src/components/ui/portal/index.tsx b/apps/mobile/src/components/ui/portal/index.tsx new file mode 100644 index 000000000..d2c1dd9de --- /dev/null +++ b/apps/mobile/src/components/ui/portal/index.tsx @@ -0,0 +1,2 @@ +export { Portal } from "./Portal" +export { PortalHost } from "./PortalHost" diff --git a/apps/mobile/src/components/ui/portal/type.ts b/apps/mobile/src/components/ui/portal/type.ts new file mode 100644 index 000000000..1ebc2963a --- /dev/null +++ b/apps/mobile/src/components/ui/portal/type.ts @@ -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 +} diff --git a/apps/mobile/src/constants/ui.ts b/apps/mobile/src/constants/ui.ts new file mode 100644 index 000000000..c115777b3 --- /dev/null +++ b/apps/mobile/src/constants/ui.ts @@ -0,0 +1 @@ +export const TIMELINE_VIEW_SELECTOR_HEIGHT = 58 diff --git a/apps/mobile/src/modules/discover/Recommendations.tsx b/apps/mobile/src/modules/discover/Recommendations.tsx index 793639d63..170233ce0 100644 --- a/apps/mobile/src/modules/discover/Recommendations.tsx +++ b/apps/mobile/src/modules/discover/Recommendations.tsx @@ -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" diff --git a/apps/mobile/src/modules/entry-content/HeaderActions.tsx b/apps/mobile/src/modules/entry-content/EntryContentHeaderRightActions.tsx similarity index 93% rename from apps/mobile/src/modules/entry-content/HeaderActions.tsx rename to apps/mobile/src/modules/entry-content/EntryContentHeaderRightActions.tsx index b9f637fba..36a37f265 100644 --- a/apps/mobile/src/modules/entry-content/HeaderActions.tsx +++ b/apps/mobile/src/modules/entry-content/EntryContentHeaderRightActions.tsx @@ -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 isHeaderTitleVisible: boolean } + const HeaderRightActionsImpl = ({ entryId, titleOpacityShareValue, @@ -93,15 +95,23 @@ const HeaderRightActionsImpl = ({ }) } + const [extraActionContainerWidth, setExtraActionContainerWidth] = useState(0) + return ( + {!isHeaderTitleVisible && ( + + )} { + 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 && ( ( - - {title} - + + + + {title} + + + )} /> {children} diff --git a/apps/mobile/src/modules/screen/TimelineViewSelector.tsx b/apps/mobile/src/modules/screen/TimelineViewSelector.tsx index 993591ac2..9a530ffe3 100644 --- a/apps/mobile/src/modules/screen/TimelineViewSelector.tsx +++ b/apps/mobile/src/modules/screen/TimelineViewSelector.tsx @@ -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 ( - + ) => { - return ( - {children} - ) + return {children} } export function HomeLeftAction() { const user = useWhoami() if (!user) return null return ( - + router.push("/profile")}> @@ -37,7 +35,7 @@ export function HomeLeftAction() { export function HomeSharedRightAction(props: PropsWithChildren) { return ( - + {props.children}