@react-aria/collections 3.0.0-alpha.1 → 3.0.0-nightly.2986
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/README.md +1 -1
- package/dist/BaseCollection.main.js +157 -0
- package/dist/BaseCollection.main.js.map +1 -0
- package/dist/BaseCollection.mjs +151 -0
- package/dist/BaseCollection.module.js +151 -0
- package/dist/BaseCollection.module.js.map +1 -0
- package/dist/CollectionBuilder.main.js +233 -0
- package/dist/CollectionBuilder.main.js.map +1 -0
- package/dist/CollectionBuilder.mjs +221 -0
- package/dist/CollectionBuilder.module.js +221 -0
- package/dist/CollectionBuilder.module.js.map +1 -0
- package/dist/Document.main.js +316 -0
- package/dist/Document.main.js.map +1 -0
- package/dist/Document.mjs +311 -0
- package/dist/Document.module.js +311 -0
- package/dist/Document.module.js.map +1 -0
- package/dist/Hidden.main.js +79 -0
- package/dist/Hidden.main.js.map +1 -0
- package/dist/Hidden.mjs +68 -0
- package/dist/Hidden.module.js +68 -0
- package/dist/Hidden.module.js.map +1 -0
- package/dist/import.mjs +23 -0
- package/dist/main.js +27 -348
- package/dist/main.js.map +1 -0
- package/dist/module.js +17 -318
- package/dist/module.js.map +1 -0
- package/dist/types.d.ts +81 -24
- package/dist/types.d.ts.map +1 -1
- package/dist/useCachedChildren.main.js +63 -0
- package/dist/useCachedChildren.main.js.map +1 -0
- package/dist/useCachedChildren.mjs +58 -0
- package/dist/useCachedChildren.module.js +58 -0
- package/dist/useCachedChildren.module.js.map +1 -0
- package/package.json +17 -10
- package/src/BaseCollection.ts +211 -0
- package/src/CollectionBuilder.tsx +237 -0
- package/src/Document.ts +453 -0
- package/src/Hidden.tsx +84 -0
- package/src/index.ts +19 -0
- package/src/useCachedChildren.ts +70 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {useMemo as $luMFQ$useMemo, cloneElement as $luMFQ$cloneElement} from "react";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright 2024 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
function $e948873055cbafe4$export$727c8fc270210f13(props) {
|
|
15
|
+
let { children: children, items: items, idScope: idScope, addIdAndValue: addIdAndValue, dependencies: dependencies = [] } = props;
|
|
16
|
+
// Invalidate the cache whenever the parent value changes.
|
|
17
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
18
|
+
let cache = (0, $luMFQ$useMemo)(()=>new WeakMap(), dependencies);
|
|
19
|
+
return (0, $luMFQ$useMemo)(()=>{
|
|
20
|
+
if (items && typeof children === 'function') {
|
|
21
|
+
let res = [];
|
|
22
|
+
for (let item of items){
|
|
23
|
+
let rendered = cache.get(item);
|
|
24
|
+
if (!rendered) {
|
|
25
|
+
rendered = children(item);
|
|
26
|
+
var _rendered_props_id, _ref;
|
|
27
|
+
// @ts-ignore
|
|
28
|
+
let key = (_ref = (_rendered_props_id = rendered.props.id) !== null && _rendered_props_id !== void 0 ? _rendered_props_id : item.key) !== null && _ref !== void 0 ? _ref : item.id;
|
|
29
|
+
// eslint-disable-next-line max-depth
|
|
30
|
+
if (key == null) throw new Error('Could not determine key for item');
|
|
31
|
+
// eslint-disable-next-line max-depth
|
|
32
|
+
if (idScope) key = idScope + ':' + key;
|
|
33
|
+
// Note: only works if wrapped Item passes through id...
|
|
34
|
+
rendered = (0, $luMFQ$cloneElement)(rendered, addIdAndValue ? {
|
|
35
|
+
key: key,
|
|
36
|
+
id: key,
|
|
37
|
+
value: item
|
|
38
|
+
} : {
|
|
39
|
+
key: key
|
|
40
|
+
});
|
|
41
|
+
cache.set(item, rendered);
|
|
42
|
+
}
|
|
43
|
+
res.push(rendered);
|
|
44
|
+
}
|
|
45
|
+
return res;
|
|
46
|
+
} else if (typeof children !== 'function') return children;
|
|
47
|
+
}, [
|
|
48
|
+
children,
|
|
49
|
+
items,
|
|
50
|
+
cache,
|
|
51
|
+
idScope,
|
|
52
|
+
addIdAndValue
|
|
53
|
+
]);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
export {$e948873055cbafe4$export$727c8fc270210f13 as useCachedChildren};
|
|
58
|
+
//# sourceMappingURL=useCachedChildren.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;AAAA;;;;;;;;;;CAUC;AAsBM,SAAS,0CAAoC,KAA+B;IACjF,IAAI,YAAC,QAAQ,SAAE,KAAK,WAAE,OAAO,iBAAE,aAAa,gBAAE,eAAe,EAAE,EAAC,GAAG;IAEnE,0DAA0D;IAC1D,uDAAuD;IACvD,IAAI,QAAQ,CAAA,GAAA,cAAM,EAAE,IAAM,IAAI,WAAW;IACzC,OAAO,CAAA,GAAA,cAAM,EAAE;QACb,IAAI,SAAS,OAAO,aAAa,YAAY;YAC3C,IAAI,MAAsB,EAAE;YAC5B,KAAK,IAAI,QAAQ,MAAO;gBACtB,IAAI,WAAW,MAAM,GAAG,CAAC;gBACzB,IAAI,CAAC,UAAU;oBACb,WAAW,SAAS;wBAEV,oBAAA;oBADV,aAAa;oBACb,IAAI,MAAM,CAAA,OAAA,CAAA,qBAAA,SAAS,KAAK,CAAC,EAAE,cAAjB,gCAAA,qBAAqB,KAAK,GAAG,cAA7B,kBAAA,OAAiC,KAAK,EAAE;oBAClD,qCAAqC;oBACrC,IAAI,OAAO,MACT,MAAM,IAAI,MAAM;oBAElB,qCAAqC;oBACrC,IAAI,SACF,MAAM,UAAU,MAAM;oBAExB,wDAAwD;oBACxD,WAAW,CAAA,GAAA,mBAAW,EACpB,UACA,gBAAgB;6BAAC;wBAAK,IAAI;wBAAK,OAAO;oBAAI,IAAI;6BAAC;oBAAG;oBAEpD,MAAM,GAAG,CAAC,MAAM;gBAClB;gBACA,IAAI,IAAI,CAAC;YACX;YACA,OAAO;QACT,OAAO,IAAI,OAAO,aAAa,YAC7B,OAAO;IAEX,GAAG;QAAC;QAAU;QAAO;QAAO;QAAS;KAAc;AACrD","sources":["packages/@react-aria/collections/src/useCachedChildren.ts"],"sourcesContent":["/*\n * Copyright 2024 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {cloneElement, ReactElement, ReactNode, useMemo} from 'react';\nimport {Key} from '@react-types/shared';\n\nexport interface CachedChildrenOptions<T> {\n /** Item objects in the collection. */\n items?: Iterable<T>,\n /** The contents of the collection. */\n children?: ReactNode | ((item: T) => ReactNode),\n /** Values that should invalidate the item cache when using dynamic collections. */\n dependencies?: any[],\n /** A scope to prepend to all child item ids to ensure they are unique. */\n idScope?: Key,\n /** Whether to add `id` and `value` props to all child items. */\n addIdAndValue?: boolean\n}\n\n/**\n * Maps over a list of items and renders React elements for them. Each rendered item is\n * cached based on object identity, and React keys are generated from the `key` or `id` property.\n */\nexport function useCachedChildren<T extends object>(props: CachedChildrenOptions<T>): ReactNode {\n let {children, items, idScope, addIdAndValue, dependencies = []} = props;\n\n // Invalidate the cache whenever the parent value changes.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n let cache = useMemo(() => new WeakMap(), dependencies);\n return useMemo(() => {\n if (items && typeof children === 'function') {\n let res: ReactElement[] = [];\n for (let item of items) {\n let rendered = cache.get(item);\n if (!rendered) {\n rendered = children(item);\n // @ts-ignore\n let key = rendered.props.id ?? item.key ?? item.id;\n // eslint-disable-next-line max-depth\n if (key == null) {\n throw new Error('Could not determine key for item');\n }\n // eslint-disable-next-line max-depth\n if (idScope) {\n key = idScope + ':' + key;\n }\n // Note: only works if wrapped Item passes through id...\n rendered = cloneElement(\n rendered,\n addIdAndValue ? {key, id: key, value: item} : {key}\n );\n cache.set(item, rendered);\n }\n res.push(rendered);\n }\n return res;\n } else if (typeof children !== 'function') {\n return children;\n }\n }, [children, items, cache, idScope, addIdAndValue]);\n}\n"],"names":[],"version":3,"file":"useCachedChildren.module.js.map"}
|
package/package.json
CHANGED
|
@@ -1,32 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-aria/collections",
|
|
3
|
-
"version": "3.0.0-
|
|
3
|
+
"version": "3.0.0-nightly.2986+b940126e4",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
7
7
|
"module": "dist/module.js",
|
|
8
8
|
"types": "dist/types.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
"types": "./dist/types.d.ts",
|
|
11
|
+
"import": "./dist/import.mjs",
|
|
12
|
+
"require": "./dist/main.js"
|
|
13
|
+
},
|
|
9
14
|
"source": "src/index.ts",
|
|
10
15
|
"files": [
|
|
11
|
-
"dist"
|
|
16
|
+
"dist",
|
|
17
|
+
"src"
|
|
12
18
|
],
|
|
13
19
|
"sideEffects": false,
|
|
14
20
|
"repository": {
|
|
15
21
|
"type": "git",
|
|
16
|
-
"url": "https://github.com/adobe
|
|
22
|
+
"url": "https://github.com/adobe/react-spectrum"
|
|
17
23
|
},
|
|
18
24
|
"dependencies": {
|
|
19
|
-
"@
|
|
20
|
-
"@react-aria/utils": "
|
|
21
|
-
"@react-
|
|
22
|
-
"@
|
|
25
|
+
"@react-aria/ssr": "3.9.5-nightly.4698+b940126e4",
|
|
26
|
+
"@react-aria/utils": "3.0.0-nightly.2986+b940126e4",
|
|
27
|
+
"@react-types/shared": "3.0.0-nightly.2986+b940126e4",
|
|
28
|
+
"@swc/helpers": "^0.5.0",
|
|
29
|
+
"use-sync-external-store": "^1.2.0"
|
|
23
30
|
},
|
|
24
31
|
"peerDependencies": {
|
|
25
|
-
"react": "^16.8.0",
|
|
26
|
-
"react-dom": "^16.8.0"
|
|
32
|
+
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0",
|
|
33
|
+
"react-dom": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0"
|
|
27
34
|
},
|
|
28
35
|
"publishConfig": {
|
|
29
36
|
"access": "public"
|
|
30
37
|
},
|
|
31
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "b940126e4d67a11d28b7c0b098eab23205598b6c"
|
|
32
39
|
}
|
|
@@ -0,0 +1,211 @@
|
|
|
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
|
+
/** An immutable object representing a Node in a Collection. */
|
|
21
|
+
export class NodeValue<T> implements Node<T> {
|
|
22
|
+
readonly type: string;
|
|
23
|
+
readonly key: Key;
|
|
24
|
+
readonly value: T | null = null;
|
|
25
|
+
readonly level: number = 0;
|
|
26
|
+
readonly hasChildNodes: boolean = false;
|
|
27
|
+
readonly rendered: ReactNode = null;
|
|
28
|
+
readonly textValue: string = '';
|
|
29
|
+
readonly 'aria-label'?: string = undefined;
|
|
30
|
+
readonly index: number = 0;
|
|
31
|
+
readonly parentKey: Key | null = null;
|
|
32
|
+
readonly prevKey: Key | null = null;
|
|
33
|
+
readonly nextKey: Key | null = null;
|
|
34
|
+
readonly firstChildKey: Key | null = null;
|
|
35
|
+
readonly lastChildKey: Key | null = null;
|
|
36
|
+
readonly props: any = {};
|
|
37
|
+
readonly render?: (node: Node<any>) => ReactElement;
|
|
38
|
+
|
|
39
|
+
constructor(type: string, key: Key) {
|
|
40
|
+
this.type = type;
|
|
41
|
+
this.key = key;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get childNodes(): Iterable<Node<T>> {
|
|
45
|
+
throw new Error('childNodes is not supported');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
clone(): NodeValue<T> {
|
|
49
|
+
let node: Mutable<NodeValue<T>> = new NodeValue(this.type, this.key);
|
|
50
|
+
node.value = this.value;
|
|
51
|
+
node.level = this.level;
|
|
52
|
+
node.hasChildNodes = this.hasChildNodes;
|
|
53
|
+
node.rendered = this.rendered;
|
|
54
|
+
node.textValue = this.textValue;
|
|
55
|
+
node['aria-label'] = this['aria-label'];
|
|
56
|
+
node.index = this.index;
|
|
57
|
+
node.parentKey = this.parentKey;
|
|
58
|
+
node.prevKey = this.prevKey;
|
|
59
|
+
node.nextKey = this.nextKey;
|
|
60
|
+
node.firstChildKey = this.firstChildKey;
|
|
61
|
+
node.lastChildKey = this.lastChildKey;
|
|
62
|
+
node.props = this.props;
|
|
63
|
+
node.render = this.render;
|
|
64
|
+
return node;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* An immutable Collection implementation. Updates are only allowed
|
|
70
|
+
* when it is not marked as frozen. This can be subclassed to implement
|
|
71
|
+
* custom collection behaviors.
|
|
72
|
+
*/
|
|
73
|
+
export class BaseCollection<T> implements ICollection<Node<T>> {
|
|
74
|
+
private keyMap: Map<Key, NodeValue<T>> = new Map();
|
|
75
|
+
private firstKey: Key | null = null;
|
|
76
|
+
private lastKey: Key | null = null;
|
|
77
|
+
private frozen = false;
|
|
78
|
+
|
|
79
|
+
get size() {
|
|
80
|
+
return this.keyMap.size;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
getKeys() {
|
|
84
|
+
return this.keyMap.keys();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
*[Symbol.iterator]() {
|
|
88
|
+
let node: Node<T> | undefined = this.firstKey != null ? this.keyMap.get(this.firstKey) : undefined;
|
|
89
|
+
while (node) {
|
|
90
|
+
yield node;
|
|
91
|
+
node = node.nextKey != null ? this.keyMap.get(node.nextKey) : undefined;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
getChildren(key: Key): Iterable<Node<T>> {
|
|
96
|
+
let keyMap = this.keyMap;
|
|
97
|
+
return {
|
|
98
|
+
*[Symbol.iterator]() {
|
|
99
|
+
let parent = keyMap.get(key);
|
|
100
|
+
let node = parent?.firstChildKey != null ? keyMap.get(parent.firstChildKey) : null;
|
|
101
|
+
while (node) {
|
|
102
|
+
yield node as Node<T>;
|
|
103
|
+
node = node.nextKey != null ? keyMap.get(node.nextKey) : undefined;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
getKeyBefore(key: Key) {
|
|
110
|
+
let node = this.keyMap.get(key);
|
|
111
|
+
if (!node) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (node.prevKey != null) {
|
|
116
|
+
node = this.keyMap.get(node.prevKey);
|
|
117
|
+
|
|
118
|
+
while (node && node.type !== 'item' && node.lastChildKey != null) {
|
|
119
|
+
node = this.keyMap.get(node.lastChildKey);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return node?.key ?? null;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return node.parentKey;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
getKeyAfter(key: Key) {
|
|
129
|
+
let node = this.keyMap.get(key);
|
|
130
|
+
if (!node) {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (node.type !== 'item' && node.firstChildKey != null) {
|
|
135
|
+
return node.firstChildKey;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
while (node) {
|
|
139
|
+
if (node.nextKey != null) {
|
|
140
|
+
return node.nextKey;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (node.parentKey != null) {
|
|
144
|
+
node = this.keyMap.get(node.parentKey);
|
|
145
|
+
} else {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
getFirstKey() {
|
|
154
|
+
return this.firstKey;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
getLastKey() {
|
|
158
|
+
let node = this.lastKey != null ? this.keyMap.get(this.lastKey) : null;
|
|
159
|
+
while (node?.lastChildKey != null) {
|
|
160
|
+
node = this.keyMap.get(node.lastChildKey);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return node?.key ?? null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
getItem(key: Key): Node<T> | null {
|
|
167
|
+
return this.keyMap.get(key) ?? null;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
at(): Node<T> {
|
|
171
|
+
throw new Error('Not implemented');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
clone(): this {
|
|
175
|
+
// We need to clone using this.constructor so that subclasses have the right prototype.
|
|
176
|
+
// TypeScript isn't happy about this yet.
|
|
177
|
+
// https://github.com/microsoft/TypeScript/issues/3841
|
|
178
|
+
let Constructor: any = this.constructor;
|
|
179
|
+
let collection: this = new Constructor();
|
|
180
|
+
collection.keyMap = new Map(this.keyMap);
|
|
181
|
+
collection.firstKey = this.firstKey;
|
|
182
|
+
collection.lastKey = this.lastKey;
|
|
183
|
+
return collection;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
addNode(node: NodeValue<T>) {
|
|
187
|
+
if (this.frozen) {
|
|
188
|
+
throw new Error('Cannot add a node to a frozen collection');
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
this.keyMap.set(node.key, node);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
removeNode(key: Key) {
|
|
195
|
+
if (this.frozen) {
|
|
196
|
+
throw new Error('Cannot remove a node to a frozen collection');
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
this.keyMap.delete(key);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
commit(firstKey: Key | null, lastKey: Key | null, isSSR = false) {
|
|
203
|
+
if (this.frozen) {
|
|
204
|
+
throw new Error('Cannot commit a frozen collection');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
this.firstKey = firstKey;
|
|
208
|
+
this.lastKey = lastKey;
|
|
209
|
+
this.frozen = !isSSR;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
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} from './BaseCollection';
|
|
14
|
+
import {BaseNode, Document, ElementNode} from './Document';
|
|
15
|
+
import {CachedChildrenOptions, useCachedChildren} from './useCachedChildren';
|
|
16
|
+
import {createPortal} from 'react-dom';
|
|
17
|
+
import {forwardRefType, Node} from '@react-types/shared';
|
|
18
|
+
import {Hidden} from './Hidden';
|
|
19
|
+
import React, {createContext, ForwardedRef, forwardRef, JSX, ReactElement, ReactNode, useCallback, useContext, useMemo, useRef, useState} from 'react';
|
|
20
|
+
import {useIsSSR} from '@react-aria/ssr';
|
|
21
|
+
import {useLayoutEffect} from '@react-aria/utils';
|
|
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>) {
|
|
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
|
+
return props.content;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Otherwise, render a hidden copy of the children so that we can build the collection before constructing the state.
|
|
44
|
+
// This should always come before the real DOM content so we have built the collection by the time it renders during SSR.
|
|
45
|
+
|
|
46
|
+
// This is fine. CollectionDocumentContext never changes after mounting.
|
|
47
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
48
|
+
let {collection, document} = useCollectionDocument(props.createCollection);
|
|
49
|
+
return (
|
|
50
|
+
<>
|
|
51
|
+
<Hidden>
|
|
52
|
+
<CollectionDocumentContext.Provider value={document}>
|
|
53
|
+
{props.content}
|
|
54
|
+
</CollectionDocumentContext.Provider>
|
|
55
|
+
</Hidden>
|
|
56
|
+
<CollectionInner render={props.children} collection={collection} />
|
|
57
|
+
</>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function CollectionInner({collection, render}) {
|
|
62
|
+
return render(collection);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface CollectionDocumentResult<T, C extends BaseCollection<T>> {
|
|
66
|
+
collection: C,
|
|
67
|
+
document: Document<T, C>
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// React 16 and 17 don't support useSyncExternalStore natively, and the shim provided by React does not support getServerSnapshot.
|
|
71
|
+
// This wrapper uses the shim, but additionally calls getServerSnapshot during SSR (according to SSRProvider).
|
|
72
|
+
function useSyncExternalStoreFallback<C>(subscribe: (onStoreChange: () => void) => () => void, getSnapshot: () => C, getServerSnapshot: () => C): C {
|
|
73
|
+
let isSSR = useIsSSR();
|
|
74
|
+
let isSSRRef = useRef(isSSR);
|
|
75
|
+
// This is read immediately inside the wrapper, which also runs during render.
|
|
76
|
+
// We just need a ref to avoid invalidating the callback itself, which
|
|
77
|
+
// would cause React to re-run the callback more than necessary.
|
|
78
|
+
// eslint-disable-next-line rulesdir/pure-render
|
|
79
|
+
isSSRRef.current = isSSR;
|
|
80
|
+
|
|
81
|
+
let getSnapshotWrapper = useCallback(() => {
|
|
82
|
+
return isSSRRef.current ? getServerSnapshot() : getSnapshot();
|
|
83
|
+
}, [getSnapshot, getServerSnapshot]);
|
|
84
|
+
return useSyncExternalStoreShim(subscribe, getSnapshotWrapper);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const useSyncExternalStore = typeof React['useSyncExternalStore'] === 'function'
|
|
88
|
+
? React['useSyncExternalStore']
|
|
89
|
+
: useSyncExternalStoreFallback;
|
|
90
|
+
|
|
91
|
+
function useCollectionDocument<T extends object, C extends BaseCollection<T>>(createCollection?: () => C): CollectionDocumentResult<T, C> {
|
|
92
|
+
// The document instance is mutable, and should never change between renders.
|
|
93
|
+
// useSyncExternalStore is used to subscribe to updates, which vends immutable Collection objects.
|
|
94
|
+
let [document] = useState(() => new Document<T, C>(createCollection?.() || new BaseCollection() as C));
|
|
95
|
+
let subscribe = useCallback((fn: () => void) => document.subscribe(fn), [document]);
|
|
96
|
+
let getSnapshot = useCallback(() => {
|
|
97
|
+
let collection = document.getCollection();
|
|
98
|
+
if (document.isSSR) {
|
|
99
|
+
// After SSR is complete, reset the document to empty so it is ready for React to render the portal into.
|
|
100
|
+
// We do this _after_ getting the collection above so that the collection still has content in it from SSR
|
|
101
|
+
// during the current render, before React has finished the client render.
|
|
102
|
+
document.resetAfterSSR();
|
|
103
|
+
}
|
|
104
|
+
return collection;
|
|
105
|
+
}, [document]);
|
|
106
|
+
let getServerSnapshot = useCallback(() => {
|
|
107
|
+
document.isSSR = true;
|
|
108
|
+
return document.getCollection();
|
|
109
|
+
}, [document]);
|
|
110
|
+
let collection = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
111
|
+
useLayoutEffect(() => {
|
|
112
|
+
document.isMounted = true;
|
|
113
|
+
return () => {
|
|
114
|
+
// Mark unmounted so we can skip all of the collection updates caused by
|
|
115
|
+
// React calling removeChild on every item in the collection.
|
|
116
|
+
document.isMounted = false;
|
|
117
|
+
};
|
|
118
|
+
}, [document]);
|
|
119
|
+
return {collection, document};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const SSRContext = createContext<BaseNode<any> | null>(null);
|
|
123
|
+
|
|
124
|
+
function useSSRCollectionNode<T extends Element>(Type: string, props: object, ref: ForwardedRef<T>, rendered?: any, children?: ReactNode, render?: (node: Node<T>) => ReactElement) {
|
|
125
|
+
// During SSR, portals are not supported, so the collection children will be wrapped in an SSRContext.
|
|
126
|
+
// Since SSR occurs only once, we assume that the elements are rendered in order and never re-render.
|
|
127
|
+
// Therefore we can create elements in our collection document during render so that they are in the
|
|
128
|
+
// collection by the time we need to use the collection to render to the real DOM.
|
|
129
|
+
// After hydration, we switch to client rendering using the portal.
|
|
130
|
+
let itemRef = useCallback((element: ElementNode<any> | null) => {
|
|
131
|
+
element?.setProps(props, ref, rendered, render);
|
|
132
|
+
}, [props, ref, rendered, render]);
|
|
133
|
+
let parentNode = useContext(SSRContext);
|
|
134
|
+
if (parentNode) {
|
|
135
|
+
// Guard against double rendering in strict mode.
|
|
136
|
+
let element = parentNode.ownerDocument.nodesByProps.get(props);
|
|
137
|
+
if (!element) {
|
|
138
|
+
element = parentNode.ownerDocument.createElement(Type);
|
|
139
|
+
element.setProps(props, ref, rendered, render);
|
|
140
|
+
parentNode.appendChild(element);
|
|
141
|
+
parentNode.ownerDocument.updateCollection();
|
|
142
|
+
parentNode.ownerDocument.nodesByProps.set(props, element);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return children
|
|
146
|
+
? <SSRContext.Provider value={element}>{children}</SSRContext.Provider>
|
|
147
|
+
: null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// @ts-ignore
|
|
151
|
+
return <Type ref={itemRef}>{children}</Type>;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function createLeafComponent<T extends object, P extends object, E extends Element>(type: string, render: (props: P, ref: ForwardedRef<E>) => ReactNode): (props: P & React.RefAttributes<T>) => ReactNode | null;
|
|
155
|
+
export function createLeafComponent<T extends object, P extends object, E extends Element>(type: string, render: (props: P, ref: ForwardedRef<E>, node: Node<T>) => ReactNode): (props: P & React.RefAttributes<T>) => ReactNode | null;
|
|
156
|
+
export function createLeafComponent<P extends object, E extends Element>(type: string, render: (props: P, ref: ForwardedRef<E>, node?: any) => ReactNode) {
|
|
157
|
+
let Component = ({node}) => render(node.props, node.props.ref, node);
|
|
158
|
+
let Result = (forwardRef as forwardRefType)((props: P, ref: ForwardedRef<E>) => {
|
|
159
|
+
let isShallow = useContext(ShallowRenderContext);
|
|
160
|
+
if (!isShallow) {
|
|
161
|
+
if (render.length >= 3) {
|
|
162
|
+
throw new Error(render.name + ' cannot be rendered outside a collection.');
|
|
163
|
+
}
|
|
164
|
+
return render(props, ref);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return useSSRCollectionNode(type, props, ref, 'children' in props ? props.children : null, null, node => <Component node={node} />);
|
|
168
|
+
});
|
|
169
|
+
// @ts-ignore
|
|
170
|
+
Result.displayName = render.name;
|
|
171
|
+
return Result;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function createBranchComponent<T extends object, P extends {children?: any}, E extends Element>(type: string, render: (props: P, ref: ForwardedRef<E>, node: Node<T>) => ReactNode, useChildren: (props: P) => ReactNode = useCollectionChildren) {
|
|
175
|
+
let Component = ({node}) => render(node.props, node.props.ref, node);
|
|
176
|
+
let Result = (forwardRef as forwardRefType)((props: P, ref: ForwardedRef<E>) => {
|
|
177
|
+
let children = useChildren(props);
|
|
178
|
+
return useSSRCollectionNode(type, props, ref, null, children, node => <Component node={node} />) ?? <></>;
|
|
179
|
+
});
|
|
180
|
+
// @ts-ignore
|
|
181
|
+
Result.displayName = render.name;
|
|
182
|
+
return Result;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function useCollectionChildren<T extends object>(options: CachedChildrenOptions<T>) {
|
|
186
|
+
return useCachedChildren({...options, addIdAndValue: true});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export interface CollectionProps<T> extends CachedChildrenOptions<T> {}
|
|
190
|
+
|
|
191
|
+
const CollectionContext = createContext<CachedChildrenOptions<unknown> | null>(null);
|
|
192
|
+
|
|
193
|
+
/** A Collection renders a list of items, automatically managing caching and keys. */
|
|
194
|
+
export function Collection<T extends object>(props: CollectionProps<T>): JSX.Element {
|
|
195
|
+
let ctx = useContext(CollectionContext)!;
|
|
196
|
+
let dependencies = (ctx?.dependencies || []).concat(props.dependencies);
|
|
197
|
+
let idScope = props.idScope || ctx?.idScope;
|
|
198
|
+
let children = useCollectionChildren({
|
|
199
|
+
...props,
|
|
200
|
+
idScope,
|
|
201
|
+
dependencies
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
let doc = useContext(CollectionDocumentContext);
|
|
205
|
+
if (doc) {
|
|
206
|
+
children = <CollectionRoot>{children}</CollectionRoot>;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Propagate dependencies and idScope to child collections.
|
|
210
|
+
ctx = useMemo(() => ({
|
|
211
|
+
dependencies,
|
|
212
|
+
idScope
|
|
213
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
214
|
+
}), [idScope, ...dependencies]);
|
|
215
|
+
|
|
216
|
+
return (
|
|
217
|
+
<CollectionContext.Provider value={ctx}>
|
|
218
|
+
{children}
|
|
219
|
+
</CollectionContext.Provider>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function CollectionRoot({children}) {
|
|
224
|
+
let doc = useContext(CollectionDocumentContext);
|
|
225
|
+
let wrappedChildren = useMemo(() => (
|
|
226
|
+
<CollectionDocumentContext.Provider value={null}>
|
|
227
|
+
<ShallowRenderContext.Provider value>
|
|
228
|
+
{children}
|
|
229
|
+
</ShallowRenderContext.Provider>
|
|
230
|
+
</CollectionDocumentContext.Provider>
|
|
231
|
+
), [children]);
|
|
232
|
+
// During SSR, we render the content directly, and append nodes to the document during render.
|
|
233
|
+
// The collection children return null so that nothing is actually rendered into the HTML.
|
|
234
|
+
return useIsSSR()
|
|
235
|
+
? <SSRContext.Provider value={doc}>{wrappedChildren}</SSRContext.Provider>
|
|
236
|
+
: createPortal(wrappedChildren, doc as unknown as Element);
|
|
237
|
+
}
|