Skip to content

Commit 07a93bd

Browse files
committed
restructure content
1 parent a156e2f commit 07a93bd

File tree

1 file changed

+24
-36
lines changed

1 file changed

+24
-36
lines changed

src/content/reference/react/Fragment.md

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: <Fragment> (<>...</>)
66

77
`<Fragment>`, often used via `<>...</>` syntax, lets you group elements without a wrapper node.
88

9-
<ExperimentalBadge /> Fragments can also accept refs, which enable interacting with underlying DOM nodes without adding wrapper elements.
9+
<Experimental> Fragments can also accept refs, which enable interacting with underlying DOM nodes without adding wrapper elements. See reference and usage below.</Experimental>
1010

1111
```js
1212
<>
@@ -27,43 +27,37 @@ title: <Fragment> (<>...</>)
2727

2828
Wrap elements in `<Fragment>` to group them together in situations where you need a single element. Grouping elements in `Fragment` has no effect on the resulting DOM; it is the same as if the elements were not grouped. The empty JSX tag `<></>` is shorthand for `<Fragment></Fragment>` in most cases.
2929

30-
<ExperimentalBadge /> Fragments can accept refs to provide access to the underlying DOM nodes they wrap, enabling interactions like event handling, intersection observation, and focus management without requiring additional wrapper elements.
31-
32-
#### Props {/*props*/}
33-
34-
- **optional** `key`: Fragments declared with the explicit `<Fragment>` syntax may have [keys.](/learn/rendering-lists#keeping-list-items-in-order-with-key)
35-
- <ExperimentalBadge /> **optional** `ref`: A ref object or callback function. React will forward the ref to a `FragmentInstance` object that provides methods for interacting with the DOM nodes wrapped by the Fragment.
36-
37-
<Experimental>
38-
#### FragmentInstance {/*fragmentinstance*/}
30+
### <ExperimentalBadge /> FragmentInstance {/*fragmentinstance*/}
3931

4032
When you pass a ref to a fragment, React provides a `FragmentInstance` object with methods for interacting with the DOM nodes wrapped by the fragment:
4133

4234
**Event handling methods:**
43-
- `addEventListener(type, listener, options?)`: Adds an event listener to all first-level DOM children of the fragment
44-
- `removeEventListener(type, listener, options?)`: Removes an event listener from all first-level DOM children of the fragment
35+
- `addEventListener(type, listener, options?)`: Adds an event listener to all first-level DOM children of the Fragment
36+
- `removeEventListener(type, listener, options?)`: Removes an event listener from all first-level DOM children of the Fragment
4537
- `dispatchEvent(event)`: Dispatches an event to a virtual child of the Fragment to call any added listeners and bubble to the DOM parent.
4638

4739
**Layout methods:**
48-
- `compareDocumentPosition(otherNode)`: Compares the document position of the fragment with another node
49-
- An element preceding a fragment is `Node.DOCUMENT_POSITION_PRECEDING`
50-
- An element following a fragment is `Node.DOCUMENT_POSITION_FOLLOWING`
51-
- An element containing the fragment is `Node.DOCUMENT_POSITION_PRECEDING` and `Node.DOCUMENT_POSITION_CONTAINING`
52-
- An element within the fragment is `NODE.DOCUMENT_POSITION_CONTAINED_BY`
40+
- `compareDocumentPosition(otherNode)`: Compares the document position of the Fragment with another node
41+
- If the Fragment has children, the native `compareDocumentPosition` value is returned.
5342
- Empty Fragments will attempt to compare positioning within the React tree and include `NODE.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC`
5443
- Elements that have a different relationship in the React tree and DOM tree due to portaling or other insertions are `NODE.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC`
5544
- `getClientRects()`: Returns a flat array of `ClientRect` objects representing the bounding rectangles of all DOM nodes
56-
- `getRootNode()`: Returns the root node containing the fragment's DOM nodes
45+
- `getRootNode()`: Returns the root node containing the Fragment's DOM nodes
5746

5847
**Focus management methods:**
5948
- `focus(options?)`: Focuses the first focusable DOM node in the Fragment
6049
- `focusLast(options?)`: Focuses the last focusable DOM node in the Fragment
61-
- `blur()`: Removes focus from all DOM nodes in the Fragment
50+
- `blur()`: Removes focus if `document.activeElement` is within the Fragment
6251

6352
**Observer methods:**
64-
- `observeUsing(observer)`: Starts observing the fragment's DOM children with an IntersectionObserver or ResizeObserver
65-
- `unobserveUsing(observer)`: Stops observing the fragment's DOM children with the specified observer
66-
</Experimental>
53+
- `observeUsing(observer)`: Starts observing the Fragment's DOM children with an IntersectionObserver or ResizeObserver
54+
- `unobserveUsing(observer)`: Stops observing the Fragment's DOM children with the specified observer
55+
56+
57+
#### Props {/*props*/}
58+
59+
- **optional** `key`: Fragments declared with the explicit `<Fragment>` syntax may have [keys.](/learn/rendering-lists#keeping-list-items-in-order-with-key)
60+
- <ExperimentalBadge /> **optional** `ref`: A ref object or callback function. React will forward the ref to a `FragmentInstance` object that provides methods for interacting with the DOM nodes wrapped by the Fragment.
6761

6862
#### Caveats {/*caveats*/}
6963

@@ -247,10 +241,9 @@ function PostBody({ body }) {
247241
248242
</Sandpack>
249243
250-
<Experimental>
251244
---
252245
253-
### Using Fragment refs for DOM interaction {/*using-fragment-refs-for-dom-interaction*/}
246+
### <ExperimentalBadge /> Using Fragment refs for DOM interaction {/*using-fragment-refs-for-dom-interaction*/}
254247
255248
Fragment refs allow you to interact with the DOM nodes wrapped by a Fragment without adding extra wrapper elements. This is useful for event handling, visibility tracking, focus management, and replacing deprecated patterns like `ReactDOM.findDOMNode()`.
256249
@@ -278,7 +271,7 @@ function ClickableWrapper({ children, onClick }) {
278271
```
279272
---
280273
281-
### Tracking visibility with Fragment refs {/*tracking-visibility-with-fragment-refs*/}
274+
### <ExperimentalBadge /> Tracking visibility with Fragment refs {/*tracking-visibility-with-fragment-refs*/}
282275
283276
Fragment refs are particularly useful for visibility tracking and intersection observation. This enables you to monitor when content becomes visible without requiring the child components to expose refs:
284277
@@ -291,7 +284,7 @@ function VisibilityObserver({ threshold = 0.5, onVisibilityChange, children }) {
291284
useEffect(() => {
292285
const observer = new IntersectionObserver(
293286
(entries) => {
294-
onVisibilityChange(entries[0].isIntersecting);
287+
onVisibilityChange(entries.some(entry => entry.isIntersecting))
295288
},
296289
{ threshold }
297290
);
@@ -325,27 +318,22 @@ This pattern replaces effect-based visibility logging, which can be unreliable d
325318
326319
---
327320
328-
### Focus management with Fragment refs {/*focus-management-with-fragment-refs*/}
321+
### <ExperimentalBadge /> Focus management with Fragment refs {/*focus-management-with-fragment-refs*/}
329322
330323
Fragment refs provide focus management methods that work across all DOM nodes within the Fragment:
331324
332325
```js
333326
import { Fragment, useRef } from 'react';
334327

335-
function FocusableGroup({ children }) {
328+
function Focus({ children }) {
336329
const fragmentRef = useRef(null);
337330

338331
return (
339-
<>
340-
<button onClick={() => {fragmentRef.current.focus()}}>Focus</button>
341-
<Fragment ref={fragmentRef}>
342-
{children}
343-
</Fragment>
344-
</>
332+
<Fragment ref={(fragmentInstance) => fragmentInstance?.focus()}>
333+
{children}
334+
</Fragment>
345335
);
346336
}
347337
```
348338
349339
The `focus()` method focuses the first focusable element within the Fragment, while `focusLast()` focuses the last focusable element.
350-
351-
</Experimental>

0 commit comments

Comments
 (0)