react-window 2.0.0-alpha.1 → 2.0.0-alpha.3
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/LICENSE.md +21 -0
- package/dist/react-window.cjs +1 -1
- package/dist/react-window.d.ts +23 -8
- package/dist/react-window.js +242 -230
- package/docs/assets/index-C0dH_qJq.js +67 -0
- package/docs/assets/index-DuD8RcK_.css +1 -0
- package/docs/generated/code-snippets/CellComponent.json +2 -2
- package/docs/generated/code-snippets/FixedHeightList.json +2 -2
- package/docs/generated/code-snippets/FixedHeightRowComponent.json +2 -2
- package/docs/generated/code-snippets/FlexboxLayout.json +2 -2
- package/docs/generated/code-snippets/Grid.json +2 -2
- package/docs/generated/code-snippets/ListVariableRowHeights.json +2 -2
- package/docs/generated/code-snippets/columnWidth.json +2 -2
- package/docs/generated/code-snippets/gridRefClickEventHandler.json +2 -2
- package/docs/generated/code-snippets/listRefClickEventHandler.json +2 -2
- package/docs/generated/code-snippets/rowHeight.json +2 -2
- package/docs/generated/code-snippets/useGridCallbackRef.json +4 -0
- package/docs/generated/code-snippets/useGridRef.json +2 -2
- package/docs/generated/code-snippets/useGridRefImport.json +1 -1
- package/docs/generated/code-snippets/useListCallbackRef.json +4 -0
- package/docs/generated/code-snippets/useListRef.json +2 -2
- package/docs/generated/code-snippets/useListRefImport.json +1 -1
- package/docs/generated/js-docs/Grid.json +44 -28
- package/docs/index.html +2 -2
- package/docs/stats.html +1 -1
- package/package.json +1 -1
- package/docs/assets/index-B8YZlUKM.js +0 -67
- package/docs/assets/index-BMf3NlnJ.css +0 -1
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Brian Vaughn
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
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 j=require("react/jsx-runtime"),n=require("react"),W=typeof window<"u"?n.useLayoutEffect:n.useEffect;function G(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:r,defaultWidth:l,disabled:s,element:t,mode:f,style:i}){const{styleHeight:u,styleWidth:o}=n.useMemo(()=>({styleHeight:G(i?.height),styleWidth:G(i?.width)}),[i?.height,i?.width]),[a,w]=n.useState({height:r,width:l}),v=s||f==="only-height"&&u!==void 0||f==="only-width"&&o!==void 0||u!==void 0&&o!==void 0;return W(()=>{if(t===null||v)return;const O=new ResizeObserver(M=>{for(const I of M){const{contentRect:d,target:S}=I;t===S&&w(h=>h.height===d.height&&h.width===d.width?h:{height:d.height,width:d.width})}});return O.observe(t,{box:e}),()=>{O?.unobserve(t)}},[e,v,t,u,o]),n.useMemo(()=>({height:u??a.height,width:o??a.width}),[a,u,o])}function R(e,r="Assertion error"){if(!e)throw console.error(r),Error(r)}function F({cachedBounds:e,itemCount:r,itemSize:l}){if(typeof l=="number")return r*l;if(e.size>0){const s=e.get(e.size-1);R(s!==void 0,"Unexpected bounds cache miss");const t=(s.scrollOffset+s.size)/e.size;return r*t}else return 0}function C({align:e,cachedBounds:r,index:l,itemCount:s,containerScrollOffset:t,containerSize:f}){const i=r.get(s-1).scrollOffset,u=i+i-f,o=Math.max(0,r.get(l+1).scrollOffset-f),a=Math.min(i,r.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 w=Math.round(o+(a-o)/2);return w<Math.ceil(f/2)?0:w>u?u:w}case"auto":default:return t>=o&&t<=a?t:t<o?o:a}}function N({cachedBounds:e,containerScrollOffset:r,containerSize:l,itemCount:s,overscanCount:t}){const f=s-1;let i=0,u=-1,o=0;for(;o<f;){const a=e.get(o);if(a.scrollOffset+a.size>r)break;o++}for(i=o,i=Math.max(0,i-t);o<f;){const a=e.get(o);if(a.scrollOffset+a.size>=r+l)break;o++}return u=Math.min(f,o),u=Math.min(s-1,u+t),i<0?[0,-1]:[i,u]}function Y({itemCount:e,itemProps:r,itemSize:l}){const s=new Map;return{get(t){for(R(t<e,`Invalid index ${t}`);s.size-1<t;){const i=s.size,u=typeof l=="number"?l:l(i,r);if(i===0)s.set(i,{size:u,scrollOffset:0});else{const o=s.get(i-1);R(o!==void 0,`Unexpected bounds cache miss for index ${t}`),s.set(i,{scrollOffset:o.scrollOffset+o.size,size:u})}}const f=s.get(t);return R(f!==void 0,`Unexpected bounds cache miss for index ${t}`),f},set(t,f){s.set(t,f)},get size(){return s.size}}}function A({itemCount:e,itemProps:r,itemSize:l}){return n.useMemo(()=>Y({itemCount:e,itemProps:r,itemSize:l}),[e,r,l])}function $({containerElement:e,containerStyle:r,defaultContainerSize:l=0,direction:s,itemCount:t,itemProps:f,itemSize:i,overscanCount:u}){const[o,a]=n.useState([0,-1]),[w,v]=[Math.min(t-1,o[0]),Math.min(t-1,o[1])],{height:O=l,width:M=l}=q({defaultHeight:s==="vertical"?l:void 0,defaultWidth:s==="horizontal"?l:void 0,element:e,mode:s==="vertical"?"only-height":"only-width",style:r}),I=s==="vertical"?O:M,d=A({itemCount:t,itemProps:f,itemSize:i}),S=n.useCallback(c=>d.get(c),[d]),h=n.useCallback(()=>F({cachedBounds:d,itemCount:t,itemSize:i}),[d,t,i]),k=n.useCallback(c=>N({cachedBounds:d,containerScrollOffset:c,containerSize:I,itemCount:t,overscanCount:u}),[d,I,t,u]);W(()=>{const c=(s==="vertical"?e?.scrollTop:e?.scrollLeft)??0;a(k(c))},[e,s,k]);const x=n.useCallback(({align:c="auto",behavior:p="auto",containerScrollOffset:z,index:m})=>{const g=C({align:c,cachedBounds:d,containerScrollOffset:z,containerSize:I,index:m,itemCount:t});s==="horizontal"?e?.scrollTo({left:g,behavior:p||void 0}):e?.scrollTo({behavior:p||void 0,top:g})},[d,e,I,s,t]);return{getCellBounds:S,getEstimatedHeight:h,onScroll:()=>{const c=k((s==="vertical"?e?.scrollTop:e?.scrollLeft)??0);(c[0]!==w||c[1]!==v)&&a(c)},scrollToIndex:x,startIndex:w,stopIndex:v}}function B(e){return n.useMemo(()=>e,Object.values(e))}function P(e,r){if(e===r)return!0;if(!!e!=!!r||(R(e!==void 0),R(r!==void 0),Object.keys(e).length!==Object.keys(r).length))return!1;for(const l in e)if(!Object.is(r[l],e[l]))return!1;return!0}function U(e,r){const{style:l,...s}=e,{style:t,...f}=r;return P(l,t)&&P(s,f)}const V=()=>{};function _({cellComponent:e,cellProps:r,className:l,columnCount:s,columnWidth:t,defaultHeight:f=0,defaultWidth:i=0,gridRef:u,onCellsRendered:o,onScroll:a=V,overscanCount:w=3,rowCount:v,rowHeight:O,style:M,...I}){const d=B(r),S=n.useMemo(()=>n.memo(e,U),[e]),[h,k]=n.useState(null),x=$({containerElement:h,defaultContainerSize:i,direction:"horizontal",itemCount:s,itemProps:d,itemSize:t,overscanCount:w}),c=$({containerElement:h,defaultContainerSize:f,direction:"vertical",itemCount:v,itemProps:d,itemSize:O,overscanCount:w}),{startIndex:p,stopIndex:z}=x,{startIndex:m,stopIndex:g}=c;n.useImperativeHandle(u,()=>({get element(){return h},scrollToCell({behavior:b="auto",columnAlign:y="auto",columnIndex:T,rowAlign:E="auto",rowIndex:L}){c?.scrollToIndex({align:E,behavior:b,containerScrollOffset:h?.scrollTop??0,index:L}),x?.scrollToIndex({align:y,behavior:b,containerScrollOffset:h?.scrollLeft??0,index:T})},scrollToColumn({align:b="auto",behavior:y="auto",index:T}){x?.scrollToIndex({align:b,behavior:y,containerScrollOffset:h?.scrollLeft??0,index:T})},scrollToRow({align:b="auto",behavior:y="auto",index:T}){c?.scrollToIndex({align:b,behavior:y,containerScrollOffset:h?.scrollTop??0,index:T})}}),[x,h,c]),n.useEffect(()=>{p>=0&&z>=0&&m>=0&&g>=0&&o&&o({columnStartIndex:p,columnStopIndex:z,rowStartIndex:m,rowStopIndex:g})},[o,p,z,m,g]);const H=n.useMemo(()=>{const b=[];if(x&&s>0&&c&&v>0)for(let y=m;y<=g;y++){const T=c.getCellBounds(y);for(let E=p;E<=z;E++){const L=x.getCellBounds(E);b.push(n.createElement(S,{...d,columnIndex:E,key:`${y}-${E}`,rowIndex:y,style:{position:"absolute",left:0,transform:`translate(${L.scrollOffset}px, ${T.scrollOffset}px)`,height:T.size,width:L.size}}))}}return b},[S,d,s,p,z,x,v,m,g,c]);return j.jsx("div",{role:"grid",...I,className:l,onScroll:b=>{x.onScroll(),c.onScroll(),a(b)},ref:k,style:{...M,maxHeight:"100%",maxWidth:"100%",flexGrow:1,overflow:"auto"},children:j.jsx("div",{className:l,style:{position:"relative",height:c?.getEstimatedHeight(),width:x?.getEstimatedHeight()},children:H})})}const D=n.useState,J=n.useRef;function K({className:e,defaultHeight:r=0,listRef:l,onRowsRendered:s,onScroll:t=V,overscanCount:f=3,rowComponent:i,rowCount:u,rowHeight:o,rowProps:a,style:w,...v}){const O=B(a),M=n.useMemo(()=>n.memo(i,U),[i]),[I,d]=n.useState(null),{getCellBounds:S,getEstimatedHeight:h,onScroll:k,scrollToIndex:x,startIndex:c,stopIndex:p}=$({containerElement:I,defaultContainerSize:r,direction:"vertical",itemCount:u,itemProps:O,itemSize:o,overscanCount:f});n.useImperativeHandle(l,()=>({get element(){return I},scrollToRow({align:m="auto",behavior:g="auto",index:H}){x({align:m,behavior:g,containerScrollOffset:I?.scrollTop??0,index:H})}}),[I,x]),n.useEffect(()=>{c>=0&&p>=0&&s&&s({startIndex:c,stopIndex:p})},[s,c,p]);const z=n.useMemo(()=>{const m=[];if(u>0)for(let g=c;g<=p;g++){const H=S(g);m.push(n.createElement(M,{...O,key:g,index:g,style:{position:"absolute",left:0,transform:`translateY(${H.scrollOffset}px)`,height:H.size,width:"100%"}}))}return m},[M,S,u,O,c,p]);return j.jsx("div",{role:"list",...v,className:e,onScroll:m=>{k(),t(m)},ref:d,style:{...w,maxHeight:"100%",flexGrow:1,overflowY:"auto"},children:j.jsx("div",{className:e,style:{height:h(),position:"relative",width:"100%"},children:z})})}const Q=n.useState,X=n.useRef;exports.Grid=_;exports.List=K;exports.useGridCallbackRef=D;exports.useGridRef=J;exports.useListCallbackRef=Q;exports.useListRef=X;
|
package/dist/react-window.d.ts
CHANGED
|
@@ -5,9 +5,14 @@ import { JSX } from 'react/jsx-runtime';
|
|
|
5
5
|
import { ReactNode } from 'react';
|
|
6
6
|
import { Ref } from 'react';
|
|
7
7
|
import { useRef } from 'react';
|
|
8
|
+
import { useState } from 'react';
|
|
8
9
|
|
|
9
10
|
export declare type Align = "auto" | "center" | "end" | "smart" | "start";
|
|
10
11
|
|
|
12
|
+
declare type CellComponent<CellProps extends object> = GridProps<CellProps>["cellComponent"];
|
|
13
|
+
|
|
14
|
+
export declare type CellComponentProps<CellProps extends object = object> = ComponentProps<CellComponent<CellProps>>;
|
|
15
|
+
|
|
11
16
|
declare type ExcludeForbiddenKeys<Type> = {
|
|
12
17
|
[Key in keyof Type]: Key extends ForbiddenKeys ? never : Type[Key];
|
|
13
18
|
};
|
|
@@ -20,11 +25,7 @@ declare type ForbiddenKeys = "columnIndex" | "rowIndex" | "style";
|
|
|
20
25
|
|
|
21
26
|
declare type ForbiddenKeys_2 = "index" | "style";
|
|
22
27
|
|
|
23
|
-
export declare function Grid<CellProps extends object>({ cellComponent: CellComponentProp, cellProps: cellPropsUnstable, className, columnCount, columnWidth, defaultHeight, defaultWidth, gridRef, onCellsRendered, overscanCount, rowCount, rowHeight, style, ...rest }: GridProps<CellProps>): JSX.Element;
|
|
24
|
-
|
|
25
|
-
declare type GridComponent<CellProps extends object> = GridProps<CellProps>["cellComponent"];
|
|
26
|
-
|
|
27
|
-
export declare type GridComponentProps<CellProps extends object = object> = ComponentProps<GridComponent<CellProps>>;
|
|
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
29
|
|
|
29
30
|
export declare type GridImperativeAPI = {
|
|
30
31
|
get element(): HTMLDivElement | null;
|
|
@@ -47,7 +48,7 @@ export declare type GridImperativeAPI = {
|
|
|
47
48
|
}): void;
|
|
48
49
|
};
|
|
49
50
|
|
|
50
|
-
export declare type GridProps<CellProps extends object> = {
|
|
51
|
+
export declare type GridProps<CellProps extends object> = HTMLAttributes<HTMLDivElement> & {
|
|
51
52
|
/**
|
|
52
53
|
* CSS class name.
|
|
53
54
|
*/
|
|
@@ -68,7 +69,7 @@ export declare type GridProps<CellProps extends object> = {
|
|
|
68
69
|
* This component will receive an `index` and `style` prop by default.
|
|
69
70
|
* Additionally it will receive prop values passed to `cellProps`.
|
|
70
71
|
*
|
|
71
|
-
* ⚠️ The prop types for this component are exported as `
|
|
72
|
+
* ⚠️ The prop types for this component are exported as `CellComponentProps`
|
|
72
73
|
*/
|
|
73
74
|
cellComponent: (props: {
|
|
74
75
|
columnIndex: number;
|
|
@@ -126,7 +127,7 @@ export declare type GridProps<CellProps extends object> = {
|
|
|
126
127
|
style?: CSSProperties;
|
|
127
128
|
};
|
|
128
129
|
|
|
129
|
-
export declare function List<RowProps extends object>({ className, defaultHeight, listRef, onRowsRendered, overscanCount, rowComponent: RowComponentProp, rowCount, rowHeight, rowProps: rowPropsUnstable, style, ...rest }: ListProps<RowProps>): JSX.Element;
|
|
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;
|
|
130
131
|
|
|
131
132
|
export declare type ListImperativeAPI = {
|
|
132
133
|
get element(): HTMLDivElement | null;
|
|
@@ -204,11 +205,25 @@ declare type RowComponent<RowProps extends object> = ListProps<RowProps>["rowCom
|
|
|
204
205
|
|
|
205
206
|
export declare type RowComponentProps<RowProps extends object = object> = ComponentProps<RowComponent<RowProps>>;
|
|
206
207
|
|
|
208
|
+
/**
|
|
209
|
+
* Convenience hook to return a properly typed ref callback for the Grid component.
|
|
210
|
+
*
|
|
211
|
+
* Use this hook when you need to share the ref with another component or hook.
|
|
212
|
+
*/
|
|
213
|
+
export declare const useGridCallbackRef: typeof useState<GridImperativeAPI | null>;
|
|
214
|
+
|
|
207
215
|
/**
|
|
208
216
|
* Convenience hook to return a properly typed ref for the Grid component.
|
|
209
217
|
*/
|
|
210
218
|
export declare const useGridRef: typeof useRef<GridImperativeAPI>;
|
|
211
219
|
|
|
220
|
+
/**
|
|
221
|
+
* Convenience hook to return a properly typed ref callback for the List component.
|
|
222
|
+
*
|
|
223
|
+
* Use this hook when you need to share the ref with another component or hook.
|
|
224
|
+
*/
|
|
225
|
+
export declare const useListCallbackRef: typeof useState<ListImperativeAPI | null>;
|
|
226
|
+
|
|
212
227
|
/**
|
|
213
228
|
* Convenience hook to return a properly typed ref for the List component.
|
|
214
229
|
*/
|