Skip to content

Commit 1cbd79a

Browse files
committed
Add FragmentInstance operations reference list
1 parent d7b6049 commit 1cbd79a

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

src/content/reference/react/Fragment.md

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,15 @@ Wrap elements in `<Fragment>` to group them together in situations where you nee
4646

4747
When you pass a `ref` to a Fragment, React provides a `FragmentInstance` object. It implements methods for interacting with the first-level DOM children wrapped by the Fragment.
4848

49-
```js
50-
import { Fragment, useRef } from 'react';
51-
52-
function MyComponent() {
53-
const ref = useRef(null);
54-
return (
55-
<Fragment ref={ref}>
56-
<div id="A" />
57-
<Wrapper>
58-
<div id="B">
59-
<div id="C" />
60-
</div>
61-
</Wrapper>
62-
<div id="D" />
63-
</Fragment>
64-
);
65-
}
66-
```
49+
* [`addEventListener`](#addeventlistener) and [`removeEventListener`](#removeeventlistener) manage event listeners across all first-level DOM children.
50+
* [`dispatchEvent`](#dispatchevent) dispatches an event on the Fragment, which can bubble to the DOM parent.
51+
* [`focus`](#focus), [`focusLast`](#focuslast), and [`blur`](#blur) manage focus across all nested children depth-first.
52+
* [`observeUsing`](#observeusing) and [`unobserveUsing`](#unobserveusing) attach and detach `IntersectionObserver` or `ResizeObserver` instances.
53+
* [`getClientRects`](#getclientrects) returns bounding rectangles of all first-level DOM children.
54+
* [`getRootNode`](#getrootnode) returns the root node of the Fragment's parent.
55+
* [`compareDocumentPosition`](#comparedocumentposition) compares the Fragment's position with another node.
6756

68-
In the example above, the `FragmentInstance` targets `A`, `B`, and `D`—the first-level host (DOM) children. `C` is not targeted because it is nested inside `B`. If a new host sibling is dynamically added alongside `A`, `B`, or `D`, the `FragmentInstance` automatically tracks it and applies existing event listeners and observers.
57+
---
6958

7059
#### `addEventListener(type, listener, options?)` {/*addeventlistener*/}
7160

@@ -85,6 +74,8 @@ fragmentRef.current.addEventListener('click', handleClick);
8574

8675
`addEventListener` does not return anything (`undefined`).
8776

77+
---
78+
8879
#### `removeEventListener(type, listener, options?)` {/*removeeventlistener*/}
8980

9081
Removes an event listener from all first-level DOM children of the Fragment.
@@ -103,6 +94,8 @@ fragmentRef.current.removeEventListener('click', handleClick);
10394

10495
`removeEventListener` does not return anything (`undefined`).
10596

97+
---
98+
10699
#### `dispatchEvent(event)` {/*dispatchevent*/}
107100

108101
Dispatches an event on the Fragment. Added event listeners are called, and the event can bubble to the Fragment's DOM parent.
@@ -119,6 +112,8 @@ fragmentRef.current.dispatchEvent(new Event('custom', { bubbles: true }));
119112

120113
`true` if the event was not cancelled, `false` if `preventDefault()` was called.
121114

115+
---
116+
122117
#### `focus(options?)` {/*focus*/}
123118

124119
Focuses the first focusable DOM node in the Fragment. Unlike calling `element.focus()` on a DOM element, this method searches *all* nested children depth-first until it finds a focusable element—not just the element itself or its direct children.
@@ -135,6 +130,8 @@ fragmentRef.current.focus();
135130

136131
`focus` does not return anything (`undefined`).
137132

133+
---
134+
138135
#### `focusLast(options?)` {/*focuslast*/}
139136

140137
Focuses the last focusable DOM node in the Fragment. Searches nested children depth-first, then iterates in reverse.
@@ -151,6 +148,8 @@ fragmentRef.current.focusLast();
151148

152149
`focusLast` does not return anything (`undefined`).
153150

151+
---
152+
154153
#### `blur()` {/*blur*/}
155154

156155
Removes focus from the active element if it is within the Fragment. If `document.activeElement` is not within the Fragment, `blur` does nothing.
@@ -163,6 +162,8 @@ fragmentRef.current.blur();
163162

164163
`blur` does not return anything (`undefined`).
165164

165+
---
166+
166167
#### `observeUsing(observer)` {/*observeusing*/}
167168

168169
Starts observing all first-level DOM children of the Fragment with the provided observer.
@@ -180,6 +181,8 @@ fragmentRef.current.observeUsing(observer);
180181

181182
`observeUsing` does not return anything (`undefined`).
182183

184+
---
185+
183186
#### `unobserveUsing(observer)` {/*unobserveusing*/}
184187

185188
Stops observing the Fragment's DOM children with the specified observer.
@@ -196,6 +199,8 @@ fragmentRef.current.unobserveUsing(observer);
196199

197200
`unobserveUsing` does not return anything (`undefined`).
198201

202+
---
203+
199204
#### `getClientRects()` {/*getclientrects*/}
200205

201206
Returns a flat array of [`DOMRect`](https://developer.mozilla.org/en-US/docs/Web/API/DOMRect) objects representing the bounding rectangles of all first-level DOM children.
@@ -208,6 +213,8 @@ const rects = fragmentRef.current.getClientRects();
208213

209214
An `Array<DOMRect>` containing the bounding rectangles of all children.
210215

216+
---
217+
211218
#### `getRootNode(options?)` {/*getrootnode*/}
212219

213220
Returns the root node containing the Fragment's parent DOM node, matching the behavior of [`Node.getRootNode()`](https://developer.mozilla.org/en-US/docs/Web/API/Node/getRootNode).
@@ -224,6 +231,8 @@ const root = fragmentRef.current.getRootNode();
224231

225232
A `Document`, `ShadowRoot`, or the `FragmentInstance` itself if there is no parent DOM node.
226233

234+
---
235+
227236
#### `compareDocumentPosition(otherNode)` {/*comparedocumentposition*/}
228237

229238
Compares the document position of the Fragment with another node, returning a bitmask matching the behavior of [`Node.compareDocumentPosition()`](https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition).
@@ -240,6 +249,8 @@ const position = fragmentRef.current.compareDocumentPosition(otherElement);
240249

241250
A bitmask of [position flags](https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition#return_value). Empty Fragments and Fragments with children rendered through a [portal](/reference/react-dom/createPortal) include `Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC` in the result.
242251

252+
---
253+
243254
#### `FragmentInstance` Caveats {/*fragmentinstance-caveats*/}
244255

245256
* 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.

0 commit comments

Comments
 (0)