UNPKG

@pastable/react

Version:
3 lines (2 loc) 6.17 kB
var t=require("jotai"),r=require("jotai/utils"),e=require("react"),n=require("@pastable/utils");function u(r,e){void 0===e&&(e=t.atom);var n=e(r);return t.atom(function(t){return t(n)},function(t,r,e){var u=null!=e?e:!t(n);r(n,u)})}function o(){return o=Object.assign||function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])}return t},o.apply(this,arguments)}function a(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e<r;e++)n[e]=t[e];return n}function i(t,r){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(e)return(e=e.call(t)).next.bind(e);if(Array.isArray(t)||(e=function(t,r){if(t){if("string"==typeof t)return a(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?a(t,r):void 0}}(t))||r&&t&&"number"==typeof t.length){e&&(t=e);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var c=["mousedown","touchstart"];function f(){var t=e.useRef(null);return e.useEffect(function(){return t.current=!0,function(){t.current=!1}},[]),t}function s(t,r,u){void 0===u&&(u=window);var o=e.useRef(),a=e.useRef(),i=f(),c=e.useRef(function(){i.current&&o.current.apply(null,arguments)});e.useEffect(function(){return r===o.current||(null==a.current||a.current(),o.current=r,o.current&&(a.current=n.on(u,t,c.current))),function(){return null==a.current?void 0:a.current()}},[r,u])}var l=function(){var t=e.useState({})[1];return e.useCallback(function(){return t({})},[])},p=function(){return n.isBrowser()?window.location:{search:{}}},d=function(t){var r=p(),e=new URLSearchParams(r.search),u=b(Object.fromEntries(e.entries()));return s("pushstate",l()),t?n.format(u,t):u},v=function(){},m=function(){return n.isBrowser()?window.history:{pushState:v,replaceState:v}},y=function(t){var r=void 0===t?{}:t,e=r.toPath,n=h(r.formater);return function(t,r,u){void 0===r&&(r="push");var o=m(),a=p().pathname,i=a.endsWith("/")?a.slice(0,-1):a,c=u||e,f=(c?"string"==typeof c?c:c(i):i)+n(t);"push"===r?o.pushState(t,"",f):o.replaceState(t,"",f)}},h=function(t){var r=d();return function(e){return g(o({},r,e),t)}},b=function(t){return n.format(t,decodeURIComponent)},g=function(t,r){return"?"+Object.entries(t).map(function(t){var e=t[0];return[e,w(t[1],r,e)]}).filter(function(t){return r=t[1],Array.isArray(r)?r.length:"object"==typeof r?r instanceof Date:n.isDefined(r);var r}).map(function(t){return t[0]+"="+decodeURIComponent(t[1])}).join("&")};function w(t,r,e){return(null==r?void 0:r(t,e))||t}function x(t){var r=window.history,e=r[t];r[t]=function(r){var n=e.apply(this,arguments),u=new Event(t.toLowerCase());return u.state=r,window.dispatchEvent(u),n}}n.isBrowser()&&(x("pushState"),x("replaceState"));var S=function(t,r){var n=f();return e.useEffect(function(){n.current&&t()},r),n.current};exports.atomWithToggle=u,exports.atomWithToggleAndStorage=function(t,e,n){return u(e,function(e){return r.atomWithStorage(t,e,n)})},exports.formatObjToQueryString=g,exports.getHistory=m,exports.getLocation=p,exports.useClickAway=function(t,r,u){void 0===u&&(u=c);var o=e.useRef(r);e.useEffect(function(){o.current=r},[r]),e.useEffect(function(){for(var r,e=function(r){t.current&&!t.current.contains(r.target)&&o.current(r)},a=i(u);!(r=a()).done;)n.on(document,r.value,e);return function(){for(var t,r=i(u);!(t=r()).done;)n.off(document,t.value,e)}},[u,t])},exports.useCurrentQueryParams=d,exports.useEvent=s,exports.useForceUpdate=l,exports.useIsMounted=function(){var t=e.useState(!1),r=t[0],n=t[1];return e.useEffect(function(){return n(!0),function(){return n(!1)}},[]),r},exports.useIsMountedRef=f,exports.useQueryParams=function(t){void 0===t&&(t={});var r=t.setterFormater,n=t.toPath,u=d(t.getterFormater),a=y({formater:r,toPath:n});return e.useEffect(function(){t.defaultValues&&a(o({},t.defaultValues,u))},[]),[u,a,function(t){return a(Object.fromEntries((t||Object.keys(u)).map(function(t){return[t,void 0]})))}]},exports.useQueryParamsMerger=h,exports.useQueryParamsState=function(t,r){void 0===r&&(r={});var e=r.setterFormater,u=r.toPath,o=r.defaultValue,a=d(r.getterFormater),i=y({formater:e,toPath:u}),c=a[t]||o;return[c,function(r){var e,u=n.isType(r,"function"==typeof r)?r(c):r;i(((e={})[t]=u,e))}]},exports.useSelection=function(t){var r=t.getId,u=t.max,a=t.initial,i=void 0===a?[]:a,c=t.sortBy,s=t.sortDirection,l=void 0===s?"asc":s,p=t.sortFn,d=t.updateFromInitial,v=t.onUpdate,m=e.useState(i),y=m[0],h=m[1],b=f(),g=e.useCallback(function(t){if(b.current&&(h(t),v)){var r="function"==typeof t?t(y):t;v(r)}},[v]),w=e.useCallback(function(t){var r=Array.isArray(t)?[].concat(t):o({},t);if(u){if(y.length>=u)return;Array.isArray(t)&&(r=t.slice(0,u-y.length))}g(y.concat(r))},[y,u]),x=e.useCallback(function(t){var r=t;"number"!=typeof t&&(r=j(t,!0));var e=[].concat(y);e.splice(r,1),g([].concat(e))},[y]),C=e.useCallback(function(){return g([])},[]),j=e.useCallback(function(t,e){return y[e?"findIndex":"find"](function(e){return r(e)===r(t)})},[y]),k=e.useCallback(function(t,e){return y[e?"findIndex":"find"](function(e){return r(e)===t})},[y]),A=e.useCallback(function(t){return void 0!==j(t)},[y]),E=function(t){g(function(r){var e=j(t,!0),n=[].concat(r);return n[e]=t,n})},I=e.useCallback(function(t){var r=A(t);return r?x(t):w(t),r},[y]),P=e.useCallback(function(t){g(function(r){return[].concat(r).sort(t)})},[]),O=e.useMemo(function(){return c?n.sortBy(y,c,l):p?[].concat(y).sort(p):y},[y,c,p]),R=e.useCallback(function(){return O},[O]),B=e.useMemo(function(){return i},[i]),F=e.useCallback(function(){return g(i)},[i]);return S(function(){d&&g(B)},[B,d]),[O,{get:R,set:g,clear:C,reset:F,add:w,remove:x,find:j,findById:k,has:A,toggle:I,update:E,upsert:function(t){return A(t)?E(t):w(t)},sortBy:P}]},exports.useSetQueryParams=y,exports.useUpdateEffect=S; //# sourceMappingURL=index.js.map