/** * DOM discovery and inline style management for AppScreen transitions. * * Elements are found via data-part attributes. Inline styles are the * source of truth for element positions between animations. */ import { appBarAnatomy } from "../AppBar/anatomy"; import { appScreenAnatomy } from "../AppScreen/anatomy"; import { BEHIND_OFFSET_PERCENT, BEHIND_PARALLAX, OPACITY_FADE_MULTIPLIER, TITLE_OFFSET_PERCENT, TITLE_TRANSLATE_RATIO, } from "./constants"; // ─── Types ────────────────────────────────────────────────────────────────── export interface TransitionTargets { topLayer: HTMLElement | null; topDim: HTMLElement | null; behindLayer: HTMLElement | null; behindDim: HTMLElement | null; topTitle: HTMLElement | null; behindTitle: HTMLElement | null; topIcons: HTMLElement[]; behindIcons: HTMLElement[]; topAppBarRoot: HTMLElement | null; behindAppBarRoot: HTMLElement | null; topAppBarBackground: HTMLElement | null; behindAppBarBackground: HTMLElement | null; } export type TransitionStyle = "slideFromRightIOS" | "fadeFromBottomAndroid" | "fadeIn"; // ─── DOM Discovery ────────────────────────────────────────────────────────── function queryParts(activity: HTMLElement | null, part: string): HTMLElement | null { return activity?.querySelector(`[data-part='${part}']`) ?? null; } function queryAllParts(activity: HTMLElement | null, ...parts: string[]): HTMLElement[] { if (!activity) return []; const selector = parts.map((p) => `[data-part='${p}']`).join(", "); return Array.from(activity.querySelectorAll(selector)); } /** Read transition style directly from the top activity DOM element. */ export function readTransitionStyle(stackEl: HTMLElement): TransitionStyle { const topActivity = stackEl.querySelector("[data-activity-is-top]"); return (topActivity?.dataset["transitionStyle"] as TransitionStyle) ?? "slideFromRightIOS"; } /** Find all transition target elements. Call once per gesture/transition. */ export function findTransitionTargets(stackEl: HTMLElement): TransitionTargets { const topActivity = stackEl.querySelector("[data-activity-is-top]"); let behindActivity: HTMLElement | null = null; if (topActivity) { const all = Array.from( stackEl.querySelectorAll(`[data-part='${appScreenAnatomy.activity}']`), ); const topId = topActivity.dataset["activityId"]; const topIdx = all.findIndex((el) => el.dataset["activityId"] === topId); for (let i = topIdx - 1; i >= 0; i--) { if (all[i].dataset["activityId"]) { behindActivity = all[i]; break; } } } return { topLayer: queryParts(topActivity, appScreenAnatomy.layer), topDim: queryParts(topActivity, appScreenAnatomy.dim), behindLayer: queryParts(behindActivity, appScreenAnatomy.layer), behindDim: queryParts(behindActivity, appScreenAnatomy.dim), topTitle: queryParts(topActivity, appBarAnatomy.main), behindTitle: queryParts(behindActivity, appBarAnatomy.main), topIcons: queryAllParts( topActivity, appBarAnatomy.icon, appBarAnatomy.textButtonLabel, appBarAnatomy.custom, ), behindIcons: queryAllParts( behindActivity, appBarAnatomy.icon, appBarAnatomy.textButtonLabel, appBarAnatomy.custom, ), topAppBarRoot: queryParts(topActivity, appBarAnatomy.root), behindAppBarRoot: queryParts(behindActivity, appBarAnatomy.root), topAppBarBackground: queryParts(topActivity, appBarAnatomy.background), behindAppBarBackground: queryParts(behindActivity, appBarAnatomy.background), }; } // ─── Inline Style Helpers ─────────────────────────────────────────────────── export function setTransform(el: HTMLElement | null, value: string) { if (el) el.style.transform = value; } export function setOpacity(el: HTMLElement | null, value: string) { if (el) el.style.opacity = value; } function clearStyles(el: HTMLElement | null) { if (!el) return; el.style.transform = ""; el.style.opacity = ""; } // ─── Position Management ──────────────────────────────────────────────────── /** * Clear ALL inline styles from every target, including appBar roots. * This is the foundation — call before setting any specific positions. */ export function clearAllStyles(t: TransitionTargets) { const all = [ t.topLayer, t.topDim, t.behindLayer, t.behindDim, t.topTitle, t.behindTitle, t.topAppBarRoot, t.behindAppBarRoot, t.topAppBarBackground, t.behindAppBarBackground, ...t.topIcons, ...t.behindIcons, ]; for (const el of all) clearStyles(el); } /** * Remove all leftover inline styles from the top activity once the * transition has settled (globalTransitionState === "idle"). * * The top + behind pair model only handles the single immediately adjacent * behind layer. When transitions overlap (concurrent pop, swipe-back race, * etc.) the landing screen can get stuck with the temporary styles of a * "behind" or an "exiting top" — the layer stays at -30% and shifts by 1/3, * or the appBar root stays at opacity 0 (setPostExitPositions) and the whole * app bar disappears. * * At idle the top must always be in a clean default state (setIdlePositions * only clears the top and styles the behind), so wipe any leftover inline * back to the CSS defaults (layer 0%, appBar visible). The behind is left * untouched, and it's a no-op if already clean. */ export function clearTopActivityStyles(stackEl: HTMLElement) { const t = findTransitionTargets(stackEl); const topParts = [ t.topLayer, t.topDim, t.topTitle, t.topAppBarRoot, t.topAppBarBackground, ...t.topIcons, ]; for (const el of topParts) clearStyles(el); } /** * Set idle positions after push completes. * Pattern: clear everything first, then set only non-default positions. */ export function setIdlePositions( t: TransitionTargets, style: TransitionStyle = "slideFromRightIOS", ) { clearAllStyles(t); if (style === "slideFromRightIOS") { // Behind layer parallax offset setTransform(t.behindLayer, `translate3d(${BEHIND_OFFSET_PERCENT}%, 0, 0)`); setOpacity(t.behindTitle, "0"); setTransform(t.behindTitle, `translate3d(${-TITLE_OFFSET_PERCENT}%, 0, 0)`); for (const icon of t.behindIcons) setOpacity(icon, "0"); } else { // Android/fadeIn: behind is at same position, hide its appBar to prevent bleed-through setOpacity(t.behindAppBarRoot, "0"); } } /** * Set positions after pop/swipe-complete. * Pattern: clear everything first, then pin top in exit position. */ export function setPostExitPositions( t: TransitionTargets, style: TransitionStyle = "slideFromRightIOS", ) { clearAllStyles(t); // Pin top activity off-screen (stackflow will remove it from DOM later) setOpacity(t.topDim, "0"); setOpacity(t.topAppBarRoot, "0"); if (style === "slideFromRightIOS") { setTransform(t.topLayer, "translate3d(100%, 0, 0)"); setOpacity(t.topTitle, "0"); setTransform(t.topTitle, `translate3d(${TITLE_OFFSET_PERCENT}%, 0, 0)`); for (const icon of t.topIcons) { setOpacity(icon, "0"); setTransform(icon, `translate3d(${TITLE_OFFSET_PERCENT}%, 0, 0)`); } } else if (style === "fadeFromBottomAndroid") { setOpacity(t.topLayer, "0"); setTransform(t.topLayer, "translate3d(0, 8vh, 0)"); setTransform(t.topAppBarRoot, "translate3d(0, 8vh, 0)"); } else { setOpacity(t.topLayer, "0"); } } /** Apply inline styles during swiping (touchmove). */ export function applySwipeStyles(t: TransitionTargets, displacement: number, ratio: number) { setTransform(t.topLayer, `translate3d(${displacement}px, 0, 0)`); setTransform( t.behindLayer, `translate3d(calc(${BEHIND_OFFSET_PERCENT}% + ${displacement * BEHIND_PARALLAX}px), 0, 0)`, ); setOpacity(t.topDim, `${1 - ratio}`); const topFade = Math.max(0, 1 - ratio * OPACITY_FADE_MULTIPLIER); if (t.topTitle) { setOpacity(t.topTitle, `${topFade}`); setTransform(t.topTitle, `translate3d(${displacement * TITLE_TRANSLATE_RATIO}px, 0, 0)`); } if (t.behindTitle) { setOpacity(t.behindTitle, `${ratio}`); setTransform( t.behindTitle, `translate3d(calc(${-TITLE_OFFSET_PERCENT}% + ${displacement * TITLE_TRANSLATE_RATIO}px), 0, 0)`, ); } for (const icon of t.topIcons) { setOpacity(icon, `${topFade}`); setTransform(icon, `translate3d(${displacement * TITLE_TRANSLATE_RATIO}px, 0, 0)`); } for (const icon of t.behindIcons) { setOpacity(icon, `${ratio}`); } }