react-fast-hooks
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -1,1 +0,1 @@ | ||
"use strict";var e=require("react"),t=function(){return"undefined"!=typeof window&&void 0!==window.document},n="(prefers-color-scheme: dark)",r=function(){if(t()&&window.matchMedia&&window.matchMedia(n).matches)return"dark";return"light"};function o(e,t,n,r){return new(n||(n=Promise))((function(o,i){function u(e){try{c(r.next(e))}catch(e){i(e)}}function a(e){try{c(r.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(u,a)}c((r=r.apply(e,t||[])).next())}))}function i(e,t){var n,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(a){return function(c){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,a[0]&&(u=0)),u;)try{if(n=1,r&&(o=2&a[0]?r.return:a[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return u.label++,{value:a[1],done:!1};case 5:u.label++,r=a[1],a=[0];continue;case 7:a=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==a[0]&&2!==a[0])){u=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]<o[3])){u.label=a[1];break}if(6===a[0]&&u.label<o[1]){u.label=o[1],o=a;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(a);break}o[2]&&u.ops.pop(),u.trys.pop();continue}a=t.call(e,u)}catch(e){a=[6,e],r=0}finally{n=o=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}"function"==typeof SuppressedError&&SuppressedError;exports.useClipboard=function(){var n=e.useState(!1),r=n[0],u=n[1];return{copy:function(e){return o(void 0,void 0,void 0,(function(){var n;return i(this,(function(r){switch(r.label){case 0:if(!t())return console.warn("Clipboard API is not available in SSR."),[2];r.label=1;case 1:return r.trys.push([1,3,,4]),[4,navigator.clipboard.writeText(e)];case 2:return r.sent(),u(!0),setTimeout((function(){return u(!1)}),2e3),[3,4];case 3:return n=r.sent(),console.error("Failed to copy text to clipboard:",n),u(!1),[3,4];case 4:return[2]}}))}))},isCopied:r}},exports.useGeolocation=function(){var n=e.useState({accuracy:null,altitude:null,altitudeAccuracy:null,heading:null,latitude:null,longitude:null,speed:null,timestamp:Date.now()}),r=n[0],o=n[1];return e.useEffect((function(){if(t()){var e,n=!0,r=function(e){n&&o({accuracy:e.coords.accuracy,altitude:e.coords.altitude,altitudeAccuracy:e.coords.altitudeAccuracy,heading:e.coords.heading,latitude:e.coords.latitude,longitude:e.coords.longitude,speed:e.coords.speed,timestamp:e.timestamp})},i=function(e){o(e)};return navigator.geolocation.getCurrentPosition(r,i),e=navigator.geolocation.watchPosition(r,i),function(){n=!1,navigator.geolocation.clearWatch(e)}}}),[]),r},exports.useOnlineStatus=function(){var n=e.useState((function(){return!t()||navigator.onLine})),r=n[0],o=n[1];return e.useEffect((function(){if(t()){var e=function(){return o(!0)},n=function(){return o(!1)};return window.addEventListener("online",e),window.addEventListener("offline",n),function(){window.removeEventListener("online",e),window.removeEventListener("offline",n)}}}),[]),r},exports.useSystemTheme=function(){var o=e.useState((function(){return r()})),i=o[0],u=o[1];return e.useEffect((function(){if(t()&&window.matchMedia){var e=window.matchMedia(n),o=function(){u(r())};return e.addEventListener("change",o),function(){e.removeEventListener("change",o)}}}),[]),i},exports.useTitle=function(n){var r=e.useState((function(){return t()?document.title:""})),o=r[0],i=r[1];return e.useEffect((function(){t()&&(document.title=n,i(n))}),[n]),[o,e.useCallback((function(e){t()&&(document.title=e,i(e))}),[])]}; | ||
"use strict";var e=require("react"),t=function(){return"undefined"!=typeof window&&void 0!==window.document},n="(prefers-color-scheme: dark)",r=function(){if(t()&&window.matchMedia&&window.matchMedia(n).matches)return"dark";return"light"};function o(e,t,n,r){return new(n||(n=Promise))((function(o,i){function u(e){try{c(r.next(e))}catch(e){i(e)}}function a(e){try{c(r.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(u,a)}c((r=r.apply(e,t||[])).next())}))}function i(e,t){var n,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(a){return function(c){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;i&&(i=0,a[0]&&(u=0)),u;)try{if(n=1,r&&(o=2&a[0]?r.return:a[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return u.label++,{value:a[1],done:!1};case 5:u.label++,r=a[1],a=[0];continue;case 7:a=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==a[0]&&2!==a[0])){u=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]<o[3])){u.label=a[1];break}if(6===a[0]&&u.label<o[1]){u.label=o[1],o=a;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(a);break}o[2]&&u.ops.pop(),u.trys.pop();continue}a=t.call(e,u)}catch(e){a=[6,e],r=0}finally{n=o=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}"function"==typeof SuppressedError&&SuppressedError;exports.useClipboard=function(){var n=e.useState(!1),r=n[0],u=n[1];return{copy:function(e){return o(void 0,void 0,void 0,(function(){var n;return i(this,(function(r){switch(r.label){case 0:if(!t())return console.warn("Clipboard API is not available in SSR."),[2];r.label=1;case 1:return r.trys.push([1,3,,4]),[4,navigator.clipboard.writeText(e)];case 2:return r.sent(),u(!0),setTimeout((function(){return u(!1)}),2e3),[3,4];case 3:return n=r.sent(),console.error("Failed to copy text to clipboard:",n),u(!1),[3,4];case 4:return[2]}}))}))},isCopied:r}},exports.useGeolocation=function(){var n=e.useState({accuracy:null,altitude:null,altitudeAccuracy:null,heading:null,latitude:null,longitude:null,speed:null,timestamp:Date.now(),error:null}),r=n[0],o=n[1];return e.useEffect((function(){if(t()){var e,n=!0,r=function(e){n&&o({accuracy:e.coords.accuracy,altitude:e.coords.altitude,altitudeAccuracy:e.coords.altitudeAccuracy,heading:e.coords.heading,latitude:e.coords.latitude,longitude:e.coords.longitude,speed:e.coords.speed,timestamp:e.timestamp,error:null})},i=function(e){o({accuracy:null,altitude:null,altitudeAccuracy:null,heading:null,latitude:null,longitude:null,speed:null,timestamp:Date.now(),error:e})};return navigator.geolocation.getCurrentPosition(r,i),e=navigator.geolocation.watchPosition(r,i),function(){n=!1,navigator.geolocation.clearWatch(e)}}}),[]),r},exports.useOnlineStatus=function(){var n=e.useState((function(){return!t()||navigator.onLine})),r=n[0],o=n[1];return e.useEffect((function(){if(t()){var e=function(){return o(!0)},n=function(){return o(!1)};return window.addEventListener("online",e),window.addEventListener("offline",n),function(){window.removeEventListener("online",e),window.removeEventListener("offline",n)}}}),[]),r},exports.useSystemTheme=function(){var o=e.useState((function(){return r()})),i=o[0],u=o[1];return e.useEffect((function(){if(t()&&window.matchMedia){var e=window.matchMedia(n),o=function(){u(r())};return e.addEventListener("change",o),function(){e.removeEventListener("change",o)}}}),[]),i},exports.useTitle=function(n){var r=e.useCallback((function(e){t()&&(document.title=e)}),[]);return e.useEffect((function(){t()&&(document.title=n)}),[n]),{setTitle:r}}; |
@@ -17,10 +17,14 @@ declare const LIGHT_THEME: "light"; | ||
interface ClipboardHook { | ||
copy: (text: string) => Promise<void>; | ||
isCopied: boolean; | ||
} | ||
/** | ||
* Hook to copy text to the clipboard. | ||
* | ||
* @returns {object} Returns an object with `copy` function and `isCopied` boolean state. | ||
* @returns {ClipboardHook} Returns an object with `copy` function and `isCopied` boolean state. | ||
*/ | ||
declare const useClipboard: () => object; | ||
declare const useClipboard: () => ClipboardHook; | ||
type GeolocationState = { | ||
interface GeolocationState { | ||
accuracy: number | null; | ||
@@ -34,10 +38,14 @@ altitude: number | null; | ||
timestamp: number; | ||
}; | ||
error: GeolocationPositionError | null; | ||
} | ||
/** | ||
* Hook to get the current geolocation. | ||
* | ||
* @returns {GeolocationState | GeolocationPositionError} Returns the geolocation state or an error object. | ||
* @returns {GeolocationState} Returns the geolocation state. | ||
*/ | ||
declare const useGeolocation: () => GeolocationState | GeolocationPositionError; | ||
declare const useGeolocation: () => GeolocationState; | ||
interface UseTitleResult { | ||
setTitle: (newTitle: string) => void; | ||
} | ||
/** | ||
@@ -47,6 +55,6 @@ * Hook to set and get the document title. | ||
* @param {string} initialTitle - The initial title to set. | ||
* @returns {Array} The current document title and a function to set the title. | ||
* @returns {UseTitleResult} An object with the setTitle function. | ||
*/ | ||
declare const useTitle: (initialTitle: string) => [string, (newTitle: string) => void]; | ||
declare const useTitle: (initialTitle: string) => UseTitleResult; | ||
export { useClipboard, useGeolocation, useOnlineStatus, useSystemTheme, useTitle }; |
@@ -1,1 +0,1 @@ | ||
import{useState as n,useEffect as t,useCallback as e}from"react";var r=function(){return"undefined"!=typeof window&&void 0!==window.document},o="(prefers-color-scheme: dark)",i=function(){if(r()&&window.matchMedia&&window.matchMedia(o).matches)return"dark";return"light"},u=function(){var e=n((function(){return i()})),u=e[0],a=e[1];return t((function(){if(r()&&window.matchMedia){var n=window.matchMedia(o),t=function(){a(i())};return n.addEventListener("change",t),function(){n.removeEventListener("change",t)}}}),[]),u},a=function(){var e=n((function(){return!r()||navigator.onLine})),o=e[0],i=e[1];return t((function(){if(r()){var n=function(){return i(!0)},t=function(){return i(!1)};return window.addEventListener("online",n),window.addEventListener("offline",t),function(){window.removeEventListener("online",n),window.removeEventListener("offline",t)}}}),[]),o};function c(n,t,e,r){return new(e||(e=Promise))((function(o,i){function u(n){try{c(r.next(n))}catch(n){i(n)}}function a(n){try{c(r.throw(n))}catch(n){i(n)}}function c(n){var t;n.done?o(n.value):(t=n.value,t instanceof e?t:new e((function(n){n(t)}))).then(u,a)}c((r=r.apply(n,t||[])).next())}))}function l(n,t){var e,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(a){return function(c){return function(a){if(e)throw new TypeError("Generator is already executing.");for(;i&&(i=0,a[0]&&(u=0)),u;)try{if(e=1,r&&(o=2&a[0]?r.return:a[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return u.label++,{value:a[1],done:!1};case 5:u.label++,r=a[1],a=[0];continue;case 7:a=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==a[0]&&2!==a[0])){u=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]<o[3])){u.label=a[1];break}if(6===a[0]&&u.label<o[1]){u.label=o[1],o=a;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(a);break}o[2]&&u.ops.pop(),u.trys.pop();continue}a=t.call(n,u)}catch(n){a=[6,n],r=0}finally{e=o=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}"function"==typeof SuppressedError&&SuppressedError;var d=function(){var t=n(!1),e=t[0],o=t[1];return{copy:function(n){return c(void 0,void 0,void 0,(function(){var t;return l(this,(function(e){switch(e.label){case 0:if(!r())return console.warn("Clipboard API is not available in SSR."),[2];e.label=1;case 1:return e.trys.push([1,3,,4]),[4,navigator.clipboard.writeText(n)];case 2:return e.sent(),o(!0),setTimeout((function(){return o(!1)}),2e3),[3,4];case 3:return t=e.sent(),console.error("Failed to copy text to clipboard:",t),o(!1),[3,4];case 4:return[2]}}))}))},isCopied:e}},f=function(){var e=n({accuracy:null,altitude:null,altitudeAccuracy:null,heading:null,latitude:null,longitude:null,speed:null,timestamp:Date.now()}),o=e[0],i=e[1];return t((function(){if(r()){var n,t=!0,e=function(n){t&&i({accuracy:n.coords.accuracy,altitude:n.coords.altitude,altitudeAccuracy:n.coords.altitudeAccuracy,heading:n.coords.heading,latitude:n.coords.latitude,longitude:n.coords.longitude,speed:n.coords.speed,timestamp:n.timestamp})},o=function(n){i(n)};return navigator.geolocation.getCurrentPosition(e,o),n=navigator.geolocation.watchPosition(e,o),function(){t=!1,navigator.geolocation.clearWatch(n)}}}),[]),o},s=function(o){var i=n((function(){return r()?document.title:""})),u=i[0],a=i[1];return t((function(){r()&&(document.title=o,a(o))}),[o]),[u,e((function(n){r()&&(document.title=n,a(n))}),[])]};export{d as useClipboard,f as useGeolocation,a as useOnlineStatus,u as useSystemTheme,s as useTitle}; | ||
import{useState as n,useEffect as t,useCallback as e}from"react";var r=function(){return"undefined"!=typeof window&&void 0!==window.document},o="(prefers-color-scheme: dark)",i=function(){if(r()&&window.matchMedia&&window.matchMedia(o).matches)return"dark";return"light"},u=function(){var e=n((function(){return i()})),u=e[0],a=e[1];return t((function(){if(r()&&window.matchMedia){var n=window.matchMedia(o),t=function(){a(i())};return n.addEventListener("change",t),function(){n.removeEventListener("change",t)}}}),[]),u},a=function(){var e=n((function(){return!r()||navigator.onLine})),o=e[0],i=e[1];return t((function(){if(r()){var n=function(){return i(!0)},t=function(){return i(!1)};return window.addEventListener("online",n),window.addEventListener("offline",t),function(){window.removeEventListener("online",n),window.removeEventListener("offline",t)}}}),[]),o};function c(n,t,e,r){return new(e||(e=Promise))((function(o,i){function u(n){try{c(r.next(n))}catch(n){i(n)}}function a(n){try{c(r.throw(n))}catch(n){i(n)}}function c(n){var t;n.done?o(n.value):(t=n.value,t instanceof e?t:new e((function(n){n(t)}))).then(u,a)}c((r=r.apply(n,t||[])).next())}))}function l(n,t){var e,r,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(a){return function(c){return function(a){if(e)throw new TypeError("Generator is already executing.");for(;i&&(i=0,a[0]&&(u=0)),u;)try{if(e=1,r&&(o=2&a[0]?r.return:a[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return u.label++,{value:a[1],done:!1};case 5:u.label++,r=a[1],a=[0];continue;case 7:a=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==a[0]&&2!==a[0])){u=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]<o[3])){u.label=a[1];break}if(6===a[0]&&u.label<o[1]){u.label=o[1],o=a;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(a);break}o[2]&&u.ops.pop(),u.trys.pop();continue}a=t.call(n,u)}catch(n){a=[6,n],r=0}finally{e=o=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}"function"==typeof SuppressedError&&SuppressedError;var d=function(){var t=n(!1),e=t[0],o=t[1];return{copy:function(n){return c(void 0,void 0,void 0,(function(){var t;return l(this,(function(e){switch(e.label){case 0:if(!r())return console.warn("Clipboard API is not available in SSR."),[2];e.label=1;case 1:return e.trys.push([1,3,,4]),[4,navigator.clipboard.writeText(n)];case 2:return e.sent(),o(!0),setTimeout((function(){return o(!1)}),2e3),[3,4];case 3:return t=e.sent(),console.error("Failed to copy text to clipboard:",t),o(!1),[3,4];case 4:return[2]}}))}))},isCopied:e}},f=function(){var e=n({accuracy:null,altitude:null,altitudeAccuracy:null,heading:null,latitude:null,longitude:null,speed:null,timestamp:Date.now(),error:null}),o=e[0],i=e[1];return t((function(){if(r()){var n,t=!0,e=function(n){t&&i({accuracy:n.coords.accuracy,altitude:n.coords.altitude,altitudeAccuracy:n.coords.altitudeAccuracy,heading:n.coords.heading,latitude:n.coords.latitude,longitude:n.coords.longitude,speed:n.coords.speed,timestamp:n.timestamp,error:null})},o=function(n){i({accuracy:null,altitude:null,altitudeAccuracy:null,heading:null,latitude:null,longitude:null,speed:null,timestamp:Date.now(),error:n})};return navigator.geolocation.getCurrentPosition(e,o),n=navigator.geolocation.watchPosition(e,o),function(){t=!1,navigator.geolocation.clearWatch(n)}}}),[]),o},s=function(n){var o=e((function(n){r()&&(document.title=n)}),[]);return t((function(){r()&&(document.title=n)}),[n]),{setTitle:o}};export{d as useClipboard,f as useGeolocation,a as useOnlineStatus,u as useSystemTheme,s as useTitle}; |
@@ -0,7 +1,11 @@ | ||
interface ClipboardHook { | ||
copy: (text: string) => Promise<void>; | ||
isCopied: boolean; | ||
} | ||
/** | ||
* Hook to copy text to the clipboard. | ||
* | ||
* @returns {object} Returns an object with `copy` function and `isCopied` boolean state. | ||
* @returns {ClipboardHook} Returns an object with `copy` function and `isCopied` boolean state. | ||
*/ | ||
declare const useClipboard: () => object; | ||
declare const useClipboard: () => ClipboardHook; | ||
export default useClipboard; |
@@ -1,2 +0,2 @@ | ||
type GeolocationState = { | ||
interface GeolocationState { | ||
accuracy: number | null; | ||
@@ -10,9 +10,10 @@ altitude: number | null; | ||
timestamp: number; | ||
}; | ||
error: GeolocationPositionError | null; | ||
} | ||
/** | ||
* Hook to get the current geolocation. | ||
* | ||
* @returns {GeolocationState | GeolocationPositionError} Returns the geolocation state or an error object. | ||
* @returns {GeolocationState} Returns the geolocation state. | ||
*/ | ||
declare const useGeolocation: () => GeolocationState | GeolocationPositionError; | ||
declare const useGeolocation: () => GeolocationState; | ||
export default useGeolocation; |
@@ -0,1 +1,4 @@ | ||
interface UseTitleResult { | ||
setTitle: (newTitle: string) => void; | ||
} | ||
/** | ||
@@ -5,5 +8,5 @@ * Hook to set and get the document title. | ||
* @param {string} initialTitle - The initial title to set. | ||
* @returns {Array} The current document title and a function to set the title. | ||
* @returns {UseTitleResult} An object with the setTitle function. | ||
*/ | ||
declare const useTitle: (initialTitle: string) => [string, (newTitle: string) => void]; | ||
declare const useTitle: (initialTitle: string) => UseTitleResult; | ||
export default useTitle; |
{ | ||
"name": "react-fast-hooks", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A collection of custom React hooks", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.cjs.js", |
14086
144