UNPKG

@fewings/react

Version:
2 lines (1 loc) 3.86 kB
import{useEffect as N,useMemo as j,useRef as q}from"react";var se=e=>{let t=q(e);return N(()=>{t.current=e}),j(()=>(...n)=>t.current?.(...n),[])};import{useEffect as T,useRef as S}from"react";var le=(e,t)=>{let n=S(null),o=S(void 0);return T(()=>{o.current=e}),T(()=>{let r=s=>{n.current&&(n.current.contains(s.target)||t?.current&&t.current.contains(s.target)||o.current?.(s))};return document.addEventListener("mousedown",r),()=>{document.removeEventListener("mousedown",r)}},[]),n};import{useState as X}from"react";import{useCallbackRef as E}from"@fewings/react/hooks";var pe=({value:e,defaultValue:t,onChange:n})=>{let[o,r]=X(t),s=e!==void 0,u=s?e:o,a=E(n),f=E(c=>{let h=typeof c=="function"?c(e):c;s?a(h):r(c)});return[u,f]};import{useEffect as b,useRef as x,useState as Y}from"react";import{useControlledState as C}from"@fewings/react/hooks";var _={position:"absolute",top:0,left:0},xe=e=>{let t=e?.dir||"xy",n=x({x:null,y:null,left:null,top:null}),o=x(null),r=x(null),[s=_,u]=C({value:e?.style,defaultValue:e?.defaultStyle,onChange:e?.onChangeStyle}),[a,f]=Y({minLeft:-1/0,maxLeft:1/0,maxTop:1/0,minTop:-1/0}),c=()=>{let i=r.current?.getBoundingClientRect(),m=i?0:-1/0,d=i?i.width:1/0,p=i?0:-1/0,l=i?i.height:1/0;return f({minLeft:m,maxLeft:d,minTop:p,maxTop:l}),{minLeft:m,maxLeft:d,minTop:p,maxTop:l}},[h=!1,v]=C({value:e?.isDragging,onChange:e?.onChangeDragging});return b(()=>{requestAnimationFrame(c)},[]),b(()=>{let i=o.current;if(!i)return;let m=l=>{v(!0);let g=getComputedStyle(i);n.current={x:l.clientX,y:l.clientY,left:parseFloat(g.left),top:parseFloat(g.top)}},d=()=>{v(!1),n.current={x:null,y:null,left:null,top:null}},p=l=>{let{x:g,y:L,left:R,top:M}=n.current;if(g==null)return;let{minLeft:w,maxLeft:D,minTop:I,maxTop:P}=c(),U=t.includes("x"),k=t.includes("y"),A=U?l.clientX-g:0,O=k?l.clientY-L:0,F=Math.max(w,Math.min(D,R+A)),G=Math.max(I,Math.min(P,M+O));u(H=>({...H,top:G,left:F}))};return i.addEventListener("pointerdown",m),window.addEventListener("pointerup",d),window.addEventListener("pointermove",p),()=>{i.removeEventListener("pointerdown",m),window.removeEventListener("pointerup",d),window.removeEventListener("pointermove",p)}},[t]),{handleRef:o,boundRef:r,style:s,isDragging:h,bounds:a}};import{useEffect as z,useRef as J}from"react";var Se=(e,t,n)=>{let o=J(null);z(()=>{let r,s=()=>{if(e.current){let u=e.current.getBoundingClientRect(),a=o.current;(!a||a.top!==u.top||a.left!==u.left||a.width!==u.width||a.height!==u.height)&&(o.current=u,t(u))}n&&(r=requestAnimationFrame(s))};return n&&(r=requestAnimationFrame(s)),()=>cancelAnimationFrame(r)},[e,t,n])};import{useCallback as K,useState as Q}from"react";var Ce=()=>{let[,e]=Q({});return K(()=>e({}),[])};import{useEffect as W}from"react";import{useCallbackRef as Z}from"@fewings/react/hooks";var we=(e,t)=>{let n=Z(e);W(()=>{let o=setInterval(n,t);return()=>clearInterval(o)},[t])};import{useLayoutEffect as $,useEffect as B}from"react";var Pe=typeof window<"u"?$:B;import{useEffect as V,useState as ee}from"react";var Ae=()=>{let[e,t]=ee(!1);return V(()=>{t(!0)},[]),e};import{useMemo as y}from"react";var Ge=({currentPage:e,totalPages:t,maxVisiblePageButtons:n})=>{let o=y(()=>Math.floor((e-1)/n),[e,n]),r=y(()=>Math.ceil(t/n),[t,n]),s=y(()=>o===r-1,[o,r]);return{pageNumbers:y(()=>Array.from({length:s?t-o*n:n},(a,f)=>o*n+f+1),[s,t,o,n]),currentGroupIdx:o,totalGroupLength:r,isLastGroup:s}};import{useEffect as te,useRef as ne}from"react";var je=e=>{let t=ne(void 0);return te(()=>{t.current=e}),t.current};import{useState as oe}from"react";var Ye=()=>{let[e,t]=oe();return o=>{t(()=>{throw o})}};export{se as useCallbackRef,le as useClickOutside,pe as useControlledState,xe as useDragPosition,Se as useElementPositionObserver,Ce as useForceUpdate,we as useInterval,Pe as useIsomorphicLayoutEffect,Ae as useMount,Ge as usePagination,je as usePrevState,Ye as useThrow};