@react-aria/collections 3.0.3 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/import.mjs +7 -5
- package/dist/main.js +20 -18
- package/dist/main.js.map +1 -1
- package/dist/module.js +7 -5
- package/dist/module.js.map +1 -1
- package/dist/types/src/index.d.ts +8 -0
- package/package.json +14 -15
- package/src/index.ts +8 -7
- package/dist/BaseCollection.main.js +0 -271
- package/dist/BaseCollection.main.js.map +0 -1
- package/dist/BaseCollection.mjs +0 -260
- package/dist/BaseCollection.module.js +0 -260
- package/dist/BaseCollection.module.js.map +0 -1
- package/dist/CollectionBuilder.main.js +0 -242
- package/dist/CollectionBuilder.main.js.map +0 -1
- package/dist/CollectionBuilder.mjs +0 -230
- package/dist/CollectionBuilder.module.js +0 -230
- package/dist/CollectionBuilder.module.js.map +0 -1
- package/dist/Document.main.js +0 -369
- package/dist/Document.main.js.map +0 -1
- package/dist/Document.mjs +0 -364
- package/dist/Document.module.js +0 -364
- package/dist/Document.module.js.map +0 -1
- package/dist/Hidden.main.js +0 -90
- package/dist/Hidden.main.js.map +0 -1
- package/dist/Hidden.mjs +0 -79
- package/dist/Hidden.module.js +0 -79
- package/dist/Hidden.module.js.map +0 -1
- package/dist/types.d.ts +0 -119
- package/dist/types.d.ts.map +0 -1
- package/dist/useCachedChildren.main.js +0 -61
- package/dist/useCachedChildren.main.js.map +0 -1
- package/dist/useCachedChildren.mjs +0 -56
- package/dist/useCachedChildren.module.js +0 -56
- package/dist/useCachedChildren.module.js.map +0 -1
- package/src/BaseCollection.ts +0 -353
- package/src/CollectionBuilder.tsx +0 -265
- package/src/Document.ts +0 -569
- package/src/Hidden.tsx +0 -97
- package/src/useCachedChildren.ts +0 -70
package/src/Document.ts
DELETED
|
@@ -1,569 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2024 Adobe. All rights reserved.
|
|
3
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
*
|
|
7
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
* governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import {BaseCollection, CollectionNode, Mutable} from './BaseCollection';
|
|
14
|
-
import {CollectionNodeClass} from './CollectionBuilder';
|
|
15
|
-
import {CSSProperties, ForwardedRef, ReactElement, ReactNode} from 'react';
|
|
16
|
-
import {Node} from '@react-types/shared';
|
|
17
|
-
|
|
18
|
-
// This Collection implementation is perhaps a little unusual. It works by rendering the React tree into a
|
|
19
|
-
// Portal to a fake DOM implementation. This gives us efficient access to the tree of rendered objects, and
|
|
20
|
-
// supports React features like composition and context. We use this fake DOM to access the full set of elements
|
|
21
|
-
// before we render into the real DOM, which allows us to render a subset of the elements (e.g. virtualized scrolling),
|
|
22
|
-
// and compute properties like the total number of items. It also enables keyboard navigation, selection, and other features.
|
|
23
|
-
// React takes care of efficiently rendering components and updating the collection for us via this fake DOM.
|
|
24
|
-
//
|
|
25
|
-
// The DOM is a mutable API, and React expects the node instances to remain stable over time. So the implementation is split
|
|
26
|
-
// into two parts. Each mutable fake DOM node owns an instance of an immutable collection node. When a fake DOM node is updated,
|
|
27
|
-
// it queues a second render for the collection. Multiple updates to a collection can be queued at once. Collection nodes are
|
|
28
|
-
// lazily copied on write, so only the changed nodes need to be cloned. During the second render, the new immutable collection
|
|
29
|
-
// is finalized by updating the map of Key -> Node with the new cloned nodes. Then the new collection is frozen so it can no
|
|
30
|
-
// longer be mutated, and returned to the calling component to render.
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* A mutable node in the fake DOM tree. When mutated, it marks itself as dirty
|
|
34
|
-
* and queues an update with the owner document.
|
|
35
|
-
*/
|
|
36
|
-
export class BaseNode<T> {
|
|
37
|
-
private _firstChild: ElementNode<T> | null = null;
|
|
38
|
-
private _lastChild: ElementNode<T> | null = null;
|
|
39
|
-
private _previousSibling: ElementNode<T> | null = null;
|
|
40
|
-
private _nextSibling: ElementNode<T> | null = null;
|
|
41
|
-
private _parentNode: BaseNode<T> | null = null;
|
|
42
|
-
private _minInvalidChildIndex: ElementNode<T> | null = null;
|
|
43
|
-
ownerDocument: Document<T, any>;
|
|
44
|
-
|
|
45
|
-
constructor(ownerDocument: Document<T, any>) {
|
|
46
|
-
this.ownerDocument = ownerDocument;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
*[Symbol.iterator](): Iterator<ElementNode<T>> {
|
|
50
|
-
let node = this.firstChild;
|
|
51
|
-
while (node) {
|
|
52
|
-
yield node;
|
|
53
|
-
node = node.nextSibling;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
get firstChild(): ElementNode<T> | null {
|
|
58
|
-
return this._firstChild;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
set firstChild(firstChild: ElementNode<T> | null) {
|
|
62
|
-
this._firstChild = firstChild;
|
|
63
|
-
this.ownerDocument.markDirty(this);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
get lastChild(): ElementNode<T> | null {
|
|
67
|
-
return this._lastChild;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
set lastChild(lastChild: ElementNode<T> | null) {
|
|
71
|
-
this._lastChild = lastChild;
|
|
72
|
-
this.ownerDocument.markDirty(this);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
get previousSibling(): ElementNode<T> | null {
|
|
76
|
-
return this._previousSibling;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
set previousSibling(previousSibling: ElementNode<T> | null) {
|
|
80
|
-
this._previousSibling = previousSibling;
|
|
81
|
-
this.ownerDocument.markDirty(this);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
get nextSibling(): ElementNode<T> | null {
|
|
85
|
-
return this._nextSibling;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
set nextSibling(nextSibling: ElementNode<T> | null) {
|
|
89
|
-
this._nextSibling = nextSibling;
|
|
90
|
-
this.ownerDocument.markDirty(this);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
get parentNode(): BaseNode<T> | null {
|
|
94
|
-
return this._parentNode;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
set parentNode(parentNode: BaseNode<T> | null) {
|
|
98
|
-
this._parentNode = parentNode;
|
|
99
|
-
this.ownerDocument.markDirty(this);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
get isConnected(): boolean {
|
|
103
|
-
return this.parentNode?.isConnected || false;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
private invalidateChildIndices(child: ElementNode<T>): void {
|
|
107
|
-
if (this._minInvalidChildIndex == null || !this._minInvalidChildIndex.isConnected || child.index < this._minInvalidChildIndex.index) {
|
|
108
|
-
this._minInvalidChildIndex = child;
|
|
109
|
-
this.ownerDocument.markDirty(this);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
updateChildIndices(): void {
|
|
114
|
-
let node = this._minInvalidChildIndex;
|
|
115
|
-
while (node) {
|
|
116
|
-
node.index = node.previousSibling ? node.previousSibling.index + 1 : 0;
|
|
117
|
-
node = node.nextSibling;
|
|
118
|
-
}
|
|
119
|
-
this._minInvalidChildIndex = null;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
appendChild(child: ElementNode<T>): void {
|
|
123
|
-
if (child.parentNode) {
|
|
124
|
-
child.parentNode.removeChild(child);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (this.firstChild == null) {
|
|
128
|
-
this.firstChild = child;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (this.lastChild) {
|
|
132
|
-
this.lastChild.nextSibling = child;
|
|
133
|
-
child.index = this.lastChild.index + 1;
|
|
134
|
-
child.previousSibling = this.lastChild;
|
|
135
|
-
} else {
|
|
136
|
-
child.previousSibling = null;
|
|
137
|
-
child.index = 0;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
child.parentNode = this;
|
|
141
|
-
child.nextSibling = null;
|
|
142
|
-
this.lastChild = child;
|
|
143
|
-
|
|
144
|
-
this.ownerDocument.markDirty(this);
|
|
145
|
-
if (this.isConnected) {
|
|
146
|
-
this.ownerDocument.queueUpdate();
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
insertBefore(newNode: ElementNode<T>, referenceNode: ElementNode<T>): void {
|
|
151
|
-
if (referenceNode == null) {
|
|
152
|
-
return this.appendChild(newNode);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
if (newNode.parentNode) {
|
|
156
|
-
newNode.parentNode.removeChild(newNode);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
newNode.nextSibling = referenceNode;
|
|
160
|
-
newNode.previousSibling = referenceNode.previousSibling;
|
|
161
|
-
// Ensure that the newNode's index is less than that of the reference node so that
|
|
162
|
-
// invalidateChildIndices will properly use the newNode as the _minInvalidChildIndex, thus making sure
|
|
163
|
-
// we will properly update the indexes of all sibiling nodes after the newNode. The value here doesn't matter
|
|
164
|
-
// since updateChildIndices should calculate the proper indexes.
|
|
165
|
-
newNode.index = referenceNode.index - 1;
|
|
166
|
-
if (this.firstChild === referenceNode) {
|
|
167
|
-
this.firstChild = newNode;
|
|
168
|
-
} else if (referenceNode.previousSibling) {
|
|
169
|
-
referenceNode.previousSibling.nextSibling = newNode;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
referenceNode.previousSibling = newNode;
|
|
173
|
-
newNode.parentNode = referenceNode.parentNode;
|
|
174
|
-
|
|
175
|
-
this.invalidateChildIndices(newNode);
|
|
176
|
-
if (this.isConnected) {
|
|
177
|
-
this.ownerDocument.queueUpdate();
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
removeChild(child: ElementNode<T>): void {
|
|
182
|
-
if (child.parentNode !== this) {
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (this._minInvalidChildIndex === child) {
|
|
187
|
-
this._minInvalidChildIndex = null;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
if (child.nextSibling) {
|
|
191
|
-
this.invalidateChildIndices(child.nextSibling);
|
|
192
|
-
child.nextSibling.previousSibling = child.previousSibling;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
if (child.previousSibling) {
|
|
196
|
-
child.previousSibling.nextSibling = child.nextSibling;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
if (this.firstChild === child) {
|
|
200
|
-
this.firstChild = child.nextSibling;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
if (this.lastChild === child) {
|
|
204
|
-
this.lastChild = child.previousSibling;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
child.parentNode = null;
|
|
208
|
-
child.nextSibling = null;
|
|
209
|
-
child.previousSibling = null;
|
|
210
|
-
child.index = 0;
|
|
211
|
-
|
|
212
|
-
this.ownerDocument.markDirty(child);
|
|
213
|
-
if (this.isConnected) {
|
|
214
|
-
this.ownerDocument.queueUpdate();
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
addEventListener(): void {}
|
|
219
|
-
removeEventListener(): void {}
|
|
220
|
-
|
|
221
|
-
get previousVisibleSibling(): ElementNode<T> | null {
|
|
222
|
-
let node = this.previousSibling;
|
|
223
|
-
while (node && node.isHidden) {
|
|
224
|
-
node = node.previousSibling;
|
|
225
|
-
}
|
|
226
|
-
return node;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
get nextVisibleSibling(): ElementNode<T> | null {
|
|
230
|
-
let node = this.nextSibling;
|
|
231
|
-
while (node && node.isHidden) {
|
|
232
|
-
node = node.nextSibling;
|
|
233
|
-
}
|
|
234
|
-
return node;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
get firstVisibleChild(): ElementNode<T> | null {
|
|
238
|
-
let node = this.firstChild;
|
|
239
|
-
while (node && node.isHidden) {
|
|
240
|
-
node = node.nextSibling;
|
|
241
|
-
}
|
|
242
|
-
return node;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
get lastVisibleChild(): ElementNode<T> | null {
|
|
246
|
-
let node = this.lastChild;
|
|
247
|
-
while (node && node.isHidden) {
|
|
248
|
-
node = node.previousSibling;
|
|
249
|
-
}
|
|
250
|
-
return node;
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* A mutable element node in the fake DOM tree. It owns an immutable
|
|
256
|
-
* Collection Node which is copied on write.
|
|
257
|
-
*/
|
|
258
|
-
export class ElementNode<T> extends BaseNode<T> {
|
|
259
|
-
nodeType = 8; // COMMENT_NODE (we'd use ELEMENT_NODE but React DevTools will fail to get its dimensions)
|
|
260
|
-
node: CollectionNode<T> | null;
|
|
261
|
-
isMutated = true;
|
|
262
|
-
private _index: number = 0;
|
|
263
|
-
isHidden = false;
|
|
264
|
-
|
|
265
|
-
constructor(type: string, ownerDocument: Document<T, any>) {
|
|
266
|
-
super(ownerDocument);
|
|
267
|
-
this.node = null;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
get index(): number {
|
|
271
|
-
return this._index;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
set index(index: number) {
|
|
275
|
-
this._index = index;
|
|
276
|
-
this.ownerDocument.markDirty(this);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
get level(): number {
|
|
280
|
-
if (this.parentNode instanceof ElementNode) {
|
|
281
|
-
return this.parentNode.level + (this.parentNode.node?.type === 'item' ? 1 : 0);
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
return 0;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* Lazily gets a mutable instance of a Node. If the node has already
|
|
289
|
-
* been cloned during this update cycle, it just returns the existing one.
|
|
290
|
-
*/
|
|
291
|
-
private getMutableNode(): Mutable<CollectionNode<T>> | null {
|
|
292
|
-
if (this.node == null) {
|
|
293
|
-
return null;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
if (!this.isMutated) {
|
|
297
|
-
this.node = this.node.clone();
|
|
298
|
-
this.isMutated = true;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
this.ownerDocument.markDirty(this);
|
|
302
|
-
return this.node;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
updateNode(): void {
|
|
306
|
-
let nextSibling = this.nextVisibleSibling;
|
|
307
|
-
let node = this.getMutableNode();
|
|
308
|
-
if (node == null) {
|
|
309
|
-
return;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
node.index = this.index;
|
|
313
|
-
node.level = this.level;
|
|
314
|
-
node.parentKey = this.parentNode instanceof ElementNode ? this.parentNode.node?.key ?? null : null;
|
|
315
|
-
node.prevKey = this.previousVisibleSibling?.node?.key ?? null;
|
|
316
|
-
node.nextKey = nextSibling?.node?.key ?? null;
|
|
317
|
-
node.hasChildNodes = !!this.firstChild;
|
|
318
|
-
node.firstChildKey = this.firstVisibleChild?.node?.key ?? null;
|
|
319
|
-
node.lastChildKey = this.lastVisibleChild?.node?.key ?? null;
|
|
320
|
-
|
|
321
|
-
// Update the colIndex of sibling nodes if this node has a colSpan.
|
|
322
|
-
if ((node.colSpan != null || node.colIndex != null) && nextSibling) {
|
|
323
|
-
// This queues the next sibling for update, which means this happens recursively.
|
|
324
|
-
let nextColIndex = (node.colIndex ?? node.index) + (node.colSpan ?? 1);
|
|
325
|
-
if (nextSibling.node != null && nextColIndex !== nextSibling.node.colIndex) {
|
|
326
|
-
let siblingNode = nextSibling.getMutableNode();
|
|
327
|
-
siblingNode!.colIndex = nextColIndex;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
setProps<E extends Element>(obj: {[key: string]: any}, ref: ForwardedRef<E>, CollectionNodeClass: CollectionNodeClass<any>, rendered?: ReactNode, render?: (node: Node<T>) => ReactElement): void {
|
|
333
|
-
let node;
|
|
334
|
-
let {value, textValue, id, ...props} = obj;
|
|
335
|
-
if (this.node == null) {
|
|
336
|
-
node = new CollectionNodeClass(id ?? `react-aria-${++this.ownerDocument.nodeId}`);
|
|
337
|
-
this.node = node;
|
|
338
|
-
} else {
|
|
339
|
-
node = this.getMutableNode();
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
props.ref = ref;
|
|
343
|
-
node.props = props;
|
|
344
|
-
node.rendered = rendered;
|
|
345
|
-
node.render = render;
|
|
346
|
-
node.value = value;
|
|
347
|
-
if (obj['aria-label']) {
|
|
348
|
-
node['aria-label'] = obj['aria-label'];
|
|
349
|
-
}
|
|
350
|
-
node.textValue = textValue || (typeof props.children === 'string' ? props.children : '') || obj['aria-label'] || '';
|
|
351
|
-
if (id != null && id !== node.key) {
|
|
352
|
-
throw new Error('Cannot change the id of an item');
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
if (props.colSpan != null) {
|
|
356
|
-
node.colSpan = props.colSpan;
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
if (this.isConnected) {
|
|
360
|
-
this.ownerDocument.queueUpdate();
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
get style(): CSSProperties {
|
|
365
|
-
// React sets display: none to hide elements during Suspense.
|
|
366
|
-
// We'll handle this by setting the element to hidden and invalidating
|
|
367
|
-
// its siblings/parent. Hidden elements remain in the Document, but
|
|
368
|
-
// are removed from the Collection.
|
|
369
|
-
let element = this;
|
|
370
|
-
return {
|
|
371
|
-
get display() {
|
|
372
|
-
return element.isHidden ? 'none' : '';
|
|
373
|
-
},
|
|
374
|
-
set display(value) {
|
|
375
|
-
let isHidden = value === 'none';
|
|
376
|
-
if (element.isHidden !== isHidden) {
|
|
377
|
-
// Mark parent node dirty if this element is currently the first or last visible child.
|
|
378
|
-
if (element.parentNode?.firstVisibleChild === element || element.parentNode?.lastVisibleChild === element) {
|
|
379
|
-
element.ownerDocument.markDirty(element.parentNode);
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
// Mark sibling visible elements dirty.
|
|
383
|
-
let prev = element.previousVisibleSibling;
|
|
384
|
-
let next = element.nextVisibleSibling;
|
|
385
|
-
if (prev) {
|
|
386
|
-
element.ownerDocument.markDirty(prev);
|
|
387
|
-
}
|
|
388
|
-
if (next) {
|
|
389
|
-
element.ownerDocument.markDirty(next);
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
// Mark self dirty.
|
|
393
|
-
element.isHidden = isHidden;
|
|
394
|
-
element.ownerDocument.markDirty(element);
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
};
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
hasAttribute(): void {}
|
|
401
|
-
setAttribute(): void {}
|
|
402
|
-
setAttributeNS(): void {}
|
|
403
|
-
removeAttribute(): void {}
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
/**
|
|
407
|
-
* A mutable Document in the fake DOM. It owns an immutable Collection instance,
|
|
408
|
-
* which is lazily copied on write during updates.
|
|
409
|
-
*/
|
|
410
|
-
export class Document<T, C extends BaseCollection<T> = BaseCollection<T>> extends BaseNode<T> {
|
|
411
|
-
nodeType = 11; // DOCUMENT_FRAGMENT_NODE
|
|
412
|
-
ownerDocument: Document<T, C> = this;
|
|
413
|
-
dirtyNodes: Set<BaseNode<T>> = new Set();
|
|
414
|
-
isSSR = false;
|
|
415
|
-
nodeId = 0;
|
|
416
|
-
nodesByProps: WeakMap<object, ElementNode<T>> = new WeakMap<object, ElementNode<T>>();
|
|
417
|
-
private collection: C;
|
|
418
|
-
private nextCollection: C | null = null;
|
|
419
|
-
private subscriptions: Set<() => void> = new Set();
|
|
420
|
-
private queuedRender = false;
|
|
421
|
-
private inSubscription = false;
|
|
422
|
-
|
|
423
|
-
constructor(collection: C) {
|
|
424
|
-
// @ts-ignore
|
|
425
|
-
super(null);
|
|
426
|
-
this.collection = collection;
|
|
427
|
-
this.nextCollection = collection;
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
get isConnected(): boolean {
|
|
431
|
-
return true;
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
createElement(type: string): ElementNode<T> {
|
|
435
|
-
return new ElementNode(type, this);
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
private getMutableCollection() {
|
|
439
|
-
if (!this.nextCollection) {
|
|
440
|
-
this.nextCollection = this.collection.clone();
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
return this.nextCollection;
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
markDirty(node: BaseNode<T>): void {
|
|
447
|
-
this.dirtyNodes.add(node);
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
private addNode(element: ElementNode<T>): void {
|
|
451
|
-
if (element.isHidden || element.node == null) {
|
|
452
|
-
return;
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
let collection = this.getMutableCollection();
|
|
456
|
-
if (!collection.getItem(element.node.key)) {
|
|
457
|
-
for (let child of element) {
|
|
458
|
-
this.addNode(child);
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
collection.addNode(element.node);
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
private removeNode(node: ElementNode<T>): void {
|
|
466
|
-
for (let child of node) {
|
|
467
|
-
this.removeNode(child);
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
if (node.node) {
|
|
471
|
-
let collection = this.getMutableCollection();
|
|
472
|
-
collection.removeNode(node.node.key);
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
/** Finalizes the collection update, updating all nodes and freezing the collection. */
|
|
477
|
-
getCollection(): C {
|
|
478
|
-
// If in a subscription update, return return the existing collection.
|
|
479
|
-
// React will call getCollection again during render, at which point all the updates will be complete.
|
|
480
|
-
if (this.inSubscription) {
|
|
481
|
-
return this.collection;
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
// Reset queuedRender to false when getCollection is called during render.
|
|
485
|
-
this.queuedRender = false;
|
|
486
|
-
|
|
487
|
-
this.updateCollection();
|
|
488
|
-
return this.collection;
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
updateCollection(): void {
|
|
492
|
-
// First, remove disconnected nodes and update the indices of dirty element children.
|
|
493
|
-
for (let element of this.dirtyNodes) {
|
|
494
|
-
if (element instanceof ElementNode && (!element.isConnected || element.isHidden)) {
|
|
495
|
-
this.removeNode(element);
|
|
496
|
-
} else {
|
|
497
|
-
element.updateChildIndices();
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
// Next, update dirty collection nodes.
|
|
502
|
-
for (let element of this.dirtyNodes) {
|
|
503
|
-
if (element instanceof ElementNode) {
|
|
504
|
-
if (element.isConnected && !element.isHidden) {
|
|
505
|
-
element.updateNode();
|
|
506
|
-
this.addNode(element);
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
if (element.node) {
|
|
510
|
-
this.dirtyNodes.delete(element);
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
element.isMutated = false;
|
|
514
|
-
} else {
|
|
515
|
-
this.dirtyNodes.delete(element);
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
// Finally, update the collection.
|
|
520
|
-
if (this.nextCollection) {
|
|
521
|
-
this.nextCollection.commit(this.firstVisibleChild?.node?.key ?? null, this.lastVisibleChild?.node?.key ?? null, this.isSSR);
|
|
522
|
-
if (!this.isSSR) {
|
|
523
|
-
this.collection = this.nextCollection;
|
|
524
|
-
this.nextCollection = null;
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
queueUpdate(): void {
|
|
530
|
-
if (this.dirtyNodes.size === 0 || this.queuedRender) {
|
|
531
|
-
return;
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
// Only trigger subscriptions once during an update, when the first item changes.
|
|
535
|
-
// React's useSyncExternalStore will call getCollection immediately, to check whether the snapshot changed.
|
|
536
|
-
// If so, React will queue a render to happen after the current commit to our fake DOM finishes.
|
|
537
|
-
// We track whether getCollection is called in a subscription, and once it is called during render,
|
|
538
|
-
// we reset queuedRender back to false.
|
|
539
|
-
this.queuedRender = true;
|
|
540
|
-
this.inSubscription = true;
|
|
541
|
-
|
|
542
|
-
// Clone the collection to ensure that React queues a render. It will call getCollection again
|
|
543
|
-
// during render, at which point all the updates will be complete and we can return
|
|
544
|
-
// the new collection.
|
|
545
|
-
if (!this.isSSR) {
|
|
546
|
-
this.collection = this.collection.clone();
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
for (let fn of this.subscriptions) {
|
|
550
|
-
fn();
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
this.inSubscription = false;
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
subscribe(fn: () => void) {
|
|
557
|
-
this.subscriptions.add(fn);
|
|
558
|
-
return (): boolean => this.subscriptions.delete(fn);
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
resetAfterSSR(): void {
|
|
562
|
-
if (this.isSSR) {
|
|
563
|
-
this.isSSR = false;
|
|
564
|
-
this.firstChild = null;
|
|
565
|
-
this.lastChild = null;
|
|
566
|
-
this.nodeId = 0;
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
}
|
package/src/Hidden.tsx
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2024 Adobe. All rights reserved.
|
|
3
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
*
|
|
7
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
* governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import {forwardRefType} from '@react-types/shared';
|
|
14
|
-
import React, {Context, createContext, forwardRef, JSX, ReactElement, ReactNode, useContext} from 'react';
|
|
15
|
-
|
|
16
|
-
// React doesn't understand the <template> element, which doesn't have children like a normal element.
|
|
17
|
-
// It will throw an error during hydration when it expects the firstChild to contain content rendered
|
|
18
|
-
// on the server, when in reality, the browser will have placed this inside the `content` document fragment.
|
|
19
|
-
// This monkey patches the firstChild property for our special hidden template elements to work around this error.
|
|
20
|
-
// does the same for appendChild/removeChild/insertBefore as per the issue below
|
|
21
|
-
// See https://github.com/facebook/react/issues/19932
|
|
22
|
-
if (typeof HTMLTemplateElement !== 'undefined') {
|
|
23
|
-
Object.defineProperty(HTMLTemplateElement.prototype, 'firstChild', {
|
|
24
|
-
configurable: true,
|
|
25
|
-
enumerable: true,
|
|
26
|
-
get: function () {
|
|
27
|
-
return this.content.firstChild;
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
Object.defineProperty(HTMLTemplateElement.prototype, 'appendChild', {
|
|
32
|
-
configurable: true,
|
|
33
|
-
enumerable: true,
|
|
34
|
-
value: function (node) {
|
|
35
|
-
return this.content.appendChild(node);
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
Object.defineProperty(HTMLTemplateElement.prototype, 'removeChild', {
|
|
40
|
-
configurable: true,
|
|
41
|
-
enumerable: true,
|
|
42
|
-
value: function (node) {
|
|
43
|
-
return this.content.removeChild(node);
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
Object.defineProperty(HTMLTemplateElement.prototype, 'insertBefore', {
|
|
48
|
-
configurable: true,
|
|
49
|
-
enumerable: true,
|
|
50
|
-
value: function (node, child) {
|
|
51
|
-
return this.content.insertBefore(node, child);
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export const HiddenContext: Context<boolean> = createContext<boolean>(false);
|
|
57
|
-
|
|
58
|
-
export function Hidden(props: {children: ReactNode}): JSX.Element {
|
|
59
|
-
let isHidden = useContext(HiddenContext);
|
|
60
|
-
|
|
61
|
-
if (isHidden) {
|
|
62
|
-
// Don't hide again if we are already hidden.
|
|
63
|
-
return <>{props.children}</>;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
let children = (
|
|
67
|
-
<HiddenContext.Provider value>
|
|
68
|
-
{props.children}
|
|
69
|
-
</HiddenContext.Provider>
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
// In SSR, portals are not supported by React. Instead, always render into a <template>
|
|
73
|
-
// element, which the browser will never display to the user. In addition, the
|
|
74
|
-
// content is not part of the accessible DOM tree, so it won't affect ids or other accessibility attributes.
|
|
75
|
-
return <template>{children}</template>;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** Creates a component that forwards its ref and returns null if it is in a hidden subtree. */
|
|
79
|
-
// Note: this function is handled specially in the documentation generator. If you change it, you'll need to update DocsTransformer as well.
|
|
80
|
-
export function createHideableComponent<T, P = {}>(fn: (props: P, ref: React.Ref<T>) => ReactElement | null): (props: P & React.RefAttributes<T>) => ReactElement | null {
|
|
81
|
-
let Wrapper = (props: P, ref: React.Ref<T>) => {
|
|
82
|
-
let isHidden = useContext(HiddenContext);
|
|
83
|
-
if (isHidden) {
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return fn(props, ref);
|
|
88
|
-
};
|
|
89
|
-
// @ts-ignore - for react dev tools
|
|
90
|
-
Wrapper.displayName = fn.displayName || fn.name;
|
|
91
|
-
return (forwardRef as forwardRefType)(Wrapper);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** Returns whether the component is in a hidden subtree. */
|
|
95
|
-
export function useIsHidden(): boolean {
|
|
96
|
-
return useContext(HiddenContext);
|
|
97
|
-
}
|