@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/BaseCollection.ts
DELETED
|
@@ -1,353 +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 {Collection as ICollection, Key, Node} from '@react-types/shared';
|
|
14
|
-
import {ReactElement, ReactNode} from 'react';
|
|
15
|
-
|
|
16
|
-
export type Mutable<T> = {
|
|
17
|
-
-readonly[P in keyof T]: T[P]
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
type FilterFn<T> = (textValue: string, node: Node<T>) => boolean;
|
|
21
|
-
|
|
22
|
-
/** An immutable object representing a Node in a Collection. */
|
|
23
|
-
export class CollectionNode<T> implements Node<T> {
|
|
24
|
-
static readonly type: string;
|
|
25
|
-
readonly type: string;
|
|
26
|
-
readonly key: Key;
|
|
27
|
-
readonly value: T | null = null;
|
|
28
|
-
readonly level: number = 0;
|
|
29
|
-
readonly hasChildNodes: boolean = false;
|
|
30
|
-
readonly rendered: ReactNode = null;
|
|
31
|
-
readonly textValue: string = '';
|
|
32
|
-
readonly 'aria-label'?: string = undefined;
|
|
33
|
-
readonly index: number = 0;
|
|
34
|
-
readonly parentKey: Key | null = null;
|
|
35
|
-
readonly prevKey: Key | null = null;
|
|
36
|
-
readonly nextKey: Key | null = null;
|
|
37
|
-
readonly firstChildKey: Key | null = null;
|
|
38
|
-
readonly lastChildKey: Key | null = null;
|
|
39
|
-
readonly props: any = {};
|
|
40
|
-
readonly render?: (node: Node<any>) => ReactElement;
|
|
41
|
-
readonly colSpan: number | null = null;
|
|
42
|
-
readonly colIndex: number | null = null;
|
|
43
|
-
|
|
44
|
-
constructor(key: Key) {
|
|
45
|
-
this.type = (this.constructor as typeof CollectionNode).type;
|
|
46
|
-
this.key = key;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
get childNodes(): Iterable<Node<T>> {
|
|
50
|
-
throw new Error('childNodes is not supported');
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
clone(): this {
|
|
54
|
-
let node: Mutable<this> = new (this.constructor as any)(this.key);
|
|
55
|
-
node.value = this.value;
|
|
56
|
-
node.level = this.level;
|
|
57
|
-
node.hasChildNodes = this.hasChildNodes;
|
|
58
|
-
node.rendered = this.rendered;
|
|
59
|
-
node.textValue = this.textValue;
|
|
60
|
-
node['aria-label'] = this['aria-label'];
|
|
61
|
-
node.index = this.index;
|
|
62
|
-
node.parentKey = this.parentKey;
|
|
63
|
-
node.prevKey = this.prevKey;
|
|
64
|
-
node.nextKey = this.nextKey;
|
|
65
|
-
node.firstChildKey = this.firstChildKey;
|
|
66
|
-
node.lastChildKey = this.lastChildKey;
|
|
67
|
-
node.props = this.props;
|
|
68
|
-
node.render = this.render;
|
|
69
|
-
node.colSpan = this.colSpan;
|
|
70
|
-
node.colIndex = this.colIndex;
|
|
71
|
-
return node;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
75
|
-
filter(collection: BaseCollection<T>, newCollection: BaseCollection<T>, filterFn: FilterFn<T>): CollectionNode<T> | null {
|
|
76
|
-
let clone = this.clone();
|
|
77
|
-
newCollection.addDescendants(clone, collection);
|
|
78
|
-
return clone;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export class FilterableNode<T> extends CollectionNode<T> {
|
|
83
|
-
filter(collection: BaseCollection<T>, newCollection: BaseCollection<T>, filterFn: FilterFn<T>): CollectionNode<T> | null {
|
|
84
|
-
let [firstKey, lastKey] = filterChildren(collection, newCollection, this.firstChildKey, filterFn);
|
|
85
|
-
let newNode: Mutable<CollectionNode<T>> = this.clone();
|
|
86
|
-
newNode.firstChildKey = firstKey;
|
|
87
|
-
newNode.lastChildKey = lastKey;
|
|
88
|
-
return newNode;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export class HeaderNode extends CollectionNode<unknown> {
|
|
93
|
-
static readonly type = 'header';
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export class LoaderNode extends CollectionNode<unknown> {
|
|
97
|
-
static readonly type = 'loader';
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export class ItemNode<T> extends FilterableNode<T> {
|
|
101
|
-
static readonly type = 'item';
|
|
102
|
-
|
|
103
|
-
filter(collection: BaseCollection<T>, newCollection: BaseCollection<T>, filterFn: FilterFn<T>): ItemNode<T> | null {
|
|
104
|
-
if (filterFn(this.textValue, this)) {
|
|
105
|
-
let clone = this.clone();
|
|
106
|
-
newCollection.addDescendants(clone, collection);
|
|
107
|
-
return clone;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return null;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export class SectionNode<T> extends FilterableNode<T> {
|
|
115
|
-
static readonly type = 'section';
|
|
116
|
-
|
|
117
|
-
filter(collection: BaseCollection<T>, newCollection: BaseCollection<T>, filterFn: FilterFn<T>): SectionNode<T> | null {
|
|
118
|
-
let filteredSection = super.filter(collection, newCollection, filterFn);
|
|
119
|
-
if (filteredSection) {
|
|
120
|
-
if (filteredSection.lastChildKey !== null) {
|
|
121
|
-
let lastChild = collection.getItem(filteredSection.lastChildKey);
|
|
122
|
-
if (lastChild && lastChild.type !== 'header') {
|
|
123
|
-
return filteredSection;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return null;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* An immutable Collection implementation. Updates are only allowed
|
|
134
|
-
* when it is not marked as frozen. This can be subclassed to implement
|
|
135
|
-
* custom collection behaviors.
|
|
136
|
-
*/
|
|
137
|
-
export class BaseCollection<T> implements ICollection<Node<T>> {
|
|
138
|
-
protected keyMap: Map<Key, CollectionNode<T>> = new Map();
|
|
139
|
-
protected firstKey: Key | null = null;
|
|
140
|
-
protected lastKey: Key | null = null;
|
|
141
|
-
protected frozen = false;
|
|
142
|
-
protected itemCount: number = 0;
|
|
143
|
-
|
|
144
|
-
get size(): number {
|
|
145
|
-
return this.itemCount;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
getKeys(): IterableIterator<Key> {
|
|
149
|
-
return this.keyMap.keys();
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
*[Symbol.iterator](): IterableIterator<Node<T>> {
|
|
153
|
-
let node: Node<T> | undefined = this.firstKey != null ? this.keyMap.get(this.firstKey) : undefined;
|
|
154
|
-
while (node) {
|
|
155
|
-
yield node;
|
|
156
|
-
node = node.nextKey != null ? this.keyMap.get(node.nextKey) : undefined;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
getChildren(key: Key): Iterable<Node<T>> {
|
|
161
|
-
let keyMap = this.keyMap;
|
|
162
|
-
return {
|
|
163
|
-
*[Symbol.iterator]() {
|
|
164
|
-
let parent = keyMap.get(key);
|
|
165
|
-
let node = parent?.firstChildKey != null ? keyMap.get(parent.firstChildKey) : null;
|
|
166
|
-
while (node) {
|
|
167
|
-
yield node as Node<T>;
|
|
168
|
-
node = node.nextKey != null ? keyMap.get(node.nextKey) : undefined;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
getKeyBefore(key: Key): Key | null {
|
|
175
|
-
let node = this.keyMap.get(key);
|
|
176
|
-
if (!node) {
|
|
177
|
-
return null;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (node.prevKey != null) {
|
|
181
|
-
node = this.keyMap.get(node.prevKey);
|
|
182
|
-
|
|
183
|
-
while (node && node.type !== 'item' && node.lastChildKey != null) {
|
|
184
|
-
node = this.keyMap.get(node.lastChildKey);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
return node?.key ?? null;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return node.parentKey;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
getKeyAfter(key: Key): Key | null {
|
|
194
|
-
let node = this.keyMap.get(key);
|
|
195
|
-
if (!node) {
|
|
196
|
-
return null;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
if (node.type !== 'item' && node.firstChildKey != null) {
|
|
200
|
-
return node.firstChildKey;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
while (node) {
|
|
204
|
-
if (node.nextKey != null) {
|
|
205
|
-
return node.nextKey;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if (node.parentKey != null) {
|
|
209
|
-
node = this.keyMap.get(node.parentKey);
|
|
210
|
-
} else {
|
|
211
|
-
return null;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
return null;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
getFirstKey(): Key | null {
|
|
219
|
-
return this.firstKey;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
getLastKey(): Key | null {
|
|
223
|
-
let node = this.lastKey != null ? this.keyMap.get(this.lastKey) : null;
|
|
224
|
-
while (node?.lastChildKey != null) {
|
|
225
|
-
node = this.keyMap.get(node.lastChildKey);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
return node?.key ?? null;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
getItem(key: Key): Node<T> | null {
|
|
232
|
-
return this.keyMap.get(key) ?? null;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
at(): Node<T> {
|
|
236
|
-
throw new Error('Not implemented');
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
clone(): this {
|
|
240
|
-
// We need to clone using this.constructor so that subclasses have the right prototype.
|
|
241
|
-
// TypeScript isn't happy about this yet.
|
|
242
|
-
// https://github.com/microsoft/TypeScript/issues/3841
|
|
243
|
-
let Constructor: any = this.constructor;
|
|
244
|
-
let collection: this = new Constructor();
|
|
245
|
-
collection.keyMap = new Map(this.keyMap);
|
|
246
|
-
collection.firstKey = this.firstKey;
|
|
247
|
-
collection.lastKey = this.lastKey;
|
|
248
|
-
collection.itemCount = this.itemCount;
|
|
249
|
-
return collection;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
addNode(node: CollectionNode<T>): void {
|
|
253
|
-
if (this.frozen) {
|
|
254
|
-
throw new Error('Cannot add a node to a frozen collection');
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
if (node.type === 'item' && this.keyMap.get(node.key) == null) {
|
|
258
|
-
this.itemCount++;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
this.keyMap.set(node.key, node);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// Deeply add a node and its children to the collection from another collection, primarily used when filtering a collection
|
|
265
|
-
addDescendants(node: CollectionNode<T>, oldCollection: BaseCollection<T>): void {
|
|
266
|
-
this.addNode(node);
|
|
267
|
-
let children = oldCollection.getChildren(node.key);
|
|
268
|
-
for (let child of children) {
|
|
269
|
-
this.addDescendants(child as CollectionNode<T>, oldCollection);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
removeNode(key: Key): void {
|
|
274
|
-
if (this.frozen) {
|
|
275
|
-
throw new Error('Cannot remove a node to a frozen collection');
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
let node = this.keyMap.get(key);
|
|
279
|
-
if (node != null && node.type === 'item') {
|
|
280
|
-
this.itemCount--;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
this.keyMap.delete(key);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
commit(firstKey: Key | null, lastKey: Key | null, isSSR = false): void {
|
|
287
|
-
if (this.frozen) {
|
|
288
|
-
throw new Error('Cannot commit a frozen collection');
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
this.firstKey = firstKey;
|
|
292
|
-
this.lastKey = lastKey;
|
|
293
|
-
this.frozen = !isSSR;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
filter(filterFn: FilterFn<T>): this {
|
|
297
|
-
let newCollection = new (this.constructor as any)();
|
|
298
|
-
let [firstKey, lastKey] = filterChildren(this, newCollection, this.firstKey, filterFn);
|
|
299
|
-
newCollection?.commit(firstKey, lastKey);
|
|
300
|
-
return newCollection;
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
function filterChildren<T>(collection: BaseCollection<T>, newCollection: BaseCollection<T>, firstChildKey: Key | null, filterFn: FilterFn<T>): [Key | null, Key | null] {
|
|
305
|
-
// loop over the siblings for firstChildKey
|
|
306
|
-
// create new nodes based on calling node.filter for each child
|
|
307
|
-
// if it returns null then don't include it, otherwise update its prev/next keys
|
|
308
|
-
// add them to the newCollection
|
|
309
|
-
if (firstChildKey == null) {
|
|
310
|
-
return [null, null];
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
let firstNode: Node<T> | null = null;
|
|
314
|
-
let lastNode: Node<T> | null = null;
|
|
315
|
-
let currentNode = collection.getItem(firstChildKey);
|
|
316
|
-
|
|
317
|
-
while (currentNode != null) {
|
|
318
|
-
let newNode: Mutable<CollectionNode<T>> | null = (currentNode as CollectionNode<T>).filter(collection, newCollection, filterFn);
|
|
319
|
-
if (newNode != null) {
|
|
320
|
-
newNode.nextKey = null;
|
|
321
|
-
if (lastNode) {
|
|
322
|
-
newNode.prevKey = lastNode.key;
|
|
323
|
-
lastNode.nextKey = newNode.key;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
if (firstNode == null) {
|
|
327
|
-
firstNode = newNode;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
newCollection.addNode(newNode);
|
|
331
|
-
lastNode = newNode;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
currentNode = currentNode.nextKey ? collection.getItem(currentNode.nextKey) : null;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
// TODO: this is pretty specific to dividers but doesn't feel like there is a good way to get around it since we only can know
|
|
338
|
-
// to filter the last separator in a collection only after performing a filter for the rest of the contents after it
|
|
339
|
-
// Its gross that it needs to live here, might be nice if somehow we could have this live in the separator code
|
|
340
|
-
if (lastNode && lastNode.type === 'separator') {
|
|
341
|
-
let prevKey = lastNode.prevKey;
|
|
342
|
-
newCollection.removeNode(lastNode.key);
|
|
343
|
-
|
|
344
|
-
if (prevKey) {
|
|
345
|
-
lastNode = newCollection.getItem(prevKey) as Mutable<CollectionNode<T>>;
|
|
346
|
-
lastNode.nextKey = null;
|
|
347
|
-
} else {
|
|
348
|
-
lastNode = null;
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
return [firstNode?.key ?? null, lastNode?.key ?? null];
|
|
353
|
-
}
|
|
@@ -1,265 +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} from './BaseCollection';
|
|
14
|
-
import {BaseNode, Document, ElementNode} from './Document';
|
|
15
|
-
import {CachedChildrenOptions, useCachedChildren} from './useCachedChildren';
|
|
16
|
-
import {createPortal} from 'react-dom';
|
|
17
|
-
import {FocusableContext} from '@react-aria/interactions';
|
|
18
|
-
import {forwardRefType, Key, Node} from '@react-types/shared';
|
|
19
|
-
import {Hidden} from './Hidden';
|
|
20
|
-
import React, {createContext, ForwardedRef, forwardRef, JSX, ReactElement, ReactNode, useCallback, useContext, useMemo, useRef, useState} from 'react';
|
|
21
|
-
import {useIsSSR} from '@react-aria/ssr';
|
|
22
|
-
import {useSyncExternalStore as useSyncExternalStoreShim} from 'use-sync-external-store/shim/index.js';
|
|
23
|
-
|
|
24
|
-
const ShallowRenderContext = createContext(false);
|
|
25
|
-
const CollectionDocumentContext = createContext<Document<any, BaseCollection<any>> | null>(null);
|
|
26
|
-
|
|
27
|
-
export interface CollectionBuilderProps<C extends BaseCollection<object>> {
|
|
28
|
-
content: ReactNode,
|
|
29
|
-
children: (collection: C) => ReactNode,
|
|
30
|
-
createCollection?: () => C
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Builds a `Collection` from the children provided to the `content` prop, and passes it to the child render prop function.
|
|
35
|
-
*/
|
|
36
|
-
export function CollectionBuilder<C extends BaseCollection<object>>(props: CollectionBuilderProps<C>): ReactElement {
|
|
37
|
-
// If a document was provided above us, we're already in a hidden tree. Just render the content.
|
|
38
|
-
let doc = useContext(CollectionDocumentContext);
|
|
39
|
-
if (doc) {
|
|
40
|
-
// The React types prior to 18 did not allow returning ReactNode from components
|
|
41
|
-
// even though the actual implementation since React 16 did.
|
|
42
|
-
// We must return ReactElement so that TS does not complain that <CollectionBuilder>
|
|
43
|
-
// is not a valid JSX element with React 16 and 17 types.
|
|
44
|
-
// https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20544
|
|
45
|
-
return props.content as ReactElement;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// Otherwise, render a hidden copy of the children so that we can build the collection before constructing the state.
|
|
49
|
-
// This should always come before the real DOM content so we have built the collection by the time it renders during SSR.
|
|
50
|
-
|
|
51
|
-
// This is fine. CollectionDocumentContext never changes after mounting.
|
|
52
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
53
|
-
let {collection, document} = useCollectionDocument(props.createCollection);
|
|
54
|
-
return (
|
|
55
|
-
<>
|
|
56
|
-
<Hidden>
|
|
57
|
-
<CollectionDocumentContext.Provider value={document}>
|
|
58
|
-
{props.content}
|
|
59
|
-
</CollectionDocumentContext.Provider>
|
|
60
|
-
</Hidden>
|
|
61
|
-
<CollectionInner render={props.children} collection={collection} />
|
|
62
|
-
</>
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function CollectionInner({collection, render}) {
|
|
67
|
-
return render(collection);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
interface CollectionDocumentResult<T, C extends BaseCollection<T>> {
|
|
71
|
-
collection: C,
|
|
72
|
-
document: Document<T, C>
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// React 16 and 17 don't support useSyncExternalStore natively, and the shim provided by React does not support getServerSnapshot.
|
|
76
|
-
// This wrapper uses the shim, but additionally calls getServerSnapshot during SSR (according to SSRProvider).
|
|
77
|
-
function useSyncExternalStoreFallback<C>(subscribe: (onStoreChange: () => void) => () => void, getSnapshot: () => C, getServerSnapshot: () => C): C {
|
|
78
|
-
let isSSR = useIsSSR();
|
|
79
|
-
let isSSRRef = useRef(isSSR);
|
|
80
|
-
// This is read immediately inside the wrapper, which also runs during render.
|
|
81
|
-
// We just need a ref to avoid invalidating the callback itself, which
|
|
82
|
-
// would cause React to re-run the callback more than necessary.
|
|
83
|
-
// eslint-disable-next-line rulesdir/pure-render
|
|
84
|
-
isSSRRef.current = isSSR;
|
|
85
|
-
|
|
86
|
-
let getSnapshotWrapper = useCallback(() => {
|
|
87
|
-
return isSSRRef.current ? getServerSnapshot() : getSnapshot();
|
|
88
|
-
}, [getSnapshot, getServerSnapshot]);
|
|
89
|
-
return useSyncExternalStoreShim(subscribe, getSnapshotWrapper);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const useSyncExternalStore = typeof React['useSyncExternalStore'] === 'function'
|
|
93
|
-
? React['useSyncExternalStore']
|
|
94
|
-
: useSyncExternalStoreFallback;
|
|
95
|
-
|
|
96
|
-
function useCollectionDocument<T extends object, C extends BaseCollection<T>>(createCollection?: () => C): CollectionDocumentResult<T, C> {
|
|
97
|
-
// The document instance is mutable, and should never change between renders.
|
|
98
|
-
// useSyncExternalStore is used to subscribe to updates, which vends immutable Collection objects.
|
|
99
|
-
let [document] = useState(() => new Document<T, C>(createCollection?.() || new BaseCollection() as C));
|
|
100
|
-
let subscribe = useCallback((fn: () => void) => document.subscribe(fn), [document]);
|
|
101
|
-
let getSnapshot = useCallback(() => {
|
|
102
|
-
let collection = document.getCollection();
|
|
103
|
-
if (document.isSSR) {
|
|
104
|
-
// After SSR is complete, reset the document to empty so it is ready for React to render the portal into.
|
|
105
|
-
// We do this _after_ getting the collection above so that the collection still has content in it from SSR
|
|
106
|
-
// during the current render, before React has finished the client render.
|
|
107
|
-
document.resetAfterSSR();
|
|
108
|
-
}
|
|
109
|
-
return collection;
|
|
110
|
-
}, [document]);
|
|
111
|
-
let getServerSnapshot = useCallback(() => {
|
|
112
|
-
document.isSSR = true;
|
|
113
|
-
return document.getCollection();
|
|
114
|
-
}, [document]);
|
|
115
|
-
let collection = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
116
|
-
return {collection, document};
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const SSRContext = createContext<BaseNode<any> | null>(null);
|
|
120
|
-
|
|
121
|
-
export type CollectionNodeClass<T> = {
|
|
122
|
-
new (key: Key): CollectionNode<T>,
|
|
123
|
-
readonly type: string
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
function createCollectionNodeClass(type: string): CollectionNodeClass<any> {
|
|
127
|
-
let NodeClass = class extends CollectionNode<any> {
|
|
128
|
-
static readonly type = type;
|
|
129
|
-
};
|
|
130
|
-
return NodeClass;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function useSSRCollectionNode<T extends Element>(CollectionNodeClass: CollectionNodeClass<T> | string, props: object, ref: ForwardedRef<T>, rendered?: any, children?: ReactNode, render?: (node: Node<any>) => ReactElement) {
|
|
134
|
-
// To prevent breaking change, if CollectionNodeClass is a string, create a CollectionNodeClass using the string as the type
|
|
135
|
-
if (typeof CollectionNodeClass === 'string') {
|
|
136
|
-
CollectionNodeClass = createCollectionNodeClass(CollectionNodeClass);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// During SSR, portals are not supported, so the collection children will be wrapped in an SSRContext.
|
|
140
|
-
// Since SSR occurs only once, we assume that the elements are rendered in order and never re-render.
|
|
141
|
-
// Therefore we can create elements in our collection document during render so that they are in the
|
|
142
|
-
// collection by the time we need to use the collection to render to the real DOM.
|
|
143
|
-
// After hydration, we switch to client rendering using the portal.
|
|
144
|
-
let itemRef = useCallback((element: ElementNode<any> | null) => {
|
|
145
|
-
element?.setProps(props, ref, CollectionNodeClass, rendered, render);
|
|
146
|
-
}, [props, ref, rendered, render, CollectionNodeClass]);
|
|
147
|
-
let parentNode = useContext(SSRContext);
|
|
148
|
-
if (parentNode) {
|
|
149
|
-
// Guard against double rendering in strict mode.
|
|
150
|
-
let element = parentNode.ownerDocument.nodesByProps.get(props);
|
|
151
|
-
if (!element) {
|
|
152
|
-
element = parentNode.ownerDocument.createElement(CollectionNodeClass.type);
|
|
153
|
-
element.setProps(props, ref, CollectionNodeClass, rendered, render);
|
|
154
|
-
parentNode.appendChild(element);
|
|
155
|
-
parentNode.ownerDocument.updateCollection();
|
|
156
|
-
parentNode.ownerDocument.nodesByProps.set(props, element);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
return children
|
|
160
|
-
? <SSRContext.Provider value={element}>{children}</SSRContext.Provider>
|
|
161
|
-
: null;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// @ts-ignore
|
|
165
|
-
return <CollectionNodeClass.type ref={itemRef}>{children}</CollectionNodeClass.type>;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
169
|
-
export function createLeafComponent<T extends object, P extends object, E extends Element>(CollectionNodeClass: CollectionNodeClass<any> | string, render: (props: P, ref: ForwardedRef<E>) => ReactElement | null): (props: P & React.RefAttributes<E>) => ReactElement | null;
|
|
170
|
-
export function createLeafComponent<T extends object, P extends object, E extends Element>(CollectionNodeClass: CollectionNodeClass<any> | string, render: (props: P, ref: ForwardedRef<E>, node: Node<T>) => ReactElement | null): (props: P & React.RefAttributes<E>) => ReactElement | null;
|
|
171
|
-
export function createLeafComponent<P extends object, E extends Element>(CollectionNodeClass: CollectionNodeClass<any> | string, render: (props: P, ref: ForwardedRef<E>, node?: any) => ReactElement | null): (props: P & React.RefAttributes<any>) => ReactElement | null {
|
|
172
|
-
let Component = ({node}) => render(node.props, node.props.ref, node);
|
|
173
|
-
let Result = (forwardRef as forwardRefType)((props: P, ref: ForwardedRef<E>) => {
|
|
174
|
-
let focusableProps = useContext(FocusableContext);
|
|
175
|
-
let isShallow = useContext(ShallowRenderContext);
|
|
176
|
-
if (!isShallow) {
|
|
177
|
-
if (render.length >= 3) {
|
|
178
|
-
throw new Error(render.name + ' cannot be rendered outside a collection.');
|
|
179
|
-
}
|
|
180
|
-
return render(props, ref);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
return useSSRCollectionNode(
|
|
184
|
-
CollectionNodeClass,
|
|
185
|
-
props,
|
|
186
|
-
ref,
|
|
187
|
-
'children' in props ? props.children : null,
|
|
188
|
-
null,
|
|
189
|
-
node => (
|
|
190
|
-
// Forward FocusableContext to real DOM tree so tooltips work.
|
|
191
|
-
<FocusableContext.Provider value={focusableProps}>
|
|
192
|
-
<Component node={node} />
|
|
193
|
-
</FocusableContext.Provider>
|
|
194
|
-
)
|
|
195
|
-
);
|
|
196
|
-
});
|
|
197
|
-
// @ts-ignore
|
|
198
|
-
Result.displayName = render.name;
|
|
199
|
-
return Result;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
export function createBranchComponent<T extends object, P extends {children?: any}, E extends Element>(CollectionNodeClass: CollectionNodeClass<any> | string, render: (props: P, ref: ForwardedRef<E>, node: Node<T>) => ReactElement | null, useChildren: (props: P) => ReactNode = useCollectionChildren): (props: P & React.RefAttributes<E>) => ReactElement | null {
|
|
203
|
-
let Component = ({node}) => render(node.props, node.props.ref, node);
|
|
204
|
-
let Result = (forwardRef as forwardRefType)((props: P, ref: ForwardedRef<E>) => {
|
|
205
|
-
let children = useChildren(props);
|
|
206
|
-
return useSSRCollectionNode(CollectionNodeClass, props, ref, null, children, node => <Component node={node} />) ?? <></>;
|
|
207
|
-
});
|
|
208
|
-
// @ts-ignore
|
|
209
|
-
Result.displayName = render.name;
|
|
210
|
-
return Result;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
function useCollectionChildren<T extends object>(options: CachedChildrenOptions<T>) {
|
|
214
|
-
return useCachedChildren({...options, addIdAndValue: true});
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
export interface CollectionProps<T> extends CachedChildrenOptions<T> {}
|
|
218
|
-
|
|
219
|
-
const CollectionContext = createContext<CachedChildrenOptions<unknown> | null>(null);
|
|
220
|
-
|
|
221
|
-
/** A Collection renders a list of items, automatically managing caching and keys. */
|
|
222
|
-
export function Collection<T extends object>(props: CollectionProps<T>): JSX.Element {
|
|
223
|
-
let ctx = useContext(CollectionContext)!;
|
|
224
|
-
let dependencies = (ctx?.dependencies || []).concat(props.dependencies);
|
|
225
|
-
let idScope = props.idScope ?? ctx?.idScope;
|
|
226
|
-
let children = useCollectionChildren({
|
|
227
|
-
...props,
|
|
228
|
-
idScope,
|
|
229
|
-
dependencies
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
let doc = useContext(CollectionDocumentContext);
|
|
233
|
-
if (doc) {
|
|
234
|
-
children = <CollectionRoot>{children}</CollectionRoot>;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// Propagate dependencies and idScope to child collections.
|
|
238
|
-
ctx = useMemo(() => ({
|
|
239
|
-
dependencies,
|
|
240
|
-
idScope
|
|
241
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
242
|
-
}), [idScope, ...dependencies]);
|
|
243
|
-
|
|
244
|
-
return (
|
|
245
|
-
<CollectionContext.Provider value={ctx}>
|
|
246
|
-
{children}
|
|
247
|
-
</CollectionContext.Provider>
|
|
248
|
-
);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
function CollectionRoot({children}) {
|
|
252
|
-
let doc = useContext(CollectionDocumentContext);
|
|
253
|
-
let wrappedChildren = useMemo(() => (
|
|
254
|
-
<CollectionDocumentContext.Provider value={null}>
|
|
255
|
-
<ShallowRenderContext.Provider value>
|
|
256
|
-
{children}
|
|
257
|
-
</ShallowRenderContext.Provider>
|
|
258
|
-
</CollectionDocumentContext.Provider>
|
|
259
|
-
), [children]);
|
|
260
|
-
// During SSR, we render the content directly, and append nodes to the document during render.
|
|
261
|
-
// The collection children return null so that nothing is actually rendered into the HTML.
|
|
262
|
-
return useIsSSR()
|
|
263
|
-
? <SSRContext.Provider value={doc}>{wrappedChildren}</SSRContext.Provider>
|
|
264
|
-
: createPortal(wrappedChildren, doc as unknown as Element);
|
|
265
|
-
}
|