virtua-restoration
Version:
<img src="./virtua-restoration.png" alt="Virtua Restoration" style="max-width: 1000px; width: 100%; display: block; margin: 0 auto 20px auto;">
82 lines (72 loc) • 3.85 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/components/VirtualizedList.tsx
var _react = require('react');
var _virtua = require('virtua');
// src/components/getCacheProvider.tsx
function getCacheProvider(cacheKey, cacheSource = "sessionStorage", customProvider) {
if (cacheSource === "custom" && customProvider) return customProvider;
const storage = cacheSource === "localStorage" ? window.localStorage : window.sessionStorage;
return {
get: () => {
const serialized = storage.getItem(cacheKey);
if (!serialized) return void 0;
try {
return JSON.parse(serialized);
} catch (e) {
console.error("Error parsing storage cache:", e);
return void 0;
}
},
set: (data) => {
storage.setItem(cacheKey, JSON.stringify(data));
}
};
}
// src/components/VirtualizedList.tsx
var _jsxruntime = require('react/jsx-runtime');
var VirtualizedList = ({ cacheKey, children, cacheSourceType = "sessionStorage", customProvider, className, style }) => {
const ref = _react.useRef.call(void 0, null);
const provider = _react.useMemo.call(void 0, () => getCacheProvider(cacheKey, cacheSourceType, customProvider), [cacheKey, cacheSourceType, customProvider]);
const [offset, cache] = _react.useMemo.call(void 0, () => {
return _nullishCoalesce(_optionalChain([provider, 'optionalAccess', _ => _.get, 'call', _2 => _2()]), () => ( []));
}, [provider]);
_react.useLayoutEffect.call(void 0, () => {
if (!ref.current) return;
const handle = ref.current;
if (offset) {
handle.scrollTo(offset);
}
return () => {
if (provider) {
provider.set([handle.scrollOffset, handle.cache]);
}
};
}, [offset, provider]);
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _virtua.VList, { ref, cache, className, style: _nullishCoalesce(style, () => ( { height: "100vh", overflow: "auto" })), children });
};
// src/components/WindowVirtualized.tsx
var WindowVirtualizedList = ({ cacheKey, children, cacheSourceType = "sessionStorage", customProvider }) => {
const ref = _react.useRef.call(void 0, null);
const provider = _react.useMemo.call(void 0, () => getCacheProvider(cacheKey, cacheSourceType, customProvider), [cacheKey, cacheSourceType, customProvider]);
const [offset, cache] = _react.useMemo.call(void 0, () => {
return _nullishCoalesce(_optionalChain([provider, 'optionalAccess', _3 => _3.get, 'call', _4 => _4()]), () => ( []));
}, [provider]);
_react.useLayoutEffect.call(void 0, () => {
if (!ref.current) return;
const handle = ref.current;
window.scrollTo(0, _nullishCoalesce(offset, () => ( 0)));
let scrollY = 0;
const onScroll = () => {
scrollY = window.scrollY;
};
window.addEventListener("scroll", onScroll);
onScroll();
return () => {
window.removeEventListener("scroll", onScroll);
if (provider) {
provider.set([scrollY, handle.cache]);
}
};
}, [offset, provider]);
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _virtua.WindowVirtualizer, { ref, cache, children });
};
exports.VirtualizedList = VirtualizedList; exports.WindowVirtualizedList = WindowVirtualizedList;