Unverified Commit 68206c98 authored by Sam Ko's avatar Sam Ko Committed by GitHub

docs(link): add good to know comment about scroll behavior (#63152)

## Why?

We need to clarify that we are scrolling to the top of `{children}` when
it is not shown in the current viewport.

Closes NEXT-2780

---------
Co-authored-by: 's avatarZack Tanner <zacktanner@gmail.com>
parent cc57fe85
...@@ -130,7 +130,7 @@ export function Links() { ...@@ -130,7 +130,7 @@ export function Links() {
#### Scrolling to an `id` #### Scrolling to an `id`
The default behavior of the Next.js App Router is to scroll to the top of a new route or to maintain the scroll position for backwards and forwards navigation. The default behavior of the Next.js App Router is to **scroll to the top of a new route or to maintain the scroll position for backwards and forwards navigation.**
If you'd like to scroll to a specific `id` on navigation, you can append your URL with a `#` hash link or just pass a hash link to the `href` prop. This is possible since `<Link>` renders to an `<a>` element. If you'd like to scroll to a specific `id` on navigation, you can append your URL with a `#` hash link or just pass a hash link to the `href` prop. This is possible since `<Link>` renders to an `<a>` element.
...@@ -141,9 +141,13 @@ If you'd like to scroll to a specific `id` on navigation, you can append your UR ...@@ -141,9 +141,13 @@ If you'd like to scroll to a specific `id` on navigation, you can append your UR
<a href="/dashboard#settings">Settings</a> <a href="/dashboard#settings">Settings</a>
``` ```
> **Good to know**:
>
> - Next.js will scroll to the [Page](/docs/app/building-your-application/routing/pages-and-layouts#pages) if it is not visible in the viewport upon navigation.
#### Disabling scroll restoration #### Disabling scroll restoration
The default behavior of the Next.js App Router is to scroll to the top of a new route or to maintain the scroll position for backwards and forwards navigation. If you'd like to disable this behavior, you can pass `scroll={false}` to the `<Link>` component, or `scroll: false` to `router.push()` or `router.replace()`. The default behavior of the Next.js App Router is to **scroll to the top of a new route or to maintain the scroll position for backwards and forwards navigation.** If you'd like to disable this behavior, you can pass `scroll={false}` to the `<Link>` component, or `scroll: false` to `router.push()` or `router.replace()`.
```jsx ```jsx
// next/link // next/link
......
...@@ -153,7 +153,7 @@ export default function Page() { ...@@ -153,7 +153,7 @@ export default function Page() {
### `scroll` ### `scroll`
**Defaults to `true`.** The default behavior of `<Link>` is to scroll to the top of a new route or to maintain the scroll position for backwards and forwards navigation. When `false`, `next/link` will _not_ scroll to the top of the page after a navigation. **Defaults to `true`.** The default behavior of `<Link>` **is to scroll to the top of a new route or to maintain the scroll position for backwards and forwards navigation.** When `false`, `next/link` will _not_ scroll to the top of the page after a navigation.
```tsx filename="app/page.tsx" switcher ```tsx filename="app/page.tsx" switcher
import Link from 'next/link' import Link from 'next/link'
...@@ -179,6 +179,10 @@ export default function Page() { ...@@ -179,6 +179,10 @@ export default function Page() {
} }
``` ```
> **Good to know**:
>
> - Next.js will scroll to the [Page](/docs/app/building-your-application/routing/pages-and-layouts#pages) if it is not visible in the viewport upon navigation.
### `prefetch` ### `prefetch`
<AppOnly> <AppOnly>
......
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