@alessiofrittoli/react-hooks 1.0.0
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 +830 -0
- package/dist/browser-api/index.d.mts +6 -0
- package/dist/browser-api/index.d.ts +6 -0
- package/dist/browser-api/index.js +2 -0
- package/dist/browser-api/index.js.map +1 -0
- package/dist/browser-api/index.mjs +2 -0
- package/dist/browser-api/index.mjs.map +1 -0
- package/dist/browser-api/storage/index.d.mts +4 -0
- package/dist/browser-api/storage/index.d.ts +4 -0
- package/dist/browser-api/storage/index.js +2 -0
- package/dist/browser-api/storage/index.js.map +1 -0
- package/dist/browser-api/storage/index.mjs +2 -0
- package/dist/browser-api/storage/index.mjs.map +1 -0
- package/dist/browser-api/storage/useLocalStorage.d.mts +11 -0
- package/dist/browser-api/storage/useLocalStorage.d.ts +11 -0
- package/dist/browser-api/storage/useLocalStorage.js +2 -0
- package/dist/browser-api/storage/useLocalStorage.js.map +1 -0
- package/dist/browser-api/storage/useLocalStorage.mjs +2 -0
- package/dist/browser-api/storage/useLocalStorage.mjs.map +1 -0
- package/dist/browser-api/storage/useSessionStorage.d.mts +11 -0
- package/dist/browser-api/storage/useSessionStorage.d.ts +11 -0
- package/dist/browser-api/storage/useSessionStorage.js +2 -0
- package/dist/browser-api/storage/useSessionStorage.js.map +1 -0
- package/dist/browser-api/storage/useSessionStorage.mjs +2 -0
- package/dist/browser-api/storage/useSessionStorage.mjs.map +1 -0
- package/dist/browser-api/storage/useStorage.d.mts +12 -0
- package/dist/browser-api/storage/useStorage.d.ts +12 -0
- package/dist/browser-api/storage/useStorage.js +2 -0
- package/dist/browser-api/storage/useStorage.js.map +1 -0
- package/dist/browser-api/storage/useStorage.mjs +2 -0
- package/dist/browser-api/storage/useStorage.mjs.map +1 -0
- package/dist/browser-api/useIsPortrait.d.mts +10 -0
- package/dist/browser-api/useIsPortrait.d.ts +10 -0
- package/dist/browser-api/useIsPortrait.js +2 -0
- package/dist/browser-api/useIsPortrait.js.map +1 -0
- package/dist/browser-api/useIsPortrait.mjs +2 -0
- package/dist/browser-api/useIsPortrait.mjs.map +1 -0
- package/dist/browser-api/useMediaQuery.d.mts +11 -0
- package/dist/browser-api/useMediaQuery.d.ts +11 -0
- package/dist/browser-api/useMediaQuery.js +2 -0
- package/dist/browser-api/useMediaQuery.js.map +1 -0
- package/dist/browser-api/useMediaQuery.mjs +2 -0
- package/dist/browser-api/useMediaQuery.mjs.map +1 -0
- package/dist/dom-api/index.d.mts +2 -0
- package/dist/dom-api/index.d.ts +2 -0
- package/dist/dom-api/index.js +2 -0
- package/dist/dom-api/index.js.map +1 -0
- package/dist/dom-api/index.mjs +2 -0
- package/dist/dom-api/index.mjs.map +1 -0
- package/dist/dom-api/useFocusTrap.d.mts +15 -0
- package/dist/dom-api/useFocusTrap.d.ts +15 -0
- package/dist/dom-api/useFocusTrap.js +2 -0
- package/dist/dom-api/useFocusTrap.js.map +1 -0
- package/dist/dom-api/useFocusTrap.mjs +2 -0
- package/dist/dom-api/useFocusTrap.mjs.map +1 -0
- package/dist/dom-api/useScrollBlock.d.mts +8 -0
- package/dist/dom-api/useScrollBlock.d.ts +8 -0
- package/dist/dom-api/useScrollBlock.js +2 -0
- package/dist/dom-api/useScrollBlock.js.map +1 -0
- package/dist/dom-api/useScrollBlock.mjs +2 -0
- package/dist/dom-api/useScrollBlock.mjs.map +1 -0
- package/dist/eslint.d.mts +11 -0
- package/dist/eslint.d.ts +11 -0
- package/dist/eslint.js +2 -0
- package/dist/eslint.js.map +1 -0
- package/dist/eslint.mjs +2 -0
- package/dist/eslint.mjs.map +1 -0
- package/dist/index.d.mts +11 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/dist/misc/index.d.mts +3 -0
- package/dist/misc/index.d.ts +3 -0
- package/dist/misc/index.js +2 -0
- package/dist/misc/index.js.map +1 -0
- package/dist/misc/index.mjs +2 -0
- package/dist/misc/index.mjs.map +1 -0
- package/dist/misc/useIsClient.d.mts +8 -0
- package/dist/misc/useIsClient.d.ts +8 -0
- package/dist/misc/useIsClient.js +2 -0
- package/dist/misc/useIsClient.js.map +1 -0
- package/dist/misc/useIsClient.mjs +2 -0
- package/dist/misc/useIsClient.mjs.map +1 -0
- package/dist/misc/useIsFirstRender.d.mts +9 -0
- package/dist/misc/useIsFirstRender.d.ts +9 -0
- package/dist/misc/useIsFirstRender.js +2 -0
- package/dist/misc/useIsFirstRender.js.map +1 -0
- package/dist/misc/useIsFirstRender.mjs +2 -0
- package/dist/misc/useIsFirstRender.mjs.map +1 -0
- package/dist/misc/useUpdateEffect.d.mts +9 -0
- package/dist/misc/useUpdateEffect.d.ts +9 -0
- package/dist/misc/useUpdateEffect.js +2 -0
- package/dist/misc/useUpdateEffect.js.map +1 -0
- package/dist/misc/useUpdateEffect.mjs +2 -0
- package/dist/misc/useUpdateEffect.mjs.map +1 -0
- package/license.md +21 -0
- package/package.json +144 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Linter } from 'eslint';
|
|
2
|
+
|
|
3
|
+
interface ESLintConfiguration {
|
|
4
|
+
recommended: Linter.Config[];
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* [`@alessiofrittoli/react-hooks`](https://npmjs.com/package/@alessiofrittoli/react-hooks) Recommended ESLint configurations.
|
|
8
|
+
*/
|
|
9
|
+
declare const config: ESLintConfiguration;
|
|
10
|
+
|
|
11
|
+
export { config };
|
package/dist/eslint.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Linter } from 'eslint';
|
|
2
|
+
|
|
3
|
+
interface ESLintConfiguration {
|
|
4
|
+
recommended: Linter.Config[];
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* [`@alessiofrittoli/react-hooks`](https://npmjs.com/package/@alessiofrittoli/react-hooks) Recommended ESLint configurations.
|
|
8
|
+
*/
|
|
9
|
+
declare const config: ESLintConfiguration;
|
|
10
|
+
|
|
11
|
+
export { config };
|
package/dist/eslint.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/eslint.ts"],"names":["config"],"mappings":"aAWO,IAAMA,EAA8B,CAC1C,WAAA,CAAa,CACZ,CACC,MAAO,CACN,6BAAA,CAA+B,CAC9B,MAAA,CACA,CACC,eAAiB,CAAA,mBAClB,CACD,CACD,CACD,CACD,CACD","file":"eslint.js","sourcesContent":["import type { Linter } from 'eslint'\n\ninterface ESLintConfiguration\n{\n\trecommended: Linter.Config[]\n}\n\n\n/**\n * [`@alessiofrittoli/react-hooks`](https://npmjs.com/package/@alessiofrittoli/react-hooks) Recommended ESLint configurations.\n */\nexport const config: ESLintConfiguration = {\n\trecommended: [\n\t\t{\n\t\t\trules: {\n\t\t\t\t'react-hooks/exhaustive-deps': [\n\t\t\t\t\t'warn',\n\t\t\t\t\t{\n\t\t\t\t\t\tadditionalHooks: '(useUpdateEffect)',\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t}\n\t\t}\n\t]\n}"]}
|
package/dist/eslint.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/eslint.ts"],"names":["config"],"mappings":"AAWO,IAAMA,EAA8B,CAC1C,WAAA,CAAa,CACZ,CACC,MAAO,CACN,6BAAA,CAA+B,CAC9B,MAAA,CACA,CACC,eAAiB,CAAA,mBAClB,CACD,CACD,CACD,CACD,CACD","file":"eslint.mjs","sourcesContent":["import type { Linter } from 'eslint'\n\ninterface ESLintConfiguration\n{\n\trecommended: Linter.Config[]\n}\n\n\n/**\n * [`@alessiofrittoli/react-hooks`](https://npmjs.com/package/@alessiofrittoli/react-hooks) Recommended ESLint configurations.\n */\nexport const config: ESLintConfiguration = {\n\trecommended: [\n\t\t{\n\t\t\trules: {\n\t\t\t\t'react-hooks/exhaustive-deps': [\n\t\t\t\t\t'warn',\n\t\t\t\t\t{\n\t\t\t\t\t\tadditionalHooks: '(useUpdateEffect)',\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t}\n\t\t}\n\t]\n}"]}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { useStorage } from './browser-api/storage/useStorage.mjs';
|
|
2
|
+
export { useLocalStorage } from './browser-api/storage/useLocalStorage.mjs';
|
|
3
|
+
export { useSessionStorage } from './browser-api/storage/useSessionStorage.mjs';
|
|
4
|
+
export { useIsPortrait } from './browser-api/useIsPortrait.mjs';
|
|
5
|
+
export { useMediaQuery } from './browser-api/useMediaQuery.mjs';
|
|
6
|
+
export { useFocusTrap } from './dom-api/useFocusTrap.mjs';
|
|
7
|
+
export { useScrollBlock } from './dom-api/useScrollBlock.mjs';
|
|
8
|
+
export { useIsClient } from './misc/useIsClient.mjs';
|
|
9
|
+
export { useIsFirstRender } from './misc/useIsFirstRender.mjs';
|
|
10
|
+
export { useUpdateEffect } from './misc/useUpdateEffect.mjs';
|
|
11
|
+
import 'react';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { useStorage } from './browser-api/storage/useStorage.js';
|
|
2
|
+
export { useLocalStorage } from './browser-api/storage/useLocalStorage.js';
|
|
3
|
+
export { useSessionStorage } from './browser-api/storage/useSessionStorage.js';
|
|
4
|
+
export { useIsPortrait } from './browser-api/useIsPortrait.js';
|
|
5
|
+
export { useMediaQuery } from './browser-api/useMediaQuery.js';
|
|
6
|
+
export { useFocusTrap } from './dom-api/useFocusTrap.js';
|
|
7
|
+
export { useScrollBlock } from './dom-api/useScrollBlock.js';
|
|
8
|
+
export { useIsClient } from './misc/useIsClient.js';
|
|
9
|
+
export { useIsFirstRender } from './misc/useIsFirstRender.js';
|
|
10
|
+
export { useUpdateEffect } from './misc/useUpdateEffect.js';
|
|
11
|
+
import 'react';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
'use strict';var react=require('react'),LocalStorage=require('@alessiofrittoli/web-utils/storage/LocalStorage'),SessionStorage=require('@alessiofrittoli/web-utils/storage/SessionStorage'),browserApi=require('@alessiofrittoli/web-utils/browser-api'),device=require('@alessiofrittoli/web-utils/device'),dom=require('@alessiofrittoli/web-utils/dom');var i=(e,t,r="local")=>{let s=react.useCallback(()=>(r==="local"?LocalStorage.LocalStorage:SessionStorage.SessionStorage).get(e)??t,[r,e,t]),[n,u]=react.useState(t),c=react.useCallback(o=>{u(l=>{let a=o instanceof Function?o(l):o;return (typeof window<"u"&&r==="local"?LocalStorage.LocalStorage:SessionStorage.SessionStorage).set(e,a),a});},[r,e]);return react.useEffect(()=>{u(s());},[s]),[n,c]};var P=(e,t)=>i(e,t,"local");var G=(e,t)=>i(e,t,"session");var S=e=>{let[t,r]=react.useState(browserApi.getMediaMatches(e)),s=react.useCallback(()=>r(browserApi.getMediaMatches(e)),[e]);return react.useEffect(()=>{let n=window.matchMedia(e);return s(),n.addEventListener("change",s),()=>{n.removeEventListener("change",s);}},[e,s]),t};var _=()=>S(device.portraitMediaQuery);var V=["input","select","textarea","button","[href]",'[tabindex]:not([tabindex="-1"])'].join(", "),ee=e=>{let[t,r]=react.useState(false),s=react.useRef(null),n=react.useCallback(c=>{s.current=document.activeElement;let o=c||e?.current||false;if(o)return r(o)},[e]),u=react.useCallback(()=>{s.current?.focus(),r(false);},[]);return react.useEffect(()=>{if(!t)return;let c=o=>{if(o.key!=="Tab")return;let l=Array.from(t.querySelectorAll(V)),a=l.at(0),f=l.at(-1);if(!o.shiftKey){document.activeElement===f&&(o.preventDefault(),a?.focus());return}document.activeElement===a&&(o.preventDefault(),f?.focus());};return document.addEventListener("keydown",c),()=>{document.removeEventListener("keydown",c);}},[t]),[n,u]};var se=e=>{let t=react.useCallback(()=>dom.blockScroll(e?.current||void 0),[e]),r=react.useCallback(()=>dom.restoreScroll(e?.current||void 0),[e]);return [t,r]};var ae=()=>{let[e,t]=react.useState(false);return react.useEffect(()=>t(true),[]),e};var F=()=>{let e=react.useRef(true);return e.current?(e.current=false,true):e.current};var pe=(e,t)=>{let r=F();react.useEffect(()=>{if(!r)return e()},t);};exports.useFocusTrap=ee;exports.useIsClient=ae;exports.useIsFirstRender=F;exports.useIsPortrait=_;exports.useLocalStorage=P;exports.useMediaQuery=S;exports.useScrollBlock=se;exports.useSessionStorage=G;exports.useStorage=i;exports.useUpdateEffect=pe;//# sourceMappingURL=index.js.map
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/browser-api/storage/useStorage.ts","../src/browser-api/storage/useLocalStorage.ts","../src/browser-api/storage/useSessionStorage.ts","../src/browser-api/useMediaQuery.ts","../src/browser-api/useIsPortrait.ts","../src/dom-api/useFocusTrap.ts","../src/dom-api/useScrollBlock.ts","../src/misc/useIsClient.ts","../src/misc/useIsFirstRender.ts","../src/misc/useUpdateEffect.ts"],"names":["useStorage","key","initialValue","type","readValue","useCallback","LocalStorage","SessionStorage","storedValue","setStoredValue","useState","setValue","value","valueToStore","useEffect","useLocalStorage","useSessionStorage","useMediaQuery","query","matches","setMatches","getMediaMatches","matchMediaChangeHandler","matchMedia","useIsPortrait","portraitMediaQuery","focusableSelector","useFocusTrap","target","focusTrap","setFocusTrapDispatch","lastActiveElement","useRef","setFocusTrap","onDemandTarget","restoreFocusTrap","keyDownHandler","event","focusableElements","firstFocusableElement","lastFocusableElement","useScrollBlock","blockScroll","blockScrollHandler","restoreScroll","restoreScrollHandler","useIsClient","isClient","setClient","useIsFirstRender","isFirst","useUpdateEffect","effect","deps"],"mappings":"2VAcO,IAAMA,CAAa,CAAA,CACzBC,CACAC,CAAAA,CAAAA,CACAC,CAA+B,CAAA,OAAA,GACO,CAEtC,IAAMC,CAAYC,CAAAA,iBAAAA,CAAa,IAC5BF,CAAAA,CAAAA,GAAS,QAAUG,yBAAeC,CAAAA,6BAAAA,EAClC,GAAQN,CAAAA,CAAI,CAAKC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IAAK,CAAA,CAAG,EAAG,CAEhCD,CAAAA,CAER,ECPO,IAAME,CAAmB,CAAA,IAAM,CAErC,IAAMC,CAAUlB,CAAAA,YAAAA,CAAQ,IAAK,CAAA,CAE7B,OAAKkB,CAAAA,CAAQ,OACZA,EAAAA,CAAAA,CAAQ,OAAU,CAAA,KAAA,CAEX,MAGDA,CAAQ,CAAA,OAEhB,ECVO,IAAMC,EAAkB,CAAA,CAC9BC,CACAC,CAAAA,CAAAA,GACI,CAEJ,IAAMH,CAAUD,CAAAA,CAAAA,EAEhBnC,CAAAA,eAAAA,CAAW,IAAM,CAChB,GAAK,CAAEoC,CAAU,CAAA,OAAOE,CAAO,EAEhC,CAAGC,CAAAA,CAAK,EAET","file":"index.js","sourcesContent":["import { useCallback, useEffect, useState } from 'react'\nimport { LocalStorage } from '@alessiofrittoli/web-utils/storage/LocalStorage'\nimport { SessionStorage } from '@alessiofrittoli/web-utils/storage/SessionStorage'\n\ntype Value<T>\t\t= T | undefined | null\ntype SetValue<T>\t= React.Dispatch<React.SetStateAction<T>>\n\n/**\n * Easly handle Local or Session Storage State.\n * \n * @param\tkey\t\t\t\tThe storage item key.\n * @param\tinitialValue\tThe storage item initial value.\n * @param\ttype\t\t\t( Optional ) The storage API to use. Default: `local`.\n */\nexport const useStorage = <T = string>(\n\tkey\t\t\t\t: string,\n\tinitialValue?\t: T,\n\ttype\t\t\t: 'local' | 'session' = 'local'\n): [ Value<T>, SetValue<Value<T>> ] => {\n\n\tconst readValue = useCallback( () => (\n\t\t( type === 'local' ? LocalStorage : SessionStorage )\n\t\t\t.get<T>( key ) ?? initialValue\n\t), [ type, key, initialValue ] )\n\n\n\t/**\n\t * State to store our value.\n\t * Pass initial state function to useState so logic is only executed once.\n\t * \n\t */\n\tconst [ storedValue, setStoredValue ] = useState<Value<T>>( initialValue )\n\n\t/**\n\t * Return a wrapped version of useState's setter function that\n\t * persists the new value to localStorage | sessionStorage.\n\t * \n\t * @param value The SetStateAction value.\n\t */\n\tconst setValue = useCallback<SetValue<Value<T>>>( value => {\n\n\t\tsetStoredValue( storedValue => {\n\t\t\tconst valueToStore = value instanceof Function ? value( storedValue ) : value\n\t\n\t\t\t;(\n\t\t\t\ttypeof window !== 'undefined' &&\n\t\t\t\ttype === 'local' ? LocalStorage : SessionStorage\n\t\t\t).set( key, valueToStore )\n\n\t\t\treturn valueToStore\n\t\t} )\n\n\n\t}, [ type, key ] )\n\n\tuseEffect( () => {\n\t\tsetStoredValue( readValue() )\n\t}, [ readValue ] )\n\n\treturn [ storedValue, setValue ]\n}","import { useStorage } from './useStorage'\n\n/**\n * useLocalStorage hook.\n * \n * @param\tkey\t\t\t\tThe local storage item key.\n * @param\tinitialValue\tThe local storage item initial value.\n */\nexport const useLocalStorage = <T = string>(\n\tkey\t\t\t\t: string,\n\tinitialValue?\t: T\n) => useStorage( key, initialValue, 'local' )","import { useStorage } from './useStorage'\n\n/**\n * useSessionStorage hook.\n * \n * @param\tkey\t\t\t\tThe session item key.\n * @param\tinitialValue\tThe session item initial value.\n */\nexport const useSessionStorage = <T = string>(\n\tkey\t\t\t\t: string,\n\tinitialValue?\t: T\n) => useStorage( key, initialValue, 'session' )","import { useCallback, useEffect, useState } from 'react'\nimport { getMediaMatches } from '@alessiofrittoli/web-utils/browser-api'\n\n\n/**\n * Get Document Media matches and listen for changes.\n *\n * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia)\n *\n * @param\tquery A string specifying the media query to parse into a `MediaQueryList`.\n * @returns\tA boolean value that returns `true` if the document currently matches the media query list, or `false` if not.\n */\nexport const useMediaQuery = ( query: string ) => {\n\n\tconst [ matches, setMatches ]\t= useState( getMediaMatches( query ) )\n\tconst matchMediaChangeHandler\t= useCallback( () => setMatches( getMediaMatches( query ) ), [ query ] )\n\n\n\tuseEffect( () => {\n\n\t\tconst matchMedia = window.matchMedia( query )\n\t\t// Triggered at the first client-side load\n\t\tmatchMediaChangeHandler()\n\t\t// Listen matchMedia\n\t\tmatchMedia.addEventListener( 'change', matchMediaChangeHandler )\n\n\t\treturn () => {\n\t\t\tmatchMedia.removeEventListener( 'change', matchMediaChangeHandler )\n\t\t}\n\n\t}, [ query, matchMediaChangeHandler ] )\n\n\treturn matches\n\t\n}","import { useMediaQuery } from './useMediaQuery'\nimport { portraitMediaQuery } from '@alessiofrittoli/web-utils/device'\n\n/**\n * Check if device is portrait oriented.\n * \n * State get updated when device orientation changes.\n *\n * @returns\t`true` if the device is portrait oriented, `false` otherwise.\n */\nexport const useIsPortrait = () => useMediaQuery( portraitMediaQuery )","import { useCallback, useEffect, useRef, useState } from 'react'\n\ntype SetFocusTrap = ( target?: HTMLElement ) => void\ntype RestoreFocusTrap = () => void\n\nconst focusableSelector = [\n\t'input', 'select', 'textarea',\n\t'button', '[href]', '[tabindex]:not([tabindex=\"-1\"])',\n].join( ', ' )\n\n\n/**\n * Trap focus inside the given HTML Element.\n * \n * @param target (Optional) The target HTMLElement React RefObject to trap focus within.\n * \t\t\t\t\tIf no target is given, you must provide the target HTMLElement when calling `setFocusTrap`.\n * \n * @returns A tuple containing:\n * - `setFocusTrap`: A function to enable the focus trap. Optionally accept an HTMLElement as target.\n * - `restoreFocusTrap`: A function to restore the previous focus state.\n */\nexport const useFocusTrap = (\n\ttarget?: React.RefObject<HTMLElement | null>\n): readonly [ SetFocusTrap, RestoreFocusTrap ] => {\n\n\tconst [ focusTrap, setFocusTrapDispatch ] = (\n\t\tuseState<HTMLElement | false>( false )\n\t)\n\n\tconst lastActiveElement = useRef<HTMLElement>( null )\n\t\n\t/**\n\t * Enable the focus trap.\n\t * \n\t */\n\tconst setFocusTrap = useCallback<SetFocusTrap>( onDemandTarget => {\n\n\t\tlastActiveElement.current\t= document.activeElement as HTMLElement\n\t\tconst focusTrap\t\t\t\t= onDemandTarget || target?.current || false\n\t\t\n\t\tif ( ! focusTrap ) return\n\n\t\treturn setFocusTrapDispatch( focusTrap )\n\t\n\t}, [ target ] )\n\n\n\t/**\n\t * Restore the focus to the latest Document active Element.\n\t */\n\tconst restoreFocusTrap = useCallback<RestoreFocusTrap>( () => {\n\n\t\tlastActiveElement.current?.focus()\n\t\tsetFocusTrapDispatch( false )\n\t\n\t}, [] )\n\n\t\n\tuseEffect( () => {\n\n\t\tif ( ! focusTrap ) return\n\n\t\tconst keyDownHandler = ( event: KeyboardEvent ) => {\n\n\t\t\tif ( event.key !== 'Tab' ) return\n\n\t\t\tconst focusableElements\t\t= Array.from( focusTrap.querySelectorAll<HTMLElement>( focusableSelector ) ),\n\t\t\t\tfirstFocusableElement\t= focusableElements.at( 0 ),\n\t\t\t\tlastFocusableElement\t= focusableElements.at( -1 );\n\n\t\t\tif ( ! event.shiftKey ) {\n\t\t\t\t/**\n\t\t\t\t * Focust the firs element if\n\t\t\t\t * focusing forward and the current focused element is the last one.\n\t\t\t\t */\n\t\t\t\tif ( document.activeElement === lastFocusableElement ) {\n\t\t\t\t\tevent.preventDefault()\n\t\t\t\t\tfirstFocusableElement?.focus()\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\t}\n\n\n\t\t\t/**\n\t\t\t * Focus the last focusable element if\n\t\t\t * focusing backward and the current focused element is the first one.\n\t\t\t * \n\t\t\t */\n\t\t\tif ( document.activeElement === firstFocusableElement ) {\n\t\t\t\tevent.preventDefault()\n\t\t\t\tlastFocusableElement?.focus()\n\t\t\t}\n\n\t\t}\n\n\t\tdocument.addEventListener( 'keydown', keyDownHandler );\n\t\n\t\treturn () => {\n\t\t\tdocument.removeEventListener( 'keydown', keyDownHandler );\n\t\t}\n\n\t}, [ focusTrap ] )\n\n\treturn [ setFocusTrap, restoreFocusTrap ]\n\n}","import { useCallback } from 'react'\nimport {\n\tblockScroll as blockScrollHandler,\n\trestoreScroll as restoreScrollHandler\n} from '@alessiofrittoli/web-utils/dom'\n\n\n/**\n * Prevent Element overflow.\n * \n * @param target (Optional) The React RefObject target HTMLElement. Default: `Document.documentElement`.\n */\nexport const useScrollBlock = ( target?: React.RefObject<HTMLElement | null> ) => {\n\t\n\t/**\n\t * Block scroll.\n\t * \n\t */\n\tconst blockScroll = useCallback(\n\t\t() => blockScrollHandler( target?.current || undefined ), [ target ]\n\t)\n\n\n\t/**\n\t * Restore scroll.\n\t * \n\t */\n\tconst restoreScroll = useCallback(\n\t\t() => restoreScrollHandler( target?.current || undefined ), [ target ]\n\t)\n\n\t\n\treturn [ blockScroll, restoreScroll ] as const\n\n}","import { useEffect, useState } from 'react'\n\n/**\n * Check if the React Hook or Component where this hook is executed is running in a browser environment.\n * \n * @returns `true` if the React Hook or Component is running in a browser environment, `false` otherwise.\n */\nexport const useIsClient = () => {\n\n\tconst [ isClient, setClient ] = useState( false )\n\n\tuseEffect( () => setClient( true ), [] )\n\n\treturn isClient\n\n}","import { useRef } from 'react'\n\n/**\n * Check if is first React Hook/Component render.\n * \n * @returns `true` at the mount time, then always `false`.\n * - Note that if the React Hook/Component has no state updates, `useIsFirstRender` will always return `true`.\n */\nexport const useIsFirstRender = () => {\n\n\tconst isFirst = useRef( true )\n\n\tif ( isFirst.current ) {\n\t\tisFirst.current = false\n\n\t\treturn true\n\t}\n\n\treturn isFirst.current\n\t\n}","import { useEffect } from 'react'\nimport { useIsFirstRender } from './useIsFirstRender'\n\n\n/**\n * Modified version of `useEffect` that skips the first render.\n *\n * @param\teffect\tImperative function that can return a cleanup function.\n * @param\tdeps\tIf present, effect will only activate if the values in the list change.\n */\nexport const useUpdateEffect = (\n\teffect\t: React.EffectCallback,\n\tdeps?\t: React.DependencyList,\n) => {\n\n\tconst isFirst = useIsFirstRender()\n\n\tuseEffect( () => {\n\t\tif ( ! isFirst ) return effect()\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t}, deps )\n\t\n}"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import {useCallback,useState,useEffect,useRef}from'react';import {LocalStorage}from'@alessiofrittoli/web-utils/storage/LocalStorage';import {SessionStorage}from'@alessiofrittoli/web-utils/storage/SessionStorage';import {getMediaMatches}from'@alessiofrittoli/web-utils/browser-api';import {portraitMediaQuery}from'@alessiofrittoli/web-utils/device';import {blockScroll,restoreScroll}from'@alessiofrittoli/web-utils/dom';var i=(e,t,r="local")=>{let s=useCallback(()=>(r==="local"?LocalStorage:SessionStorage).get(e)??t,[r,e,t]),[n,u]=useState(t),c=useCallback(o=>{u(l=>{let a=o instanceof Function?o(l):o;return (typeof window<"u"&&r==="local"?LocalStorage:SessionStorage).set(e,a),a});},[r,e]);return useEffect(()=>{u(s());},[s]),[n,c]};var P=(e,t)=>i(e,t,"local");var G=(e,t)=>i(e,t,"session");var S=e=>{let[t,r]=useState(getMediaMatches(e)),s=useCallback(()=>r(getMediaMatches(e)),[e]);return useEffect(()=>{let n=window.matchMedia(e);return s(),n.addEventListener("change",s),()=>{n.removeEventListener("change",s);}},[e,s]),t};var _=()=>S(portraitMediaQuery);var V=["input","select","textarea","button","[href]",'[tabindex]:not([tabindex="-1"])'].join(", "),ee=e=>{let[t,r]=useState(false),s=useRef(null),n=useCallback(c=>{s.current=document.activeElement;let o=c||e?.current||false;if(o)return r(o)},[e]),u=useCallback(()=>{s.current?.focus(),r(false);},[]);return useEffect(()=>{if(!t)return;let c=o=>{if(o.key!=="Tab")return;let l=Array.from(t.querySelectorAll(V)),a=l.at(0),f=l.at(-1);if(!o.shiftKey){document.activeElement===f&&(o.preventDefault(),a?.focus());return}document.activeElement===a&&(o.preventDefault(),f?.focus());};return document.addEventListener("keydown",c),()=>{document.removeEventListener("keydown",c);}},[t]),[n,u]};var se=e=>{let t=useCallback(()=>blockScroll(e?.current||void 0),[e]),r=useCallback(()=>restoreScroll(e?.current||void 0),[e]);return [t,r]};var ae=()=>{let[e,t]=useState(false);return useEffect(()=>t(true),[]),e};var F=()=>{let e=useRef(true);return e.current?(e.current=false,true):e.current};var pe=(e,t)=>{let r=F();useEffect(()=>{if(!r)return e()},t);};export{ee as useFocusTrap,ae as useIsClient,F as useIsFirstRender,_ as useIsPortrait,P as useLocalStorage,S as useMediaQuery,se as useScrollBlock,G as useSessionStorage,i as useStorage,pe as useUpdateEffect};//# sourceMappingURL=index.mjs.map
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/browser-api/storage/useStorage.ts","../src/browser-api/storage/useLocalStorage.ts","../src/browser-api/storage/useSessionStorage.ts","../src/browser-api/useMediaQuery.ts","../src/browser-api/useIsPortrait.ts","../src/dom-api/useFocusTrap.ts","../src/dom-api/useScrollBlock.ts","../src/misc/useIsClient.ts","../src/misc/useIsFirstRender.ts","../src/misc/useUpdateEffect.ts"],"names":["useStorage","key","initialValue","type","readValue","useCallback","LocalStorage","SessionStorage","storedValue","setStoredValue","useState","setValue","value","valueToStore","useEffect","useLocalStorage","useSessionStorage","useMediaQuery","query","matches","setMatches","getMediaMatches","matchMediaChangeHandler","matchMedia","useIsPortrait","portraitMediaQuery","focusableSelector","useFocusTrap","target","focusTrap","setFocusTrapDispatch","lastActiveElement","useRef","setFocusTrap","onDemandTarget","restoreFocusTrap","keyDownHandler","event","focusableElements","firstFocusableElement","lastFocusableElement","useScrollBlock","blockScroll","blockScrollHandler","restoreScroll","restoreScrollHandler","useIsClient","isClient","setClient","useIsFirstRender","isFirst","useUpdateEffect","effect","deps"],"mappings":"maAcO,IAAMA,CAAa,CAAA,CACzBC,CACAC,CAAAA,CAAAA,CACAC,CAA+B,CAAA,OAAA,GACO,CAEtC,IAAMC,CAAYC,CAAAA,WAAAA,CAAa,IAC5BF,CAAAA,CAAAA,GAAS,QAAUG,YAAeC,CAAAA,cAAAA,EAClC,GAAQN,CAAAA,CAAI,CAAKC,EAAAA,CAAAA,CACjB,CAAEC,CAAAA,CAAMF,CAAKC,CAAAA,CAAa,CAAE,CAAA,CAQzB,CAAEM,CAAAA,CAAaC,CAAe,CAAA,CAAIC,QAAoBR,CAAAA,CAAa,CAQnES,CAAAA,CAAAA,CAAWN,WAAiCO,CAAAA,CAAAA,EAAS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GDA,CAAQ,CAAA,OAEhB,ECVO,IAAMC,EAAkB,CAAA,CAC9BC,CACAC,CAAAA,CAAAA,GACI,CAEJ,IAAMH,CAAUD,CAAAA,CAAAA,EAEhBnC,CAAAA,SAAAA,CAAW,IAAM,CAChB,GAAK,CAAEoC,CAAU,CAAA,OAAOE,CAAO,EAEhC,CAAGC,CAAAA,CAAK,EAET","file":"index.mjs","sourcesContent":["import { useCallback, useEffect, useState } from 'react'\nimport { LocalStorage } from '@alessiofrittoli/web-utils/storage/LocalStorage'\nimport { SessionStorage } from '@alessiofrittoli/web-utils/storage/SessionStorage'\n\ntype Value<T>\t\t= T | undefined | null\ntype SetValue<T>\t= React.Dispatch<React.SetStateAction<T>>\n\n/**\n * Easly handle Local or Session Storage State.\n * \n * @param\tkey\t\t\t\tThe storage item key.\n * @param\tinitialValue\tThe storage item initial value.\n * @param\ttype\t\t\t( Optional ) The storage API to use. Default: `local`.\n */\nexport const useStorage = <T = string>(\n\tkey\t\t\t\t: string,\n\tinitialValue?\t: T,\n\ttype\t\t\t: 'local' | 'session' = 'local'\n): [ Value<T>, SetValue<Value<T>> ] => {\n\n\tconst readValue = useCallback( () => (\n\t\t( type === 'local' ? LocalStorage : SessionStorage )\n\t\t\t.get<T>( key ) ?? initialValue\n\t), [ type, key, initialValue ] )\n\n\n\t/**\n\t * State to store our value.\n\t * Pass initial state function to useState so logic is only executed once.\n\t * \n\t */\n\tconst [ storedValue, setStoredValue ] = useState<Value<T>>( initialValue )\n\n\t/**\n\t * Return a wrapped version of useState's setter function that\n\t * persists the new value to localStorage | sessionStorage.\n\t * \n\t * @param value The SetStateAction value.\n\t */\n\tconst setValue = useCallback<SetValue<Value<T>>>( value => {\n\n\t\tsetStoredValue( storedValue => {\n\t\t\tconst valueToStore = value instanceof Function ? value( storedValue ) : value\n\t\n\t\t\t;(\n\t\t\t\ttypeof window !== 'undefined' &&\n\t\t\t\ttype === 'local' ? LocalStorage : SessionStorage\n\t\t\t).set( key, valueToStore )\n\n\t\t\treturn valueToStore\n\t\t} )\n\n\n\t}, [ type, key ] )\n\n\tuseEffect( () => {\n\t\tsetStoredValue( readValue() )\n\t}, [ readValue ] )\n\n\treturn [ storedValue, setValue ]\n}","import { useStorage } from './useStorage'\n\n/**\n * useLocalStorage hook.\n * \n * @param\tkey\t\t\t\tThe local storage item key.\n * @param\tinitialValue\tThe local storage item initial value.\n */\nexport const useLocalStorage = <T = string>(\n\tkey\t\t\t\t: string,\n\tinitialValue?\t: T\n) => useStorage( key, initialValue, 'local' )","import { useStorage } from './useStorage'\n\n/**\n * useSessionStorage hook.\n * \n * @param\tkey\t\t\t\tThe session item key.\n * @param\tinitialValue\tThe session item initial value.\n */\nexport const useSessionStorage = <T = string>(\n\tkey\t\t\t\t: string,\n\tinitialValue?\t: T\n) => useStorage( key, initialValue, 'session' )","import { useCallback, useEffect, useState } from 'react'\nimport { getMediaMatches } from '@alessiofrittoli/web-utils/browser-api'\n\n\n/**\n * Get Document Media matches and listen for changes.\n *\n * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia)\n *\n * @param\tquery A string specifying the media query to parse into a `MediaQueryList`.\n * @returns\tA boolean value that returns `true` if the document currently matches the media query list, or `false` if not.\n */\nexport const useMediaQuery = ( query: string ) => {\n\n\tconst [ matches, setMatches ]\t= useState( getMediaMatches( query ) )\n\tconst matchMediaChangeHandler\t= useCallback( () => setMatches( getMediaMatches( query ) ), [ query ] )\n\n\n\tuseEffect( () => {\n\n\t\tconst matchMedia = window.matchMedia( query )\n\t\t// Triggered at the first client-side load\n\t\tmatchMediaChangeHandler()\n\t\t// Listen matchMedia\n\t\tmatchMedia.addEventListener( 'change', matchMediaChangeHandler )\n\n\t\treturn () => {\n\t\t\tmatchMedia.removeEventListener( 'change', matchMediaChangeHandler )\n\t\t}\n\n\t}, [ query, matchMediaChangeHandler ] )\n\n\treturn matches\n\t\n}","import { useMediaQuery } from './useMediaQuery'\nimport { portraitMediaQuery } from '@alessiofrittoli/web-utils/device'\n\n/**\n * Check if device is portrait oriented.\n * \n * State get updated when device orientation changes.\n *\n * @returns\t`true` if the device is portrait oriented, `false` otherwise.\n */\nexport const useIsPortrait = () => useMediaQuery( portraitMediaQuery )","import { useCallback, useEffect, useRef, useState } from 'react'\n\ntype SetFocusTrap = ( target?: HTMLElement ) => void\ntype RestoreFocusTrap = () => void\n\nconst focusableSelector = [\n\t'input', 'select', 'textarea',\n\t'button', '[href]', '[tabindex]:not([tabindex=\"-1\"])',\n].join( ', ' )\n\n\n/**\n * Trap focus inside the given HTML Element.\n * \n * @param target (Optional) The target HTMLElement React RefObject to trap focus within.\n * \t\t\t\t\tIf no target is given, you must provide the target HTMLElement when calling `setFocusTrap`.\n * \n * @returns A tuple containing:\n * - `setFocusTrap`: A function to enable the focus trap. Optionally accept an HTMLElement as target.\n * - `restoreFocusTrap`: A function to restore the previous focus state.\n */\nexport const useFocusTrap = (\n\ttarget?: React.RefObject<HTMLElement | null>\n): readonly [ SetFocusTrap, RestoreFocusTrap ] => {\n\n\tconst [ focusTrap, setFocusTrapDispatch ] = (\n\t\tuseState<HTMLElement | false>( false )\n\t)\n\n\tconst lastActiveElement = useRef<HTMLElement>( null )\n\t\n\t/**\n\t * Enable the focus trap.\n\t * \n\t */\n\tconst setFocusTrap = useCallback<SetFocusTrap>( onDemandTarget => {\n\n\t\tlastActiveElement.current\t= document.activeElement as HTMLElement\n\t\tconst focusTrap\t\t\t\t= onDemandTarget || target?.current || false\n\t\t\n\t\tif ( ! focusTrap ) return\n\n\t\treturn setFocusTrapDispatch( focusTrap )\n\t\n\t}, [ target ] )\n\n\n\t/**\n\t * Restore the focus to the latest Document active Element.\n\t */\n\tconst restoreFocusTrap = useCallback<RestoreFocusTrap>( () => {\n\n\t\tlastActiveElement.current?.focus()\n\t\tsetFocusTrapDispatch( false )\n\t\n\t}, [] )\n\n\t\n\tuseEffect( () => {\n\n\t\tif ( ! focusTrap ) return\n\n\t\tconst keyDownHandler = ( event: KeyboardEvent ) => {\n\n\t\t\tif ( event.key !== 'Tab' ) return\n\n\t\t\tconst focusableElements\t\t= Array.from( focusTrap.querySelectorAll<HTMLElement>( focusableSelector ) ),\n\t\t\t\tfirstFocusableElement\t= focusableElements.at( 0 ),\n\t\t\t\tlastFocusableElement\t= focusableElements.at( -1 );\n\n\t\t\tif ( ! event.shiftKey ) {\n\t\t\t\t/**\n\t\t\t\t * Focust the firs element if\n\t\t\t\t * focusing forward and the current focused element is the last one.\n\t\t\t\t */\n\t\t\t\tif ( document.activeElement === lastFocusableElement ) {\n\t\t\t\t\tevent.preventDefault()\n\t\t\t\t\tfirstFocusableElement?.focus()\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\t}\n\n\n\t\t\t/**\n\t\t\t * Focus the last focusable element if\n\t\t\t * focusing backward and the current focused element is the first one.\n\t\t\t * \n\t\t\t */\n\t\t\tif ( document.activeElement === firstFocusableElement ) {\n\t\t\t\tevent.preventDefault()\n\t\t\t\tlastFocusableElement?.focus()\n\t\t\t}\n\n\t\t}\n\n\t\tdocument.addEventListener( 'keydown', keyDownHandler );\n\t\n\t\treturn () => {\n\t\t\tdocument.removeEventListener( 'keydown', keyDownHandler );\n\t\t}\n\n\t}, [ focusTrap ] )\n\n\treturn [ setFocusTrap, restoreFocusTrap ]\n\n}","import { useCallback } from 'react'\nimport {\n\tblockScroll as blockScrollHandler,\n\trestoreScroll as restoreScrollHandler\n} from '@alessiofrittoli/web-utils/dom'\n\n\n/**\n * Prevent Element overflow.\n * \n * @param target (Optional) The React RefObject target HTMLElement. Default: `Document.documentElement`.\n */\nexport const useScrollBlock = ( target?: React.RefObject<HTMLElement | null> ) => {\n\t\n\t/**\n\t * Block scroll.\n\t * \n\t */\n\tconst blockScroll = useCallback(\n\t\t() => blockScrollHandler( target?.current || undefined ), [ target ]\n\t)\n\n\n\t/**\n\t * Restore scroll.\n\t * \n\t */\n\tconst restoreScroll = useCallback(\n\t\t() => restoreScrollHandler( target?.current || undefined ), [ target ]\n\t)\n\n\t\n\treturn [ blockScroll, restoreScroll ] as const\n\n}","import { useEffect, useState } from 'react'\n\n/**\n * Check if the React Hook or Component where this hook is executed is running in a browser environment.\n * \n * @returns `true` if the React Hook or Component is running in a browser environment, `false` otherwise.\n */\nexport const useIsClient = () => {\n\n\tconst [ isClient, setClient ] = useState( false )\n\n\tuseEffect( () => setClient( true ), [] )\n\n\treturn isClient\n\n}","import { useRef } from 'react'\n\n/**\n * Check if is first React Hook/Component render.\n * \n * @returns `true` at the mount time, then always `false`.\n * - Note that if the React Hook/Component has no state updates, `useIsFirstRender` will always return `true`.\n */\nexport const useIsFirstRender = () => {\n\n\tconst isFirst = useRef( true )\n\n\tif ( isFirst.current ) {\n\t\tisFirst.current = false\n\n\t\treturn true\n\t}\n\n\treturn isFirst.current\n\t\n}","import { useEffect } from 'react'\nimport { useIsFirstRender } from './useIsFirstRender'\n\n\n/**\n * Modified version of `useEffect` that skips the first render.\n *\n * @param\teffect\tImperative function that can return a cleanup function.\n * @param\tdeps\tIf present, effect will only activate if the values in the list change.\n */\nexport const useUpdateEffect = (\n\teffect\t: React.EffectCallback,\n\tdeps?\t: React.DependencyList,\n) => {\n\n\tconst isFirst = useIsFirstRender()\n\n\tuseEffect( () => {\n\t\tif ( ! isFirst ) return effect()\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t}, deps )\n\t\n}"]}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
'use strict';var react=require('react');var i=()=>{let[e,t]=react.useState(false);return react.useEffect(()=>t(true),[]),e};var r=()=>{let e=react.useRef(true);return e.current?(e.current=false,true):e.current};var R=(e,t)=>{let s=r();react.useEffect(()=>{if(!s)return e()},t);};exports.useIsClient=i;exports.useIsFirstRender=r;exports.useUpdateEffect=R;//# sourceMappingURL=index.js.map
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/misc/useIsClient.ts","../../src/misc/useIsFirstRender.ts","../../src/misc/useUpdateEffect.ts"],"names":["useIsClient","isClient","setClient","useState","useEffect","useIsFirstRender","isFirst","useRef","useUpdateEffect","effect","deps"],"mappings":"4CAOaA,CAAc,CAAA,IAAM,CAEhC,GAAM,CAAEC,CAAUC,CAAAA,CAAU,CAAIC,CAAAA,cAAAA,CAAU,KAAM,CAEhD,CAAA,OAAAC,eAAW,CAAA,IAAMF,EAAW,IAAK,CAAA,CAAG,EAAG,EAEhCD,CAER,ECPO,IAAMI,CAAmB,CAAA,IAAM,CAErC,IAAMC,CAAAA,CAAUC,YAAQ,CAAA,IAAK,EAE7B,OAAKD,CAAAA,CAAQ,OACZA,EAAAA,CAAAA,CAAQ,QAAU,KAEX,CAAA,IAAA,EAGDA,CAAQ,CAAA,OAEhB,MCVaE,CAAkB,CAAA,CAC9BC,CACAC,CAAAA,CAAAA,GACI,CAEJ,IAAMJ,CAAAA,CAAUD,CAAiB,EAAA,CAEjCD,gBAAW,IAAM,CAChB,GAAK,CAAEE,EAAU,OAAOG,CAAAA,EAEzB,CAAA,CAAGC,CAAK,EAET","file":"index.js","sourcesContent":["import { useEffect, useState } from 'react'\n\n/**\n * Check if the React Hook or Component where this hook is executed is running in a browser environment.\n * \n * @returns `true` if the React Hook or Component is running in a browser environment, `false` otherwise.\n */\nexport const useIsClient = () => {\n\n\tconst [ isClient, setClient ] = useState( false )\n\n\tuseEffect( () => setClient( true ), [] )\n\n\treturn isClient\n\n}","import { useRef } from 'react'\n\n/**\n * Check if is first React Hook/Component render.\n * \n * @returns `true` at the mount time, then always `false`.\n * - Note that if the React Hook/Component has no state updates, `useIsFirstRender` will always return `true`.\n */\nexport const useIsFirstRender = () => {\n\n\tconst isFirst = useRef( true )\n\n\tif ( isFirst.current ) {\n\t\tisFirst.current = false\n\n\t\treturn true\n\t}\n\n\treturn isFirst.current\n\t\n}","import { useEffect } from 'react'\nimport { useIsFirstRender } from './useIsFirstRender'\n\n\n/**\n * Modified version of `useEffect` that skips the first render.\n *\n * @param\teffect\tImperative function that can return a cleanup function.\n * @param\tdeps\tIf present, effect will only activate if the values in the list change.\n */\nexport const useUpdateEffect = (\n\teffect\t: React.EffectCallback,\n\tdeps?\t: React.DependencyList,\n) => {\n\n\tconst isFirst = useIsFirstRender()\n\n\tuseEffect( () => {\n\t\tif ( ! isFirst ) return effect()\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t}, deps )\n\t\n}"]}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import {useState,useEffect,useRef}from'react';var i=()=>{let[e,t]=useState(false);return useEffect(()=>t(true),[]),e};var r=()=>{let e=useRef(true);return e.current?(e.current=false,true):e.current};var R=(e,t)=>{let s=r();useEffect(()=>{if(!s)return e()},t);};export{i as useIsClient,r as useIsFirstRender,R as useUpdateEffect};//# sourceMappingURL=index.mjs.map
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/misc/useIsClient.ts","../../src/misc/useIsFirstRender.ts","../../src/misc/useUpdateEffect.ts"],"names":["useIsClient","isClient","setClient","useState","useEffect","useIsFirstRender","isFirst","useRef","useUpdateEffect","effect","deps"],"mappings":"kDAOaA,CAAc,CAAA,IAAM,CAEhC,GAAM,CAAEC,CAAUC,CAAAA,CAAU,CAAIC,CAAAA,QAAAA,CAAU,KAAM,CAEhD,CAAA,OAAAC,SAAW,CAAA,IAAMF,EAAW,IAAK,CAAA,CAAG,EAAG,EAEhCD,CAER,ECPO,IAAMI,CAAmB,CAAA,IAAM,CAErC,IAAMC,CAAAA,CAAUC,MAAQ,CAAA,IAAK,EAE7B,OAAKD,CAAAA,CAAQ,OACZA,EAAAA,CAAAA,CAAQ,QAAU,KAEX,CAAA,IAAA,EAGDA,CAAQ,CAAA,OAEhB,MCVaE,CAAkB,CAAA,CAC9BC,CACAC,CAAAA,CAAAA,GACI,CAEJ,IAAMJ,CAAAA,CAAUD,CAAiB,EAAA,CAEjCD,UAAW,IAAM,CAChB,GAAK,CAAEE,EAAU,OAAOG,CAAAA,EAEzB,CAAA,CAAGC,CAAK,EAET","file":"index.mjs","sourcesContent":["import { useEffect, useState } from 'react'\n\n/**\n * Check if the React Hook or Component where this hook is executed is running in a browser environment.\n * \n * @returns `true` if the React Hook or Component is running in a browser environment, `false` otherwise.\n */\nexport const useIsClient = () => {\n\n\tconst [ isClient, setClient ] = useState( false )\n\n\tuseEffect( () => setClient( true ), [] )\n\n\treturn isClient\n\n}","import { useRef } from 'react'\n\n/**\n * Check if is first React Hook/Component render.\n * \n * @returns `true` at the mount time, then always `false`.\n * - Note that if the React Hook/Component has no state updates, `useIsFirstRender` will always return `true`.\n */\nexport const useIsFirstRender = () => {\n\n\tconst isFirst = useRef( true )\n\n\tif ( isFirst.current ) {\n\t\tisFirst.current = false\n\n\t\treturn true\n\t}\n\n\treturn isFirst.current\n\t\n}","import { useEffect } from 'react'\nimport { useIsFirstRender } from './useIsFirstRender'\n\n\n/**\n * Modified version of `useEffect` that skips the first render.\n *\n * @param\teffect\tImperative function that can return a cleanup function.\n * @param\tdeps\tIf present, effect will only activate if the values in the list change.\n */\nexport const useUpdateEffect = (\n\teffect\t: React.EffectCallback,\n\tdeps?\t: React.DependencyList,\n) => {\n\n\tconst isFirst = useIsFirstRender()\n\n\tuseEffect( () => {\n\t\tif ( ! isFirst ) return effect()\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t}, deps )\n\t\n}"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if the React Hook or Component where this hook is executed is running in a browser environment.
|
|
3
|
+
*
|
|
4
|
+
* @returns `true` if the React Hook or Component is running in a browser environment, `false` otherwise.
|
|
5
|
+
*/
|
|
6
|
+
declare const useIsClient: () => boolean;
|
|
7
|
+
|
|
8
|
+
export { useIsClient };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if the React Hook or Component where this hook is executed is running in a browser environment.
|
|
3
|
+
*
|
|
4
|
+
* @returns `true` if the React Hook or Component is running in a browser environment, `false` otherwise.
|
|
5
|
+
*/
|
|
6
|
+
declare const useIsClient: () => boolean;
|
|
7
|
+
|
|
8
|
+
export { useIsClient };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/misc/useIsClient.ts"],"names":["useIsClient","isClient","setClient","useState","useEffect"],"mappings":"wCAOaA,IAAAA,CAAAA,CAAc,IAAM,CAEhC,GAAM,CAAEC,CAAUC,CAAAA,CAAU,EAAIC,cAAU,CAAA,KAAM,CAEhD,CAAA,OAAAC,eAAW,CAAA,IAAMF,CAAW,CAAA,IAAK,CAAG,CAAA,EAAG,CAAA,CAEhCD,CAER","file":"useIsClient.js","sourcesContent":["import { useEffect, useState } from 'react'\n\n/**\n * Check if the React Hook or Component where this hook is executed is running in a browser environment.\n * \n * @returns `true` if the React Hook or Component is running in a browser environment, `false` otherwise.\n */\nexport const useIsClient = () => {\n\n\tconst [ isClient, setClient ] = useState( false )\n\n\tuseEffect( () => setClient( true ), [] )\n\n\treturn isClient\n\n}"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/misc/useIsClient.ts"],"names":["useIsClient","isClient","setClient","useState","useEffect"],"mappings":"uCAOaA,IAAAA,CAAAA,CAAc,IAAM,CAEhC,GAAM,CAAEC,CAAUC,CAAAA,CAAU,EAAIC,QAAU,CAAA,KAAM,CAEhD,CAAA,OAAAC,SAAW,CAAA,IAAMF,CAAW,CAAA,IAAK,CAAG,CAAA,EAAG,CAAA,CAEhCD,CAER","file":"useIsClient.mjs","sourcesContent":["import { useEffect, useState } from 'react'\n\n/**\n * Check if the React Hook or Component where this hook is executed is running in a browser environment.\n * \n * @returns `true` if the React Hook or Component is running in a browser environment, `false` otherwise.\n */\nexport const useIsClient = () => {\n\n\tconst [ isClient, setClient ] = useState( false )\n\n\tuseEffect( () => setClient( true ), [] )\n\n\treturn isClient\n\n}"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if is first React Hook/Component render.
|
|
3
|
+
*
|
|
4
|
+
* @returns `true` at the mount time, then always `false`.
|
|
5
|
+
* - Note that if the React Hook/Component has no state updates, `useIsFirstRender` will always return `true`.
|
|
6
|
+
*/
|
|
7
|
+
declare const useIsFirstRender: () => boolean;
|
|
8
|
+
|
|
9
|
+
export { useIsFirstRender };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if is first React Hook/Component render.
|
|
3
|
+
*
|
|
4
|
+
* @returns `true` at the mount time, then always `false`.
|
|
5
|
+
* - Note that if the React Hook/Component has no state updates, `useIsFirstRender` will always return `true`.
|
|
6
|
+
*/
|
|
7
|
+
declare const useIsFirstRender: () => boolean;
|
|
8
|
+
|
|
9
|
+
export { useIsFirstRender };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/misc/useIsFirstRender.ts"],"names":["useIsFirstRender","isFirst","useRef"],"mappings":"wCAQO,IAAMA,EAAmB,IAAM,CAErC,IAAMC,CAAUC,CAAAA,YAAAA,CAAQ,IAAK,CAE7B,CAAA,OAAKD,EAAQ,OACZA,EAAAA,CAAAA,CAAQ,QAAU,KAEX,CAAA,IAAA,EAGDA,EAAQ,OAEhB","file":"useIsFirstRender.js","sourcesContent":["import { useRef } from 'react'\n\n/**\n * Check if is first React Hook/Component render.\n * \n * @returns `true` at the mount time, then always `false`.\n * - Note that if the React Hook/Component has no state updates, `useIsFirstRender` will always return `true`.\n */\nexport const useIsFirstRender = () => {\n\n\tconst isFirst = useRef( true )\n\n\tif ( isFirst.current ) {\n\t\tisFirst.current = false\n\n\t\treturn true\n\t}\n\n\treturn isFirst.current\n\t\n}"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/misc/useIsFirstRender.ts"],"names":["useIsFirstRender","isFirst","useRef"],"mappings":"2BAQO,IAAMA,EAAmB,IAAM,CAErC,IAAMC,CAAUC,CAAAA,MAAAA,CAAQ,IAAK,CAE7B,CAAA,OAAKD,EAAQ,OACZA,EAAAA,CAAAA,CAAQ,QAAU,KAEX,CAAA,IAAA,EAGDA,EAAQ,OAEhB","file":"useIsFirstRender.mjs","sourcesContent":["import { useRef } from 'react'\n\n/**\n * Check if is first React Hook/Component render.\n * \n * @returns `true` at the mount time, then always `false`.\n * - Note that if the React Hook/Component has no state updates, `useIsFirstRender` will always return `true`.\n */\nexport const useIsFirstRender = () => {\n\n\tconst isFirst = useRef( true )\n\n\tif ( isFirst.current ) {\n\t\tisFirst.current = false\n\n\t\treturn true\n\t}\n\n\treturn isFirst.current\n\t\n}"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modified version of `useEffect` that skips the first render.
|
|
3
|
+
*
|
|
4
|
+
* @param effect Imperative function that can return a cleanup function.
|
|
5
|
+
* @param deps If present, effect will only activate if the values in the list change.
|
|
6
|
+
*/
|
|
7
|
+
declare const useUpdateEffect: (effect: React.EffectCallback, deps?: React.DependencyList) => void;
|
|
8
|
+
|
|
9
|
+
export { useUpdateEffect };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modified version of `useEffect` that skips the first render.
|
|
3
|
+
*
|
|
4
|
+
* @param effect Imperative function that can return a cleanup function.
|
|
5
|
+
* @param deps If present, effect will only activate if the values in the list change.
|
|
6
|
+
*/
|
|
7
|
+
declare const useUpdateEffect: (effect: React.EffectCallback, deps?: React.DependencyList) => void;
|
|
8
|
+
|
|
9
|
+
export { useUpdateEffect };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
'use strict';var react=require('react');var r=()=>{let e=react.useRef(true);return e.current?(e.current=false,true):e.current};var p=(e,t)=>{let s=r();react.useEffect(()=>{if(!s)return e()},t);};exports.useUpdateEffect=p;//# sourceMappingURL=useUpdateEffect.js.map
|
|
2
|
+
//# sourceMappingURL=useUpdateEffect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/misc/useIsFirstRender.ts","../../src/misc/useUpdateEffect.ts"],"names":["useIsFirstRender","isFirst","useRef","useUpdateEffect","effect","deps","useEffect"],"mappings":"wCAQO,IAAMA,CAAmB,CAAA,IAAM,CAErC,IAAMC,CAAAA,CAAUC,aAAQ,IAAK,CAAA,CAE7B,OAAKD,CAAQ,CAAA,OAAA,EACZA,CAAQ,CAAA,OAAA,CAAU,KAEX,CAAA,IAAA,EAGDA,EAAQ,OAEhB,CAAA,KCVaE,CAAkB,CAAA,CAC9BC,EACAC,CACI,GAAA,CAEJ,IAAMJ,CAAAA,CAAUD,CAAiB,EAAA,CAEjCM,gBAAW,IAAM,CAChB,GAAK,CAAEL,CAAAA,CAAU,OAAOG,CAAO,EAEhC,CAAGC,CAAAA,CAAK,EAET","file":"useUpdateEffect.js","sourcesContent":["import { useRef } from 'react'\n\n/**\n * Check if is first React Hook/Component render.\n * \n * @returns `true` at the mount time, then always `false`.\n * - Note that if the React Hook/Component has no state updates, `useIsFirstRender` will always return `true`.\n */\nexport const useIsFirstRender = () => {\n\n\tconst isFirst = useRef( true )\n\n\tif ( isFirst.current ) {\n\t\tisFirst.current = false\n\n\t\treturn true\n\t}\n\n\treturn isFirst.current\n\t\n}","import { useEffect } from 'react'\nimport { useIsFirstRender } from './useIsFirstRender'\n\n\n/**\n * Modified version of `useEffect` that skips the first render.\n *\n * @param\teffect\tImperative function that can return a cleanup function.\n * @param\tdeps\tIf present, effect will only activate if the values in the list change.\n */\nexport const useUpdateEffect = (\n\teffect\t: React.EffectCallback,\n\tdeps?\t: React.DependencyList,\n) => {\n\n\tconst isFirst = useIsFirstRender()\n\n\tuseEffect( () => {\n\t\tif ( ! isFirst ) return effect()\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t}, deps )\n\t\n}"]}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import {useEffect,useRef}from'react';var r=()=>{let e=useRef(true);return e.current?(e.current=false,true):e.current};var p=(e,t)=>{let s=r();useEffect(()=>{if(!s)return e()},t);};export{p as useUpdateEffect};//# sourceMappingURL=useUpdateEffect.mjs.map
|
|
2
|
+
//# sourceMappingURL=useUpdateEffect.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/misc/useIsFirstRender.ts","../../src/misc/useUpdateEffect.ts"],"names":["useIsFirstRender","isFirst","useRef","useUpdateEffect","effect","deps","useEffect"],"mappings":"qCAQO,IAAMA,CAAmB,CAAA,IAAM,CAErC,IAAMC,CAAAA,CAAUC,OAAQ,IAAK,CAAA,CAE7B,OAAKD,CAAQ,CAAA,OAAA,EACZA,CAAQ,CAAA,OAAA,CAAU,KAEX,CAAA,IAAA,EAGDA,EAAQ,OAEhB,CAAA,KCVaE,CAAkB,CAAA,CAC9BC,EACAC,CACI,GAAA,CAEJ,IAAMJ,CAAAA,CAAUD,CAAiB,EAAA,CAEjCM,UAAW,IAAM,CAChB,GAAK,CAAEL,CAAAA,CAAU,OAAOG,CAAO,EAEhC,CAAGC,CAAAA,CAAK,EAET","file":"useUpdateEffect.mjs","sourcesContent":["import { useRef } from 'react'\n\n/**\n * Check if is first React Hook/Component render.\n * \n * @returns `true` at the mount time, then always `false`.\n * - Note that if the React Hook/Component has no state updates, `useIsFirstRender` will always return `true`.\n */\nexport const useIsFirstRender = () => {\n\n\tconst isFirst = useRef( true )\n\n\tif ( isFirst.current ) {\n\t\tisFirst.current = false\n\n\t\treturn true\n\t}\n\n\treturn isFirst.current\n\t\n}","import { useEffect } from 'react'\nimport { useIsFirstRender } from './useIsFirstRender'\n\n\n/**\n * Modified version of `useEffect` that skips the first render.\n *\n * @param\teffect\tImperative function that can return a cleanup function.\n * @param\tdeps\tIf present, effect will only activate if the values in the list change.\n */\nexport const useUpdateEffect = (\n\teffect\t: React.EffectCallback,\n\tdeps?\t: React.DependencyList,\n) => {\n\n\tconst isFirst = useIsFirstRender()\n\n\tuseEffect( () => {\n\t\tif ( ! isFirst ) return effect()\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t}, deps )\n\t\n}"]}
|
package/license.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2025 Alessio Frittoli.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alessiofrittoli/react-hooks",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "TypeScript React utility Hooks",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Alessio Frittoli",
|
|
7
|
+
"email": "info@alessiofrittoli.it",
|
|
8
|
+
"url": "https://alessiofrittoli.it"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"funding": [
|
|
12
|
+
{
|
|
13
|
+
"type": "github",
|
|
14
|
+
"url": "https://github.com/sponsors/alessiofrittoli"
|
|
15
|
+
}
|
|
16
|
+
],
|
|
17
|
+
"keywords": [
|
|
18
|
+
"react",
|
|
19
|
+
"react-hooks"
|
|
20
|
+
],
|
|
21
|
+
"homepage": "https://github.com/alessiofrittoli/react-hooks#readme",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/alessiofrittoli/react-hooks/issues",
|
|
24
|
+
"email": "info@alessiofrittoli.it"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/alessiofrittoli/react-hooks.git"
|
|
29
|
+
},
|
|
30
|
+
"main": "./dist/index.js",
|
|
31
|
+
"module": "./dist/index.mjs",
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"exports": {
|
|
37
|
+
".": {
|
|
38
|
+
"types": "./dist/index.d.ts",
|
|
39
|
+
"import": "./dist/index.mjs",
|
|
40
|
+
"require": "./dist/index.js"
|
|
41
|
+
},
|
|
42
|
+
"./browser-api": {
|
|
43
|
+
"types": "./dist/browser-api/index.d.ts",
|
|
44
|
+
"import": "./dist/browser-api/index.mjs",
|
|
45
|
+
"require": "./dist/browser-api/index.js"
|
|
46
|
+
},
|
|
47
|
+
"./browser-api/storage": {
|
|
48
|
+
"types": "./dist/browser-api/storage/index.d.ts",
|
|
49
|
+
"import": "./dist/browser-api/storage/index.mjs",
|
|
50
|
+
"require": "./dist/browser-api/storage/index.js"
|
|
51
|
+
},
|
|
52
|
+
"./dom-api": {
|
|
53
|
+
"types": "./dist/dom-api/index.d.ts",
|
|
54
|
+
"import": "./dist/dom-api/index.mjs",
|
|
55
|
+
"require": "./dist/dom-api/index.js"
|
|
56
|
+
},
|
|
57
|
+
"./misc": {
|
|
58
|
+
"types": "./dist/misc/index.d.ts",
|
|
59
|
+
"import": "./dist/misc/index.mjs",
|
|
60
|
+
"require": "./dist/misc/index.js"
|
|
61
|
+
},
|
|
62
|
+
"./eslint": {
|
|
63
|
+
"types": "./dist/eslint.d.ts",
|
|
64
|
+
"import": "./dist/eslint.mjs",
|
|
65
|
+
"require": "./dist/eslint.js"
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
"sideEffects": false,
|
|
69
|
+
"scripts": {
|
|
70
|
+
"//1a": "*********************************************************************",
|
|
71
|
+
"//1b": "*********************** BUILD - LINT - RELEASE **********************",
|
|
72
|
+
"//1c": "*********************************************************************",
|
|
73
|
+
"build": "pnpm lint && pnpm test:ci && tsup",
|
|
74
|
+
"build:watch": "tsup --watch",
|
|
75
|
+
"lint": "eslint",
|
|
76
|
+
"release": "node scripts/publish.js --verbose --npm",
|
|
77
|
+
"//2a": "*********************************************************************",
|
|
78
|
+
"//2b": "***************************** UNIT TESTS ****************************",
|
|
79
|
+
"//2c": "*********************************************************************",
|
|
80
|
+
"test": "jest --verbose",
|
|
81
|
+
"test:watch": "jest --watchAll --verbose",
|
|
82
|
+
"test:ci": "jest --ci --verbose",
|
|
83
|
+
"//3a": "*********************************************************************",
|
|
84
|
+
"//3b": "************************ UNIT TESTS COVERAGE ************************",
|
|
85
|
+
"//3c": "*********************************************************************",
|
|
86
|
+
"test:coverage": "pnpm test:watch --coverage",
|
|
87
|
+
"test:coverage:ci": "pnpm test:ci --coverage",
|
|
88
|
+
"test:serve-coverage": "http-server ./coverage/lcov-report --gzip true -p 0 -o --silent",
|
|
89
|
+
"test:coverage:serve": "concurrently --prefix none --kill-others \"pnpm test:coverage\" \"pnpm test:serve-coverage\"",
|
|
90
|
+
"//4a": "*********************************************************************",
|
|
91
|
+
"//4b": "************************ TARGETED UNIT TESTS ************************",
|
|
92
|
+
"//4c": "*********************************************************************",
|
|
93
|
+
"test:storage": "pnpm test:watch browser-api/storage/*",
|
|
94
|
+
"test:browser-api": "pnpm test:watch --testPathPattern='browser-api/(?!storage/).*\\.test\\.(js|jsx|ts|tsx)'",
|
|
95
|
+
"test:dom-api": "pnpm test:watch dom-api/*",
|
|
96
|
+
"test:misc": "pnpm test:watch misc/*"
|
|
97
|
+
},
|
|
98
|
+
"pnpm": {
|
|
99
|
+
"onlyBuiltDependencies": [
|
|
100
|
+
"@alessiofrittoli/type-utils",
|
|
101
|
+
"esbuild"
|
|
102
|
+
]
|
|
103
|
+
},
|
|
104
|
+
"devDependencies": {
|
|
105
|
+
"@alessiofrittoli/node-scripts": "^2.4.0",
|
|
106
|
+
"@eslint/compat": "^1.2.8",
|
|
107
|
+
"@eslint/eslintrc": "^3.3.1",
|
|
108
|
+
"@eslint/js": "^9.25.1",
|
|
109
|
+
"@jest/globals": "^29.7.0",
|
|
110
|
+
"@testing-library/dom": "^10.4.0",
|
|
111
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
112
|
+
"@testing-library/react": "^16.3.0",
|
|
113
|
+
"@testing-library/user-event": "^14.6.1",
|
|
114
|
+
"@types/jest": "^29.5.14",
|
|
115
|
+
"@types/node": "^22.14.1",
|
|
116
|
+
"@types/react": "^19.1.2",
|
|
117
|
+
"@types/react-dom": "^19.1.2",
|
|
118
|
+
"concurrently": "^9.1.2",
|
|
119
|
+
"dotenv": "^16.5.0",
|
|
120
|
+
"eslint": "^9.25.1",
|
|
121
|
+
"eslint-plugin-react": "^7.37.5",
|
|
122
|
+
"eslint-plugin-react-hooks": "^5.2.0",
|
|
123
|
+
"globals": "^16.0.0",
|
|
124
|
+
"http-server": "^14.1.1",
|
|
125
|
+
"jest": "^29.7.0",
|
|
126
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
127
|
+
"react": "^19.1.0",
|
|
128
|
+
"react-dom": "^19.1.0",
|
|
129
|
+
"ts-jest": "^29.3.2",
|
|
130
|
+
"ts-node": "^10.9.2",
|
|
131
|
+
"tsup": "^8.4.0",
|
|
132
|
+
"typescript": "^5.8.3",
|
|
133
|
+
"typescript-eslint": "^8.31.0"
|
|
134
|
+
},
|
|
135
|
+
"dependencies": {
|
|
136
|
+
"@alessiofrittoli/web-utils": "^1.9.1"
|
|
137
|
+
},
|
|
138
|
+
"peerDependencies": {
|
|
139
|
+
"@types/react": "^19",
|
|
140
|
+
"@types/react-dom": "^19",
|
|
141
|
+
"react": "^19",
|
|
142
|
+
"react-dom": "^19"
|
|
143
|
+
}
|
|
144
|
+
}
|