Commit 41b63438 authored by Jimmy Lai's avatar Jimmy Lai

fix: avoid SSR marker span in scroll handler, use client-only DOM walking

Replace the marker <span> element in InnerScrollHandlerNew with a
findElementInFragment() helper that uses getClientRects() and
elementFromPoint() to locate a real DOM element within the fragment.

The marker span was causing CI failures under the experimental
__NEXT_EXPERIMENTAL_APP_NEW_SCROLL_HANDLER flag:
- hydration-error-count: extra span changed component tree snapshots
- css-modules-rsc-postcss: querySelector('span') matched the marker
  before the test fixture's styled span

Since FragmentInstance doesn't expose parentElement or child references,
we use getClientRects() to find where the fragment's children are
rendered, then elementFromPoint() to resolve a real Element we can walk
up from for sticky/fixed container detection.
parent 02f63dec
...@@ -80,6 +80,7 @@ const rectProperties = [ ...@@ -80,6 +80,7 @@ const rectProperties = [
'x', 'x',
'y', 'y',
] as const ] as const
/** /**
* Check if a HTMLElement is hidden or fixed/sticky position * Check if a HTMLElement is hidden or fixed/sticky position
*/ */
...@@ -281,16 +282,42 @@ class InnerScrollAndFocusHandlerOld extends React.Component<ScrollAndMaybeFocusH ...@@ -281,16 +282,42 @@ class InnerScrollAndFocusHandlerOld extends React.Component<ScrollAndMaybeFocusH
} }
} }
/**
* Find the first child Element of a FragmentInstance by querying its client
* rects and using document.elementFromPoint to resolve a real DOM element.
* FragmentInstance does not expose parentElement or child references, so this
* is a workaround to locate an element we can walk up from for ancestor
* style checks (e.g. sticky/fixed container detection).
*
* Returns null if no element can be found (e.g. the fragment is empty or
* all children are outside the viewport).
*/
function findElementInFragment(fragment: FragmentInstance): Element | null {
const rects = fragment.getClientRects()
for (let i = 0; i < rects.length; i++) {
const rect = rects[i]
// Use the center of the rect to avoid hitting borders/edges of
// adjacent elements.
const x = rect.left + rect.width / 2
const y = rect.top + rect.height / 2
const el = document.elementFromPoint(x, y)
if (
el !== null &&
el !== document.documentElement &&
el !== document.body
) {
return el
}
}
return null
}
/** /**
* Fork of InnerScrollAndFocusHandlerOld using Fragment refs for scrolling. * Fork of InnerScrollAndFocusHandlerOld using Fragment refs for scrolling.
* No longer focuses the first host descendant. * No longer focuses the first host descendant.
*/ */
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(
() => { () => {
...@@ -326,9 +353,9 @@ function InnerScrollHandlerNew(props: ScrollAndMaybeFocusHandlerProps) { ...@@ -326,9 +353,9 @@ function InnerScrollHandlerNew(props: ScrollAndMaybeFocusHandlerProps) {
instance instanceof Element instance instanceof Element
? instance ? instance
: // FragmentInstance does not expose parentElement or child : // FragmentInstance does not expose parentElement or child
// references, so we use the marker ref (an actual DOM element // references, so we find a real DOM element within the
// rendered alongside the children) to walk up the tree. // fragment to walk up from.
markerRef.current findElementInFragment(instance)
if ( if (
elementToCheck !== null && elementToCheck !== null &&
isInStickyOrFixedContainer(elementToCheck) isInStickyOrFixedContainer(elementToCheck)
...@@ -402,19 +429,7 @@ function InnerScrollHandlerNew(props: ScrollAndMaybeFocusHandlerProps) { ...@@ -402,19 +429,7 @@ function InnerScrollHandlerNew(props: ScrollAndMaybeFocusHandlerProps) {
undefined undefined
) )
return ( return <Fragment ref={childrenRef}>{props.children}</Fragment>
<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