@gadeoli/rjs-hooks-library 0.2.5 → 0.2.9
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 +2 -0
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/types/hooks/index.d.ts +1 -0
- package/dist/cjs/types/hooks/useCabinet.d.ts +3 -0
- package/dist/cjs/types/hooks/useCabinet.types.d.ts +13 -0
- package/dist/esm/index.js +1 -1
- package/dist/esm/types/hooks/index.d.ts +1 -0
- package/dist/esm/types/hooks/useCabinet.d.ts +3 -0
- package/dist/esm/types/hooks/useCabinet.types.d.ts +13 -0
- package/dist/index.d.ts +17 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,6 +15,7 @@ Some (almost all) hooks has been copied from internet foruns. If necessary look
|
|
|
15
15
|
- useOnPressKey (An event handler to trigger an action by key press using ref)
|
|
16
16
|
- useGhostInFirstRender (A negative boolean value that is setted positive after first render and a small delay)
|
|
17
17
|
- useOnInfiniteScrollTrigger (Trigger a function when "reffed" element shows up)
|
|
18
|
+
- useCabinet (A boolean array control like a file cabinet)
|
|
18
19
|
|
|
19
20
|
## Use
|
|
20
21
|
|
|
@@ -43,6 +44,7 @@ useClickOutside
|
|
|
43
44
|
useOnPressKey
|
|
44
45
|
useGhostInFirstRender
|
|
45
46
|
[useOnInfiniteScrollTrigger](https://github.com/WebDevSimplified/React-Infinite-Scrolling)
|
|
47
|
+
[useCabinet] ChatGPT
|
|
46
48
|
some sources could be missing
|
|
47
49
|
|
|
48
50
|
|
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
|
|
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,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
|
+
}
|
package/dist/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import n,{useState as t,useEffect as
|
|
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,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
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -83,4 +83,20 @@ interface useOnInfiniteScrollTriggerProps {
|
|
|
83
83
|
|
|
84
84
|
declare const useOnInfiniteScrollTrigger: useOnInfiniteScrollTriggerProps;
|
|
85
85
|
|
|
86
|
-
|
|
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 };
|