MCPcopy Create free account
hub / github.com/adobe/react-spectrum / insertBefore

Method insertBefore

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

Source from the content-addressed store, hash-verified

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