-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix inconsistent scroll restoration behavior
- Loading branch information
Showing
8 changed files
with
148 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
test/e2e/app-dir/navigation/app/scroll-restoration/layout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use client' | ||
import { useState, createContext } from 'react' | ||
|
||
interface Item { | ||
id: number | ||
} | ||
|
||
export const ItemsContext = createContext<{ | ||
items: Item[] | ||
loadMoreItems: () => void | ||
}>({ items: [], loadMoreItems: () => {} }) | ||
|
||
const createItems = (start: number, end: number): Item[] => { | ||
const items: Item[] = [] | ||
for (let i = start; i <= end; i++) { | ||
items.push({ id: i }) | ||
} | ||
return items | ||
} | ||
|
||
export default function Layout({ children }: { children: React.ReactNode }) { | ||
const [items, setItems] = useState<Item[]>(createItems(1, 50)) | ||
|
||
const loadMoreItems = () => { | ||
const start = items.length + 1 | ||
const end = start + 50 - 1 | ||
setItems((prevItems) => [...prevItems, ...createItems(start, end)]) | ||
} | ||
|
||
return ( | ||
<ItemsContext.Provider value={{ items, loadMoreItems }}> | ||
{children} | ||
</ItemsContext.Provider> | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
test/e2e/app-dir/navigation/app/scroll-restoration/other/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use client' | ||
import { useRouter } from 'next/navigation' | ||
|
||
export default function Page() { | ||
const router = useRouter() | ||
return ( | ||
<div> | ||
<button onClick={() => router.back()} id="back-button"> | ||
Go Back | ||
</button> | ||
</div> | ||
) | ||
} |
23 changes: 23 additions & 0 deletions
23
test/e2e/app-dir/navigation/app/scroll-restoration/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use client' | ||
import Link from 'next/link' | ||
import React, { useContext, useState } from 'react' | ||
import { ItemsContext } from './layout' | ||
|
||
export default function Page() { | ||
const { items, loadMoreItems } = useContext(ItemsContext) | ||
|
||
return ( | ||
<div> | ||
<h1>Page</h1> | ||
<ul> | ||
{items.map((item) => ( | ||
<li key={item.id}>Item {item.id}</li> | ||
))} | ||
</ul> | ||
<button id="load-more" onClick={loadMoreItems}> | ||
Load More | ||
</button> | ||
<Link href="/scroll-restoration/other">Go to Other</Link> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters