MCPcopy
hub / github.com/adobe/react-spectrum / insertBefore

Method insertBefore

packages/react-aria/src/collections/Document.ts:154–183  ·  view source on GitHub ↗
(newNode: ElementNode<T>, referenceNode: ElementNode<T>)

Source from the content-addressed store, hash-verified

152 }
153
154 insertBefore(newNode: ElementNode<T>, referenceNode: ElementNode<T>): void {
155 if (referenceNode == null) {
156 return this.appendChild(newNode);
157 }
158
159 if (newNode.parentNode) {
160 newNode.parentNode.removeChild(newNode);
161 }
162
163 newNode.nextSibling = referenceNode;
164 newNode.previousSibling = referenceNode.previousSibling;
165 // Ensure that the newNode's index is less than that of the reference node so that
166 // invalidateChildIndices will properly use the newNode as the _minInvalidChildIndex, thus making sure
167 // we will properly update the indexes of all sibiling nodes after the newNode. The value here doesn't matter
168 // since updateChildIndices should calculate the proper indexes.
169 newNode.index = referenceNode.index - 1;
170 if (this.firstChild === referenceNode) {
171 this.firstChild = newNode;
172 } else if (referenceNode.previousSibling) {
173 referenceNode.previousSibling.nextSibling = newNode;
174 }
175
176 referenceNode.previousSibling = newNode;
177 newNode.parentNode = referenceNode.parentNode;
178
179 this.invalidateChildIndices(newNode);
180 if (this.isConnected) {
181 this.ownerDocument.queueUpdate();
182 }
183 }
184
185 removeChild(child: ElementNode<T>): void {
186 if (child.parentNode !== this) {

Callers

nothing calls this directly

Calls 4

appendChildMethod · 0.95
queueUpdateMethod · 0.80
removeChildMethod · 0.45

Tested by

no test coverage detected