Unverified Commit 17f6f135 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[fragment-scroll] Rename `ScrollAndMaybeFocusHandler` to `ScrollHandler` (#96828)

As of https://github.com/vercel/next.js/pull/95602, the old
scroll-and-focus handler doesn't exist anymore
As of https://github.com/vercel/next.js/pull/96113, the new handler no
longer handles focus.

This updates the relevant variable names to reflect that we no longer
handle focus.
parent 2966db44
...@@ -452,7 +452,7 @@ function Router({ ...@@ -452,7 +452,7 @@ function Router({
} }
}, []) }, [])
const { cache, tree, nextUrl, focusAndScrollRef, previousNextUrl } = state const { cache, tree, nextUrl, scrollRef, previousNextUrl } = state
const matchingHead = useMemo(() => { const matchingHead = useMemo(() => {
return findHeadInCache(cache, tree[1]) return findHeadInCache(cache, tree[1])
...@@ -500,11 +500,11 @@ function Router({ ...@@ -500,11 +500,11 @@ function Router({
const globalLayoutRouterContext = useMemo(() => { const globalLayoutRouterContext = useMemo(() => {
return { return {
tree, tree,
focusAndScrollRef, scrollRef,
nextUrl, nextUrl,
previousNextUrl, previousNextUrl,
} }
}, [tree, focusAndScrollRef, nextUrl, previousNextUrl]) }, [tree, scrollRef, nextUrl, previousNextUrl])
let head let head
if (matchingHead !== null) { if (matchingHead !== null) {
......
...@@ -8,7 +8,7 @@ import type { ...@@ -8,7 +8,7 @@ import type {
Segment, Segment,
} from '../../shared/lib/app-router-types' } from '../../shared/lib/app-router-types'
import type { ErrorComponent } from './error-boundary' import type { ErrorComponent } from './error-boundary'
import type { FocusAndScrollRef } from './router-reducer/router-reducer-types' import type { ScrollHandlerRef } from './router-reducer/router-reducer-types'
import React, { import React, {
Activity, Activity,
...@@ -133,8 +133,8 @@ function getHashFragmentDomNode(hashFragment: string) { ...@@ -133,8 +133,8 @@ function getHashFragmentDomNode(hashFragment: string) {
null null
) )
} }
interface ScrollAndMaybeFocusHandlerProps { interface ScrollHandlerProps {
focusAndScrollRef: FocusAndScrollRef scrollRef: ScrollHandlerRef
children: React.ReactNode children: React.ReactNode
cacheNode: CacheNode cacheNode: CacheNode
} }
...@@ -143,22 +143,20 @@ interface ScrollAndMaybeFocusHandlerProps { ...@@ -143,22 +143,20 @@ interface ScrollAndMaybeFocusHandlerProps {
* Uses Fragment refs for scrolling. * Uses Fragment refs for scrolling.
* Does not focus the first host descendant. * Does not focus the first host descendant.
*/ */
function InnerScrollAndMaybeFocusHandler( function InnerScrollHandler(props: ScrollHandlerProps) {
props: ScrollAndMaybeFocusHandlerProps
) {
const childrenRef = React.useRef<FragmentInstance>(null) const childrenRef = React.useRef<FragmentInstance>(null)
useLayoutEffect( useLayoutEffect(
() => { () => {
const { focusAndScrollRef, cacheNode } = props const { scrollRef: scrollHandlerRef, cacheNode } = props
const scrollRef = focusAndScrollRef.forceScroll const scrollRef = scrollHandlerRef.forceScroll
? focusAndScrollRef.scrollRef ? scrollHandlerRef.scrollRef
: cacheNode.scrollRef : cacheNode.scrollRef
if (scrollRef === null || !scrollRef.current) return if (scrollRef === null || !scrollRef.current) return
let instance: FragmentInstance | HTMLElement | null = null let instance: FragmentInstance | HTMLElement | null = null
const hashFragment = focusAndScrollRef.hashFragment const hashFragment = scrollHandlerRef.hashFragment
if (hashFragment) { if (hashFragment) {
instance = getHashFragmentDomNode(hashFragment) instance = getHashFragmentDomNode(hashFragment)
...@@ -166,8 +164,8 @@ function InnerScrollAndMaybeFocusHandler( ...@@ -166,8 +164,8 @@ function InnerScrollAndMaybeFocusHandler(
// A missing hash target is still a handled scroll intent. Do not // A missing hash target is still a handled scroll intent. Do not
// fall back to the route Fragment or leave the intent pending. // fall back to the route Fragment or leave the intent pending.
scrollRef.current = false scrollRef.current = false
focusAndScrollRef.onlyHashChange = false scrollHandlerRef.onlyHashChange = false
focusAndScrollRef.hashFragment = null scrollHandlerRef.hashFragment = null
return return
} }
} else { } else {
...@@ -257,7 +255,7 @@ function InnerScrollAndMaybeFocusHandler( ...@@ -257,7 +255,7 @@ function InnerScrollAndMaybeFocusHandler(
{ {
// We will force layout by querying domNode position // We will force layout by querying domNode position
dontForceLayout: true, dontForceLayout: true,
onlyHashChange: focusAndScrollRef.onlyHashChange, onlyHashChange: scrollHandlerRef.onlyHashChange,
} }
) )
...@@ -266,8 +264,8 @@ function InnerScrollAndMaybeFocusHandler( ...@@ -266,8 +264,8 @@ function InnerScrollAndMaybeFocusHandler(
} }
// Mutate after scrolling so that it can be read by `disableSmoothScrollDuringRouteTransition` // Mutate after scrolling so that it can be read by `disableSmoothScrollDuringRouteTransition`
focusAndScrollRef.onlyHashChange = false scrollHandlerRef.onlyHashChange = false
focusAndScrollRef.hashFragment = null scrollHandlerRef.hashFragment = null
}, },
// Used to run on every commit. We may be able to be smarter about this // Used to run on every commit. We may be able to be smarter about this
// but be prepared for lots of manual testing. // but be prepared for lots of manual testing.
...@@ -277,7 +275,7 @@ function InnerScrollAndMaybeFocusHandler( ...@@ -277,7 +275,7 @@ function InnerScrollAndMaybeFocusHandler(
return <Fragment ref={childrenRef}>{props.children}</Fragment> return <Fragment ref={childrenRef}>{props.children}</Fragment>
} }
function ScrollAndMaybeFocusHandler({ function ScrollHandler({
children, children,
cacheNode, cacheNode,
}: { }: {
...@@ -290,12 +288,9 @@ function ScrollAndMaybeFocusHandler({ ...@@ -290,12 +288,9 @@ function ScrollAndMaybeFocusHandler({
} }
return ( return (
<InnerScrollAndMaybeFocusHandler <InnerScrollHandler scrollRef={context.scrollRef} cacheNode={cacheNode}>
focusAndScrollRef={context.focusAndScrollRef}
cacheNode={cacheNode}
>
{children} {children}
</InnerScrollAndMaybeFocusHandler> </InnerScrollHandler>
) )
} }
...@@ -680,7 +675,7 @@ export default function OuterLayoutRouter({ ...@@ -680,7 +675,7 @@ export default function OuterLayoutRouter({
const debugNameToDisplay = isVirtual ? undefined : debugNameContext const debugNameToDisplay = isVirtual ? undefined : debugNameContext
let templateValue = ( let templateValue = (
<ScrollAndMaybeFocusHandler cacheNode={cacheNode}> <ScrollHandler cacheNode={cacheNode}>
<ErrorBoundary <ErrorBoundary
errorComponent={error} errorComponent={error}
errorStyles={errorStyles} errorStyles={errorStyles}
...@@ -721,7 +716,7 @@ export default function OuterLayoutRouter({ ...@@ -721,7 +716,7 @@ export default function OuterLayoutRouter({
</LoadingBoundary> </LoadingBoundary>
</ErrorBoundary> </ErrorBoundary>
{segmentViewStateNode} {segmentViewStateNode}
</ScrollAndMaybeFocusHandler> </ScrollHandler>
) )
if ( if (
......
...@@ -255,7 +255,7 @@ export function createInitialRouterState({ ...@@ -255,7 +255,7 @@ export function createInitialRouterState({
// to avoid it being overwritten on navigation back/forward with MPA Navigation. // to avoid it being overwritten on navigation back/forward with MPA Navigation.
preserveCustomHistoryState: true, preserveCustomHistoryState: true,
}, },
focusAndScrollRef: { scrollRef: {
scrollRef: null, scrollRef: null,
forceScroll: false, forceScroll: false,
onlyHashChange: false, onlyHashChange: false,
......
...@@ -172,7 +172,7 @@ export const enum ScrollBehavior { ...@@ -172,7 +172,7 @@ export const enum ScrollBehavior {
NoScroll = 1, NoScroll = 1,
} }
export type FocusAndScrollRef = { export type ScrollHandlerRef = {
/** /**
* The scroll ref from the most recent navigation. Set to whatever was * The scroll ref from the most recent navigation. Set to whatever was
* accumulated during tree construction (or null if nothing was * accumulated during tree construction (or null if nothing was
...@@ -182,7 +182,7 @@ export type FocusAndScrollRef = { ...@@ -182,7 +182,7 @@ export type FocusAndScrollRef = {
*/ */
scrollRef: ScrollRef | null scrollRef: ScrollRef | null
/** /**
* When true, the scroll handler uses `focusAndScrollRef.scrollRef` * When true, the scroll handler uses the navigation-level `scrollRef`
* for every segment regardless of per-node state. Used for hash-only * for every segment regardless of per-node state. Used for hash-only
* navigations where every segment should be treated as a scroll * navigations where every segment should be treated as a scroll
* target. When false, the handler checks `cacheNode.scrollRef` * target. When false, the handler checks `cacheNode.scrollRef`
...@@ -219,9 +219,9 @@ export type AppRouterState = { ...@@ -219,9 +219,9 @@ export type AppRouterState = {
*/ */
pushRef: PushRef pushRef: PushRef
/** /**
* Decides if the update should apply scroll and focus management. * Decides if the update should apply scroll management.
*/ */
focusAndScrollRef: FocusAndScrollRef scrollRef: ScrollHandlerRef
/** /**
* The canonical url that is pushed/replaced. * The canonical url that is pushed/replaced.
* - This is the url you see in the browser. * - This is the url you see in the browser.
......
...@@ -663,7 +663,7 @@ export function completeHardNavigation( ...@@ -663,7 +663,7 @@ export function completeHardNavigation(
// we should trigger the hard navigation and blocking any subsequent // we should trigger the hard navigation and blocking any subsequent
// router updates without updating React. // router updates without updating React.
renderedSearch: state.renderedSearch, renderedSearch: state.renderedSearch,
focusAndScrollRef: state.focusAndScrollRef, scrollRef: state.scrollRef,
cache: state.cache, cache: state.cache,
tree: state.tree, tree: state.tree,
nextUrl: state.nextUrl, nextUrl: state.nextUrl,
...@@ -742,7 +742,7 @@ export function completeSoftNavigation( ...@@ -742,7 +742,7 @@ export function completeSoftNavigation(
if (scrollRef !== null) { if (scrollRef !== null) {
scrollRef.current = false scrollRef.current = false
} }
activeScrollRef = oldState.focusAndScrollRef.scrollRef activeScrollRef = oldState.scrollRef.scrollRef
forceScroll = false forceScroll = false
} else if (onlyHashChange) { } else if (onlyHashChange) {
// Hash-only navigations should scroll regardless of per-node state. // Hash-only navigations should scroll regardless of per-node state.
...@@ -750,7 +750,7 @@ export function completeSoftNavigation( ...@@ -750,7 +750,7 @@ export function completeSoftNavigation(
// //
// Invalidate any scroll ref from a prior navigation that hasn't // Invalidate any scroll ref from a prior navigation that hasn't
// been consumed yet. // been consumed yet.
const oldScrollRef = oldState.focusAndScrollRef.scrollRef const oldScrollRef = oldState.scrollRef.scrollRef
if (oldScrollRef !== null) { if (oldScrollRef !== null) {
oldScrollRef.current = false oldScrollRef.current = false
} }
...@@ -771,7 +771,7 @@ export function completeSoftNavigation( ...@@ -771,7 +771,7 @@ export function completeSoftNavigation(
// If this navigation created new scroll targets, invalidate any // If this navigation created new scroll targets, invalidate any
// pending scroll from a previous navigation. // pending scroll from a previous navigation.
if (scrollRef !== null) { if (scrollRef !== null) {
const oldScrollRef = oldState.focusAndScrollRef.scrollRef const oldScrollRef = oldState.scrollRef.scrollRef
if (oldScrollRef !== null) { if (oldScrollRef !== null) {
oldScrollRef.current = false oldScrollRef.current = false
} }
...@@ -787,7 +787,7 @@ export function completeSoftNavigation( ...@@ -787,7 +787,7 @@ export function completeSoftNavigation(
mpaNavigation: false, mpaNavigation: false,
preserveCustomHistoryState: false, preserveCustomHistoryState: false,
}, },
focusAndScrollRef: { scrollRef: {
scrollRef: activeScrollRef, scrollRef: activeScrollRef,
forceScroll, forceScroll,
onlyHashChange, onlyHashChange,
...@@ -797,10 +797,10 @@ export function completeSoftNavigation( ...@@ -797,10 +797,10 @@ export function completeSoftNavigation(
// Empty hash should trigger default behavior of scrolling layout into // Empty hash should trigger default behavior of scrolling layout into
// view. #top is handled in layout-router. // view. #top is handled in layout-router.
// //
// Refer to `ScrollAndFocusHandler` for details on how this is used. // Refer to `ScrollHandler` for details on how this is used.
scrollBehavior !== ScrollBehavior.NoScroll && url.hash !== '' scrollBehavior !== ScrollBehavior.NoScroll && url.hash !== ''
? decodeURIComponent(url.hash.slice(1)) ? decodeURIComponent(url.hash.slice(1))
: oldState.focusAndScrollRef.hashFragment, : oldState.scrollRef.hashFragment,
}, },
cache, cache,
tree, tree,
...@@ -829,7 +829,7 @@ export function completeTraverseNavigation( ...@@ -829,7 +829,7 @@ export function completeTraverseNavigation(
// Ensures that the custom history state that was set is preserved when applying this update. // Ensures that the custom history state that was set is preserved when applying this update.
preserveCustomHistoryState: true, preserveCustomHistoryState: true,
}, },
focusAndScrollRef: state.focusAndScrollRef, scrollRef: state.scrollRef,
cache, cache,
// Restore provided tree // Restore provided tree
tree, tree,
......
'use client' 'use client'
import type { import type {
FocusAndScrollRef, ScrollHandlerRef,
PrefetchKind, PrefetchKind,
} from '../../client/components/router-reducer/router-reducer-types' } from '../../client/components/router-reducer/router-reducer-types'
import type { Params } from '../../server/request/params' import type { Params } from '../../server/request/params'
...@@ -105,7 +105,7 @@ export const LayoutRouterContext = React.createContext<{ ...@@ -105,7 +105,7 @@ export const LayoutRouterContext = React.createContext<{
export const GlobalLayoutRouterContext = React.createContext<{ export const GlobalLayoutRouterContext = React.createContext<{
tree: FlightRouterState tree: FlightRouterState
focusAndScrollRef: FocusAndScrollRef scrollRef: ScrollHandlerRef
nextUrl: string | null nextUrl: string | null
previousNextUrl: string | null previousNextUrl: string | null
}>(null as any) }>(null as any)
......
...@@ -377,8 +377,8 @@ describe(`Terminal Logging (${bundlerName})`, () => { ...@@ -377,8 +377,8 @@ describe(`Terminal Logging (${bundlerName})`, () => {
https://react.dev/link/hydration-mismatch https://react.dev/link/hydration-mismatch
... ...
<ScrollAndMaybeFocusHandler cacheNode={{rsc:{...}, ...}}> <ScrollHandler cacheNode={{rsc:{...}, ...}}>
<InnerScrollAndMaybeFocusHandler focusAndScrollRef={{scrollRef:null, ...}} cacheNode={{rsc:{...}, ...}}> <InnerScrollHandler scrollRef={{scrollRef:null, ...}} cacheNode={{rsc:{...}, ...}}>
<ErrorBoundary errorComponent={undefined} errorStyles={undefined} errorScripts={undefined}> <ErrorBoundary errorComponent={undefined} errorStyles={undefined} errorScripts={undefined}>
<LoadingBoundary name="hydration-..." loading={null}> <LoadingBoundary name="hydration-..." loading={null}>
<HTTPAccessFallbackBoundary notFound={undefined} forbidden={undefined} unauthorized={undefined}> <HTTPAccessFallbackBoundary notFound={undefined} forbidden={undefined} unauthorized={undefined}>
......
...@@ -1541,12 +1541,12 @@ const nextjsClientComponentNames = [ ...@@ -1541,12 +1541,12 @@ const nextjsClientComponentNames = [
'HTTPAccessFallbackBoundary', 'HTTPAccessFallbackBoundary',
'HTTPAccessFallbackErrorBoundary', 'HTTPAccessFallbackErrorBoundary',
'InnerLayoutRouter', 'InnerLayoutRouter',
'InnerScrollAndMaybeFocusHandler', 'InnerScrollHandler',
'RedirectBoundary', 'RedirectBoundary',
'RedirectErrorBoundary', 'RedirectErrorBoundary',
'RenderFromTemplateContext', 'RenderFromTemplateContext',
'Root', 'Root',
'ScrollAndMaybeFocusHandler', 'ScrollHandler',
'SegmentViewNode', 'SegmentViewNode',
'SegmentTrieNode', 'SegmentTrieNode',
// These are added due to user actions e.g. loading.js -> LoadingBoundary // These are added due to user actions e.g. loading.js -> LoadingBoundary
......
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