react-layout-virtual 0.0.4 → 0.0.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.
@@ -3,16 +3,11 @@
3
3
  * @license MIT
4
4
  * @author Alexandr Kalabin
5
5
  */
6
- import React from 'react';
7
- export interface ListItemProps<T = unknown> {
8
- data: T;
9
- ref: React.Ref<HTMLDivElement> | undefined;
10
- index: number;
11
- }
6
+ import { type ListItemProps } from './ReactRenderer';
12
7
  export interface VirtualizedListReactProps<T> {
13
- scrollerRef?: React.RefObject<HTMLDivElement>;
8
+ scrollerRef?: React.RefObject<HTMLElement>;
14
9
  overscanHeight?: number;
15
10
  data: T[];
16
11
  renderItem: (props: ListItemProps<T>) => React.ReactNode;
17
12
  }
18
- export default function VirtualizedListReact<T>(props: VirtualizedListReactProps<T>): import("react/jsx-runtime").JSX.Element;
13
+ export default function VirtualizedListReact<ItemData = unknown>(props: VirtualizedListReactProps<ItemData>): import("react/jsx-runtime").JSX.Element;
@@ -3,34 +3,38 @@
3
3
  * @license MIT
4
4
  * @author Alexandr Kalabin
5
5
  */
6
- import { ScrollableContainer } from "layout-virtual";
7
- import type { IRangeRenderer, ScrollDirection, IItemStore, IItem, VirtualScrollStructure } from "layout-virtual/types";
6
+ import { BaseRenderer } from 'layout-virtual';
7
+ import type { IRangeRenderer, ScrollDirection, VirtualScrollStructure } from "layout-virtual/types";
8
+ export interface ListItemProps<T = unknown> {
9
+ data: T;
10
+ ref: React.Ref<HTMLDivElement> | undefined;
11
+ index: number;
12
+ }
13
+ export type ItemRenderer<T> = React.FC<ListItemProps<T>>;
8
14
  type ReactRendererOptions = {
9
15
  itemsSetter: React.Dispatch<React.SetStateAction<React.ReactNode[]>>;
10
16
  } & VirtualScrollStructure;
11
17
  interface IReactRenderer {
12
18
  commit: () => void;
13
19
  }
14
- export default class ReactRenderer implements IRangeRenderer, IReactRenderer {
20
+ export default class ReactRenderer<DataType = unknown> extends BaseRenderer implements IRangeRenderer<DataType, ItemRenderer<DataType>>, IReactRenderer {
15
21
  private _store;
16
- private _scrollableContainer;
17
- private _renderedIndexRegistry;
18
- private _renderedItemsRegistry;
22
+ private _renderItem;
19
23
  private _itemsSetter;
20
24
  private _renderedRangeRefPool;
21
25
  private _listItems;
22
26
  private _flushItems;
23
- private _getRenderedBoundaryIndex;
24
27
  constructor(opts: ReactRendererOptions);
25
- render(startIndex: number, endIndex: number, direction: ScrollDirection): number;
26
28
  renderRange(startIndex: number, endIndex: number, direction: ScrollDirection): void;
27
- removeRange(startIndex: number, endIndex: number, direction: ScrollDirection): number;
29
+ removeRange(startIndex: number, endIndex: number, direction?: ScrollDirection): {
30
+ itemsToRemove: Element[];
31
+ removedHeight: number;
32
+ };
28
33
  clear(): void;
29
- getIndex(item: Element): number | undefined;
30
- getItem(index: number): Element | undefined;
31
- get scrollableContainer(): ScrollableContainer;
32
- attach(store: IItemStore<IItem>): void;
34
+ setData(store: DataType[]): void;
35
+ setRenderItem(renderItem: ItemRenderer<DataType>): void;
33
36
  flush(): Promise<void>;
34
37
  commit(): void;
38
+ get dataSize(): number;
35
39
  }
36
40
  export {};
package/dist/index.d.ts CHANGED
@@ -4,4 +4,4 @@
4
4
  * @author Alexandr Kalabin
5
5
  */
6
6
  export { default } from './ReactLayoutVirtual';
7
- export { type ListItemProps } from './ReactLayoutVirtual';
7
+ export { type ListItemProps } from './ReactRenderer';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{useRef as n,useState as s,useLayoutEffect as i,useEffect as o}from"react";import{flushSync as l,createPortal as h}from"react-dom";import a,{ScrollableContainer as d,ArrayItemStore as c,DynamicListLayout as u}from"layout-virtual";class f{t=null;i;o=/* @__PURE__ */new Map;l=/* @__PURE__ */new Map;h;u=/* @__PURE__ */new Map;m=[];v=()=>{this.h(this.m)};p(e){const t="first"===e?this.i.getFirstItem():"last"===e?this.i.getLastItem():null;if(t)return this.getIndex(t)}constructor(e){this.i=new d({...e}),this.h=e.itemsSetter}render(e,t,r){const n=this.p("first"),s=this.p("last");let i=e,o=t,l=n,h=s,a=0;return"down"===r?(console.log("SCROLLING DOWN"),void 0!==l&&void 0!==s&&(h=Math.min(i-1,s),i=Math.max(s+1,i),l<=h&&(a=this.removeRange(l,h,r)))):"up"===r&&(console.log("SCROLLING UP"),void 0!==h&&void 0!==n&&(l=Math.max(o+1,n),o=Math.min(n-1,o),l<=h&&(a=this.removeRange(l,h,r)))),i<o&&this.renderRange(i,o,r),a}renderRange(t,r,n){console.log("_renderRange",t,r,n),t>r&&console.error("_renderRange",t,r,n);const s=this.t,i=this.m,o=[],l=this.u;if(s){for(let n=t;n<=r;n++){const t=s.getByIndex(n);if(t){const r=t.render,s=l.get(n)||{current:null,idx:n};l.set(n,s),o.push(/* @__PURE__ */e(r,{data:t.data,ref:s,index:n},n))}}this.m="down"===n?i.concat(o):"up"===n?o.concat(i):this.m}}removeRange(e,t,r){const n=this.o,s=this.l;let i=0,o=1/0,l=0;for(let h=e;h<=t;h++){const e=s.get(h);if(e){const{offsetTop:t,offsetHeight:r}=e,a=getComputedStyle(e),d=parseFloat(a.marginTop),c=parseFloat(a.marginBottom);o=Math.min(o,t-d),l=Math.max(l,t+r+c),s.delete(h),n.delete(e),i++}}return i&&(this.m="down"===r?this.m.slice(i):"up"===r?this.m.slice(0,-i):this.m),console.log("_removeItems startIndex:",e,"endIndex:",t,"removedHeight:",l>o?l-o:0,"removedItemsCount:",i),l>o?l-o:0}clear(){this.o.clear(),this.l.clear(),this.m=[],this.h(this.m)}getIndex(e){return this.o.get(e)}getItem(e){return this.l.get(e)}get scrollableContainer(){return this.i}attach(e){this.t=e}flush(){return l(this.v),Promise.resolve()}commit(){const e=this.o,t=this.l,r=this.u;for(const n of r.values()){const{idx:r,current:s}=n;s&&(e.set(s,r),t.set(r,s))}r.clear()}}function m(l){const{overscanHeight:d=200,data:m,renderItem:v,scrollerRef:p}=l,g=n(null),I=p??g,w=n(null),R=n(null),_=n(null),x=n(null),M=n(null),C=n(null),[y,S]=s([]),H=n(void 0);i(()=>{H.current=new f({container:I.current,scrollHeightFiller:w.current,viewportContainer:R.current,scrollCanvas:_.current,topSpacer:x.current,contentLayer:M.current,bottomSpacer:C.current,itemsSetter:S});const e=new c,t=new u({overscanHeight:d,renderer:H.current}),r=new a({store:e,layout:t});for(let n=0;n<m.length;n++)r.insert({data:m[n],render:v},n)},[]),o(()=>{H.current?.commit()},[y]);const L=/* @__PURE__ */t(r,{children:[
2
- /* @__PURE__ */e("div",{ref:w}),
3
- /* @__PURE__ */e("div",{ref:R,children:/* @__PURE__ */t("div",{ref:_,children:[
4
- /* @__PURE__ */e("div",{ref:x}),
5
- /* @__PURE__ */e("div",{ref:M,children:y}),
6
- /* @__PURE__ */e("div",{ref:C})]})})]});return p&&p.current?h(L,p.current):/* @__PURE__ */e("div",{ref:I,children:L})}export{m as default};
1
+ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{useRef as n,useState as i,useLayoutEffect as s,useEffect as o}from"react";import{flushSync as l,createPortal as c}from"react-dom";import{BaseRenderer as h,DynamicListLayout as a,LayoutVirtual as d}from"layout-virtual";class u extends h{t=[];i=null;o;l=/* @__PURE__ */new Map;h=[];u=()=>{this.o(this.h)};constructor(e){super(e),this.o=e.itemsSetter}renderRange(t,r,n){const i=this.t,s=this.h,o=[],l=this.l;for(let c=t;c<=r;c++){const t=i[c];if(t){const r=this.i;if(r){const n=l.get(c)||{current:null,idx:c};l.set(c,n),o.push(/* @__PURE__ */e(r,{data:t,ref:n,index:c},c))}}}this.h="down"===n?s.concat(o):"up"===n?o.concat(s):this.h}removeRange(e,t,r){const n=super.removeRange(e,t),i=n.itemsToRemove.length;return i&&(this.h="down"===r?this.h.slice(i):"up"===r?this.h.slice(0,-i):this.h),n}clear(){super.clear(),this.h=[],this.o(this.h)}setData(e){this.t=e}setRenderItem(e){this.i=e}flush(){return l(this.u),Promise.resolve()}commit(){const e=this.l;for(const t of e.values()){const{idx:e,current:r}=t;r&&this.registerElement(e,r)}e.clear()}get dataSize(){return this.t.length}}function f(l){const{overscanHeight:h=200,data:f,renderItem:m,scrollerRef:p}=l,v=n(null),g=n(null),w=n(null),x=n(null),R=n(null),_=n(null),I=n(null),[S,y]=i([]),H=n(void 0);s(()=>{H.current=new u({container:p?.current||v.current,scrollHeightFiller:g.current,viewportContainer:w.current,scrollCanvas:x.current,topSpacer:R.current,contentLayer:_.current,bottomSpacer:I.current,itemsSetter:y});const e=new a({overscanHeight:h,renderer:H.current}),t=new d({layout:e});t.setData(f),t.setRenderItem(m)},[]),o(()=>{H.current?.commit()},[S]);const C=/* @__PURE__ */t(r,{children:[
2
+ /* @__PURE__ */e("div",{ref:g}),
3
+ /* @__PURE__ */e("div",{ref:w,children:/* @__PURE__ */t("div",{ref:x,children:[
4
+ /* @__PURE__ */e("div",{ref:R}),
5
+ /* @__PURE__ */e("div",{ref:_,children:S}),
6
+ /* @__PURE__ */e("div",{ref:I})]})})]});return p&&p.current?c(C,p.current):/* @__PURE__ */e("div",{ref:v,children:C})}export{f as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-layout-virtual",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "React virtual scrolling component for responsive lists and grids with dynamic item sizes.",
5
5
  "keywords": [
6
6
  "virtual",
@@ -43,7 +43,7 @@
43
43
  "prepublishOnly": "npm run build"
44
44
  },
45
45
  "dependencies": {
46
- "layout-virtual": "^0.0.3"
46
+ "layout-virtual": "^0.1.0"
47
47
  },
48
48
  "peerDependencies": {
49
49
  "react": "^19.2.5",