Skip to content

Commit a7e1ba6

Browse files
committed
Add reference to scrollIntoView operation
1 parent 1cbd79a commit a7e1ba6

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/content/reference/react/Fragment.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ When you pass a `ref` to a Fragment, React provides a `FragmentInstance` object.
5353
* [`getClientRects`](#getclientrects) returns bounding rectangles of all first-level DOM children.
5454
* [`getRootNode`](#getrootnode) returns the root node of the Fragment's parent.
5555
* [`compareDocumentPosition`](#comparedocumentposition) compares the Fragment's position with another node.
56+
* [`scrollIntoView`](#scrollintoview) scrolls the Fragment's children into view.
5657

5758
---
5859

@@ -251,6 +252,29 @@ A bitmask of [position flags](https://developer.mozilla.org/en-US/docs/Web/API/N
251252

252253
---
253254

255+
#### `scrollIntoView(alignToTop?)` {/*scrollintoview*/}
256+
257+
Scrolls the Fragment's children into view. When `alignToTop` is `true` or omitted, scrolls to align the first child with the top of the scrollable ancestor. When `alignToTop` is `false`, scrolls to align the last child with the bottom.
258+
259+
```js
260+
fragmentRef.current.scrollIntoView();
261+
```
262+
263+
##### Parameters {/*scrollintoview-parameters*/}
264+
265+
* **optional** `alignToTop`: A boolean. If `true` (the default), scrolls the first child to the top of the scrollable area. If `false`, scrolls the last child to the bottom. Unlike [`Element.scrollIntoView()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView), this method does not accept a `ScrollIntoViewOptions` object.
266+
267+
##### Returns {/*scrollintoview-returns*/}
268+
269+
`scrollIntoView` does not return anything (`undefined`).
270+
271+
##### Caveats {/*scrollintoview-caveats*/}
272+
273+
* `scrollIntoView` does not accept an options object. Passing one throws an error. Use the `alignToTop` boolean instead.
274+
* When the Fragment has no children, `scrollIntoView` scrolls the nearest sibling or parent into view as a fallback.
275+
276+
---
277+
254278
#### `FragmentInstance` Caveats {/*fragmentinstance-caveats*/}
255279

256280
* Methods that target children (such as `addEventListener`, `observeUsing`, and `getClientRects`) operate on *first-level host (DOM) children* of the Fragment. They do not directly target children nested inside another DOM element.
@@ -521,6 +545,29 @@ function App() {
521545
522546
Calling `focusFirst()` focuses the `street` input—even though it is nested inside a `<fieldset>` and `<label>`. `focus()` searches depth-first through all nested children, not just direct children of the Fragment. `focusLast()` does the same in reverse, and `blur()` removes focus if the currently focused element is within the Fragment.
523547
548+
---
549+
550+
### <CanaryBadge /> Scrolling a group of elements into view {/*scrolling-group-into-view*/}
551+
552+
Use `scrollIntoView` to scroll a Fragment's children into view without a wrapper element. Pass `true` (or omit the argument) to scroll the first child to the top. Pass `false` to scroll the last child to the bottom:
553+
554+
```js
555+
import { Fragment, useRef } from 'react';
556+
557+
function ScrollableSection({ children }) {
558+
const fragmentRef = useRef(null);
559+
560+
return (
561+
<>
562+
<button onClick={() => fragmentRef.current.scrollIntoView()}>Scroll to top</button>
563+
<button onClick={() => fragmentRef.current.scrollIntoView(false)}>Scroll to bottom</button>
564+
<Fragment ref={fragmentRef}>
565+
{children}
566+
</Fragment>
567+
</>
568+
);
569+
}
570+
```
524571
525572
---
526573

0 commit comments

Comments
 (0)