Unverified Commit fae919c2 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add focus and scroll management for new router (#38642)

parent e06983bf
...@@ -3,10 +3,12 @@ import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-we ...@@ -3,10 +3,12 @@ import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-we
import { import {
AppRouterContext, AppRouterContext,
AppTreeContext, AppTreeContext,
CacheNode,
FullAppTreeContext, FullAppTreeContext,
} from '../../shared/lib/app-router-context' } from '../../shared/lib/app-router-context'
import type { AppRouterInstance } from '../../shared/lib/app-router-context' import type {
CacheNode,
AppRouterInstance,
} from '../../shared/lib/app-router-context'
import type { FlightRouterState, FlightData } from '../../server/app-render' import type { FlightRouterState, FlightData } from '../../server/app-render'
import { reducer } from './reducer' import { reducer } from './reducer'
import { import {
...@@ -117,19 +119,19 @@ export default function AppRouter({ ...@@ -117,19 +119,19 @@ export default function AppRouter({
children: React.ReactNode children: React.ReactNode
hotReloader?: React.ReactNode hotReloader?: React.ReactNode
}) { }) {
const [{ tree, cache, pushRef, canonicalUrl }, dispatch] = React.useReducer< const [{ tree, cache, pushRef, focusRef, canonicalUrl }, dispatch] =
typeof reducer React.useReducer<typeof reducer>(reducer, {
>(reducer, { tree: initialTree,
tree: initialTree, cache: {
cache: { data: null,
data: null, subTreeData: children,
subTreeData: children, parallelRoutes:
parallelRoutes: typeof window === 'undefined' ? new Map() : initialParallelRoutes,
typeof window === 'undefined' ? new Map() : initialParallelRoutes, },
}, pushRef: { pendingPush: false, mpaNavigation: false },
pushRef: { pendingPush: false, mpaNavigation: false }, focusRef: { focus: false },
canonicalUrl: initialCanonicalUrl, canonicalUrl: initialCanonicalUrl,
}) })
useEffect(() => { useEffect(() => {
initialParallelRoutes = null! initialParallelRoutes = null!
...@@ -302,6 +304,7 @@ export default function AppRouter({ ...@@ -302,6 +304,7 @@ export default function AppRouter({
value={{ value={{
changeByServerResponse, changeByServerResponse,
tree, tree,
focusRef,
}} }}
> >
<AppRouterContext.Provider value={appRouter}> <AppRouterContext.Provider value={appRouter}>
......
import React, { useContext } from 'react' import React, { useContext, useEffect, useRef } from 'react'
import type { ChildProp } from '../../server/app-render' import type { ChildProp } from '../../server/app-render'
import type { ChildSegmentMap } from '../../shared/lib/app-router-context' import type { ChildSegmentMap } from '../../shared/lib/app-router-context'
import type { import type {
...@@ -61,8 +61,20 @@ export function InnerLayoutRouter({ ...@@ -61,8 +61,20 @@ export function InnerLayoutRouter({
isActive: boolean isActive: boolean
path: string path: string
}) { }) {
const { changeByServerResponse, tree: fullTree } = const {
useContext(FullAppTreeContext) changeByServerResponse,
tree: fullTree,
focusRef,
} = useContext(FullAppTreeContext)
const focusAndScrollRef = useRef<HTMLDivElement>(null)
useEffect(() => {
if (focusRef.focus && focusAndScrollRef.current) {
focusRef.focus = false
focusAndScrollRef.current.focus()
focusAndScrollRef.current.scrollIntoView()
}
}, [focusRef])
let childNode = childNodes.get(path) let childNode = childNodes.get(path)
...@@ -197,16 +209,18 @@ export function InnerLayoutRouter({ ...@@ -197,16 +209,18 @@ export function InnerLayoutRouter({
} }
return ( return (
<AppTreeContext.Provider <div ref={focusAndScrollRef}>
value={{ <AppTreeContext.Provider
tree: tree[1][parallelRouterKey], value={{
childNodes: childNode.parallelRoutes, tree: tree[1][parallelRouterKey],
// TODO-APP: overriding of url for parallel routes childNodes: childNode.parallelRoutes,
url: url, // TODO-APP: overriding of url for parallel routes
}} url: url,
> }}
{childNode.subTreeData} >
</AppTreeContext.Provider> {childNode.subTreeData}
</AppTreeContext.Provider>
</div>
) )
} }
......
...@@ -298,10 +298,20 @@ const walkTreeWithFlightDataPath = ( ...@@ -298,10 +298,20 @@ const walkTreeWithFlightDataPath = (
return tree return tree
} }
type PushRef = {
pendingPush: boolean
mpaNavigation: boolean
}
export type FocusRef = {
focus: boolean
}
type AppRouterState = { type AppRouterState = {
tree: FlightRouterState tree: FlightRouterState
cache: CacheNode cache: CacheNode
pushRef: { pendingPush: boolean; mpaNavigation: boolean } pushRef: PushRef
focusRef: FocusRef
canonicalUrl: string canonicalUrl: string
} }
...@@ -349,6 +359,7 @@ export function reducer( ...@@ -349,6 +359,7 @@ export function reducer(
return { return {
canonicalUrl: href, canonicalUrl: href,
pushRef: state.pushRef, pushRef: state.pushRef,
focusRef: state.focusRef,
cache: state.cache, cache: state.cache,
tree: tree, tree: tree,
} }
...@@ -377,6 +388,7 @@ export function reducer( ...@@ -377,6 +388,7 @@ export function reducer(
return { return {
canonicalUrl: href, canonicalUrl: href,
pushRef: { pendingPush, mpaNavigation: false }, pushRef: { pendingPush, mpaNavigation: false },
focusRef: { focus: true },
cache: state.cache, cache: state.cache,
tree: optimisticTree, tree: optimisticTree,
} }
...@@ -393,6 +405,7 @@ export function reducer( ...@@ -393,6 +405,7 @@ export function reducer(
return { return {
canonicalUrl: href, canonicalUrl: href,
pushRef: { pendingPush, mpaNavigation: false }, pushRef: { pendingPush, mpaNavigation: false },
focusRef: { focus: true },
cache: cache, cache: cache,
tree: mutable.patchedTree, tree: mutable.patchedTree,
} }
...@@ -430,6 +443,7 @@ export function reducer( ...@@ -430,6 +443,7 @@ export function reducer(
return { return {
canonicalUrl: href, canonicalUrl: href,
pushRef: { pendingPush, mpaNavigation: false }, pushRef: { pendingPush, mpaNavigation: false },
focusRef: { focus: true },
cache: cache, cache: cache,
tree: optimisticTree, tree: optimisticTree,
} }
...@@ -446,6 +460,7 @@ export function reducer( ...@@ -446,6 +460,7 @@ export function reducer(
return { return {
canonicalUrl: flightData, canonicalUrl: flightData,
pushRef: { pendingPush: true, mpaNavigation: true }, pushRef: { pendingPush: true, mpaNavigation: true },
focusRef: { focus: false },
cache: state.cache, cache: state.cache,
tree: state.tree, tree: state.tree,
} }
...@@ -474,6 +489,7 @@ export function reducer( ...@@ -474,6 +489,7 @@ export function reducer(
return { return {
canonicalUrl: href, canonicalUrl: href,
pushRef: { pendingPush, mpaNavigation: false }, pushRef: { pendingPush, mpaNavigation: false },
focusRef: { focus: true },
cache: cache, cache: cache,
tree: newTree, tree: newTree,
} }
...@@ -490,6 +506,7 @@ export function reducer( ...@@ -490,6 +506,7 @@ export function reducer(
return { return {
canonicalUrl: state.canonicalUrl, canonicalUrl: state.canonicalUrl,
pushRef: state.pushRef, pushRef: state.pushRef,
focusRef: state.focusRef,
tree: state.tree, tree: state.tree,
cache: state.cache, cache: state.cache,
} }
...@@ -500,6 +517,7 @@ export function reducer( ...@@ -500,6 +517,7 @@ export function reducer(
return { return {
canonicalUrl: flightData, canonicalUrl: flightData,
pushRef: { pendingPush: true, mpaNavigation: true }, pushRef: { pendingPush: true, mpaNavigation: true },
focusRef: { focus: false },
cache: state.cache, cache: state.cache,
tree: state.tree, tree: state.tree,
} }
...@@ -525,6 +543,7 @@ export function reducer( ...@@ -525,6 +543,7 @@ export function reducer(
return { return {
canonicalUrl: state.canonicalUrl, canonicalUrl: state.canonicalUrl,
pushRef: state.pushRef, pushRef: state.pushRef,
focusRef: state.focusRef,
tree: newTree, tree: newTree,
cache: cache, cache: cache,
} }
...@@ -546,6 +565,7 @@ export function reducer( ...@@ -546,6 +565,7 @@ export function reducer(
return { return {
canonicalUrl: href, canonicalUrl: href,
pushRef: { pendingPush, mpaNavigation: false }, pushRef: { pendingPush, mpaNavigation: false },
focusRef: { focus: true },
cache: cache, cache: cache,
tree: mutable.patchedTree, tree: mutable.patchedTree,
} }
...@@ -566,6 +586,7 @@ export function reducer( ...@@ -566,6 +586,7 @@ export function reducer(
return { return {
canonicalUrl: flightData, canonicalUrl: flightData,
pushRef: { pendingPush: true, mpaNavigation: true }, pushRef: { pendingPush: true, mpaNavigation: true },
focusRef: { focus: false },
cache: state.cache, cache: state.cache,
tree: state.tree, tree: state.tree,
} }
...@@ -597,6 +618,8 @@ export function reducer( ...@@ -597,6 +618,8 @@ export function reducer(
return { return {
canonicalUrl: href, canonicalUrl: href,
pushRef: { pendingPush, mpaNavigation: false }, pushRef: { pendingPush, mpaNavigation: false },
// TODO-APP: Revisit if this needs to be true in certain cases
focusRef: { focus: false },
cache: cache, cache: cache,
tree: newTree, tree: newTree,
} }
......
import React from 'react' import React from 'react'
import type { FocusRef } from '../../client/components/reducer'
import type { FlightRouterState, FlightData } from '../../server/app-render' import type { FlightRouterState, FlightData } from '../../server/app-render'
export type ChildSegmentMap = Map<string, CacheNode> export type ChildSegmentMap = Map<string, CacheNode>
...@@ -36,6 +37,7 @@ export const FullAppTreeContext = React.createContext<{ ...@@ -36,6 +37,7 @@ export const FullAppTreeContext = React.createContext<{
previousTree: FlightRouterState, previousTree: FlightRouterState,
flightData: FlightData flightData: FlightData
) => void ) => void
focusRef: FocusRef
}>(null as any) }>(null as any)
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
......
...@@ -193,10 +193,7 @@ describe('app dir - react server components', () => { ...@@ -193,10 +193,7 @@ describe('app dir - react server components', () => {
it('should support next/link in server components', async () => { it('should support next/link in server components', async () => {
const linkHTML = await renderViaHTTP(next.url, '/next-api/link') const linkHTML = await renderViaHTTP(next.url, '/next-api/link')
const linkText = getNodeBySelector( const linkText = getNodeBySelector(linkHTML, 'body a[href="/root"]').text()
linkHTML,
'body > div > a[href="/root"]'
).text()
expect(linkText).toContain('home') expect(linkText).toContain('home')
......
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