@react-aria/collections 3.0.0-alpha.1 → 3.0.0-nightly.2988
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
package/dist/module.js
CHANGED
|
@@ -1,324 +1,23 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import { Size, Rect, useCollectionState } from "@react-stately/collections";
|
|
6
|
-
import _babelRuntimeHelpersEsmObjectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
7
|
-
import { flushSync } from "react-dom";
|
|
8
|
-
export function useCollectionItem(options) {
|
|
9
|
-
let {
|
|
10
|
-
reusableView: {
|
|
11
|
-
layoutInfo,
|
|
12
|
-
collectionManager
|
|
13
|
-
},
|
|
14
|
-
ref
|
|
15
|
-
} = options;
|
|
16
|
-
let updateSize = useCallback(() => {
|
|
17
|
-
let size = $e5e366cd21c786f9652cd90121bc5e$var$getSize(ref.current);
|
|
18
|
-
collectionManager.updateItemSize(layoutInfo.key, size);
|
|
19
|
-
}, [collectionManager, layoutInfo.key, ref]);
|
|
20
|
-
useLayoutEffect(() => {
|
|
21
|
-
if (layoutInfo.estimatedSize) {
|
|
22
|
-
updateSize();
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
return {
|
|
26
|
-
updateSize
|
|
27
|
-
};
|
|
28
|
-
}
|
|
1
|
+
import {Collection as $e1995378a142960e$export$fb8073518f34e6ec, CollectionBuilder as $e1995378a142960e$export$bf788dd355e3a401, createBranchComponent as $e1995378a142960e$export$e953bb1cd0f19726, createLeafComponent as $e1995378a142960e$export$18af5c7a9e9b3664} from "./CollectionBuilder.module.js";
|
|
2
|
+
import {createHideableComponent as $f39a9eba43920ace$export$86427a43e3e48ebb, useIsHidden as $f39a9eba43920ace$export$b5d7cc18bb8d2b59} from "./Hidden.module.js";
|
|
3
|
+
import {useCachedChildren as $e948873055cbafe4$export$727c8fc270210f13} from "./useCachedChildren.module.js";
|
|
4
|
+
import {BaseCollection as $23b9f4fcf0fe224b$export$408d25a4e12db025, NodeValue as $23b9f4fcf0fe224b$export$f5d856d854e74713} from "./BaseCollection.module.js";
|
|
29
5
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
6
|
+
/*
|
|
7
|
+
* Copyright 2024 Adobe. All rights reserved.
|
|
8
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
9
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
10
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
13
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
14
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
15
|
+
* governing permissions and limitations under the License.
|
|
16
|
+
*/
|
|
33
17
|
|
|
34
|
-
export function CollectionItem(props) {
|
|
35
|
-
let {
|
|
36
|
-
reusableView,
|
|
37
|
-
parent
|
|
38
|
-
} = props;
|
|
39
|
-
let ref = useRef();
|
|
40
|
-
useCollectionItem({
|
|
41
|
-
reusableView,
|
|
42
|
-
ref
|
|
43
|
-
});
|
|
44
|
-
return (/*#__PURE__*/_react.createElement("div", {
|
|
45
|
-
role: "presentation",
|
|
46
|
-
ref: ref,
|
|
47
|
-
style: layoutInfoToStyle(reusableView.layoutInfo, parent && parent.layoutInfo)
|
|
48
|
-
}, reusableView.rendered)
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
export function layoutInfoToStyle(layoutInfo, parent) {
|
|
52
|
-
return {
|
|
53
|
-
position: 'absolute',
|
|
54
|
-
overflow: 'hidden',
|
|
55
|
-
top: layoutInfo.rect.y - (parent ? parent.rect.y : 0),
|
|
56
|
-
left: layoutInfo.rect.x - (parent ? parent.rect.x : 0),
|
|
57
|
-
transition: 'all',
|
|
58
|
-
WebkitTransition: 'all',
|
|
59
|
-
WebkitTransitionDuration: 'inherit',
|
|
60
|
-
transitionDuration: 'inherit',
|
|
61
|
-
width: layoutInfo.rect.width + 'px',
|
|
62
|
-
height: layoutInfo.rect.height + 'px',
|
|
63
|
-
opacity: layoutInfo.opacity,
|
|
64
|
-
zIndex: layoutInfo.zIndex,
|
|
65
|
-
transform: layoutInfo.transform,
|
|
66
|
-
contain: 'size layout style paint'
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
18
|
|
|
70
|
-
function $f6363181241b7deb808b097c5c1c95$var$ScrollView(props, ref) {
|
|
71
|
-
let {
|
|
72
|
-
contentSize,
|
|
73
|
-
visibleRect,
|
|
74
|
-
onVisibleRectChange,
|
|
75
|
-
children,
|
|
76
|
-
innerStyle,
|
|
77
|
-
sizeToFit,
|
|
78
|
-
onScrollStart,
|
|
79
|
-
onScrollEnd,
|
|
80
|
-
scrollDirection = 'both'
|
|
81
|
-
} = props,
|
|
82
|
-
otherProps = _babelRuntimeHelpersEsmObjectWithoutProperties(props, ["contentSize", "visibleRect", "onVisibleRectChange", "children", "innerStyle", "sizeToFit", "onScrollStart", "onScrollEnd", "scrollDirection"]);
|
|
83
19
|
|
|
84
|
-
let defaultRef = useRef();
|
|
85
|
-
ref = ref || defaultRef;
|
|
86
|
-
let state = useRef({
|
|
87
|
-
scrollTop: 0,
|
|
88
|
-
scrollLeft: 0,
|
|
89
|
-
scrollEndTime: 0,
|
|
90
|
-
scrollTimeout: null,
|
|
91
|
-
width: 0,
|
|
92
|
-
height: 0
|
|
93
|
-
}).current;
|
|
94
|
-
let [isScrolling, setScrolling] = useState(false);
|
|
95
|
-
let onScroll = useCallback(e => {
|
|
96
|
-
flushSync(() => {
|
|
97
|
-
let {
|
|
98
|
-
scrollTop,
|
|
99
|
-
scrollLeft,
|
|
100
|
-
scrollHeight,
|
|
101
|
-
scrollWidth,
|
|
102
|
-
clientHeight,
|
|
103
|
-
clientWidth
|
|
104
|
-
} = e.currentTarget; // Prevent rubber band scrolling from shaking when scrolling out of bounds
|
|
105
20
|
|
|
106
|
-
state.scrollTop = Math.max(0, Math.min(scrollTop, scrollHeight - clientHeight));
|
|
107
|
-
state.scrollLeft = Math.max(0, Math.min(scrollLeft, scrollWidth - clientWidth));
|
|
108
|
-
onVisibleRectChange(new Rect(state.scrollLeft, state.scrollTop, state.width, state.height));
|
|
109
21
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (onScrollStart) {
|
|
114
|
-
onScrollStart();
|
|
115
|
-
}
|
|
116
|
-
} // So we don't constantly call clearTimeout and setTimeout,
|
|
117
|
-
// keep track of the current timeout time and only reschedule
|
|
118
|
-
// the timer when it is getting close.
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
let now = Date.now();
|
|
122
|
-
|
|
123
|
-
if (state.scrollEndTime <= now + 50) {
|
|
124
|
-
state.scrollEndTime = now + 300;
|
|
125
|
-
clearTimeout(state.scrollTimeout);
|
|
126
|
-
state.scrollTimeout = setTimeout(() => {
|
|
127
|
-
setScrolling(false);
|
|
128
|
-
state.scrollTimeout = null;
|
|
129
|
-
|
|
130
|
-
if (onScrollEnd) {
|
|
131
|
-
onScrollEnd();
|
|
132
|
-
}
|
|
133
|
-
}, 300);
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
}, [isScrolling, onScrollEnd, onScrollStart, onVisibleRectChange, state.height, state.scrollEndTime, state.scrollLeft, state.scrollTimeout, state.scrollTop, state.width]);
|
|
137
|
-
useEffect(() => {
|
|
138
|
-
// TODO: resize observer
|
|
139
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver
|
|
140
|
-
let updateSize = () => {
|
|
141
|
-
let dom = ref.current;
|
|
142
|
-
|
|
143
|
-
if (!dom) {
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
let w = dom.clientWidth;
|
|
148
|
-
let h = dom.clientHeight;
|
|
149
|
-
|
|
150
|
-
if (sizeToFit && contentSize.width > 0 && contentSize.height > 0) {
|
|
151
|
-
let style = window.getComputedStyle(dom);
|
|
152
|
-
|
|
153
|
-
if (sizeToFit === 'width') {
|
|
154
|
-
w = contentSize.width;
|
|
155
|
-
let maxWidth = parseInt(style.maxWidth, 10);
|
|
156
|
-
|
|
157
|
-
if (!isNaN(maxWidth)) {
|
|
158
|
-
w = Math.min(maxWidth, w);
|
|
159
|
-
}
|
|
160
|
-
} else if (sizeToFit === 'height') {
|
|
161
|
-
h = contentSize.height;
|
|
162
|
-
let maxHeight = parseInt(style.maxHeight, 10);
|
|
163
|
-
|
|
164
|
-
if (!isNaN(maxHeight)) {
|
|
165
|
-
h = Math.min(maxHeight, h);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
if (state.width !== w || state.height !== h) {
|
|
171
|
-
state.width = w;
|
|
172
|
-
state.height = h;
|
|
173
|
-
onVisibleRectChange(new Rect(state.scrollLeft, state.scrollTop, w, h));
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
updateSize();
|
|
178
|
-
window.addEventListener('resize', updateSize, false);
|
|
179
|
-
return () => {
|
|
180
|
-
window.removeEventListener('resize', updateSize, false);
|
|
181
|
-
};
|
|
182
|
-
}, [onVisibleRectChange, ref, state.height, state.scrollLeft, state.scrollTop, state.width, sizeToFit, contentSize.width, contentSize.height]);
|
|
183
|
-
useLayoutEffect(() => {
|
|
184
|
-
let dom = ref.current;
|
|
185
|
-
|
|
186
|
-
if (!dom) {
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
if (visibleRect.x !== state.scrollLeft) {
|
|
191
|
-
state.scrollLeft = visibleRect.x;
|
|
192
|
-
dom.scrollLeft = visibleRect.x;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
if (visibleRect.y !== state.scrollTop) {
|
|
196
|
-
state.scrollTop = visibleRect.y;
|
|
197
|
-
dom.scrollTop = visibleRect.y;
|
|
198
|
-
}
|
|
199
|
-
}, [ref, state.scrollLeft, state.scrollTop, visibleRect.x, visibleRect.y]);
|
|
200
|
-
|
|
201
|
-
let style = _babelRuntimeHelpersEsmObjectSpread({}, otherProps.style, {
|
|
202
|
-
position: 'relative',
|
|
203
|
-
// Reset padding so that relative positioning works correctly. Padding will be done in JS layout.
|
|
204
|
-
padding: 0
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
if (scrollDirection === 'horizontal') {
|
|
208
|
-
style.overflowX = 'auto';
|
|
209
|
-
style.overflowY = 'hidden';
|
|
210
|
-
} else if (scrollDirection === 'vertical') {
|
|
211
|
-
style.overflowY = 'auto';
|
|
212
|
-
style.overflowX = 'hidden';
|
|
213
|
-
} else {
|
|
214
|
-
style.overflow = 'auto';
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
return (/*#__PURE__*/_react.createElement("div", _babelRuntimeHelpersEsmExtends({}, otherProps, {
|
|
218
|
-
style: style,
|
|
219
|
-
ref: ref,
|
|
220
|
-
onScroll: onScroll
|
|
221
|
-
}), /*#__PURE__*/_react.createElement("div", {
|
|
222
|
-
role: "presentation",
|
|
223
|
-
style: _babelRuntimeHelpersEsmObjectSpread({
|
|
224
|
-
width: contentSize.width,
|
|
225
|
-
height: contentSize.height,
|
|
226
|
-
pointerEvents: isScrolling ? 'none' : 'auto'
|
|
227
|
-
}, innerStyle)
|
|
228
|
-
}, children))
|
|
229
|
-
);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
const $f6363181241b7deb808b097c5c1c95$export$ScrollView = _react.forwardRef($f6363181241b7deb808b097c5c1c95$var$ScrollView);
|
|
233
|
-
|
|
234
|
-
function $ebc1e5916247e7181d0a82c4b9f2$var$CollectionView(props, ref) {
|
|
235
|
-
let {
|
|
236
|
-
children: renderView,
|
|
237
|
-
renderWrapper,
|
|
238
|
-
layout,
|
|
239
|
-
collection,
|
|
240
|
-
focusedKey,
|
|
241
|
-
sizeToFit,
|
|
242
|
-
scrollDirection
|
|
243
|
-
} = props,
|
|
244
|
-
otherProps = _babelRuntimeHelpersEsmObjectWithoutProperties(props, ["children", "renderWrapper", "layout", "collection", "focusedKey", "sizeToFit", "scrollDirection"]);
|
|
245
|
-
|
|
246
|
-
let {
|
|
247
|
-
visibleViews,
|
|
248
|
-
visibleRect,
|
|
249
|
-
setVisibleRect,
|
|
250
|
-
contentSize,
|
|
251
|
-
isAnimating,
|
|
252
|
-
collectionManager,
|
|
253
|
-
startScrolling,
|
|
254
|
-
endScrolling
|
|
255
|
-
} = useCollectionState({
|
|
256
|
-
layout,
|
|
257
|
-
collection,
|
|
258
|
-
renderView,
|
|
259
|
-
renderWrapper: renderWrapper || $ebc1e5916247e7181d0a82c4b9f2$var$defaultRenderWrapper
|
|
260
|
-
});
|
|
261
|
-
let fallbackRef = useRef();
|
|
262
|
-
ref = ref || fallbackRef; // Scroll to the focusedKey when it changes. Actually focusing the focusedKey
|
|
263
|
-
// is up to the implementation using CollectionView since we don't have refs
|
|
264
|
-
// to all of the item DOM nodes.
|
|
265
|
-
|
|
266
|
-
useEffect(() => {
|
|
267
|
-
if (focusedKey) {
|
|
268
|
-
collectionManager.scrollToItem(focusedKey, 0);
|
|
269
|
-
}
|
|
270
|
-
}, [focusedKey, collectionManager]);
|
|
271
|
-
let isFocusWithin = useRef(false);
|
|
272
|
-
let onFocus = useCallback(e => {
|
|
273
|
-
// If the focused item is scrolled out of view and is not in the DOM, the CollectionView
|
|
274
|
-
// will have tabIndex={0}. When tabbing in from outside, scroll the focused item into view.
|
|
275
|
-
// We only want to do this if the CollectionView itself is receiving focus, not a child
|
|
276
|
-
// element, and we aren't moving focus to the CollectionView from within (see below).
|
|
277
|
-
if (e.target === ref.current && !isFocusWithin.current) {
|
|
278
|
-
collectionManager.scrollToItem(focusedKey, 0);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
isFocusWithin.current = e.target !== ref.current;
|
|
282
|
-
}, [ref, collectionManager, focusedKey]);
|
|
283
|
-
let onBlur = useCallback(e => {
|
|
284
|
-
isFocusWithin.current = ref.current.contains(e.relatedTarget);
|
|
285
|
-
}, [ref]); // When the focused item is scrolled out of view and is removed from the DOM,
|
|
286
|
-
// move focus to the collection view as a whole if focus was within before.
|
|
287
|
-
|
|
288
|
-
let focusedView = collectionManager.getItemView(focusedKey);
|
|
289
|
-
useEffect(() => {
|
|
290
|
-
if (focusedKey && !focusedView && isFocusWithin.current && document.activeElement !== ref.current) {
|
|
291
|
-
focusWithoutScrolling(ref.current);
|
|
292
|
-
}
|
|
293
|
-
});
|
|
294
|
-
return (/*#__PURE__*/_react.createElement($f6363181241b7deb808b097c5c1c95$export$ScrollView, _babelRuntimeHelpersEsmExtends({}, otherProps, {
|
|
295
|
-
tabIndex: focusedView ? -1 : 0,
|
|
296
|
-
ref: ref,
|
|
297
|
-
onFocus: chain(otherProps.onFocus, onFocus),
|
|
298
|
-
onBlur: chain(otherProps.onBlur, onBlur),
|
|
299
|
-
innerStyle: isAnimating ? {
|
|
300
|
-
transition: "none ".concat(collectionManager.transitionDuration, "ms")
|
|
301
|
-
} : undefined,
|
|
302
|
-
contentSize: contentSize,
|
|
303
|
-
visibleRect: visibleRect,
|
|
304
|
-
onVisibleRectChange: setVisibleRect,
|
|
305
|
-
onScrollStart: startScrolling,
|
|
306
|
-
onScrollEnd: endScrolling,
|
|
307
|
-
sizeToFit: sizeToFit,
|
|
308
|
-
scrollDirection: scrollDirection
|
|
309
|
-
}), visibleViews)
|
|
310
|
-
);
|
|
311
|
-
} // forwardRef doesn't support generic parameters, so cast the result to the correct type
|
|
312
|
-
// https://stackoverflow.com/questions/58469229/react-with-typescript-generics-while-using-react-forwardref
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
export const CollectionView = _react.forwardRef($ebc1e5916247e7181d0a82c4b9f2$var$CollectionView);
|
|
316
|
-
|
|
317
|
-
function $ebc1e5916247e7181d0a82c4b9f2$var$defaultRenderWrapper(parent, reusableView) {
|
|
318
|
-
return (/*#__PURE__*/_react.createElement(CollectionItem, {
|
|
319
|
-
key: reusableView.key,
|
|
320
|
-
reusableView: reusableView,
|
|
321
|
-
parent: parent
|
|
322
|
-
})
|
|
323
|
-
);
|
|
324
|
-
}
|
|
22
|
+
export {$e1995378a142960e$export$bf788dd355e3a401 as CollectionBuilder, $e1995378a142960e$export$fb8073518f34e6ec as Collection, $e1995378a142960e$export$18af5c7a9e9b3664 as createLeafComponent, $e1995378a142960e$export$e953bb1cd0f19726 as createBranchComponent, $f39a9eba43920ace$export$86427a43e3e48ebb as createHideableComponent, $f39a9eba43920ace$export$b5d7cc18bb8d2b59 as useIsHidden, $e948873055cbafe4$export$727c8fc270210f13 as useCachedChildren, $23b9f4fcf0fe224b$export$408d25a4e12db025 as BaseCollection, $23b9f4fcf0fe224b$export$f5d856d854e74713 as NodeValue};
|
|
23
|
+
//# sourceMappingURL=module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-aria/collections/src/index.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\nexport {CollectionBuilder, Collection, createLeafComponent, createBranchComponent} from './CollectionBuilder';\nexport {createHideableComponent, useIsHidden} from './Hidden';\nexport {useCachedChildren} from './useCachedChildren';\nexport {BaseCollection, NodeValue} from './BaseCollection';\n\nexport type {CollectionBuilderProps, CollectionProps} from './CollectionBuilder';\nexport type {CachedChildrenOptions} from './useCachedChildren';\n"],"names":[],"version":3,"file":"module.js.map"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,29 +1,86 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { Collection as _Collection1, Key, Node } from "@react-types/shared";
|
|
2
|
+
import React, { ReactElement, ReactNode, ForwardedRef, JSX } from "react";
|
|
3
|
+
/** An immutable object representing a Node in a Collection. */
|
|
4
|
+
export class NodeValue<T> implements Node<T> {
|
|
5
|
+
readonly type: string;
|
|
6
|
+
readonly key: Key;
|
|
7
|
+
readonly value: T | null;
|
|
8
|
+
readonly level: number;
|
|
9
|
+
readonly hasChildNodes: boolean;
|
|
10
|
+
readonly rendered: ReactNode;
|
|
11
|
+
readonly textValue: string;
|
|
12
|
+
readonly 'aria-label'?: string;
|
|
13
|
+
readonly index: number;
|
|
14
|
+
readonly parentKey: Key | null;
|
|
15
|
+
readonly prevKey: Key | null;
|
|
16
|
+
readonly nextKey: Key | null;
|
|
17
|
+
readonly firstChildKey: Key | null;
|
|
18
|
+
readonly lastChildKey: Key | null;
|
|
19
|
+
readonly props: any;
|
|
20
|
+
readonly render?: (node: Node<any>) => ReactElement;
|
|
21
|
+
constructor(type: string, key: Key);
|
|
22
|
+
get childNodes(): Iterable<Node<T>>;
|
|
23
|
+
clone(): NodeValue<T>;
|
|
6
24
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
25
|
+
/**
|
|
26
|
+
* An immutable Collection implementation. Updates are only allowed
|
|
27
|
+
* when it is not marked as frozen. This can be subclassed to implement
|
|
28
|
+
* custom collection behaviors.
|
|
29
|
+
*/
|
|
30
|
+
export class BaseCollection<T> implements _Collection1<Node<T>> {
|
|
31
|
+
get size(): number;
|
|
32
|
+
getKeys(): IterableIterator<Key>;
|
|
33
|
+
[Symbol.iterator](): Generator<Node<T>, void, unknown>;
|
|
34
|
+
getChildren(key: Key): Iterable<Node<T>>;
|
|
35
|
+
getKeyBefore(key: Key): Key;
|
|
36
|
+
getKeyAfter(key: Key): Key;
|
|
37
|
+
getFirstKey(): Key;
|
|
38
|
+
getLastKey(): Key;
|
|
39
|
+
getItem(key: Key): Node<T> | null;
|
|
40
|
+
at(): Node<T>;
|
|
41
|
+
clone(): this;
|
|
42
|
+
addNode(node: NodeValue<T>): void;
|
|
43
|
+
removeNode(key: Key): void;
|
|
44
|
+
commit(firstKey: Key | null, lastKey: Key | null, isSSR?: boolean): void;
|
|
13
45
|
}
|
|
14
|
-
export
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
46
|
+
export interface CachedChildrenOptions<T> {
|
|
47
|
+
/** Item objects in the collection. */
|
|
48
|
+
items?: Iterable<T>;
|
|
49
|
+
/** The contents of the collection. */
|
|
50
|
+
children?: ReactNode | ((item: T) => ReactNode);
|
|
51
|
+
/** Values that should invalidate the item cache when using dynamic collections. */
|
|
52
|
+
dependencies?: any[];
|
|
53
|
+
/** A scope to prepend to all child item ids to ensure they are unique. */
|
|
54
|
+
idScope?: Key;
|
|
55
|
+
/** Whether to add `id` and `value` props to all child items. */
|
|
56
|
+
addIdAndValue?: boolean;
|
|
24
57
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Maps over a list of items and renders React elements for them. Each rendered item is
|
|
60
|
+
* cached based on object identity, and React keys are generated from the `key` or `id` property.
|
|
61
|
+
*/
|
|
62
|
+
export function useCachedChildren<T extends object>(props: CachedChildrenOptions<T>): ReactNode;
|
|
63
|
+
/** Creates a component that forwards its ref and returns null if it is in a hidden subtree. */
|
|
64
|
+
export function createHideableComponent<T, P = {}>(fn: (props: P, ref: React.Ref<T>) => ReactNode | null): (props: P & React.RefAttributes<T>) => ReactNode | null;
|
|
65
|
+
/** Returns whether the component is in a hidden subtree. */
|
|
66
|
+
export function useIsHidden(): boolean;
|
|
67
|
+
export interface CollectionBuilderProps<C extends BaseCollection<object>> {
|
|
68
|
+
content: ReactNode;
|
|
69
|
+
children: (collection: C) => ReactNode;
|
|
70
|
+
createCollection?: () => C;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Builds a `Collection` from the children provided to the `content` prop, and passes it to the child render prop function.
|
|
74
|
+
*/
|
|
75
|
+
export function CollectionBuilder<C extends BaseCollection<object>>(props: CollectionBuilderProps<C>): string | number | bigint | boolean | Iterable<React.ReactNode> | Promise<string | number | bigint | boolean | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | React.ReactPortal | Iterable<React.ReactNode>> | JSX.Element;
|
|
76
|
+
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;
|
|
77
|
+
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;
|
|
78
|
+
export function createBranchComponent<T extends object, P extends {
|
|
79
|
+
children?: any;
|
|
80
|
+
}, E extends Element>(type: string, render: (props: P, ref: ForwardedRef<E>, node: Node<T>) => ReactNode, useChildren?: (props: P) => ReactNode): (props: P & React.RefAttributes<E>) => React.ReactNode | null;
|
|
81
|
+
export interface CollectionProps<T> extends CachedChildrenOptions<T> {
|
|
82
|
+
}
|
|
83
|
+
/** A Collection renders a list of items, automatically managing caching and keys. */
|
|
84
|
+
export function Collection<T extends object>(props: CollectionProps<T>): JSX.Element;
|
|
28
85
|
|
|
29
86
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"
|
|
1
|
+
{"mappings":";;AAmBA,+DAA+D;AAC/D,uBAAuB,CAAC,CAAE,YAAW,KAAK,CAAC,CAAC;IAC1C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;IAClB,QAAQ,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,CAAQ;IAChC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAK;IAC3B,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAS;IACxC,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAQ;IACpC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAM;IAChC,QAAQ,CAAC,YAAY,CAAC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,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,aAAa,CAAC,CAAC,KAAK,SAAS,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,MAAM,aAAa,CAAC,CAAC,CAAC,KAAK,SAAS,GAAG,IAAI,CAAC;AACzN,oCAAoC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,aAAa,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,KAAK,SAAS,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,MAAM,aAAa,CAAC,CAAC,CAAC,KAAK,SAAS,GAAG,IAAI,CAAC;AAmBxO,sCAAsC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS;IAAC,QAAQ,CAAC,EAAE,GAAG,CAAA;CAAC,EAAE,CAAC,SAAS,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,aAAa,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,KAAK,SAAS,EAAE,WAAW,GAAE,CAAC,KAAK,EAAE,CAAC,KAAK,SAAiC,iEAStP;AAMD,iCAAiC,CAAC,CAAE,SAAQ,sBAAsB,CAAC,CAAC;CAAG;AAIvE,qFAAqF;AACrF,2BAA2B,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,gBAAgB,CAAC,CAAC,GAAG,IAAI,OAAO,CA2BnF","sources":["packages/@react-aria/collections/src/packages/@react-aria/collections/src/BaseCollection.ts","packages/@react-aria/collections/src/packages/@react-aria/collections/src/Document.ts","packages/@react-aria/collections/src/packages/@react-aria/collections/src/useCachedChildren.ts","packages/@react-aria/collections/src/packages/@react-aria/collections/src/Hidden.tsx","packages/@react-aria/collections/src/packages/@react-aria/collections/src/CollectionBuilder.tsx","packages/@react-aria/collections/src/packages/@react-aria/collections/src/index.ts","packages/@react-aria/collections/src/index.ts"],"sourcesContent":[null,null,null,null,null,null,"/*\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\nexport {CollectionBuilder, Collection, createLeafComponent, createBranchComponent} from './CollectionBuilder';\nexport {createHideableComponent, useIsHidden} from './Hidden';\nexport {useCachedChildren} from './useCachedChildren';\nexport {BaseCollection, NodeValue} from './BaseCollection';\n\nexport type {CollectionBuilderProps, CollectionProps} from './CollectionBuilder';\nexport type {CachedChildrenOptions} from './useCachedChildren';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
var $6Tq0m$react = require("react");
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
function $parcel$export(e, n, v, s) {
|
|
5
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
$parcel$export(module.exports, "useCachedChildren", () => $245f3f827bea6653$export$727c8fc270210f13);
|
|
9
|
+
/*
|
|
10
|
+
* Copyright 2024 Adobe. All rights reserved.
|
|
11
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
12
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
13
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
16
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
17
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
18
|
+
* governing permissions and limitations under the License.
|
|
19
|
+
*/
|
|
20
|
+
function $245f3f827bea6653$export$727c8fc270210f13(props) {
|
|
21
|
+
let { children: children, items: items, idScope: idScope, addIdAndValue: addIdAndValue, dependencies: dependencies = [] } = props;
|
|
22
|
+
// Invalidate the cache whenever the parent value changes.
|
|
23
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
24
|
+
let cache = (0, $6Tq0m$react.useMemo)(()=>new WeakMap(), dependencies);
|
|
25
|
+
return (0, $6Tq0m$react.useMemo)(()=>{
|
|
26
|
+
if (items && typeof children === 'function') {
|
|
27
|
+
let res = [];
|
|
28
|
+
for (let item of items){
|
|
29
|
+
let rendered = cache.get(item);
|
|
30
|
+
if (!rendered) {
|
|
31
|
+
rendered = children(item);
|
|
32
|
+
var _rendered_props_id, _ref;
|
|
33
|
+
// @ts-ignore
|
|
34
|
+
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;
|
|
35
|
+
// eslint-disable-next-line max-depth
|
|
36
|
+
if (key == null) throw new Error('Could not determine key for item');
|
|
37
|
+
// eslint-disable-next-line max-depth
|
|
38
|
+
if (idScope) key = idScope + ':' + key;
|
|
39
|
+
// Note: only works if wrapped Item passes through id...
|
|
40
|
+
rendered = (0, $6Tq0m$react.cloneElement)(rendered, addIdAndValue ? {
|
|
41
|
+
key: key,
|
|
42
|
+
id: key,
|
|
43
|
+
value: item
|
|
44
|
+
} : {
|
|
45
|
+
key: key
|
|
46
|
+
});
|
|
47
|
+
cache.set(item, rendered);
|
|
48
|
+
}
|
|
49
|
+
res.push(rendered);
|
|
50
|
+
}
|
|
51
|
+
return res;
|
|
52
|
+
} else if (typeof children !== 'function') return children;
|
|
53
|
+
}, [
|
|
54
|
+
children,
|
|
55
|
+
items,
|
|
56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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.main.js.map"}
|
|
@@ -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
|