Commit 02f63dec authored by Jimmy Lai's avatar Jimmy Lai

fix: handle sticky container check in cached navigations scroll path

FragmentInstance (from React's Fragment ref API) does not expose
parentElement, so the previous approach of casting to access it
silently returned undefined. This caused the sticky/fixed container
check to always fail in InnerScrollHandlerNew, meaning the sticky
header slot still consumed the shared scrollRef and prevented
scroll-to-top.

Fix: add a zero-layout-impact marker <span style=display:contents>
inside the Fragment. Its ref provides a real DOM element we can
walk up from to detect sticky/fixed ancestors.
parent df50b62e
...@@ -287,6 +287,10 @@ class InnerScrollAndFocusHandlerOld extends React.Component<ScrollAndMaybeFocusH ...@@ -287,6 +287,10 @@ class InnerScrollAndFocusHandlerOld extends React.Component<ScrollAndMaybeFocusH
*/ */
function InnerScrollHandlerNew(props: ScrollAndMaybeFocusHandlerProps) { function InnerScrollHandlerNew(props: ScrollAndMaybeFocusHandlerProps) {
const childrenRef = React.useRef<FragmentInstance>(null) const childrenRef = React.useRef<FragmentInstance>(null)
// A marker element used to detect sticky/fixed containers. FragmentInstance
// does not expose parentElement, so we need an actual DOM element in the
// same tree position to walk up from.
const markerRef = React.useRef<HTMLSpanElement>(null)
useLayoutEffect( useLayoutEffect(
() => { () => {
...@@ -321,10 +325,10 @@ function InnerScrollHandlerNew(props: ScrollAndMaybeFocusHandlerProps) { ...@@ -321,10 +325,10 @@ function InnerScrollHandlerNew(props: ScrollAndMaybeFocusHandlerProps) {
const elementToCheck: Element | null = const elementToCheck: Element | null =
instance instanceof Element instance instanceof Element
? instance ? instance
: // FragmentInstance is a DOM-backed object with parentElement at : // FragmentInstance does not expose parentElement or child
// runtime, but TypeScript's type is opaque. // references, so we use the marker ref (an actual DOM element
(instance as unknown as { parentElement: Element | null }) // rendered alongside the children) to walk up the tree.
.parentElement markerRef.current
if ( if (
elementToCheck !== null && elementToCheck !== null &&
isInStickyOrFixedContainer(elementToCheck) isInStickyOrFixedContainer(elementToCheck)
...@@ -398,7 +402,19 @@ function InnerScrollHandlerNew(props: ScrollAndMaybeFocusHandlerProps) { ...@@ -398,7 +402,19 @@ function InnerScrollHandlerNew(props: ScrollAndMaybeFocusHandlerProps) {
undefined undefined
) )
return <Fragment ref={childrenRef}>{props.children}</Fragment> return (
<Fragment ref={childrenRef}>
<span
ref={markerRef}
// This marker is used only for sticky/fixed container detection.
// display:contents ensures it generates no box and has zero layout
// impact, while still participating in the DOM tree so that
// parentElement traversal works.
style={{ display: 'contents' }}
/>
{props.children}
</Fragment>
)
} }
const InnerScrollAndMaybeFocusHandler = enableNewScrollHandler const InnerScrollAndMaybeFocusHandler = enableNewScrollHandler
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment