UNPKG

@pastable/react

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