react-window 2.0.0-alpha.4 → 2.0.0-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/react-window.cjs +1 -1
- package/dist/react-window.d.ts +36 -14
- package/dist/react-window.js +355 -339
- package/docs/assets/{index-D8tyZCWm.js → index-DcPBnht2.js} +10 -10
- package/docs/generated/js-docs/Grid.json +27 -0
- package/docs/generated/js-docs/List.json +76 -49
- package/docs/index.html +1 -1
- package/docs/stats.html +1 -1
- package/package.json +1 -1
package/dist/react-window.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const $=require("react/jsx-runtime"),n=require("react"),P=typeof window<"u"?n.useLayoutEffect:n.useEffect;function C(e){if(e!==void 0)switch(typeof e){case"number":return e;case"string":{if(e.endsWith("px"))return parseFloat(e);break}}}function q({box:e,defaultHeight:s,defaultWidth:l,disabled:r,element:t,mode:f,style:c}){const{styleHeight:u,styleWidth:o}=n.useMemo(()=>({styleHeight:C(c?.height),styleWidth:C(c?.width)}),[c?.height,c?.width]),[a,v]=n.useState({height:s,width:l}),M=r||f==="only-height"&&u!==void 0||f==="only-width"&&o!==void 0||u!==void 0&&o!==void 0;return P(()=>{if(t===null||M)return;const O=new ResizeObserver(m=>{for(const I of m){const{contentRect:w,target:b}=I;t===b&&v(d=>d.height===w.height&&d.width===w.width?d:{height:w.height,width:w.width})}});return O.observe(t,{box:e}),()=>{O?.unobserve(t)}},[e,M,t,u,o]),n.useMemo(()=>({height:u??a.height,width:o??a.width}),[a,u,o])}function F(e){const s=n.useRef(()=>{throw new Error("Cannot call an event handler while rendering.")});return P(()=>{s.current=e},[e]),n.useCallback(l=>s.current?.(l),[s])}function L(e,s="Assertion error"){if(!e)throw console.error(s),Error(s)}function N({cachedBounds:e,itemCount:s,itemSize:l}){if(typeof l=="number")return s*l;if(e.size>0){const r=e.get(e.size-1);L(r!==void 0,"Unexpected bounds cache miss");const t=(r.scrollOffset+r.size)/e.size;return s*t}else return 0}function Y({align:e,cachedBounds:s,index:l,itemCount:r,containerScrollOffset:t,containerSize:f}){const c=s.get(r-1).scrollOffset,u=c+c-f,o=Math.max(0,s.get(l+1).scrollOffset-f),a=Math.min(c,s.get(l).scrollOffset);switch(e==="smart"&&(t>=o&&t<=a?e="auto":e="center"),e){case"start":return Math.min(u,a);case"end":return o;case"center":{const v=Math.round(o+(a-o)/2);return v<Math.ceil(f/2)?0:v>u?u:v}case"auto":default:return t>=o&&t<=a?t:t<o?o:a}}function A({cachedBounds:e,containerScrollOffset:s,containerSize:l,itemCount:r,overscanCount:t}){const f=r-1;let c=0,u=-1,o=0;for(;o<f;){const a=e.get(o);if(a.scrollOffset+a.size>s)break;o++}for(c=o,c=Math.max(0,c-t);o<f;){const a=e.get(o);if(a.scrollOffset+a.size>=s+l)break;o++}return u=Math.min(f,o),u=Math.min(r-1,u+t),c<0?[0,-1]:[c,u]}function _({itemCount:e,itemProps:s,itemSize:l}){const r=new Map;return{get(t){for(L(t<e,`Invalid index ${t}`);r.size-1<t;){const c=r.size,u=typeof l=="number"?l:l(c,s);if(c===0)r.set(c,{size:u,scrollOffset:0});else{const o=r.get(c-1);L(o!==void 0,`Unexpected bounds cache miss for index ${t}`),r.set(c,{scrollOffset:o.scrollOffset+o.size,size:u})}}const f=r.get(t);return L(f!==void 0,`Unexpected bounds cache miss for index ${t}`),f},set(t,f){r.set(t,f)},get size(){return r.size}}}function D({itemCount:e,itemProps:s,itemSize:l}){return n.useMemo(()=>_({itemCount:e,itemProps:s,itemSize:l}),[e,s,l])}function G({containerElement:e,containerStyle:s,defaultContainerSize:l=0,direction:r,itemCount:t,itemProps:f,itemSize:c,onResize:u,overscanCount:o}){const[a,v]=n.useState([0,-1]),[M,O]=[Math.min(t-1,a[0]),Math.min(t-1,a[1])],{height:m=l,width:I=l}=q({defaultHeight:r==="vertical"?l:void 0,defaultWidth:r==="horizontal"?l:void 0,element:e,mode:r==="vertical"?"only-height":"only-width",style:s}),w=n.useRef({height:0,width:0});n.useLayoutEffect(()=>{if(typeof u=="function"){const i=w.current;(i.height!==m||i.width!==I)&&(u({height:m,width:I},{...i}),i.height=m,i.width=I)}},[m,u,I]);const b=r==="vertical"?m:I,d=D({itemCount:t,itemProps:f,itemSize:c}),y=n.useCallback(i=>d.get(i),[d]),R=n.useCallback(()=>N({cachedBounds:d,itemCount:t,itemSize:c}),[d,t,c]),g=n.useCallback(i=>A({cachedBounds:d,containerScrollOffset:i,containerSize:b,itemCount:t,overscanCount:o}),[d,b,t,o]);P(()=>{const i=(r==="vertical"?e?.scrollTop:e?.scrollLeft)??0;v(g(i))},[e,r,g]);const h=F(({align:i="auto",behavior:T="auto",containerScrollOffset:x,index:p})=>{const k=Y({align:i,cachedBounds:d,containerScrollOffset:x,containerSize:b,index:p,itemCount:t});r==="horizontal"?e?.scrollTo({left:k,behavior:T||void 0}):e?.scrollTo({behavior:T||void 0,top:k})});return{getCellBounds:y,getEstimatedHeight:R,onScroll:()=>{const i=g((r==="vertical"?e?.scrollTop:e?.scrollLeft)??0);(i[0]!==M||i[1]!==O)&&v(i)},scrollToIndex:h,startIndex:M,stopIndex:O}}function B(e){return n.useMemo(()=>e,Object.values(e))}function W(e,s){if(e===s)return!0;if(!!e!=!!s||(L(e!==void 0),L(s!==void 0),Object.keys(e).length!==Object.keys(s).length))return!1;for(const l in e)if(!Object.is(s[l],e[l]))return!1;return!0}function U(e,s){const{style:l,...r}=e,{style:t,...f}=s;return W(l,t)&&W(r,f)}const V=()=>{};function J({cellComponent:e,cellProps:s,className:l,columnCount:r,columnWidth:t,defaultHeight:f=0,defaultWidth:c=0,gridRef:u,onCellsRendered:o,onResize:a,onScroll:v=V,overscanCount:M=3,rowCount:O,rowHeight:m,style:I,...w}){const b=B(s),d=n.useMemo(()=>n.memo(e,U),[e]),[y,R]=n.useState(null),g=G({containerElement:y,defaultContainerSize:c,direction:"horizontal",itemCount:r,itemProps:b,itemSize:t,onResize:a,overscanCount:M}),h=G({containerElement:y,defaultContainerSize:f,direction:"vertical",itemCount:O,itemProps:b,itemSize:m,onResize:a,overscanCount:M}),{startIndex:i,stopIndex:T}=g,{startIndex:x,stopIndex:p}=h;n.useImperativeHandle(u,()=>({get element(){return y},scrollToCell({behavior:S="auto",columnAlign:z="auto",columnIndex:E,rowAlign:H="auto",rowIndex:j}){h?.scrollToIndex({align:H,behavior:S,containerScrollOffset:y?.scrollTop??0,index:j}),g?.scrollToIndex({align:z,behavior:S,containerScrollOffset:y?.scrollLeft??0,index:E})},scrollToColumn({align:S="auto",behavior:z="auto",index:E}){g?.scrollToIndex({align:S,behavior:z,containerScrollOffset:y?.scrollLeft??0,index:E})},scrollToRow({align:S="auto",behavior:z="auto",index:E}){h?.scrollToIndex({align:S,behavior:z,containerScrollOffset:y?.scrollTop??0,index:E})}}),[g,y,h]),n.useEffect(()=>{i>=0&&T>=0&&x>=0&&p>=0&&o&&o({columnStartIndex:i,columnStopIndex:T,rowStartIndex:x,rowStopIndex:p})},[o,i,T,x,p]);const k=n.useMemo(()=>{const S=[];if(g&&r>0&&h&&O>0)for(let z=x;z<=p;z++){const E=h.getCellBounds(z);for(let H=i;H<=T;H++){const j=g.getCellBounds(H);S.push(n.createElement(d,{...b,columnIndex:H,key:`${z}-${H}`,rowIndex:z,style:{position:"absolute",left:0,transform:`translate(${j.scrollOffset}px, ${E.scrollOffset}px)`,height:E.size,width:j.size}}))}}return S},[d,b,r,i,T,g,O,x,p,h]);return $.jsx("div",{role:"grid",...w,className:l,onScroll:S=>{g.onScroll(),h.onScroll(),v(S)},ref:R,style:{...I,maxHeight:"100%",maxWidth:"100%",flexGrow:1,overflow:"auto"},children:$.jsx("div",{className:l,style:{position:"relative",height:h?.getEstimatedHeight(),width:g?.getEstimatedHeight()},children:k})})}const K=n.useState,Q=n.useRef;function X({className:e,defaultHeight:s=0,listRef:l,onResize:r,onRowsRendered:t,onScroll:f=V,overscanCount:c=3,rowComponent:u,rowCount:o,rowHeight:a,rowProps:v,style:M,...O}){const m=B(v),I=n.useMemo(()=>n.memo(u,U),[u]),[w,b]=n.useState(null),{getCellBounds:d,getEstimatedHeight:y,onScroll:R,scrollToIndex:g,startIndex:h,stopIndex:i}=G({containerElement:w,defaultContainerSize:s,direction:"vertical",itemCount:o,itemProps:m,itemSize:a,onResize:r,overscanCount:c});n.useImperativeHandle(l,()=>({get element(){return w},scrollToRow({align:x="auto",behavior:p="auto",index:k}){g({align:x,behavior:p,containerScrollOffset:w?.scrollTop??0,index:k})}}),[w,g]),n.useEffect(()=>{h>=0&&i>=0&&t&&t({startIndex:h,stopIndex:i})},[t,h,i]);const T=n.useMemo(()=>{const x=[];if(o>0)for(let p=h;p<=i;p++){const k=d(p);x.push(n.createElement(I,{...m,key:p,index:p,style:{position:"absolute",left:0,transform:`translateY(${k.scrollOffset}px)`,height:k.size,width:"100%"}}))}return x},[I,d,o,m,h,i]);return $.jsx("div",{role:"list",...O,className:e,onScroll:x=>{R(),f(x)},ref:b,style:{...M,maxHeight:"100%",flexGrow:1,overflowY:"auto"},children:$.jsx("div",{className:e,style:{height:y(),position:"relative",width:"100%"},children:T})})}const Z=n.useState,ee=n.useRef;exports.Grid=J;exports.List=X;exports.useGridCallbackRef=K;exports.useGridRef=Q;exports.useListCallbackRef=Z;exports.useListRef=ee;
|
package/dist/react-window.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ declare type ForbiddenKeys = "columnIndex" | "rowIndex" | "style";
|
|
|
25
25
|
|
|
26
26
|
declare type ForbiddenKeys_2 = "index" | "style";
|
|
27
27
|
|
|
28
|
-
export declare function Grid<CellProps extends object>({ cellComponent: CellComponentProp, cellProps: cellPropsUnstable, className, columnCount, columnWidth, defaultHeight, defaultWidth, gridRef, onCellsRendered, onScroll: onScrollProp, overscanCount, rowCount, rowHeight, style, ...rest }: GridProps<CellProps>): JSX.Element;
|
|
28
|
+
export declare function Grid<CellProps extends object>({ cellComponent: CellComponentProp, cellProps: cellPropsUnstable, className, columnCount, columnWidth, defaultHeight, defaultWidth, gridRef, onCellsRendered, onResize, onScroll: onScrollProp, overscanCount, rowCount, rowHeight, style, ...rest }: GridProps<CellProps>): JSX.Element;
|
|
29
29
|
|
|
30
30
|
export declare type GridImperativeAPI = {
|
|
31
31
|
get element(): HTMLDivElement | null;
|
|
@@ -107,6 +107,17 @@ export declare type GridProps<CellProps extends object> = HTMLAttributes<HTMLDiv
|
|
|
107
107
|
rowStartIndex: number;
|
|
108
108
|
rowStopIndex: number;
|
|
109
109
|
}) => void;
|
|
110
|
+
/**
|
|
111
|
+
* Callback notified when the Grid's outermost HTMLElement resizes.
|
|
112
|
+
* This may be used to (re)scroll a cell into view.
|
|
113
|
+
*/
|
|
114
|
+
onResize?: (size: {
|
|
115
|
+
height: number;
|
|
116
|
+
width: number;
|
|
117
|
+
}, prevSize: {
|
|
118
|
+
height: number;
|
|
119
|
+
width: number;
|
|
120
|
+
}) => void;
|
|
110
121
|
/**
|
|
111
122
|
* How many additional rows/columns to render outside of the visible area.
|
|
112
123
|
* This can reduce visual flickering near the edges of a grid when scrolling.
|
|
@@ -127,7 +138,7 @@ export declare type GridProps<CellProps extends object> = HTMLAttributes<HTMLDiv
|
|
|
127
138
|
style?: CSSProperties;
|
|
128
139
|
};
|
|
129
140
|
|
|
130
|
-
export declare function List<RowProps extends object>({ className, defaultHeight, listRef, onRowsRendered, onScroll: onScrollProp, overscanCount, rowComponent: RowComponentProp, rowCount, rowHeight, rowProps: rowPropsUnstable, style, ...rest }: ListProps<RowProps>): JSX.Element;
|
|
141
|
+
export declare function List<RowProps extends object>({ className, defaultHeight, listRef, onResize, onRowsRendered, onScroll: onScrollProp, overscanCount, rowComponent: RowComponentProp, rowCount, rowHeight, rowProps: rowPropsUnstable, style, ...rest }: ListProps<RowProps>): JSX.Element;
|
|
131
142
|
|
|
132
143
|
export declare type ListImperativeAPI = {
|
|
133
144
|
get element(): HTMLDivElement | null;
|
|
@@ -156,6 +167,29 @@ export declare type ListProps<RowProps extends object> = HTMLAttributes<HTMLDivE
|
|
|
156
167
|
* ⚠️ The `useListRef` and `useListCallbackRef` hooks are exported for convenience use in TypeScript projects.
|
|
157
168
|
*/
|
|
158
169
|
listRef?: Ref<ListImperativeAPI>;
|
|
170
|
+
/**
|
|
171
|
+
* Callback notified when the List's outermost HTMLElement resizes.
|
|
172
|
+
* This may be used to (re)scroll a row into view.
|
|
173
|
+
*/
|
|
174
|
+
onResize?: (size: {
|
|
175
|
+
height: number;
|
|
176
|
+
width: number;
|
|
177
|
+
}, prevSize: {
|
|
178
|
+
height: number;
|
|
179
|
+
width: number;
|
|
180
|
+
}) => void;
|
|
181
|
+
/**
|
|
182
|
+
* Callback notified when the range of visible rows changes.
|
|
183
|
+
*/
|
|
184
|
+
onRowsRendered?: (args: {
|
|
185
|
+
startIndex: number;
|
|
186
|
+
stopIndex: number;
|
|
187
|
+
}) => void;
|
|
188
|
+
/**
|
|
189
|
+
* How many additional rows to render outside of the visible area.
|
|
190
|
+
* This can reduce visual flickering near the edges of a list when scrolling.
|
|
191
|
+
*/
|
|
192
|
+
overscanCount?: number;
|
|
159
193
|
/**
|
|
160
194
|
* React component responsible for rendering a row.
|
|
161
195
|
*
|
|
@@ -182,18 +216,6 @@ export declare type ListProps<RowProps extends object> = HTMLAttributes<HTMLDivE
|
|
|
182
216
|
* ⚠️ This object must not contain either an `index` or `style` prop.
|
|
183
217
|
*/
|
|
184
218
|
rowProps: ExcludeForbiddenKeys_2<RowProps>;
|
|
185
|
-
/**
|
|
186
|
-
* Callback notified when the range of visible rows changes.
|
|
187
|
-
*/
|
|
188
|
-
onRowsRendered?: (args: {
|
|
189
|
-
startIndex: number;
|
|
190
|
-
stopIndex: number;
|
|
191
|
-
}) => void;
|
|
192
|
-
/**
|
|
193
|
-
* How many additional rows to render outside of the visible area.
|
|
194
|
-
* This can reduce visual flickering near the edges of a list when scrolling.
|
|
195
|
-
*/
|
|
196
|
-
overscanCount?: number;
|
|
197
219
|
/**
|
|
198
220
|
* Optional CSS properties.
|
|
199
221
|
* The list of rows will fill the height defined by this style.
|