@react-aria/virtualizer 3.0.0-nightly-641446f65-240905

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.
Files changed (40) hide show
  1. package/README.md +3 -0
  2. package/dist/ScrollView.main.js +214 -0
  3. package/dist/ScrollView.main.js.map +1 -0
  4. package/dist/ScrollView.mjs +204 -0
  5. package/dist/ScrollView.module.js +204 -0
  6. package/dist/ScrollView.module.js.map +1 -0
  7. package/dist/Virtualizer.main.js +88 -0
  8. package/dist/Virtualizer.main.js.map +1 -0
  9. package/dist/Virtualizer.mjs +79 -0
  10. package/dist/Virtualizer.module.js +79 -0
  11. package/dist/Virtualizer.module.js.map +1 -0
  12. package/dist/VirtualizerItem.main.js +82 -0
  13. package/dist/VirtualizerItem.main.js.map +1 -0
  14. package/dist/VirtualizerItem.mjs +72 -0
  15. package/dist/VirtualizerItem.module.js +72 -0
  16. package/dist/VirtualizerItem.module.js.map +1 -0
  17. package/dist/import.mjs +25 -0
  18. package/dist/main.js +38 -0
  19. package/dist/main.js.map +1 -0
  20. package/dist/module.js +25 -0
  21. package/dist/module.js.map +1 -0
  22. package/dist/types.d.ts +337 -0
  23. package/dist/types.d.ts.map +1 -0
  24. package/dist/useVirtualizerItem.main.js +53 -0
  25. package/dist/useVirtualizerItem.main.js.map +1 -0
  26. package/dist/useVirtualizerItem.mjs +48 -0
  27. package/dist/useVirtualizerItem.module.js +48 -0
  28. package/dist/useVirtualizerItem.module.js.map +1 -0
  29. package/dist/utils.main.js +80 -0
  30. package/dist/utils.main.js.map +1 -0
  31. package/dist/utils.mjs +73 -0
  32. package/dist/utils.module.js +73 -0
  33. package/dist/utils.module.js.map +1 -0
  34. package/package.json +40 -0
  35. package/src/ScrollView.tsx +246 -0
  36. package/src/Virtualizer.tsx +117 -0
  37. package/src/VirtualizerItem.tsx +87 -0
  38. package/src/index.ts +19 -0
  39. package/src/useVirtualizerItem.ts +54 -0
  40. package/src/utils.ts +107 -0
package/dist/utils.mjs ADDED
@@ -0,0 +1,73 @@
1
+ /*
2
+ * Copyright 2020 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
+ */ let $ce415dc67314b753$var$cachedRTLResult = null;
12
+ function $ce415dc67314b753$export$faf7630257ad4304(recalculate = false) {
13
+ if ($ce415dc67314b753$var$cachedRTLResult === null || recalculate) {
14
+ const outerDiv = document.createElement('div');
15
+ const outerStyle = outerDiv.style;
16
+ outerStyle.width = '50px';
17
+ outerStyle.height = '50px';
18
+ outerStyle.overflow = 'scroll';
19
+ outerStyle.direction = 'rtl';
20
+ const innerDiv = document.createElement('div');
21
+ const innerStyle = innerDiv.style;
22
+ innerStyle.width = '100px';
23
+ innerStyle.height = '100px';
24
+ outerDiv.appendChild(innerDiv);
25
+ document.body.appendChild(outerDiv);
26
+ if (outerDiv.scrollLeft > 0) $ce415dc67314b753$var$cachedRTLResult = 'positive-descending';
27
+ else {
28
+ outerDiv.scrollLeft = 1;
29
+ if (outerDiv.scrollLeft === 0) $ce415dc67314b753$var$cachedRTLResult = 'negative';
30
+ else $ce415dc67314b753$var$cachedRTLResult = 'positive-ascending';
31
+ }
32
+ document.body.removeChild(outerDiv);
33
+ return $ce415dc67314b753$var$cachedRTLResult;
34
+ }
35
+ return $ce415dc67314b753$var$cachedRTLResult;
36
+ }
37
+ function $ce415dc67314b753$export$1389d168952b34b5(node, direction) {
38
+ let { scrollLeft: scrollLeft } = node;
39
+ // scrollLeft in rtl locales differs across browsers, so normalize.
40
+ // See comment by getRTLOffsetType below for details.
41
+ if (direction === 'rtl') {
42
+ let { scrollWidth: scrollWidth, clientWidth: clientWidth } = node;
43
+ switch($ce415dc67314b753$export$faf7630257ad4304()){
44
+ case 'negative':
45
+ scrollLeft = -scrollLeft;
46
+ break;
47
+ case 'positive-descending':
48
+ scrollLeft = scrollWidth - clientWidth - scrollLeft;
49
+ break;
50
+ }
51
+ }
52
+ return scrollLeft;
53
+ }
54
+ function $ce415dc67314b753$export$ed5fd5ffe5ab0ac(node, direction, scrollLeft) {
55
+ if (direction === 'rtl') switch($ce415dc67314b753$export$faf7630257ad4304()){
56
+ case 'negative':
57
+ scrollLeft = -scrollLeft;
58
+ break;
59
+ case 'positive-ascending':
60
+ break;
61
+ default:
62
+ {
63
+ const { clientWidth: clientWidth, scrollWidth: scrollWidth } = node;
64
+ scrollLeft = scrollWidth - clientWidth - scrollLeft;
65
+ break;
66
+ }
67
+ }
68
+ node.scrollLeft = scrollLeft;
69
+ }
70
+
71
+
72
+ export {$ce415dc67314b753$export$faf7630257ad4304 as getRTLOffsetType, $ce415dc67314b753$export$1389d168952b34b5 as getScrollLeft, $ce415dc67314b753$export$ed5fd5ffe5ab0ac as setScrollLeft};
73
+ //# sourceMappingURL=utils.module.js.map
@@ -0,0 +1,73 @@
1
+ /*
2
+ * Copyright 2020 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
+ */ let $ce415dc67314b753$var$cachedRTLResult = null;
12
+ function $ce415dc67314b753$export$faf7630257ad4304(recalculate = false) {
13
+ if ($ce415dc67314b753$var$cachedRTLResult === null || recalculate) {
14
+ const outerDiv = document.createElement('div');
15
+ const outerStyle = outerDiv.style;
16
+ outerStyle.width = '50px';
17
+ outerStyle.height = '50px';
18
+ outerStyle.overflow = 'scroll';
19
+ outerStyle.direction = 'rtl';
20
+ const innerDiv = document.createElement('div');
21
+ const innerStyle = innerDiv.style;
22
+ innerStyle.width = '100px';
23
+ innerStyle.height = '100px';
24
+ outerDiv.appendChild(innerDiv);
25
+ document.body.appendChild(outerDiv);
26
+ if (outerDiv.scrollLeft > 0) $ce415dc67314b753$var$cachedRTLResult = 'positive-descending';
27
+ else {
28
+ outerDiv.scrollLeft = 1;
29
+ if (outerDiv.scrollLeft === 0) $ce415dc67314b753$var$cachedRTLResult = 'negative';
30
+ else $ce415dc67314b753$var$cachedRTLResult = 'positive-ascending';
31
+ }
32
+ document.body.removeChild(outerDiv);
33
+ return $ce415dc67314b753$var$cachedRTLResult;
34
+ }
35
+ return $ce415dc67314b753$var$cachedRTLResult;
36
+ }
37
+ function $ce415dc67314b753$export$1389d168952b34b5(node, direction) {
38
+ let { scrollLeft: scrollLeft } = node;
39
+ // scrollLeft in rtl locales differs across browsers, so normalize.
40
+ // See comment by getRTLOffsetType below for details.
41
+ if (direction === 'rtl') {
42
+ let { scrollWidth: scrollWidth, clientWidth: clientWidth } = node;
43
+ switch($ce415dc67314b753$export$faf7630257ad4304()){
44
+ case 'negative':
45
+ scrollLeft = -scrollLeft;
46
+ break;
47
+ case 'positive-descending':
48
+ scrollLeft = scrollWidth - clientWidth - scrollLeft;
49
+ break;
50
+ }
51
+ }
52
+ return scrollLeft;
53
+ }
54
+ function $ce415dc67314b753$export$ed5fd5ffe5ab0ac(node, direction, scrollLeft) {
55
+ if (direction === 'rtl') switch($ce415dc67314b753$export$faf7630257ad4304()){
56
+ case 'negative':
57
+ scrollLeft = -scrollLeft;
58
+ break;
59
+ case 'positive-ascending':
60
+ break;
61
+ default:
62
+ {
63
+ const { clientWidth: clientWidth, scrollWidth: scrollWidth } = node;
64
+ scrollLeft = scrollWidth - clientWidth - scrollLeft;
65
+ break;
66
+ }
67
+ }
68
+ node.scrollLeft = scrollLeft;
69
+ }
70
+
71
+
72
+ export {$ce415dc67314b753$export$faf7630257ad4304 as getRTLOffsetType, $ce415dc67314b753$export$1389d168952b34b5 as getScrollLeft, $ce415dc67314b753$export$ed5fd5ffe5ab0ac as setScrollLeft};
73
+ //# sourceMappingURL=utils.module.js.map
@@ -0,0 +1 @@
1
+ {"mappings":"AAAA;;;;;;;;;;CAUC,GASD,IAAI,wCAAwC;AAarC,SAAS,0CAAiB,cAAuB,KAAK;IAC3D,IAAI,0CAAoB,QAAQ,aAAa;QAC3C,MAAM,WAAW,SAAS,aAAa,CAAC;QACxC,MAAM,aAAa,SAAS,KAAK;QACjC,WAAW,KAAK,GAAG;QACnB,WAAW,MAAM,GAAG;QACpB,WAAW,QAAQ,GAAG;QACtB,WAAW,SAAS,GAAG;QAEvB,MAAM,WAAW,SAAS,aAAa,CAAC;QACxC,MAAM,aAAa,SAAS,KAAK;QACjC,WAAW,KAAK,GAAG;QACnB,WAAW,MAAM,GAAG;QAEpB,SAAS,WAAW,CAAC;QAErB,SAAS,IAAI,CAAC,WAAW,CAAC;QAE1B,IAAI,SAAS,UAAU,GAAG,GACxB,wCAAkB;aACb;YACL,SAAS,UAAU,GAAG;YACtB,IAAI,SAAS,UAAU,KAAK,GAC1B,wCAAkB;iBAElB,wCAAkB;QAEtB;QAEA,SAAS,IAAI,CAAC,WAAW,CAAC;QAE1B,OAAO;IACT;IAEA,OAAO;AACT;AAEO,SAAS,0CAAc,IAAa,EAAE,SAAoB;IAC/D,IAAI,cAAC,UAAU,EAAC,GAAG;IAEnB,mEAAmE;IACnE,qDAAqD;IACrD,IAAI,cAAc,OAAO;QACvB,IAAI,eAAC,WAAW,eAAE,WAAW,EAAC,GAAG;QACjC,OAAQ;YACN,KAAK;gBACH,aAAa,CAAC;gBACd;YACF,KAAK;gBACH,aAAa,cAAc,cAAc;gBACzC;QACJ;IACF;IAEA,OAAO;AACT;AAEO,SAAS,yCAAc,IAAa,EAAE,SAAoB,EAAE,UAAkB;IACnF,IAAI,cAAc,OAChB,OAAQ;QACN,KAAK;YACH,aAAa,CAAC;YACd;QACF,KAAK;YACH;QACF;YAAS;gBACP,MAAM,eAAC,WAAW,eAAE,WAAW,EAAC,GAAG;gBACnC,aAAa,cAAc,cAAc;gBACzC;YACF;IACF;IAGF,KAAK,UAAU,GAAG;AACpB","sources":["packages/@react-aria/virtualizer/src/utils.ts"],"sourcesContent":["/*\n * Copyright 2020 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 {Direction} from '@react-types/shared';\n\nexport type RTLOffsetType =\n | 'negative'\n | 'positive-descending'\n | 'positive-ascending';\n\nlet cachedRTLResult: RTLOffsetType | null = null;\n\n\n// Original licensing for the following methods can be found in the\n// NOTICE file in the root directory of this source tree.\n// See https://github.com/bvaughn/react-window/blob/master/src/createGridComponent.js\n\n// According to the spec, scrollLeft should be negative for RTL aligned elements.\n// Chrome does not seem to adhere; its scrollLeft values are positive (measured relative to the left).\n// Safari's elastic bounce makes detecting this even more complicated wrt potential false positives.\n// The safest way to check this is to intentionally set a negative offset,\n// and then verify that the subsequent \"scroll\" event matches the negative offset.\n// If it does not match, then we can assume a non-standard RTL scroll implementation.\nexport function getRTLOffsetType(recalculate: boolean = false): RTLOffsetType {\n if (cachedRTLResult === null || recalculate) {\n const outerDiv = document.createElement('div');\n const outerStyle = outerDiv.style;\n outerStyle.width = '50px';\n outerStyle.height = '50px';\n outerStyle.overflow = 'scroll';\n outerStyle.direction = 'rtl';\n\n const innerDiv = document.createElement('div');\n const innerStyle = innerDiv.style;\n innerStyle.width = '100px';\n innerStyle.height = '100px';\n\n outerDiv.appendChild(innerDiv);\n\n document.body.appendChild(outerDiv);\n\n if (outerDiv.scrollLeft > 0) {\n cachedRTLResult = 'positive-descending';\n } else {\n outerDiv.scrollLeft = 1;\n if (outerDiv.scrollLeft === 0) {\n cachedRTLResult = 'negative';\n } else {\n cachedRTLResult = 'positive-ascending';\n }\n }\n\n document.body.removeChild(outerDiv);\n\n return cachedRTLResult;\n }\n\n return cachedRTLResult;\n}\n\nexport function getScrollLeft(node: Element, direction: Direction): number {\n let {scrollLeft} = node;\n\n // scrollLeft in rtl locales differs across browsers, so normalize.\n // See comment by getRTLOffsetType below for details.\n if (direction === 'rtl') {\n let {scrollWidth, clientWidth} = node;\n switch (getRTLOffsetType()) {\n case 'negative':\n scrollLeft = -scrollLeft;\n break;\n case 'positive-descending':\n scrollLeft = scrollWidth - clientWidth - scrollLeft;\n break;\n }\n }\n\n return scrollLeft;\n}\n\nexport function setScrollLeft(node: Element, direction: Direction, scrollLeft: number) {\n if (direction === 'rtl') {\n switch (getRTLOffsetType()) {\n case 'negative':\n scrollLeft = -scrollLeft;\n break;\n case 'positive-ascending':\n break;\n default: {\n const {clientWidth, scrollWidth} = node;\n scrollLeft = scrollWidth - clientWidth - scrollLeft;\n break;\n }\n }\n }\n\n node.scrollLeft = scrollLeft;\n}\n"],"names":[],"version":3,"file":"utils.module.js.map"}
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@react-aria/virtualizer",
3
+ "version": "3.0.0-nightly-641446f65-240905",
4
+ "description": "Spectrum UI components in React",
5
+ "license": "Apache-2.0",
6
+ "main": "dist/main.js",
7
+ "module": "dist/module.js",
8
+ "exports": {
9
+ "types": "./dist/types.d.ts",
10
+ "import": "./dist/import.mjs",
11
+ "require": "./dist/main.js"
12
+ },
13
+ "types": "dist/types.d.ts",
14
+ "source": "src/index.ts",
15
+ "files": [
16
+ "dist",
17
+ "src"
18
+ ],
19
+ "sideEffects": false,
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/adobe/react-spectrum"
23
+ },
24
+ "dependencies": {
25
+ "@react-aria/i18n": "^3.0.0-nightly-641446f65-240905",
26
+ "@react-aria/interactions": "^3.0.0-nightly-641446f65-240905",
27
+ "@react-aria/utils": "^3.0.0-nightly-641446f65-240905",
28
+ "@react-stately/virtualizer": "^3.0.0-nightly-641446f65-240905",
29
+ "@react-types/shared": "^3.0.0-nightly-641446f65-240905",
30
+ "@swc/helpers": "^0.5.0"
31
+ },
32
+ "peerDependencies": {
33
+ "react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0",
34
+ "react-dom": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "stableVersion": "4.0.2"
40
+ }
@@ -0,0 +1,246 @@
1
+ /*
2
+ * Copyright 2020 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
+ // @ts-ignore
14
+ import {flushSync} from 'react-dom';
15
+ import {getScrollLeft} from './utils';
16
+ import React, {
17
+ CSSProperties,
18
+ ForwardedRef,
19
+ HTMLAttributes,
20
+ ReactNode,
21
+ RefObject,
22
+ useCallback,
23
+ useEffect,
24
+ useRef,
25
+ useState
26
+ } from 'react';
27
+ import {Rect, Size} from '@react-stately/virtualizer';
28
+ import {useEffectEvent, useEvent, useLayoutEffect, useObjectRef, useResizeObserver} from '@react-aria/utils';
29
+ import {useLocale} from '@react-aria/i18n';
30
+
31
+ interface ScrollViewProps extends HTMLAttributes<HTMLElement> {
32
+ contentSize: Size,
33
+ onVisibleRectChange: (rect: Rect) => void,
34
+ children?: ReactNode,
35
+ innerStyle?: CSSProperties,
36
+ onScrollStart?: () => void,
37
+ onScrollEnd?: () => void,
38
+ scrollDirection?: 'horizontal' | 'vertical' | 'both'
39
+ }
40
+
41
+ function ScrollView(props: ScrollViewProps, ref: ForwardedRef<HTMLDivElement | null>) {
42
+ ref = useObjectRef(ref);
43
+ let {scrollViewProps, contentProps} = useScrollView(props, ref);
44
+
45
+ return (
46
+ <div role="presentation" {...scrollViewProps} ref={ref}>
47
+ <div role="presentation" {...contentProps}>
48
+ {props.children}
49
+ </div>
50
+ </div>
51
+ );
52
+ }
53
+
54
+ const ScrollViewForwardRef = React.forwardRef(ScrollView);
55
+ export {ScrollViewForwardRef as ScrollView};
56
+
57
+ export function useScrollView(props: ScrollViewProps, ref: RefObject<HTMLElement | null>) {
58
+ let {
59
+ contentSize,
60
+ onVisibleRectChange,
61
+ innerStyle,
62
+ onScrollStart,
63
+ onScrollEnd,
64
+ scrollDirection = 'both',
65
+ ...otherProps
66
+ } = props;
67
+
68
+ let state = useRef({
69
+ scrollTop: 0,
70
+ scrollLeft: 0,
71
+ scrollEndTime: 0,
72
+ scrollTimeout: null,
73
+ width: 0,
74
+ height: 0,
75
+ isScrolling: false
76
+ }).current;
77
+ let {direction} = useLocale();
78
+
79
+ let [isScrolling, setScrolling] = useState(false);
80
+ let onScroll = useCallback((e) => {
81
+ if (e.target !== e.currentTarget) {
82
+ return;
83
+ }
84
+
85
+ if (props.onScroll) {
86
+ props.onScroll(e);
87
+ }
88
+
89
+ flushSync(() => {
90
+ let scrollTop = e.currentTarget.scrollTop;
91
+ let scrollLeft = getScrollLeft(e.currentTarget, direction);
92
+
93
+ // Prevent rubber band scrolling from shaking when scrolling out of bounds
94
+ state.scrollTop = Math.max(0, Math.min(scrollTop, contentSize.height - state.height));
95
+ state.scrollLeft = Math.max(0, Math.min(scrollLeft, contentSize.width - state.width));
96
+
97
+ onVisibleRectChange(new Rect(state.scrollLeft, state.scrollTop, state.width, state.height));
98
+
99
+ if (!state.isScrolling) {
100
+ state.isScrolling = true;
101
+ setScrolling(true);
102
+
103
+ // Pause typekit MutationObserver during scrolling.
104
+ window.dispatchEvent(new Event('tk.disconnect-observer'));
105
+ if (onScrollStart) {
106
+ onScrollStart();
107
+ }
108
+ }
109
+
110
+ // So we don't constantly call clearTimeout and setTimeout,
111
+ // keep track of the current timeout time and only reschedule
112
+ // the timer when it is getting close.
113
+ let now = Date.now();
114
+ if (state.scrollEndTime <= now + 50) {
115
+ state.scrollEndTime = now + 300;
116
+
117
+ clearTimeout(state.scrollTimeout);
118
+ state.scrollTimeout = setTimeout(() => {
119
+ state.isScrolling = false;
120
+ setScrolling(false);
121
+ state.scrollTimeout = null;
122
+
123
+ window.dispatchEvent(new Event('tk.connect-observer'));
124
+ if (onScrollEnd) {
125
+ onScrollEnd();
126
+ }
127
+ }, 300);
128
+ }
129
+ });
130
+ }, [props, direction, state, contentSize, onVisibleRectChange, onScrollStart, onScrollEnd]);
131
+
132
+ // Attach event directly to ref so RAC Virtualizer doesn't need to send props upward.
133
+ useEvent(ref, 'scroll', onScroll);
134
+
135
+ // eslint-disable-next-line arrow-body-style
136
+ useEffect(() => {
137
+ return () => {
138
+ clearTimeout(state.scrollTimeout);
139
+ if (state.isScrolling) {
140
+ window.dispatchEvent(new Event('tk.connect-observer'));
141
+ }
142
+ };
143
+ // eslint-disable-next-line react-hooks/exhaustive-deps
144
+ }, []);
145
+
146
+ let updateSize = useEffectEvent((flush: typeof flushSync) => {
147
+ let dom = ref.current;
148
+ if (!dom) {
149
+ return;
150
+ }
151
+
152
+ let isTestEnv = process.env.NODE_ENV === 'test' && !process.env.VIRT_ON;
153
+ let isClientWidthMocked = Object.getOwnPropertyNames(window.HTMLElement.prototype).includes('clientWidth');
154
+ let isClientHeightMocked = Object.getOwnPropertyNames(window.HTMLElement.prototype).includes('clientHeight');
155
+ let clientWidth = dom.clientWidth;
156
+ let clientHeight = dom.clientHeight;
157
+ let w = isTestEnv && !isClientWidthMocked ? Infinity : clientWidth;
158
+ let h = isTestEnv && !isClientHeightMocked ? Infinity : clientHeight;
159
+
160
+ if (state.width !== w || state.height !== h) {
161
+ state.width = w;
162
+ state.height = h;
163
+ flush(() => {
164
+ onVisibleRectChange(new Rect(state.scrollLeft, state.scrollTop, w, h));
165
+ });
166
+
167
+ // If the clientWidth or clientHeight changed, scrollbars appeared or disappeared as
168
+ // a result of the layout update. In this case, re-layout again to account for the
169
+ // adjusted space. In very specific cases this might result in the scrollbars disappearing
170
+ // again, resulting in extra padding. We stop after a maximum of two layout passes to avoid
171
+ // an infinite loop. This matches how browsers behavior with native CSS grid layout.
172
+ if (!isTestEnv && clientWidth !== dom.clientWidth || clientHeight !== dom.clientHeight) {
173
+ state.width = dom.clientWidth;
174
+ state.height = dom.clientHeight;
175
+ flush(() => {
176
+ onVisibleRectChange(new Rect(state.scrollLeft, state.scrollTop, state.width, state.height));
177
+ });
178
+ }
179
+ }
180
+ });
181
+
182
+ let didUpdateSize = useRef(false);
183
+ useLayoutEffect(() => {
184
+ // React doesn't allow flushSync inside effects, so queue a microtask.
185
+ // We also need to wait until all refs are set (e.g. when passing a ref down from a parent).
186
+ queueMicrotask(() => {
187
+ if (!didUpdateSize.current) {
188
+ didUpdateSize.current = true;
189
+ updateSize(flushSync);
190
+ }
191
+ });
192
+ }, [updateSize]);
193
+ useEffect(() => {
194
+ if (!didUpdateSize.current) {
195
+ // If useEffect ran before the above microtask, we are in a synchronous render (e.g. act).
196
+ // Update the size here so that you don't need to mock timers in tests.
197
+ didUpdateSize.current = true;
198
+ updateSize(fn => fn());
199
+ }
200
+ }, [updateSize]);
201
+ let onResize = useCallback(() => {
202
+ updateSize(flushSync);
203
+ }, [updateSize]);
204
+
205
+ // Watch border-box instead of of content-box so that we don't go into
206
+ // an infinite loop when scrollbars appear or disappear.
207
+ useResizeObserver({ref, box: 'border-box', onResize});
208
+
209
+ let style: React.CSSProperties = {
210
+ // Reset padding so that relative positioning works correctly. Padding will be done in JS layout.
211
+ padding: 0,
212
+ ...otherProps.style
213
+ };
214
+
215
+ if (scrollDirection === 'horizontal') {
216
+ style.overflowX = 'auto';
217
+ style.overflowY = 'hidden';
218
+ } else if (scrollDirection === 'vertical' || contentSize.width === state.width) {
219
+ // Set overflow-x: hidden if content size is equal to the width of the scroll view.
220
+ // This prevents horizontal scrollbars from flickering during resizing due to resize observer
221
+ // firing slower than the frame rate, which may cause an infinite re-render loop.
222
+ style.overflowY = 'auto';
223
+ style.overflowX = 'hidden';
224
+ } else {
225
+ style.overflow = 'auto';
226
+ }
227
+
228
+ innerStyle = {
229
+ width: Number.isFinite(contentSize.width) ? contentSize.width : undefined,
230
+ height: Number.isFinite(contentSize.height) ? contentSize.height : undefined,
231
+ pointerEvents: isScrolling ? 'none' : 'auto',
232
+ position: 'relative',
233
+ ...innerStyle
234
+ };
235
+
236
+ return {
237
+ scrollViewProps: {
238
+ ...otherProps,
239
+ style
240
+ },
241
+ contentProps: {
242
+ role: 'presentation',
243
+ style: innerStyle
244
+ }
245
+ };
246
+ }
@@ -0,0 +1,117 @@
1
+ /*
2
+ * Copyright 2020 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, Key, RefObject} from '@react-types/shared';
14
+ import {Layout, Rect, ReusableView, useVirtualizerState} from '@react-stately/virtualizer';
15
+ import {mergeProps, useLoadMore} from '@react-aria/utils';
16
+ import React, {HTMLAttributes, ReactElement, ReactNode, useCallback, useRef} from 'react';
17
+ import {ScrollView} from './ScrollView';
18
+ import {VirtualizerItem} from './VirtualizerItem';
19
+
20
+ type RenderWrapper<T extends object, V> = (
21
+ parent: ReusableView<T, V> | null,
22
+ reusableView: ReusableView<T, V>,
23
+ children: ReusableView<T, V>[],
24
+ renderChildren: (views: ReusableView<T, V>[]) => ReactElement[]
25
+ ) => ReactElement;
26
+
27
+ interface VirtualizerProps<T extends object, V, O> extends Omit<HTMLAttributes<HTMLElement>, 'children'> {
28
+ children: (type: string, content: T) => V,
29
+ renderWrapper?: RenderWrapper<T, V>,
30
+ layout: Layout<T, O>,
31
+ collection: Collection<T>,
32
+ persistedKeys?: Set<Key> | null,
33
+ scrollDirection?: 'horizontal' | 'vertical' | 'both',
34
+ isLoading?: boolean,
35
+ onLoadMore?: () => void,
36
+ layoutOptions?: O
37
+ }
38
+
39
+ function Virtualizer<T extends object, V extends ReactNode, O>(props: VirtualizerProps<T, V, O>, ref: RefObject<HTMLDivElement | null>) {
40
+ let {
41
+ children: renderView,
42
+ renderWrapper,
43
+ layout,
44
+ collection,
45
+ scrollDirection,
46
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
47
+ isLoading,
48
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
49
+ onLoadMore,
50
+ persistedKeys,
51
+ layoutOptions,
52
+ ...otherProps
53
+ } = props;
54
+
55
+ let fallbackRef = useRef<HTMLDivElement>(undefined);
56
+ ref = ref || fallbackRef;
57
+
58
+ let state = useVirtualizerState({
59
+ layout,
60
+ collection,
61
+ renderView,
62
+ onVisibleRectChange(rect) {
63
+ ref.current.scrollLeft = rect.x;
64
+ ref.current.scrollTop = rect.y;
65
+ },
66
+ persistedKeys,
67
+ layoutOptions
68
+ });
69
+
70
+ useLoadMore({isLoading, onLoadMore, scrollOffset: 1}, ref);
71
+ let onVisibleRectChange = useCallback((rect: Rect) => {
72
+ state.setVisibleRect(rect);
73
+ }, [state]);
74
+
75
+ return (
76
+ <ScrollView
77
+ {...mergeProps(otherProps, {onVisibleRectChange})}
78
+ ref={ref}
79
+ contentSize={state.contentSize}
80
+ onScrollStart={state.startScrolling}
81
+ onScrollEnd={state.endScrolling}
82
+ scrollDirection={scrollDirection}>
83
+ {renderChildren(null, state.visibleViews, renderWrapper || defaultRenderWrapper)}
84
+ </ScrollView>
85
+ );
86
+ }
87
+
88
+ // forwardRef doesn't support generic parameters, so cast the result to the correct type
89
+ // https://stackoverflow.com/questions/58469229/react-with-typescript-generics-while-using-react-forwardref
90
+ const _Virtualizer = React.forwardRef(Virtualizer) as <T extends object, V, O>(props: VirtualizerProps<T, V, O> & {ref?: RefObject<HTMLDivElement | null>}) => ReactElement;
91
+ export {_Virtualizer as Virtualizer};
92
+
93
+ function renderChildren<T extends object, V>(parent: ReusableView<T, V> | null, views: ReusableView<T, V>[], renderWrapper: RenderWrapper<T, V>) {
94
+ return views.map(view => {
95
+ return renderWrapper(
96
+ parent,
97
+ view,
98
+ view.children ? Array.from(view.children) : [],
99
+ childViews => renderChildren(view, childViews, renderWrapper)
100
+ );
101
+ });
102
+ }
103
+
104
+ function defaultRenderWrapper<T extends object, V extends ReactNode>(
105
+ parent: ReusableView<T, V> | null,
106
+ reusableView: ReusableView<T, V>
107
+ ) {
108
+ return (
109
+ <VirtualizerItem
110
+ key={reusableView.key}
111
+ layoutInfo={reusableView.layoutInfo}
112
+ virtualizer={reusableView.virtualizer}
113
+ parent={parent?.layoutInfo}>
114
+ {reusableView.rendered}
115
+ </VirtualizerItem>
116
+ );
117
+ }
@@ -0,0 +1,87 @@
1
+ /*
2
+ * Copyright 2020 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 {Direction} from '@react-types/shared';
14
+ import {LayoutInfo} from '@react-stately/virtualizer';
15
+ import React, {CSSProperties, ReactNode, useRef} from 'react';
16
+ import {useLocale} from '@react-aria/i18n';
17
+ import {useVirtualizerItem, VirtualizerItemOptions} from './useVirtualizerItem';
18
+
19
+ interface VirtualizerItemProps extends Omit<VirtualizerItemOptions, 'ref'> {
20
+ parent?: LayoutInfo | null,
21
+ className?: string,
22
+ children: ReactNode
23
+ }
24
+
25
+ export function VirtualizerItem(props: VirtualizerItemProps) {
26
+ let {className, layoutInfo, virtualizer, parent, children} = props;
27
+ let {direction} = useLocale();
28
+ let ref = useRef(undefined);
29
+ useVirtualizerItem({
30
+ layoutInfo,
31
+ virtualizer,
32
+ ref
33
+ });
34
+
35
+ return (
36
+ <div role="presentation" ref={ref} className={className} style={layoutInfoToStyle(layoutInfo, direction, parent)}>
37
+ {children}
38
+ </div>
39
+ );
40
+ }
41
+
42
+ let cache = new WeakMap();
43
+ export function layoutInfoToStyle(layoutInfo: LayoutInfo, dir: Direction, parent?: LayoutInfo | null): CSSProperties {
44
+ let xProperty = dir === 'rtl' ? 'right' : 'left';
45
+ let cached = cache.get(layoutInfo);
46
+ if (cached && cached[xProperty] != null) {
47
+ if (!parent) {
48
+ return cached;
49
+ }
50
+
51
+ // Invalidate if the parent position changed.
52
+ let top = layoutInfo.rect.y - parent.rect.y;
53
+ let x = layoutInfo.rect.x - parent.rect.x;
54
+ if (cached.top === top && cached[xProperty] === x) {
55
+ return cached;
56
+ }
57
+ }
58
+
59
+ let rectStyles = {
60
+ top: layoutInfo.rect.y - (parent ? parent.rect.y : 0),
61
+ [xProperty]: layoutInfo.rect.x - (parent ? parent.rect.x : 0),
62
+ width: layoutInfo.rect.width,
63
+ height: layoutInfo.rect.height
64
+ };
65
+
66
+ // Get rid of any non finite values since they aren't valid css values
67
+ Object.entries(rectStyles).forEach(([key, value]) => {
68
+ if (!Number.isFinite(value)) {
69
+ rectStyles[key] = undefined;
70
+ }
71
+ });
72
+
73
+ let style: CSSProperties = {
74
+ position: layoutInfo.isSticky ? 'sticky' : 'absolute',
75
+ // Sticky elements are positioned in normal document flow. Display inline-block so that they don't push other sticky columns onto the following rows.
76
+ display: layoutInfo.isSticky ? 'inline-block' : undefined,
77
+ overflow: layoutInfo.allowOverflow ? 'visible' : 'hidden',
78
+ opacity: layoutInfo.opacity,
79
+ zIndex: layoutInfo.zIndex,
80
+ transform: layoutInfo.transform,
81
+ contain: 'size layout style',
82
+ ...rectStyles
83
+ };
84
+
85
+ cache.set(layoutInfo, style);
86
+ return style;
87
+ }
package/src/index.ts ADDED
@@ -0,0 +1,19 @@
1
+ /*
2
+ * Copyright 2020 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
+ export type {RTLOffsetType} from './utils';
14
+ export type {VirtualizerItemOptions} from './useVirtualizerItem';
15
+ export {Virtualizer} from './Virtualizer';
16
+ export {useVirtualizerItem} from './useVirtualizerItem';
17
+ export {VirtualizerItem, layoutInfoToStyle} from './VirtualizerItem';
18
+ export {ScrollView, useScrollView} from './ScrollView';
19
+ export {getRTLOffsetType, getScrollLeft, setScrollLeft} from './utils';