@gadeoli/rjs-hooks-library 0.2.5 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,17 +4,19 @@ Some (almost all) hooks has been copied from internet foruns. If necessary look
4
4
 
5
5
  ## Hooks
6
6
 
7
+ - useCabinet (A boolean array control like a file cabinet)
7
8
  - useElementSize (Get element sizes using ref)
9
+ - useGhostInFirstRender (A negative boolean value that is setted positive after first render and a small delay)
8
10
  - useHover (control when a element is hovererd using ref)
11
+ - useImagePreloader
12
+ - useInViewPort
9
13
  - useOnClickOutside (Listen when clicked outside component using ref)
14
+ - useOnInfiniteScrollTrigger (Trigger a function when "reffed" element shows up)
15
+ - useOnPressKey (An event handler to trigger an action by key press using ref)
10
16
  - usePersistedState (An option to persist state in local storage)
11
- - usePrevious (Keep state value between current and previous render)
17
+ - usePrevious (Keep state value between current and previous render)
18
+ - useTimer (a count down minute / seconds)
12
19
  - useWindowSize (Get window sizes)
13
- - useTimer (a count down minute / seconds)
14
- - useClickOutside (An event handler to trigger an action by outside-click-element using ref)
15
- - useOnPressKey (An event handler to trigger an action by key press using ref)
16
- - useGhostInFirstRender (A negative boolean value that is setted positive after first render and a small delay)
17
- - useOnInfiniteScrollTrigger (Trigger a function when "reffed" element shows up)
18
20
 
19
21
  ## Use
20
22
 
@@ -43,6 +45,7 @@ useClickOutside
43
45
  useOnPressKey
44
46
  useGhostInFirstRender
45
47
  [useOnInfiniteScrollTrigger](https://github.com/WebDevSimplified/React-Infinite-Scrolling)
48
+ [useCabinet] ChatGPT
46
49
  some sources could be missing
47
50
 
48
51
 
package/dist/cjs/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("react"),t=require("@gadeoli/js-helpers-library"),n=function(){return n=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},n.apply(this,arguments)};function r(e,t,n,r){return new(n||(n=Promise))((function(o,u){function i(e){try{s(r.next(e))}catch(e){u(e)}}function c(e){try{s(r.throw(e))}catch(e){u(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(i,c)}s((r=r.apply(e,t||[])).next())}))}function o(e,t){var n,r,o,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},i=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return i.next=c(0),i.throw=c(1),i.return=c(2),"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(c){return function(s){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(u=0)),u;)try{if(n=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return u.label++,{value:c[1],done:!1};case 5:u.label++,r=c[1],c=[0];continue;case 7:c=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){u=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){u.label=c[1];break}if(6===c[0]&&u.label<o[1]){u.label=o[1],o=c;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(c);break}o[2]&&u.ops.pop(),u.trys.pop();continue}c=t.call(e,u)}catch(e){c=[6,e],r=0}finally{n=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,s])}}}"function"==typeof SuppressedError&&SuppressedError;var u={width:1,height:1},i=function(e){return new Promise((function(t,n){var r=new Image;r.onload=function(){t(r)},r.onerror=r.onabort=function(){n(e)},r.src=e}))};exports.useElementSize=function(t,n){void 0===n&&(n=0);var r=e.useState({width:1,height:2,position:{x:1,y:1,left:1,right:1,top:1,bottom:1},screen:{width:1,height:1}}),o=r[0],u=r[1];return e.useEffect((function(){if(t.current){var e=function(){var e=t.current.getBoundingClientRect();u({width:Math.round(e.width),height:Math.round(e.height),position:{x:Math.round(e.x),y:Math.round(e.y),left:Math.round(e.left),right:Math.round(e.right),top:Math.round(e.top),bottom:Math.round(e.bottom)},screen:{width:Math.round(window.screen.width),height:Math.round(window.screen.height)}})};return window.addEventListener("resize",e),e(),function(){return window.removeEventListener("resize",e)}}}),[t,n]),o},exports.useGhostInFirstRender=function(t){void 0===t&&(t=30);var n=e.useState(!1),r=n[0],o=n[1];return e.useEffect((function(){var e=setTimeout((function(){o(!0)}),t);return function(){return clearTimeout(e)}}),[t]),r},exports.useHover=function(t){var n=e.useState(!1),r=n[0],o=n[1];return e.useEffect((function(){if(t&&t.current){var e=t.current,n=function(e){return o(!0)},r=function(e){return o(!1)};return e.addEventListener("mouseover",n),e.addEventListener("mouseout",r),function(){e.removeEventListener("mouseover",n),e.removeEventListener("mouseout",r)}}}),[t]),{show:r}},exports.useImagePreloader=function(t){var n=e.useState(!1),u=n[0],c=n[1];return e.useEffect((function(){var e=!1;return function(){r(this,void 0,void 0,(function(){var n,r,u,s;return o(this,(function(o){switch(o.label){case 0:if(e)return[2];for(n=[],r=0,u=t;r<u.length;r++)s=u[r],n.push(i(s));return[4,Promise.all(n)];case 1:return o.sent(),e?[2]:(c(!0),[2])}}))}))}(),function(){e=!0}}),[t]),{imagesPreloaded:u}},exports.useOnClickOutside=function(t,n){e.useEffect((function(){var e=function(e){t.current&&!t.current.contains(e.target)&&n(e)};return document.addEventListener("mousedown",e),document.addEventListener("touchstart",e),function(){document.removeEventListener("mousedown",e),document.removeEventListener("touchstart",e)}}),[t,n])},exports.useOnInfiniteScrollTrigger=function(t,n,r){var o=e.useRef(null);return e.useCallback((function(e){n||(o.current&&o.current.disconnect(),o.current=new IntersectionObserver((function(e){e[0].isIntersecting&&t&&r&&r()})),e&&o.current.observe(e))}),[n,t,r])},exports.useOnPressKey=function(t,n,r){void 0===r&&(r=null),e.useEffect((function(){var e=function(e){e.keyCode!==t||r&&!e[r]||n()};return document.addEventListener("keydown",e),function(){return document.removeEventListener("keydown",e)}}),[n,t,r])},exports.usePersistedState=function(n,r){var o=e.useState((function(){var e=localStorage.getItem(n);return e?JSON.parse(e):r})),u=o[0],i=o[1];return e.useEffect((function(){localStorage.setItem(n,t.secureJSONStringify(u))}),[n,u]),[u,i]},exports.usePrevious=function(t){var n=e.useRef(null);return e.useEffect((function(){n.current=t})),n.current},exports.useTimer=function(t,n,r){void 0===t&&(t=0),void 0===n&&(n=0),void 0===r&&(r=1);var o=e.useState(t),u=o[0],i=o[1],c=e.useState(n),s=c[0],a=c[1];return e.useEffect((function(){i(t),a(n)}),[r]),e.useEffect((function(){})),e.useEffect((function(){var e=setInterval((function(){s>0&&a(s-1),0===s&&(0===u?clearInterval(e):(i(u-1),a(59)))}),1e3);return function(){clearInterval(e)}})),{minutes:u,seconds:s}},exports.useWindowSize=function(){var t=e.useState(n({},u)),r=t[0],o=t[1];return e.useEffect((function(){var e=function(){o({width:window.innerWidth,height:window.innerHeight})};return window.addEventListener("resize",e),e(),function(){return window.removeEventListener("resize",e)}}),[]),r};
1
+ "use strict";var e=require("react"),t=require("@gadeoli/js-helpers-library"),n=function(){return n=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var u in t=arguments[n])Object.prototype.hasOwnProperty.call(t,u)&&(e[u]=t[u]);return e},n.apply(this,arguments)};function r(e,t,n,r){return new(n||(n=Promise))((function(u,o){function i(e){try{s(r.next(e))}catch(e){o(e)}}function c(e){try{s(r.throw(e))}catch(e){o(e)}}function s(e){var t;e.done?u(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(i,c)}s((r=r.apply(e,t||[])).next())}))}function u(e,t){var n,r,u,o={label:0,sent:function(){if(1&u[0])throw u[1];return u[1]},trys:[],ops:[]},i=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return i.next=c(0),i.throw=c(1),i.return=c(2),"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(c){return function(s){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(o=0)),o;)try{if(n=1,r&&(u=2&c[0]?r.return:c[0]?r.throw||((u=r.return)&&u.call(r),0):r.next)&&!(u=u.call(r,c[1])).done)return u;switch(r=0,u&&(c=[2&c[0],u.value]),c[0]){case 0:case 1:u=c;break;case 4:return o.label++,{value:c[1],done:!1};case 5:o.label++,r=c[1],c=[0];continue;case 7:c=o.ops.pop(),o.trys.pop();continue;default:if(!(u=o.trys,(u=u.length>0&&u[u.length-1])||6!==c[0]&&2!==c[0])){o=0;continue}if(3===c[0]&&(!u||c[1]>u[0]&&c[1]<u[3])){o.label=c[1];break}if(6===c[0]&&o.label<u[1]){o.label=u[1],u=c;break}if(u&&o.label<u[2]){o.label=u[2],o.ops.push(c);break}u[2]&&o.ops.pop(),o.trys.pop();continue}c=t.call(e,o)}catch(e){c=[6,e],r=0}finally{n=u=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,s])}}}function o(e,t,n){if(n||2===arguments.length)for(var r,u=0,o=t.length;u<o;u++)!r&&u in t||(r||(r=Array.prototype.slice.call(t,0,u)),r[u]=t[u]);return e.concat(r||Array.prototype.slice.call(t))}"function"==typeof SuppressedError&&SuppressedError;var i={width:1,height:1},c=function(e){return new Promise((function(t,n){var r=new Image;r.onload=function(){t(r)},r.onerror=r.onabort=function(){n(e)},r.src=e}))};exports.useCabinet=function(t,n){void 0===n&&(n=!0);var r=e.useMemo((function(){return Array.from({length:Math.max(0,t)},(function(e,t){return 0===t&&!!n}))}),[t,n]),u=function(e){return Number.isFinite(e)?Math.max(0,Math.min(t-1,e)):0},i=e.useState((function(){return o([],r,!0)})),c=i[0],s=i[1];return{drawers:c,reset:function(){return s(o([],r,!0)),o([],r,!0)},push:function(e){if(t<=0)return[];var n=u(e),r=c.map((function(e,t){return t===n}));return s(r),r},pushNested:function(e){if(t<=0)return[];var n=u(e),r=c.map((function(e,t){return t<=n||c[t]}));return s(r),r},pull:function(){if(t<=0)return[];var e=c.map((function(){return!1}));return s(e),e},pullNested:function(e){if(t<=0)return[];var n=u(e),r=c.map((function(e,t){return!(t>=n)&&c[t]}));return s(r),r},reverse:function(e){if(t<=0)return[];var n=u(e),r=o([],c,!0);return r[n]=!r[n],s(r),r},reverseNested:function(e){if(t<=0)return[];var n=u(e),r=c[n],i=o([],c,!0);if(r)for(a=n;a<t;a++)i[a]=!1;else for(var a=0;a<=n;a++)i[a]=!0;return s(i),i},manual:function(e){var n=Array.from({length:t},(function(t,n){return!!e[n]}));return s(n),n}}},exports.useElementSize=function(t,n){void 0===n&&(n=0);var r=e.useState({width:1,height:2,position:{x:1,y:1,left:1,right:1,top:1,bottom:1},screen:{width:1,height:1}}),u=r[0],o=r[1];return e.useEffect((function(){if(t.current){var e=function(){var e=t.current.getBoundingClientRect();o({width:Math.round(e.width),height:Math.round(e.height),position:{x:Math.round(e.x),y:Math.round(e.y),left:Math.round(e.left),right:Math.round(e.right),top:Math.round(e.top),bottom:Math.round(e.bottom)},screen:{width:Math.round(window.screen.width),height:Math.round(window.screen.height)}})};return window.addEventListener("resize",e),e(),function(){return window.removeEventListener("resize",e)}}}),[t,n]),u},exports.useGhostInFirstRender=function(t){void 0===t&&(t=30);var n=e.useState(!1),r=n[0],u=n[1];return e.useEffect((function(){var e=setTimeout((function(){u(!0)}),t);return function(){return clearTimeout(e)}}),[t]),r},exports.useHover=function(t){var n=e.useState(!1),r=n[0],u=n[1];return e.useEffect((function(){if(t&&t.current){var e=t.current,n=function(e){return u(!0)},r=function(e){return u(!1)};return e.addEventListener("mouseover",n),e.addEventListener("mouseout",r),function(){e.removeEventListener("mouseover",n),e.removeEventListener("mouseout",r)}}}),[t]),{show:r}},exports.useImagePreloader=function(t){var n=e.useState(!1),o=n[0],i=n[1];return e.useEffect((function(){var e=!1;return function(){r(this,void 0,void 0,(function(){var n,r,o,s;return u(this,(function(u){switch(u.label){case 0:if(e)return[2];for(n=[],r=0,o=t;r<o.length;r++)s=o[r],n.push(c(s));return[4,Promise.all(n)];case 1:return u.sent(),e?[2]:(i(!0),[2])}}))}))}(),function(){e=!0}}),[t]),{imagesPreloaded:o}},exports.useOnClickOutside=function(t,n){e.useEffect((function(){var e=function(e){t.current&&!t.current.contains(e.target)&&n(e)};return document.addEventListener("mousedown",e),document.addEventListener("touchstart",e),function(){document.removeEventListener("mousedown",e),document.removeEventListener("touchstart",e)}}),[t,n])},exports.useOnInfiniteScrollTrigger=function(t,n,r){var u=e.useRef(null);return e.useCallback((function(e){n||(u.current&&u.current.disconnect(),u.current=new IntersectionObserver((function(e){e[0].isIntersecting&&t&&r&&r()})),e&&u.current.observe(e))}),[n,t,r])},exports.useOnPressKey=function(t,n,r){void 0===r&&(r=null),e.useEffect((function(){var e=function(e){e.keyCode!==t||r&&!e[r]||n()};return document.addEventListener("keydown",e),function(){return document.removeEventListener("keydown",e)}}),[n,t,r])},exports.usePersistedState=function(n,r){var u=e.useState((function(){var e=localStorage.getItem(n);return e?JSON.parse(e):r})),o=u[0],i=u[1];return e.useEffect((function(){localStorage.setItem(n,t.secureJSONStringify(o))}),[n,o]),[o,i]},exports.usePrevious=function(t){var n=e.useRef(null);return e.useEffect((function(){n.current=t})),n.current},exports.useTimer=function(t,n,r){void 0===t&&(t=0),void 0===n&&(n=0),void 0===r&&(r=1);var u=e.useState(t),o=u[0],i=u[1],c=e.useState(n),s=c[0],a=c[1];return e.useEffect((function(){i(t),a(n)}),[r]),e.useEffect((function(){})),e.useEffect((function(){var e=setInterval((function(){s>0&&a(s-1),0===s&&(0===o?clearInterval(e):(i(o-1),a(59)))}),1e3);return function(){clearInterval(e)}})),{minutes:o,seconds:s}},exports.useWindowSize=function(){var t=e.useState(n({},i)),r=t[0],u=t[1];return e.useEffect((function(){var e=function(){u({width:window.innerWidth,height:window.innerHeight})};return window.addEventListener("resize",e),e(),function(){return window.removeEventListener("resize",e)}}),[]),r};
@@ -9,3 +9,4 @@ export { default as useOnPressKey } from "./useOnPressKey";
9
9
  export { default as useImagePreloader } from "./useImagePreloader";
10
10
  export { default as useGhostInFirstRender } from "./useGhostInFirstRender";
11
11
  export { default as useOnInfiniteScrollTrigger } from "./useOnInfiniteScrollTrigger";
12
+ export { default as useCabinet } from "./useCabinet";
@@ -0,0 +1,3 @@
1
+ import { useCabinetProps } from "./useCabinet.types";
2
+ declare const useCabinet: useCabinetProps;
3
+ export default useCabinet;
@@ -0,0 +1,13 @@
1
+ export interface useCabinetProps {
2
+ (n: number, rootInitial?: boolean): {
3
+ drawers: Array<boolean>;
4
+ reset: () => Array<boolean>;
5
+ push: (n: number) => Array<boolean>;
6
+ pushNested: (n: number) => Array<boolean>;
7
+ pull: () => Array<boolean>;
8
+ pullNested: (n: number) => Array<boolean>;
9
+ reverse: (n: number) => Array<boolean>;
10
+ reverseNested: (n: number) => Array<boolean>;
11
+ manual: (nxs: Array<boolean>) => Array<boolean>;
12
+ };
13
+ }
@@ -0,0 +1,3 @@
1
+ import { useInViewportProps } from './useInViewport.types';
2
+ declare const useInViewport: useInViewportProps;
3
+ export default useInViewport;
@@ -0,0 +1,4 @@
1
+ import { RefObject } from "react";
2
+ export interface useInViewportProps {
3
+ (ref: RefObject<HTMLElement>, options?: IntersectionObserverInit): boolean;
4
+ }
package/dist/esm/index.js CHANGED
@@ -1 +1 @@
1
- import n,{useState as t,useEffect as e,useRef as r,useCallback as o}from"react";import{secureJSONStringify as i}from"@gadeoli/js-helpers-library";var u=function(e,r){void 0===r&&(r=0);var o=t({width:1,height:2,position:{x:1,y:1,left:1,right:1,top:1,bottom:1},screen:{width:1,height:1}}),i=o[0],u=o[1];return n.useEffect((function(){if(e.current){var n=function(){var n=e.current.getBoundingClientRect();u({width:Math.round(n.width),height:Math.round(n.height),position:{x:Math.round(n.x),y:Math.round(n.y),left:Math.round(n.left),right:Math.round(n.right),top:Math.round(n.top),bottom:Math.round(n.bottom)},screen:{width:Math.round(window.screen.width),height:Math.round(window.screen.height)}})};return window.addEventListener("resize",n),n(),function(){return window.removeEventListener("resize",n)}}}),[e,r]),i},c=function(n){var r=t(!1),o=r[0],i=r[1];return e((function(){if(n&&n.current){var t=n.current,e=function(n){return i(!0)},r=function(n){return i(!1)};return t.addEventListener("mouseover",e),t.addEventListener("mouseout",r),function(){t.removeEventListener("mouseover",e),t.removeEventListener("mouseout",r)}}}),[n]),{show:o}},a=function(n,t){e((function(){var e=function(e){n.current&&!n.current.contains(e.target)&&t(e)};return document.addEventListener("mousedown",e),document.addEventListener("touchstart",e),function(){document.removeEventListener("mousedown",e),document.removeEventListener("touchstart",e)}}),[n,t])},f=function(n,r){var o=t((function(){var t=localStorage.getItem(n);return t?JSON.parse(t):r})),u=o[0],c=o[1];return e((function(){localStorage.setItem(n,i(u))}),[n,u]),[u,c]},s=function(n){var t=r(null);return e((function(){t.current=n})),t.current},d=function(n,r,o){void 0===n&&(n=0),void 0===r&&(r=0),void 0===o&&(o=1);var i=t(n),u=i[0],c=i[1],a=t(r),f=a[0],s=a[1];return e((function(){c(n),s(r)}),[o]),e((function(){})),e((function(){var n=setInterval((function(){f>0&&s(f-1),0===f&&(0===u?clearInterval(n):(c(u-1),s(59)))}),1e3);return function(){clearInterval(n)}})),{minutes:u,seconds:f}},l=function(){return l=Object.assign||function(n){for(var t,e=1,r=arguments.length;e<r;e++)for(var o in t=arguments[e])Object.prototype.hasOwnProperty.call(t,o)&&(n[o]=t[o]);return n},l.apply(this,arguments)};function v(n,t,e,r){return new(e||(e=Promise))((function(o,i){function u(n){try{a(r.next(n))}catch(n){i(n)}}function c(n){try{a(r.throw(n))}catch(n){i(n)}}function a(n){var t;n.done?o(n.value):(t=n.value,t instanceof e?t:new e((function(n){n(t)}))).then(u,c)}a((r=r.apply(n,t||[])).next())}))}function h(n,t){var e,r,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},u=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return u.next=c(0),u.throw=c(1),u.return=c(2),"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function c(c){return function(a){return function(c){if(e)throw new TypeError("Generator is already executing.");for(;u&&(u=0,c[0]&&(i=0)),i;)try{if(e=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return i.label++,{value:c[1],done:!1};case 5:i.label++,r=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){i=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){i.label=c[1];break}if(6===c[0]&&i.label<o[1]){i.label=o[1],o=c;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(c);break}o[2]&&i.ops.pop(),i.trys.pop();continue}c=t.call(n,i)}catch(n){c=[6,n],r=0}finally{e=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,a])}}}"function"==typeof SuppressedError&&SuppressedError;var w={width:1,height:1},p=function(){var n=t(l({},w)),r=n[0],o=n[1];return e((function(){var n=function(){o({width:window.innerWidth,height:window.innerHeight})};return window.addEventListener("resize",n),n(),function(){return window.removeEventListener("resize",n)}}),[]),r},m=function(n,t,r){void 0===r&&(r=null),e((function(){var e=function(e){e.keyCode!==n||r&&!e[r]||t()};return document.addEventListener("keydown",e),function(){return document.removeEventListener("keydown",e)}}),[t,n,r])},g=function(n){return new Promise((function(t,e){var r=new Image;r.onload=function(){t(r)},r.onerror=r.onabort=function(){e(n)},r.src=n}))},y=function(n){var r=t(!1),o=r[0],i=r[1];return e((function(){var t=!1;return function(){v(this,void 0,void 0,(function(){var e,r,o,u;return h(this,(function(c){switch(c.label){case 0:if(t)return[2];for(e=[],r=0,o=n;r<o.length;r++)u=o[r],e.push(g(u));return[4,Promise.all(e)];case 1:return c.sent(),t?[2]:(i(!0),[2])}}))}))}(),function(){t=!0}}),[n]),{imagesPreloaded:o}},b=function(n){void 0===n&&(n=30);var r=t(!1),o=r[0],i=r[1];return e((function(){var t=setTimeout((function(){i(!0)}),n);return function(){return clearTimeout(t)}}),[n]),o},E=function(n,t,e){var i=r(null);return o((function(r){t||(i.current&&i.current.disconnect(),i.current=new IntersectionObserver((function(t){t[0].isIntersecting&&n&&e&&e()})),r&&i.current.observe(r))}),[t,n,e])};export{u as useElementSize,b as useGhostInFirstRender,c as useHover,y as useImagePreloader,a as useOnClickOutside,E as useOnInfiniteScrollTrigger,m as useOnPressKey,f as usePersistedState,s as usePrevious,d as useTimer,p as useWindowSize};
1
+ import n,{useState as t,useEffect as r,useRef as e,useCallback as o,useMemo as u}from"react";import{secureJSONStringify as i}from"@gadeoli/js-helpers-library";var c=function(r,e){void 0===e&&(e=0);var o=t({width:1,height:2,position:{x:1,y:1,left:1,right:1,top:1,bottom:1},screen:{width:1,height:1}}),u=o[0],i=o[1];return n.useEffect((function(){if(r.current){var n=function(){var n=r.current.getBoundingClientRect();i({width:Math.round(n.width),height:Math.round(n.height),position:{x:Math.round(n.x),y:Math.round(n.y),left:Math.round(n.left),right:Math.round(n.right),top:Math.round(n.top),bottom:Math.round(n.bottom)},screen:{width:Math.round(window.screen.width),height:Math.round(window.screen.height)}})};return window.addEventListener("resize",n),n(),function(){return window.removeEventListener("resize",n)}}}),[r,e]),u},a=function(n){var e=t(!1),o=e[0],u=e[1];return r((function(){if(n&&n.current){var t=n.current,r=function(n){return u(!0)},e=function(n){return u(!1)};return t.addEventListener("mouseover",r),t.addEventListener("mouseout",e),function(){t.removeEventListener("mouseover",r),t.removeEventListener("mouseout",e)}}}),[n]),{show:o}},f=function(n,t){r((function(){var r=function(r){n.current&&!n.current.contains(r.target)&&t(r)};return document.addEventListener("mousedown",r),document.addEventListener("touchstart",r),function(){document.removeEventListener("mousedown",r),document.removeEventListener("touchstart",r)}}),[n,t])},s=function(n,e){var o=t((function(){var t=localStorage.getItem(n);return t?JSON.parse(t):e})),u=o[0],c=o[1];return r((function(){localStorage.setItem(n,i(u))}),[n,u]),[u,c]},l=function(n){var t=e(null);return r((function(){t.current=n})),t.current},v=function(n,e,o){void 0===n&&(n=0),void 0===e&&(e=0),void 0===o&&(o=1);var u=t(n),i=u[0],c=u[1],a=t(e),f=a[0],s=a[1];return r((function(){c(n),s(e)}),[o]),r((function(){})),r((function(){var n=setInterval((function(){f>0&&s(f-1),0===f&&(0===i?clearInterval(n):(c(i-1),s(59)))}),1e3);return function(){clearInterval(n)}})),{minutes:i,seconds:f}},d=function(){return d=Object.assign||function(n){for(var t,r=1,e=arguments.length;r<e;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(n[o]=t[o]);return n},d.apply(this,arguments)};function h(n,t,r,e){return new(r||(r=Promise))((function(o,u){function i(n){try{a(e.next(n))}catch(n){u(n)}}function c(n){try{a(e.throw(n))}catch(n){u(n)}}function a(n){var t;n.done?o(n.value):(t=n.value,t instanceof r?t:new r((function(n){n(t)}))).then(i,c)}a((e=e.apply(n,t||[])).next())}))}function p(n,t){var r,e,o,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},i=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return i.next=c(0),i.throw=c(1),i.return=c(2),"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(c){return function(a){return function(c){if(r)throw new TypeError("Generator is already executing.");for(;i&&(i=0,c[0]&&(u=0)),u;)try{if(r=1,e&&(o=2&c[0]?e.return:c[0]?e.throw||((o=e.return)&&o.call(e),0):e.next)&&!(o=o.call(e,c[1])).done)return o;switch(e=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return u.label++,{value:c[1],done:!1};case 5:u.label++,e=c[1],c=[0];continue;case 7:c=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){u=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){u.label=c[1];break}if(6===c[0]&&u.label<o[1]){u.label=o[1],o=c;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(c);break}o[2]&&u.ops.pop(),u.trys.pop();continue}c=t.call(n,u)}catch(n){c=[6,n],e=0}finally{r=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,a])}}}function m(n,t,r){if(r||2===arguments.length)for(var e,o=0,u=t.length;o<u;o++)!e&&o in t||(e||(e=Array.prototype.slice.call(t,0,o)),e[o]=t[o]);return n.concat(e||Array.prototype.slice.call(t))}"function"==typeof SuppressedError&&SuppressedError;var w={width:1,height:1},y=function(){var n=t(d({},w)),e=n[0],o=n[1];return r((function(){var n=function(){o({width:window.innerWidth,height:window.innerHeight})};return window.addEventListener("resize",n),n(),function(){return window.removeEventListener("resize",n)}}),[]),e},g=function(n,t,e){void 0===e&&(e=null),r((function(){var r=function(r){r.keyCode!==n||e&&!r[e]||t()};return document.addEventListener("keydown",r),function(){return document.removeEventListener("keydown",r)}}),[t,n,e])},b=function(n){return new Promise((function(t,r){var e=new Image;e.onload=function(){t(e)},e.onerror=e.onabort=function(){r(n)},e.src=n}))},E=function(n){var e=t(!1),o=e[0],u=e[1];return r((function(){var t=!1;return function(){h(this,void 0,void 0,(function(){var r,e,o,i;return p(this,(function(c){switch(c.label){case 0:if(t)return[2];for(r=[],e=0,o=n;e<o.length;e++)i=o[e],r.push(b(i));return[4,Promise.all(r)];case 1:return c.sent(),t?[2]:(u(!0),[2])}}))}))}(),function(){t=!0}}),[n]),{imagesPreloaded:o}},L=function(n){void 0===n&&(n=30);var e=t(!1),o=e[0],u=e[1];return r((function(){var t=setTimeout((function(){u(!0)}),n);return function(){return clearTimeout(t)}}),[n]),o},M=function(n,t,r){var u=e(null);return o((function(e){t||(u.current&&u.current.disconnect(),u.current=new IntersectionObserver((function(t){t[0].isIntersecting&&n&&r&&r()})),e&&u.current.observe(e))}),[t,n,r])},x=function(n,r){void 0===r&&(r=!0);var e=u((function(){return Array.from({length:Math.max(0,n)},(function(n,t){return 0===t&&!!r}))}),[n,r]),o=function(t){return Number.isFinite(t)?Math.max(0,Math.min(n-1,t)):0},i=t((function(){return m([],e,!0)})),c=i[0],a=i[1];return{drawers:c,reset:function(){return a(m([],e,!0)),m([],e,!0)},push:function(t){if(n<=0)return[];var r=o(t),e=c.map((function(n,t){return t===r}));return a(e),e},pushNested:function(t){if(n<=0)return[];var r=o(t),e=c.map((function(n,t){return t<=r||c[t]}));return a(e),e},pull:function(){if(n<=0)return[];var t=c.map((function(){return!1}));return a(t),t},pullNested:function(t){if(n<=0)return[];var r=o(t),e=c.map((function(n,t){return!(t>=r)&&c[t]}));return a(e),e},reverse:function(t){if(n<=0)return[];var r=o(t),e=m([],c,!0);return e[r]=!e[r],a(e),e},reverseNested:function(t){if(n<=0)return[];var r=o(t),e=c[r],u=m([],c,!0);if(e)for(i=r;i<n;i++)u[i]=!1;else for(var i=0;i<=r;i++)u[i]=!0;return a(u),u},manual:function(t){var r=Array.from({length:n},(function(n,r){return!!t[r]}));return a(r),r}}};export{x as useCabinet,c as useElementSize,L as useGhostInFirstRender,a as useHover,E as useImagePreloader,f as useOnClickOutside,M as useOnInfiniteScrollTrigger,g as useOnPressKey,s as usePersistedState,l as usePrevious,v as useTimer,y as useWindowSize};
@@ -9,3 +9,4 @@ export { default as useOnPressKey } from "./useOnPressKey";
9
9
  export { default as useImagePreloader } from "./useImagePreloader";
10
10
  export { default as useGhostInFirstRender } from "./useGhostInFirstRender";
11
11
  export { default as useOnInfiniteScrollTrigger } from "./useOnInfiniteScrollTrigger";
12
+ export { default as useCabinet } from "./useCabinet";
@@ -0,0 +1,3 @@
1
+ import { useCabinetProps } from "./useCabinet.types";
2
+ declare const useCabinet: useCabinetProps;
3
+ export default useCabinet;
@@ -0,0 +1,13 @@
1
+ export interface useCabinetProps {
2
+ (n: number, rootInitial?: boolean): {
3
+ drawers: Array<boolean>;
4
+ reset: () => Array<boolean>;
5
+ push: (n: number) => Array<boolean>;
6
+ pushNested: (n: number) => Array<boolean>;
7
+ pull: () => Array<boolean>;
8
+ pullNested: (n: number) => Array<boolean>;
9
+ reverse: (n: number) => Array<boolean>;
10
+ reverseNested: (n: number) => Array<boolean>;
11
+ manual: (nxs: Array<boolean>) => Array<boolean>;
12
+ };
13
+ }
@@ -0,0 +1,3 @@
1
+ import { useInViewportProps } from './useInViewport.types';
2
+ declare const useInViewport: useInViewportProps;
3
+ export default useInViewport;
@@ -0,0 +1,4 @@
1
+ import { RefObject } from "react";
2
+ export interface useInViewportProps {
3
+ (ref: RefObject<HTMLElement>, options?: IntersectionObserverInit): boolean;
4
+ }
package/dist/index.d.ts CHANGED
@@ -83,4 +83,20 @@ interface useOnInfiniteScrollTriggerProps {
83
83
 
84
84
  declare const useOnInfiniteScrollTrigger: useOnInfiniteScrollTriggerProps;
85
85
 
86
- export { useElementSize, useGhostInFirstRender, useHover, useImagePreloader, useOnClickOutside, useOnInfiniteScrollTrigger, useOnPressKey, usePersistedState, usePrevious, useTimer, useWindowSize };
86
+ interface useCabinetProps {
87
+ (n: number, rootInitial?: boolean): {
88
+ drawers: Array<boolean>;
89
+ reset: () => Array<boolean>;
90
+ push: (n: number) => Array<boolean>;
91
+ pushNested: (n: number) => Array<boolean>;
92
+ pull: () => Array<boolean>;
93
+ pullNested: (n: number) => Array<boolean>;
94
+ reverse: (n: number) => Array<boolean>;
95
+ reverseNested: (n: number) => Array<boolean>;
96
+ manual: (nxs: Array<boolean>) => Array<boolean>;
97
+ };
98
+ }
99
+
100
+ declare const useCabinet: useCabinetProps;
101
+
102
+ export { useCabinet, useElementSize, useGhostInFirstRender, useHover, useImagePreloader, useOnClickOutside, useOnInfiniteScrollTrigger, useOnPressKey, usePersistedState, usePrevious, useTimer, useWindowSize };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gadeoli/rjs-hooks-library",
3
- "version": "0.2.5",
3
+ "version": "0.2.10",
4
4
  "description": "Some hooks for reactJS apps in one place",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",