UNPKG

virtua-restoration

Version:

<img src="./virtua-restoration.png" alt="Virtua Restoration" style="max-width: 1000px; width: 100%; display: block; margin: 0 auto 20px auto;">

2 lines (1 loc) 1.45 kB
import{useLayoutEffect as m,useMemo as u,useRef as p}from"react";import{VList as g}from"virtua";function d(o,c="sessionStorage",t){if(c==="custom"&&t)return t;let i=c==="localStorage"?window.localStorage:window.sessionStorage;return{get:()=>{let e=i.getItem(o);if(e)try{return JSON.parse(e)}catch(r){console.error("Error parsing storage cache:",r);return}},set:e=>{i.setItem(o,JSON.stringify(e))}}}import{jsx as w}from"react/jsx-runtime";var C=({cacheKey:o,children:c,cacheSourceType:t="sessionStorage",customProvider:i,className:e,style:r})=>{let a=p(null),n=u(()=>d(o,t,i),[o,t,i]),[l,f]=u(()=>n?.get()??[],[n]);return m(()=>{if(!a.current)return;let s=a.current;return l&&s.scrollTo(l),()=>{n&&n.set([s.scrollOffset,s.cache])}},[l,n]),w(g,{ref:a,cache:f,className:e,style:r??{height:"100vh",overflow:"auto"},children:c})};import{useLayoutEffect as S,useMemo as h,useRef as v}from"react";import{WindowVirtualizer as P}from"virtua";import{jsx as V}from"react/jsx-runtime";var L=({cacheKey:o,children:c,cacheSourceType:t="sessionStorage",customProvider:i})=>{let e=v(null),r=h(()=>d(o,t,i),[o,t,i]),[a,n]=h(()=>r?.get()??[],[r]);return S(()=>{if(!e.current)return;let l=e.current;window.scrollTo(0,a??0);let f=0,s=()=>{f=window.scrollY};return window.addEventListener("scroll",s),s(),()=>{window.removeEventListener("scroll",s),r&&r.set([f,l.cache])}},[a,r]),V(P,{ref:e,cache:n,children:c})};export{C as VirtualizedList,L as WindowVirtualizedList};