Skip to content

Commit d7b6049

Browse files
committed
Small clean ups
1 parent 8b39ceb commit d7b6049

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

src/content/reference/react/Fragment.md

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,8 @@ A bitmask of [position flags](https://developer.mozilla.org/en-US/docs/Web/API/N
243243
#### `FragmentInstance` Caveats {/*fragmentinstance-caveats*/}
244244

245245
* 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.
246-
* When host children are dynamically added or removed, the `FragmentInstance` automatically updates. New children receive any previously added event listeners and active observers.
247246
* `focus` and `focusLast` search nested children depth-first for focusable elements, unlike event and observer methods which only target first-level host children.
248247
* `observeUsing` does not work on text nodes. React logs a warning in development if the Fragment contains only text children.
249-
* `focus`, `focusLast`, and `blur` have no effect when the Fragment contains only text children.
250248
* React does not apply event listeners added via `addEventListener` to hidden [`<Activity>`](/reference/react/Activity) trees. When an `Activity` boundary switches from hidden to visible, listeners are applied automatically.
251249
* Each first-level DOM child of a Fragment with a `ref` gets a `reactFragments` property—a `Set<FragmentInstance>` containing all Fragment instances that own the element. This enables [caching a shared observer](#caching-global-intersection-observer) across multiple Fragments.
252250

@@ -430,7 +428,7 @@ function PostBody({ body }) {
430428
431429
Fragment `ref`s let you add event listeners to a group of elements without adding a wrapper DOM node. Use a [ref callback](/reference/react-dom/components/common#ref-callback) to attach and clean up listeners:
432430
433-
```js {4-7}
431+
```js
434432
import { Fragment } from 'react';
435433

436434
function ClickableGroup({ children, onClick }) {
@@ -473,35 +471,6 @@ Methods like `addEventListener`, `observeUsing`, and `getClientRects` operate on
473471
474472
---
475473
476-
### <CanaryBadge /> Observing visibility without a wrapper element {/*observing-visibility-without-wrapper*/}
477-
478-
Use `observeUsing` to attach an `IntersectionObserver` to all first-level DOM children of a Fragment. This lets you track visibility without requiring child components to expose `ref`s or adding a wrapper element:
479-
480-
```js {6-14,17}
481-
import { Fragment, useRef, useLayoutEffect } from 'react';
482-
483-
function VisibleGroup({ onVisibilityChange, children }) {
484-
const fragmentRef = useRef(null);
485-
486-
useLayoutEffect(() => {
487-
const observer = new IntersectionObserver((entries) => {
488-
onVisibilityChange(entries.some(entry => entry.isIntersecting));
489-
});
490-
491-
fragmentRef.current.observeUsing(observer);
492-
return () => fragmentRef.current.unobserveUsing(observer);
493-
}, [onVisibilityChange]);
494-
495-
return (
496-
<Fragment ref={fragmentRef}>
497-
{children}
498-
</Fragment>
499-
);
500-
}
501-
```
502-
503-
---
504-
505474
### <CanaryBadge /> Managing focus across a group of elements {/*managing-focus-across-elements*/}
506475
507476
Fragment `ref`s provide `focus`, `focusLast`, and `blur` methods that operate across all DOM nodes within the Fragment:
@@ -541,6 +510,36 @@ function App() {
541510
542511
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.
543512
513+
514+
---
515+
516+
### <CanaryBadge /> Observing visibility without a wrapper element {/*observing-visibility-without-wrapper*/}
517+
518+
Use `observeUsing` to attach an `IntersectionObserver` to all first-level DOM children of a Fragment. This lets you track visibility without requiring child components to expose `ref`s or adding a wrapper element:
519+
520+
```js
521+
import { Fragment, useRef, useLayoutEffect } from 'react';
522+
523+
function VisibleGroup({ onVisibilityChange, children }) {
524+
const fragmentRef = useRef(null);
525+
526+
useLayoutEffect(() => {
527+
const observer = new IntersectionObserver((entries) => {
528+
onVisibilityChange(entries.some(entry => entry.isIntersecting));
529+
});
530+
531+
fragmentRef.current.observeUsing(observer);
532+
return () => fragmentRef.current.unobserveUsing(observer);
533+
}, [onVisibilityChange]);
534+
535+
return (
536+
<Fragment ref={fragmentRef}>
537+
{children}
538+
</Fragment>
539+
);
540+
}
541+
```
542+
544543
---
545544
546545
### <CanaryBadge /> Caching a global IntersectionObserver {/*caching-global-intersection-observer*/}

0 commit comments

Comments
 (0)