import { useActivityDataAttributes, useLazy, useMounted, useNullableActivity, usePreventTouchDuringTransition, useStyleEffectHide, useStyleEffectOffset, useStyleEffectSwipeBack, useZIndexBase, } from "@stackflow/react-ui-core"; import { assignInlineVars } from "@vanilla-extract/dynamic"; import clsx from "clsx"; import { createContext, useContext, useMemo, useRef } from "react"; import { useGlobalOptions } from "../basicUIPlugin"; import type { GlobalVars } from "../basicUIPlugin.css"; import { globalVars } from "../basicUIPlugin.css"; import { usePop } from "../usePop"; import type { PropOf } from "../utils"; import { compactMap } from "../utils"; import AppBar from "./AppBar"; import * as css from "./AppScreen.css"; export const OFFSET_PX_ANDROID = 32; export const OFFSET_PX_CUPERTINO = 80; export type AppScreenContext = { scroll: (args: { top: number }) => void; zIndices: { dim: number; paper: number; edge: number; appBar: number; }; }; const Context = createContext(null as any); export function useAppScreen() { return useContext(Context); } export type AppScreenProps = Partial< Pick > & { appBar?: Omit< PropOf, "theme" | "modalPresentationStyle" | "ref" | "key" >; preventSwipeBack?: boolean; CUPERTINO_ONLY_modalPresentationStyle?: "fullScreen"; ANDROID_ONLY_activityEnterStyle?: "slideInLeft"; className?: string; children: React.ReactNode; }; const AppScreen: React.FC = ({ backgroundColor, backgroundImage, dimBackgroundColor, appBar, preventSwipeBack, CUPERTINO_ONLY_modalPresentationStyle, ANDROID_ONLY_activityEnterStyle, className, children, }) => { const globalOptions = useGlobalOptions(); const activity = useNullableActivity(); const activityDataAttributes = useActivityDataAttributes(); const mounted = useMounted(); const pop = usePop(); const appScreenRef = useRef(null); const dimRef = useRef(null); const paperRef = useRef(null); const edgeRef = useRef(null); const appBarRef = useRef(null); const paperContentRef = useRef(null); const modalPresentationStyle = globalOptions.theme === "cupertino" ? CUPERTINO_ONLY_modalPresentationStyle : undefined; const activityEnterStyle = globalOptions.theme === "android" ? ANDROID_ONLY_activityEnterStyle : undefined; const isSwipeBackPrevented = preventSwipeBack || modalPresentationStyle === "fullScreen"; const hasAppBar = !!appBar; const zIndexBase = useZIndexBase(); let zIndexDim: number; let zIndexPaper: number; let zIndexEdge: number; let zIndexAppBar: number; switch (globalOptions.theme) { case "cupertino": { zIndexDim = zIndexBase + (modalPresentationStyle === "fullScreen" ? 2 : 0); zIndexPaper = zIndexBase + (hasAppBar && modalPresentationStyle !== "fullScreen" ? appBar.enterStyle === "cover" ? 2 : 1 : 3); zIndexEdge = zIndexBase + 4; zIndexAppBar = zIndexBase + 7; break; } default: { zIndexDim = zIndexBase; zIndexPaper = zIndexBase + (activityEnterStyle === "slideInLeft" && appBar?.enterStyle !== "cover" ? 0 : 2); zIndexEdge = zIndexBase + 4; zIndexAppBar = zIndexBase + (activityEnterStyle === "slideInLeft" ? 6 : 4); break; } } const transitionState = activity?.transitionState ?? "enter-done"; const lazyTransitionState = useLazy(transitionState); useStyleEffectHide({ refs: [appScreenRef], }); useStyleEffectOffset({ refs: (globalOptions.theme === "cupertino" || activityEnterStyle === "slideInLeft") && appBar?.enterStyle !== "cover" ? [paperRef] : [paperRef, appBarRef], offsetStyles: globalOptions.theme === "cupertino" ? { transform: `translate3d(-${OFFSET_PX_CUPERTINO / 16}rem, 0, 0)`, opacity: "1", } : activityEnterStyle === "slideInLeft" ? { transform: "translate3d(-50%, 0, 0)", opacity: "0", } : { transform: `translate3d(0, -${OFFSET_PX_ANDROID / 16}rem, 0)`, opacity: "1", }, transitionDuration: globalVars.computedTransitionDuration, hasEffect: modalPresentationStyle !== "fullScreen", }); useStyleEffectSwipeBack({ dimRef, edgeRef, paperRef, appBarRef, offset: OFFSET_PX_CUPERTINO, transitionDuration: globalVars.transitionDuration, moveAppBarTogether: appBar?.enterStyle === "cover", preventSwipeBack: isSwipeBackPrevented || globalOptions.theme !== "cupertino", getActivityTransitionState() { const $paper = paperRef.current; const $appScreen = $paper?.parentElement; if (!$appScreen) { return null; } if ($appScreen.classList.contains(css.enterActive)) { return "enter-active"; } if ($appScreen.classList.contains(css.enterDone)) { return "enter-done"; } if ($appScreen.classList.contains(css.exitActive)) { return "exit-active"; } if ($appScreen.classList.contains(css.exitDone)) { return "exit-done"; } return null; }, onSwipeEnd({ swiped }) { if (swiped) { pop(); } }, }); const onAppBarTopClick: React.MouseEventHandler = (e) => { appBar?.onTopClick?.(e); if (!e.defaultPrevented) { paperContentRef.current?.scroll({ top: 0, behavior: "smooth", }); } }; usePreventTouchDuringTransition({ ref: appScreenRef, }); return ( ({ scroll({ top }) { paperContentRef?.current?.scroll({ top, behavior: "smooth", }); }, zIndices: { dim: zIndexDim, paper: zIndexPaper, edge: zIndexEdge, appBar: zIndexAppBar, }, }), [paperContentRef, zIndexDim, zIndexPaper, zIndexEdge, zIndexAppBar], )} >
{activityEnterStyle !== "slideInLeft" && (
)} {appBar && ( )}
{children}
{!activity?.isRoot && globalOptions.theme === "cupertino" && !isSwipeBackPrevented && (
)}
); }; export default AppScreen;