You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
* 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() {
521
545
522
546
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.
523
547
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
+
functionScrollableSection({ children }) {
558
+
constfragmentRef=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>
0 commit comments