UNPKG

masonic

Version:

<hr> <div align="center"> <h1 align="center"> 🧱 masonic </h1>

3 lines (2 loc) • 11.9 kB
function e(e){var t=e.high;e.L===O&&e.R===O?e.max=t:e.L===O?e.max=Math.max(e.R.max,t):e.R===O?e.max=Math.max(e.L.max,t):e.max=Math.max(Math.max(e.L.max,e.R.max),t)}function t(t){for(var r=t;r.P!==O;)e(r.P),r=r.P}function r(t,r){if(r.R!==O){var i=r.R;r.R=i.L,i.L!==O&&(i.L.P=r),i.P=r.P,r.P===O?t.root=i:r===r.P.L?r.P.L=i:r.P.R=i,i.L=r,r.P=i,e(r),e(i)}}function i(t,r){if(r.L!==O){var i=r.L;r.L=i.R,i.R!==O&&(i.R.P=r),i.P=r.P,r.P===O?t.root=i:r===r.P.R?r.P.R=i:r.P.L=i,i.R=r,r.P=i,e(r),e(i)}}function o(e,t,r){t.P===O?e.root=r:t===t.P.L?t.P.L=r:t.P.R=r,r.P=t.P}function n(){var n={root:O,size:0},a={};return{insert(o,s,l){for(var u=n.root,f=O;u!==O&&o!==(f=u).low;)u=o<u.low?u.L:u.R;if(o===f.low&&f!==O){if(!function(e,t,r){for(var i,o=e.list;o;){if(o.index===r)return 0;if(t>o.high)break;i=o,o=o.next}return i||(e.list={index:r,high:t,next:o}),i&&(i.next={index:r,high:t,next:i.next}),1}(f,s,l))return;return f.high=Math.max(f.high,s),e(f),t(f),a[l]=f,void n.size++}var h={low:o,high:s,max:s,C:0,P:f,L:O,R:O,list:{index:l,high:s,next:null}};f===O?n.root=h:(h.low<f.low?f.L=h:f.R=h,t(h)),function(e,t){for(var o;0===t.P.C;)t.P===t.P.P.L?0===(o=t.P.P.R).C?(t.P.C=1,o.C=1,t.P.P.C=0,t=t.P.P):(t===t.P.R&&r(e,t=t.P),t.P.C=1,t.P.P.C=0,i(e,t.P.P)):0===(o=t.P.P.L).C?(t.P.C=1,o.C=1,t.P.P.C=0,t=t.P.P):(t===t.P.L&&i(e,t=t.P),t.P.C=1,t.P.P.C=0,r(e,t.P.P));e.root.C=1}(n,h),a[l]=h,n.size++},remove(s){var l=a[s];if(void 0!==l){delete a[s];var u=function(e,t){var r=e.list;if(r.index===t)return null===r.next?0:(e.list=r.next,1);var i=r;for(r=r.next;null!==r;){if(r.index===t)return i.next=r.next,1;i=r,r=r.next}}(l,s);if(void 0!==u){if(1===u)return l.high=l.list.high,e(l),t(l),void n.size--;var f,h=l,v=h.C;l.L===O?(f=l.R,o(n,l,l.R)):l.R===O?(f=l.L,o(n,l,l.L)):(v=(h=function(e){for(;e.L!==O;)e=e.L;return e}(l.R)).C,f=h.R,h.P===l?f.P=h:(o(n,h,h.R),h.R=l.R,h.R.P=h),o(n,l,h),h.L=l.L,h.L.P=h,h.C=l.C),e(f),t(f),1===v&&function(e,t){for(var o;t!==O&&1===t.C;)t===t.P.L?(0===(o=t.P.R).C&&(o.C=1,t.P.C=0,r(e,t.P),o=t.P.R),1===o.L.C&&1===o.R.C?(o.C=0,t=t.P):(1===o.R.C&&(o.L.C=1,o.C=0,i(e,o),o=t.P.R),o.C=t.P.C,t.P.C=1,o.R.C=1,r(e,t.P),t=e.root)):(0===(o=t.P.L).C&&(o.C=1,t.P.C=0,i(e,t.P),o=t.P.L),1===o.R.C&&1===o.L.C?(o.C=0,t=t.P):(1===o.L.C&&(o.R.C=1,o.C=0,r(e,o),o=t.P.L),o.C=t.P.C,t.P.C=1,o.L.C=1,i(e,t.P),t=e.root));t.C=1}(n,f),n.size--}}},search(e,t,r){for(var i=[n.root];0!==i.length;){var o=i.pop();if(o!==O&&e<=o.max&&(o.L!==O&&i.push(o.L),o.R!==O&&i.push(o.R),o.low<=t&&o.high>=e))for(var a=o.list;null!==a;)a.high<e||r(a.index,o.low),a=a.next}},get size(){return n.size}}}function a(){return(a=Object.assign||function(e){for(var t=1;arguments.length>t;t++){var r=arguments[t];for(var i in r)Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i])}return e}).apply(this,arguments)}function s(){var e=C.useState(S)[1];return C.useRef(()=>e({})).current}function l(e){var t,{positioner:r,resizeObserver:i,items:o,as:n="div",id:a,className:l,style:f,role:h="grid",tabIndex:v=0,containerRef:c,itemAs:d="div",itemStyle:m,itemHeightEstimate:p=300,itemKey:g=u,overscanBy:P=2,scrollTop:x,isScrolling:R,height:y,render:w,onRender:M}=e,b=0,z=s(),T=F(r,i),k=o.length,{columnWidth:I,columnCount:E,range:W,estimateHeight:O,size:H,shortestColumn:S}=r,N=H(),D=S(),J=[],Q="list"===h?"listitem":"grid"===h?"gridcell":void 0,U=L(M),V=x+(P*=y),X=V>D&&k>N;if(W(Math.max(0,x-P/2),V,(e,r,i)=>{var n=o[e],a=g(n,e),s={top:i,left:r,width:I,writingMode:"horizontal-tb",position:"absolute"};J.push(j(d,{key:a,ref:T(e),role:Q,style:"object"==typeof m&&null!==m?Object.assign({},s,m):s},B(w,e,n,I))),void 0===t?(b=e,t=e):(b=Math.min(b,e),t=Math.max(t,e))}),X)for(var Y=Math.min(k-N,Math.ceil((x+P-D)/p*E)),Z=N,$=q(I);N+Y>Z;Z++){var _=o[Z],ee=g(_,Z);J.push(j(d,{key:ee,ref:T(Z),role:Q,style:"object"==typeof m?Object.assign({},$,m):$},B(w,Z,_,I)))}C.useEffect(()=>{"function"==typeof U.current&&void 0!==t&&U.current(b,t,o),A="1"},[b,t,o,U]),C.useEffect(()=>{X&&z()},[X,r]);var te=G(R,O(k,p));return j(n,{ref:c,key:A,id:a,role:h,className:l,tabIndex:v,style:"object"==typeof f?K(te,f):te,children:J})}function u(e,t){return t}function f(e,t){void 0===e&&(e=0),void 0===t&&(t=12);var r=T(t),[i,o]=C.useState(0),n=C.useRef(0);return C.useEffect(()=>{1===n.current&&o(1);var e=0,r=b(()=>{e||o(0)},40+1e3/t);return n.current=1,()=>{e=1,z(r)}},[t,r]),{scrollTop:Math.max(0,r-e),isScrolling:i}}function h(e){var{scrollTop:t,isScrolling:r}=f(e.offset,e.scrollFps);return l({scrollTop:t,isScrolling:r,positioner:e.positioner,resizeObserver:e.resizeObserver,items:e.items,onRender:e.onRender,as:e.as,id:e.id,className:e.className,style:e.style,role:e.role,tabIndex:e.tabIndex,containerRef:e.containerRef,itemAs:e.itemAs,itemStyle:e.itemStyle,itemHeightEstimate:e.itemHeightEstimate,itemKey:e.itemKey,overscanBy:e.overscanBy,height:e.height,render:e.render})}function v(e,t){void 0===t&&(t=D);var[r,i]=C.useState({offset:0,width:0});return k(()=>{var{current:t}=e;if(null!==t){var o=0,n=t;do{o+=n.offsetTop||0,n=n.offsetParent}while(n);o===r.offset&&t.offsetWidth===r.width||i({offset:o,width:t.offsetWidth})}},t),r}function c(e,t){var{width:r,columnWidth:i=200,columnGutter:o=0,rowGutter:n,columnCount:a,maxColumnCount:s,maxColumnWidth:l}=e;void 0===t&&(t=V);var u=()=>{var[e,t]=U(r,i,o,a,s,l);return J(t,e,o,null!=n?n:o)},f=C.useRef();void 0===f.current&&(f.current=u());var h=C.useRef(t),v=[r,i,o,n,a,s,l],c=C.useRef(v),d=!v.every((e,t)=>c.current[t]===e);if(d||!t.every((e,t)=>h.current[t]===e)){var m=f.current,p=u();if(h.current=t,c.current=v,d)for(var g=m.size(),P=0;g>P;P++){var x=m.get(P);p.set(P,void 0!==x?x.height:0)}f.current=p}return f.current}function d(e){function t(){return i.disconnect()}var r=s(),i=X(e,r);return C.useEffect(()=>t,[i]),i}function m(e){e.cancel()}function p(e,t){var r,{align:i="top",element:o="undefined"!=typeof window&&window,offset:n=0,height:a=("undefined"!=typeof window?window.innerHeight:0)}=t,s=L({positioner:e,element:o,align:i,offset:n,height:a}),l=C.useRef(()=>{var e=s.current.element;return e&&"current"in e?e.current:e}).current,[u,f]=C.useReducer((e,t)=>{var r,i={position:e.position,index:e.index,prevTop:e.prevTop};if("scrollToIndex"===t.type)return{position:s.current.positioner.get(null!==(r=t.value)&&void 0!==r?r:-1),index:t.value,prevTop:void 0};if("setPosition"===t.type)i.position=t.value;else if("setPrevTop"===t.type)i.prevTop=t.value;else if("reset"===t.type)return Y;return i},Y),h=W(f,15);E(l(),"scroll",()=>{if(!u.position&&u.index){var e=s.current.positioner.get(u.index);e&&f({type:"setPosition",value:e})}});var v=void 0!==u.index&&(null===(r=s.current.positioner.get(u.index))||void 0===r?void 0:r.top);return C.useEffect(()=>{var e=l();if(e){var{height:t,align:r,offset:i,positioner:o}=s.current;if(u.position){var n=u.position.top;"bottom"===r?n=n-t+u.position.height:"center"===r&&(n-=(t-u.position.height)/2),e.scrollTo(0,Math.max(0,n+=i));var a=0,v=setTimeout((function(){return!a&&f({type:"reset"})}),400);return function(){a=1,clearTimeout(v)}}if(void 0!==u.index){var c=o.shortestColumn()/o.size()*u.index;u.prevTop&&(c=Math.max(c,u.prevTop+t)),e.scrollTo(0,c),h({type:"setPrevTop",value:c})}}},[v,u,s,l,h]),C.useRef(e=>{f({type:"scrollToIndex",value:e})}).current}function g(e){var t=C.useRef(null),r=R({initialWidth:e.ssrWidth,initialHeight:e.ssrHeight}),i=v(t,r),o=Object.assign({offset:i.offset,width:i.width||r[0],height:r[1],containerRef:t},e);o.positioner=c(o),o.resizeObserver=d(o.positioner);var n=p(o.positioner,{height:o.height,offset:i.offset,align:"object"==typeof e.scrollToIndex?e.scrollToIndex.align:void 0}),a=e.scrollToIndex&&("number"==typeof e.scrollToIndex?e.scrollToIndex:e.scrollToIndex.index);return C.useEffect(()=>{void 0!==a&&n(a)},[a,n]),Z(h,o)}function P(e){return $(g,a({role:"list",rowGutter:e.rowGutter,columnCount:1,columnWidth:1},e))}function x(e,t){void 0===t&&(t=ee);var{isItemLoaded:r,minimumBatchSize:i=16,threshold:o=16,totalItems:n=9e9}=t,a=L(e),s=L(r);return C.useCallback((e,t,r)=>{for(var l=function(e,t,r,i,o,n){void 0===e&&(e=_),void 0===t&&(t=16),void 0===i&&(i=9e9);for(var a,s,l=[],u=o;n>=u;u++)e(u,r)?void 0!==a&&void 0!==s&&(l.push(a,s),a=s=void 0):(s=u,void 0===a&&(a=u));if(void 0!==a&&void 0!==s){var f=Math.min(Math.max(s,a+t-1),i-1);for(u=s+1;f>=u&&!e(u,r);u++)s=u;l.push(a,s)}if(l.length)for(var h=l[0],v=l[1];t>v-h+1&&h>0;){var c=h-1;if(e(c,r))break;l[0]=h=c}return l}(s.current,i,r,n,Math.max(0,e-o),Math.min(n-1,(t||0)+o)),u=0;u<l.length-1;++u)a.current(l[u],l[++u],r)},[n,i,o,a,s])}import*as C from"react";import{useWindowSize as R}from"@react-hook/window-size";import y from"@essentials/memoize-one";import w from"@essentials/one-key-map";import L from"@react-hook/latest";import M from"trie-memoize";import{requestTimeout as b,clearRequestTimeout as z}from"@essentials/request-timeout";import T from"@react-hook/window-scroll";import k from"@react-hook/passive-layout-effect";import I from"raf-schd";import E from"@react-hook/event";import{useThrottleCallback as W}from"@react-hook/throttle";var O={low:0,max:0,high:0,C:2,P:void 0,R:void 0,L:void 0,list:void 0};O.P=O,O.L=O,O.R=O;var H=new WeakMap,S={},j=C.createElement,A="0",B=M([w,{},WeakMap,w],(e,t,r,i)=>j(e,{index:t,data:r,width:i})),G=y((e,t)=>({position:"relative",width:"100%",maxWidth:"100%",height:Math.ceil(t),maxHeight:Math.ceil(t),willChange:e?"contents":void 0,pointerEvents:e?"none":void 0})),N=(e,t)=>e[0]===t[0]&&e[1]===t[1],K=y((e,t)=>Object.assign({},e,t),N),q=y(e=>({width:e,zIndex:-1e3,visibility:"hidden",position:"absolute",writingMode:"horizontal-tb"}),(e,t)=>e[0]===t[0]),F=y((e,t)=>r=>i=>{null!==i&&(t&&(t.observe(i),H.set(i,r)),void 0===e.get(r)&&e.set(r,i.offsetHeight))},N),D=[],J=function(e,t,r,i){void 0===r&&(r=0),void 0===i&&(i=r);for(var o=n(),a=new Array(e),s=[],l=new Array(e),u=0;e>u;u++)a[u]=0,l[u]=[];return{columnCount:e,columnWidth:t,set:function(e,n){void 0===n&&(n=0);for(var u=0,f=1;f<a.length;f++)a[f]<a[u]&&(u=f);var h=a[u]||0;a[u]=h+n+i,l[u].push(e),s[e]={left:u*(t+r),top:h,height:n,column:u},o.insert(h,h+n,e)},get:e=>s[e],update:t=>{for(var r=new Array(e),n=0,u=0;n<t.length-1;n++){var f=t[n],h=s[f];h.height=t[++n],o.remove(f),o.insert(h.top,h.top+h.height,f),r[h.column]=void 0===r[h.column]?f:Math.min(f,r[h.column])}for(n=0;n<r.length;n++)if(void 0!==r[n]){var v=l[n],c=Q(v,r[n]),d=l[n][c],m=s[d];for(a[n]=m.top+m.height+i,u=c+1;u<v.length;u++){var p=v[u],g=s[p];g.top=a[n],a[n]=g.top+g.height+i,o.remove(p),o.insert(g.top,g.top+g.height,p)}}},range:(e,t,r)=>o.search(e,t,(e,t)=>r(e,s[e].left,t)),estimateHeight:(t,r)=>{var i=Math.max(0,Math.max.apply(null,a));return t===o.size?i:i+Math.ceil((t-o.size)/e)*r},shortestColumn:()=>a.length>1?Math.min.apply(null,a):a[0]||0,size:()=>o.size,all:()=>s}},Q=(e,t)=>{for(var r=0,i=e.length-1;i>=r;){var o=r+i>>>1,n=e[o];if(n===t)return o;n>t?i=o-1:r=o+1}return-1},U=function(e,t,r,i,o,n){void 0===e&&(e=0),void 0===t&&(t=0),void 0===r&&(r=8),i=i||Math.min(Math.floor((e+r)/(t+r)),o||1/0)||1;var a=Math.floor((e-r*(i-1))/i);return void 0!==n&&a>n&&(a=n),[a,i]},V=[],X=M([WeakMap],(e,t)=>{var r=[],i=I(()=>{r.length>0&&(e.update(r),t(r)),r.length=0}),o=t=>{var o=t.offsetHeight;if(o>0){var n=H.get(t);if(void 0!==n){var a=e.get(n);void 0!==a&&o!==a.height&&r.push(n,o)}}i()},n=new Map,a=new ResizeObserver(e=>{for(var t=0;t<e.length;t++){var r=e[t],i=H.get(r.target);if(void 0!==i){var a=n.get(i);a||(a=I(o),n.set(i,a)),a(r.target)}}}),s=a.disconnect.bind(a);return a.disconnect=()=>{s(),n.forEach(m)},a}),Y={index:void 0,position:void 0,prevTop:void 0},Z=C.createElement,$=C.createElement,_=(e,t)=>void 0!==t[e],ee={};export{P as List,g as Masonry,h as MasonryScroller,n as createIntervalTree,J as createPositioner,X as createResizeObserver,v as useContainerPosition,x as useInfiniteLoader,l as useMasonry,c as usePositioner,d as useResizeObserver,p as useScrollToIndex,f as useScroller}; //# sourceMappingURL=index.mjs.map