@primereact/hooks 11.0.0-alpha.1
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/LICENSE +21 -0
- package/README.md +1 -0
- package/index.d.ts +7 -0
- package/index.mjs +2 -0
- package/index.mjs.map +1 -0
- package/package.json +37 -0
- package/use-attr-selector/index.d.ts +13 -0
- package/use-attr-selector/index.mjs +2 -0
- package/use-attr-selector/index.mjs.map +1 -0
- package/use-attr-selector/index.test.d.ts +1 -0
- package/use-controlled-state/index.d.ts +46 -0
- package/use-controlled-state/index.mjs +2 -0
- package/use-controlled-state/index.mjs.map +1 -0
- package/use-controlled-state/index.test.d.ts +0 -0
- package/use-event-listener/index.d.ts +75 -0
- package/use-event-listener/index.mjs +2 -0
- package/use-event-listener/index.mjs.map +1 -0
- package/use-event-listener/index.test.d.ts +0 -0
- package/use-id/index.d.ts +19 -0
- package/use-id/index.mjs +2 -0
- package/use-id/index.mjs.map +1 -0
- package/use-id/index.test.d.ts +0 -0
- package/use-match-media/index.d.ts +16 -0
- package/use-match-media/index.mjs +2 -0
- package/use-match-media/index.mjs.map +1 -0
- package/use-match-media/index.test.d.ts +0 -0
- package/use-mount-effect/index.d.ts +18 -0
- package/use-mount-effect/index.mjs +2 -0
- package/use-mount-effect/index.mjs.map +1 -0
- package/use-mount-effect/index.test.d.ts +0 -0
- package/use-previous/index.d.ts +19 -0
- package/use-previous/index.mjs +2 -0
- package/use-previous/index.mjs.map +1 -0
- package/use-previous/index.test.d.ts +0 -0
- package/use-props/index.d.ts +26 -0
- package/use-props/index.mjs +2 -0
- package/use-props/index.mjs.map +1 -0
- package/use-props/index.test.d.ts +0 -0
- package/use-unmount-effect/index.d.ts +18 -0
- package/use-unmount-effect/index.mjs +2 -0
- package/use-unmount-effect/index.mjs.map +1 -0
- package/use-unmount-effect/index.test.d.ts +0 -0
- package/use-update-effect/index.d.ts +19 -0
- package/use-update-effect/index.mjs +2 -0
- package/use-update-effect/index.mjs.map +1 -0
- package/use-update-effect/index.test.d.ts +0 -0
- package/use-view-transition/index.d.ts +20 -0
- package/use-view-transition/index.mjs +2 -0
- package/use-view-transition/index.mjs.map +1 -0
- package/use-view-transition/index.test.d.ts +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 PrimeTek
|
|
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/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# PrimeReact Hooks
|
package/index.d.ts
ADDED
package/index.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var l=Object.defineProperty;var i=Object.getOwnPropertySymbols;var P=Object.prototype.hasOwnProperty,x=Object.prototype.propertyIsEnumerable;var p=(t,r,e)=>r in t?l(t,r,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[r]=e,R=(t,r)=>{for(var e in r||(r={}))P.call(r,e)&&p(t,e,r[e]);if(i)for(var e of i(r))x.call(r,e)&&p(t,e,r[e]);return t};import*as n from"react";function E(t=""){let r=n.useId();return n.useMemo(()=>`${t}${r.replaceAll(/:|«|»/g,"")}`.trim().toLowerCase(),[r,t])}import*as o from"react";function w(t){let r=o.useId(),[e,s]=o.useState(t||`pr_id_${r.replaceAll(/:|«|»/g,"")}`);return o.useEffect(()=>{s(t||`pr_id_${r.replaceAll(/:|«|»/g,"")}`)},[t,r]),e}import*as a from"react";function b(t){let r=a.useRef(!1);a.useEffect(()=>{r.current||(r.current=!0,t==null||t())},[])}import*as m from"react";function T(t={},r={}){return m.useMemo(()=>{let e={props:R({},r),attrs:{}};return Object.entries(t).forEach(([s,u])=>{s in r?e.props[s]=u:e.attrs[s]=u}),e},[t,r])}import*as f from"react";function S(t){f.useEffect(()=>()=>{t==null||t()},[])}import*as c from"react";function $(t,r){let e=c.useRef(!1);c.useEffect(()=>{if(!e.current){e.current=!0;return}return t==null?void 0:t()},r)}import*as d from"react";function M(){return d.useCallback(r=>{document.startViewTransition?document.startViewTransition(()=>{r()}):r()},[])}export{E as useAttrSelector,w as useId,b as useMountEffect,T as useProps,S as useUnmountEffect,$ as useUpdateEffect,M as useViewTransition};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
package/index.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/use-attr-selector/index.ts","../src/use-id/index.ts","../src/use-mount-effect/index.ts","../src/use-props/index.ts","../src/use-unmount-effect/index.ts","../src/use-update-effect/index.ts","../src/use-view-transition/index.ts"],"sourcesContent":["import * as React from 'react';\n\n/**\n * Generates a unique attribute selector based on the provided prefix.\n *\n * @param prefix - The prefix to prepend to the generated selector.\n * @returns A unique string combining the prefix and the selector.\n *\n * @example\n * ```ts\n * const selector = useAttrSelector('foo');\n * console.log(selector); // e.g., 'foo0'\n * ```\n */\nexport function useAttrSelector(prefix: string = ''): string {\n const id = React.useId();\n\n return React.useMemo(() => `${prefix}${id.replaceAll(/:|«|»/g, '')}`.trim().toLowerCase(), [id, prefix]);\n}\n","import * as React from 'react';\n\n/**\n * Generates a unique identifier.\n *\n * @param initialValue Optional initial value for the ID.\n * @returns The generated or provided ID.\n *\n * @example\n * ```tsx\n * const id = useId('foo');\n * console.log(id); // 'foo'\n * ```\n *\n * @example\n * ```tsx\n * const defaultId = useId();\n * console.log(defaultId); // e.g., 'pr_id_0'\n * ```\n */\nexport function useId(initialValue?: string): string {\n const idx = React.useId();\n const [idState, setIdState] = React.useState<string>(initialValue || `pr_id_${idx.replaceAll(/:|«|»/g, '')}`);\n\n React.useEffect(() => {\n setIdState(initialValue || `pr_id_${idx.replaceAll(/:|«|»/g, '')}`);\n }, [initialValue, idx]);\n\n return idState;\n}\n","import * as React from 'react';\n\n/**\n * Custom hook that runs a mount effect only once.\n * This is similar to `useEffect`, but it only runs on mount.\n *\n * @param {React.EffectCallback} effect the callback function\n * @returns {void}\n *\n * @example\n * ```tsx\n * const MyComponent = () => {\n * useMountEffect(() => {\n * console.log('Mounted');\n * });\n * };\n * ```\n */\nexport function useMountEffect(effect: React.EffectCallback): void {\n const mounted = React.useRef<boolean>(false);\n\n React.useEffect(() => {\n if (!mounted.current) {\n mounted.current = true;\n\n effect?.();\n }\n }, []);\n}\n","import * as React from 'react';\n\n/**\n * Used to merge and differentiate incoming props with the default props.\n * - Keys that exist in both `props1` and `props2` are placed in `props`, with values from `props1`.\n * - Keys that exist in `props1` but not in `props2` are placed in `attrs`.\n *\n * @template P1 The type of the incoming set of props.\n * @template P2 The type of the default set of props.\n *\n * @param props1 The incoming set of props (e.g., user-defined or dynamic props).\n * @param props2 The default set of props (e.g., default props).\n * @returns An object containing:\n * - `props`: A new object containing keys that exist in both `props1` and `props2`, using values from `props1`.\n * - `attrs`: A new object containing keys that exist only in `props1`, excluding any keys from `props2`.\n *\n * @example\n * ```ts\n * const { props, attrs } = useProps({ id: 'foo', className: 'bar' }, { className: 'baz' });\n *\n * console.log(props); // { className: 'bar' }\n * console.log(attrs); // { id: 'foo' }\n * ```\n */\nexport function useProps<P1, P2>(props1: P1 = {} as P1, props2: P2 = {} as P2) {\n type Props = Pick<P1 & P2, keyof P2>;\n type Attrs = Omit<P1, keyof P2>;\n type Result = { props: Props; attrs: Attrs };\n\n return React.useMemo(() => {\n const result: Result = { props: { ...props2 } as Props, attrs: {} as Attrs };\n\n Object.entries(props1 as Record<string, unknown>).forEach(([key, value]) => {\n if (key in (props2 as Record<string, unknown>)) {\n (result.props as Record<string, unknown>)[key] = value;\n } else {\n (result.attrs as Record<string, unknown>)[key] = value;\n }\n });\n\n return result;\n }, [props1, props2]);\n}\n","import * as React from 'react';\n\n/**\n * Custom hook that runs an unmount effect only once.\n * This is similar to `useEffect`, but it only runs on unmount.\n *\n * @param {React.EffectCallback} effect the callback function\n * @returns {void}\n *\n * @example\n * ```tsx\n * const Component = () => {\n * useUnmountEffect(() => {\n * console.log('Unmounted');\n * });\n * };\n * ```\n */\nexport function useUnmountEffect(effect: React.EffectCallback): void {\n React.useEffect(() => {\n return () => {\n effect?.();\n };\n }, []);\n}\n","import * as React from 'react';\n\n/**\n * Custom hook that runs an update effect whenever dependencies change.\n * This is similar to `useEffect`, but it does not run on mount.\n *\n * @param {React.EffectCallback} effect the callback function\n * @param {React.DependencyList} dependencies the dependencies\n * @returns {void}\n *\n * @example\n * ```tsx\n * const Component = ({ value }) => {\n * useUpdateEffect(() => {\n * console.log('Updated');\n * }, [value]);\n * };\n * ```\n */\nexport function useUpdateEffect(effect: React.EffectCallback, dependencies?: React.DependencyList): void {\n const mounted = React.useRef<boolean>(false);\n\n React.useEffect(() => {\n if (!mounted.current) {\n mounted.current = true;\n\n return;\n }\n\n return effect?.();\n }, dependencies);\n}\n","import * as React from 'react';\n\n/**\n * Starts a view transition.\n *\n * @returns A function that starts a view transition.\n *\n * @example\n * ```tsx\n * const Component = () => {\n * const startTransition = useViewTransition();\n *\n * const handleClick = () => {\n * startTransition(() => {\n * // Perform some work\n * });\n * };\n *\n * return <button onClick={handleClick}>Click me</button>;\n * };\n */\nexport function useViewTransition() {\n const startTransition = React.useCallback((callback: () => void) => {\n if (document.startViewTransition) {\n document.startViewTransition(() => {\n callback();\n });\n } else {\n callback();\n }\n }, []);\n\n return startTransition;\n}\n"],"mappings":"yVAAA,UAAYA,MAAW,QAchB,SAASC,EAAgBC,EAAiB,GAAY,CACzD,IAAMC,EAAW,QAAM,EAEvB,OAAa,UAAQ,IAAM,GAAGD,CAAM,GAAGC,EAAG,WAAW,SAAU,EAAE,CAAC,GAAG,KAAK,EAAE,YAAY,EAAG,CAACA,EAAID,CAAM,CAAC,CAC3G,CClBA,UAAYE,MAAW,QAoBhB,SAASC,EAAMC,EAA+B,CACjD,IAAMC,EAAY,QAAM,EAClB,CAACC,EAASC,CAAU,EAAU,WAAiBH,GAAgB,SAASC,EAAI,WAAW,SAAU,EAAE,CAAC,EAAE,EAE5G,OAAM,YAAU,IAAM,CAClBE,EAAWH,GAAgB,SAASC,EAAI,WAAW,SAAU,EAAE,CAAC,EAAE,CACtE,EAAG,CAACD,EAAcC,CAAG,CAAC,EAEfC,CACX,CC7BA,UAAYE,MAAW,QAkBhB,SAASC,EAAeC,EAAoC,CAC/D,IAAMC,EAAgB,SAAgB,EAAK,EAErC,YAAU,IAAM,CACbA,EAAQ,UACTA,EAAQ,QAAU,GAElBD,GAAA,MAAAA,IAER,EAAG,CAAC,CAAC,CACT,CC5BA,UAAYE,MAAW,QAwBhB,SAASC,EAAiBC,EAAa,CAAC,EAASC,EAAa,CAAC,EAAS,CAK3E,OAAa,UAAQ,IAAM,CACvB,IAAMC,EAAiB,CAAE,MAAOC,EAAA,GAAKF,GAAmB,MAAO,CAAC,CAAW,EAE3E,cAAO,QAAQD,CAAiC,EAAE,QAAQ,CAAC,CAACI,EAAKC,CAAK,IAAM,CACpED,KAAQH,EACPC,EAAO,MAAkCE,CAAG,EAAIC,EAEhDH,EAAO,MAAkCE,CAAG,EAAIC,CAEzD,CAAC,EAEMH,CACX,EAAG,CAACF,EAAQC,CAAM,CAAC,CACvB,CC1CA,UAAYK,MAAW,QAkBhB,SAASC,EAAiBC,EAAoC,CAC3D,YAAU,IACL,IAAM,CACTA,GAAA,MAAAA,GACJ,EACD,CAAC,CAAC,CACT,CCxBA,UAAYC,MAAW,QAmBhB,SAASC,EAAgBC,EAA8BC,EAA2C,CACrG,IAAMC,EAAgB,SAAgB,EAAK,EAErC,YAAU,IAAM,CAClB,GAAI,CAACA,EAAQ,QAAS,CAClBA,EAAQ,QAAU,GAElB,MACJ,CAEA,OAAOF,GAAA,YAAAA,GACX,EAAGC,CAAY,CACnB,CC/BA,UAAYE,MAAW,QAqBhB,SAASC,GAAoB,CAWhC,OAV8B,cAAaC,GAAyB,CAC5D,SAAS,oBACT,SAAS,oBAAoB,IAAM,CAC/BA,EAAS,CACb,CAAC,EAEDA,EAAS,CAEjB,EAAG,CAAC,CAAC,CAGT","names":["React","useAttrSelector","prefix","id","React","useId","initialValue","idx","idState","setIdState","React","useMountEffect","effect","mounted","React","useProps","props1","props2","result","__spreadValues","key","value","React","useUnmountEffect","effect","React","useUpdateEffect","effect","dependencies","mounted","React","useViewTransition","callback"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@primereact/hooks",
|
|
3
|
+
"version": "11.0.0-alpha.1",
|
|
4
|
+
"author": "PrimeTek Informatics",
|
|
5
|
+
"description": "",
|
|
6
|
+
"homepage": "https://primereact.org/",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/primefaces/primereact.git",
|
|
11
|
+
"directory": "packages/hooks"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/primefaces/primereact/issues"
|
|
15
|
+
},
|
|
16
|
+
"main": "./index.mjs",
|
|
17
|
+
"module": "./index.mjs",
|
|
18
|
+
"types": "./index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./index.d.ts",
|
|
22
|
+
"import": "./index.mjs",
|
|
23
|
+
"default": "./index.mjs"
|
|
24
|
+
},
|
|
25
|
+
"./*": {
|
|
26
|
+
"types": "./*/index.d.ts",
|
|
27
|
+
"import": "./*/index.mjs",
|
|
28
|
+
"default": "./*/index.mjs"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@primeuix/utils": "^0.6.0-beta.2"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a unique attribute selector based on the provided prefix.
|
|
3
|
+
*
|
|
4
|
+
* @param prefix - The prefix to prepend to the generated selector.
|
|
5
|
+
* @returns A unique string combining the prefix and the selector.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const selector = useAttrSelector('foo');
|
|
10
|
+
* console.log(selector); // e.g., 'foo0'
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare function useAttrSelector(prefix?: string): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-attr-selector/index.ts"],"sourcesContent":["import * as React from 'react';\n\n/**\n * Generates a unique attribute selector based on the provided prefix.\n *\n * @param prefix - The prefix to prepend to the generated selector.\n * @returns A unique string combining the prefix and the selector.\n *\n * @example\n * ```ts\n * const selector = useAttrSelector('foo');\n * console.log(selector); // e.g., 'foo0'\n * ```\n */\nexport function useAttrSelector(prefix: string = ''): string {\n const id = React.useId();\n\n return React.useMemo(() => `${prefix}${id.replaceAll(/:|«|»/g, '')}`.trim().toLowerCase(), [id, prefix]);\n}\n"],"mappings":"AAAA,UAAYA,MAAW,QAchB,SAASC,EAAgBC,EAAiB,GAAY,CACzD,IAAMC,EAAW,QAAM,EAEvB,OAAa,UAAQ,IAAM,GAAGD,CAAM,GAAGC,EAAG,WAAW,SAAU,EAAE,CAAC,GAAG,KAAK,EAAE,YAAY,EAAG,CAACA,EAAID,CAAM,CAAC,CAC3G","names":["React","useAttrSelector","prefix","id"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The options for the `useControlledState` hook.
|
|
3
|
+
*/
|
|
4
|
+
export interface UseControlledStateOptions<T = unknown, E = unknown> {
|
|
5
|
+
/**
|
|
6
|
+
* The value of the controlled state.
|
|
7
|
+
*/
|
|
8
|
+
value?: T | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* The default value of the uncontrolled state.
|
|
11
|
+
*/
|
|
12
|
+
defaultValue?: T | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* Callback function that is called when the value changes.
|
|
15
|
+
*/
|
|
16
|
+
onChange?: (newValue: E) => void;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The return type of the `useControlledState` hook.
|
|
20
|
+
* A tuple containing the current value and a function to update it.
|
|
21
|
+
*/
|
|
22
|
+
export type UseControlledStateReturnType<T = unknown> = [T | undefined, (inValue: unknown | ((prev?: T) => unknown)) => void, boolean];
|
|
23
|
+
/**
|
|
24
|
+
* A custom hook that manages controlled and uncontrolled state.
|
|
25
|
+
*
|
|
26
|
+
* @param {UseControlledStateOptions} options - The options for the controlled state.
|
|
27
|
+
* @returns A tuple containing the current value and a function to update it.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```tsx
|
|
31
|
+
* const ControlledComponent = () => {
|
|
32
|
+
* const [controlledValue, setControlledValue] = React.useState('');
|
|
33
|
+
*
|
|
34
|
+
* const [value, setValue] = useControlledState({
|
|
35
|
+
* value: controlledValue,
|
|
36
|
+
* defaultValue: 'initial value',
|
|
37
|
+
* onChange: (newValue) => {
|
|
38
|
+
* setControlledValue(newValue);
|
|
39
|
+
* }
|
|
40
|
+
* });
|
|
41
|
+
*
|
|
42
|
+
* return <Component value={value} onChange={(e) => setValue(e.target.value)} />;
|
|
43
|
+
* };
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare function useControlledState<T = unknown, E = T>({ value, defaultValue, onChange }: UseControlledStateOptions<T, E>): UseControlledStateReturnType<T | undefined>;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{resolve as p}from"@primeuix/utils";import*as t from"react";function w({value:o,defaultValue:r,onChange:n}){let[s,d]=t.useState(r!=null?r:o),e=o!==void 0,u=e?o:s,a=t.useCallback(i=>{let[l,T]=p(i,u,e);n==null||n(T),e||d(l)},[u,e,n]);return[u,a,e]}export{w as useControlledState};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-controlled-state/index.ts"],"sourcesContent":["import { resolve } from '@primeuix/utils';\nimport * as React from 'react';\n\n/**\n * The options for the `useControlledState` hook.\n */\nexport interface UseControlledStateOptions<T = unknown, E = unknown> {\n /**\n * The value of the controlled state.\n */\n value?: T | undefined;\n /**\n * The default value of the uncontrolled state.\n */\n defaultValue?: T | undefined;\n /**\n * Callback function that is called when the value changes.\n */\n onChange?: (newValue: E) => void;\n}\n\n/**\n * The return type of the `useControlledState` hook.\n * A tuple containing the current value and a function to update it.\n */\nexport type UseControlledStateReturnType<T = unknown> = [T | undefined, (inValue: unknown | ((prev?: T) => unknown)) => void, boolean];\n\n/**\n * A custom hook that manages controlled and uncontrolled state.\n *\n * @param {UseControlledStateOptions} options - The options for the controlled state.\n * @returns A tuple containing the current value and a function to update it.\n *\n * @example\n * ```tsx\n * const ControlledComponent = () => {\n * const [controlledValue, setControlledValue] = React.useState('');\n *\n * const [value, setValue] = useControlledState({\n * value: controlledValue,\n * defaultValue: 'initial value',\n * onChange: (newValue) => {\n * setControlledValue(newValue);\n * }\n * });\n *\n * return <Component value={value} onChange={(e) => setValue(e.target.value)} />;\n * };\n * ```\n */\nexport function useControlledState<T = unknown, E = T>({ value, defaultValue, onChange }: UseControlledStateOptions<T, E>): UseControlledStateReturnType<T | undefined> {\n const [valueState, setValueState] = React.useState<T | undefined>(defaultValue ?? value);\n\n const isControlled = value !== undefined;\n const computedValue = isControlled ? (value as T) : valueState;\n\n const setValue = React.useCallback(\n (inValue: unknown | ((prev?: T) => unknown)) => {\n // @todo - update resolve to accept multiple parameters\n const [newValue, onChangeParam] = resolve(inValue, computedValue, isControlled) as [T, E];\n\n onChange?.(onChangeParam);\n\n if (!isControlled) {\n setValueState(newValue);\n }\n },\n [computedValue, isControlled, onChange]\n );\n\n return [computedValue, setValue, isControlled];\n}\n"],"mappings":"AAAA,OAAS,WAAAA,MAAe,kBACxB,UAAYC,MAAW,QAiDhB,SAASC,EAAuC,CAAE,MAAAC,EAAO,aAAAC,EAAc,SAAAC,CAAS,EAAiF,CACpK,GAAM,CAACC,EAAYC,CAAa,EAAU,WAAwBH,GAAA,KAAAA,EAAgBD,CAAK,EAEjFK,EAAeL,IAAU,OACzBM,EAAgBD,EAAgBL,EAAcG,EAE9CI,EAAiB,cAClBC,GAA+C,CAE5C,GAAM,CAACC,EAAUC,CAAa,EAAIb,EAAQW,EAASF,EAAeD,CAAY,EAE9EH,GAAA,MAAAA,EAAWQ,GAENL,GACDD,EAAcK,CAAQ,CAE9B,EACA,CAACH,EAAeD,EAAcH,CAAQ,CAC1C,EAEA,MAAO,CAACI,EAAeC,EAAUF,CAAY,CACjD","names":["resolve","React","useControlledState","value","defaultValue","onChange","valueState","setValueState","isControlled","computedValue","setValue","inValue","newValue","onChangeParam"]}
|
|
File without changes
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* The options for the `useEventListener` hook.
|
|
4
|
+
*/
|
|
5
|
+
export interface UseEventListenerOptions<T = keyof HTMLElementEventMap> {
|
|
6
|
+
/**
|
|
7
|
+
* The target element to bind the event listener to.
|
|
8
|
+
* @default 'document'
|
|
9
|
+
*/
|
|
10
|
+
target?: 'document' | 'window' | 'body' | HTMLElement | (() => HTMLElement) | React.Ref<HTMLElement> | string | null;
|
|
11
|
+
/**
|
|
12
|
+
* The event type to listen for.
|
|
13
|
+
*/
|
|
14
|
+
type: T | string;
|
|
15
|
+
/**
|
|
16
|
+
* The event listener callback.
|
|
17
|
+
*/
|
|
18
|
+
listener: EventListener;
|
|
19
|
+
/**
|
|
20
|
+
* The event listener options
|
|
21
|
+
*/
|
|
22
|
+
options?: AddEventListenerOptions | boolean;
|
|
23
|
+
/**
|
|
24
|
+
* A boolean indicating whether the event listener should be active.
|
|
25
|
+
*/
|
|
26
|
+
when?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The return type of the `useEventListener` hook.
|
|
30
|
+
* A tuple containing functions;
|
|
31
|
+
* 1. `bind` function is used to bind the event listener.
|
|
32
|
+
* 2. `unbind` function is used to unbind the event listener.
|
|
33
|
+
*/
|
|
34
|
+
export type UseEventListenerReturnType = [(options?: Partial<Omit<UseEventListenerOptions, 'target'>> & {
|
|
35
|
+
target?: UseEventListenerOptions['target'] | Document | null;
|
|
36
|
+
}) => void, () => void];
|
|
37
|
+
/**
|
|
38
|
+
* Listens for the specified event type on the target element.
|
|
39
|
+
*
|
|
40
|
+
* @param {UseEventListenerOptions} options - The options for the event listener.
|
|
41
|
+
* @returns A tuple containing functions;
|
|
42
|
+
* 1. `bind` function is used to bind the event listener.
|
|
43
|
+
* 2. `unbind` function is used to unbind the event listener.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```tsx
|
|
47
|
+
* const Component = () => {
|
|
48
|
+
* const [bind, unbind] = useEventListener({
|
|
49
|
+
* target: 'document',
|
|
50
|
+
* type: 'click',
|
|
51
|
+
* listener: (event) => {
|
|
52
|
+
* console.log(event);
|
|
53
|
+
* },
|
|
54
|
+
* when: true
|
|
55
|
+
* });
|
|
56
|
+
*
|
|
57
|
+
* return <div>Click me</div>;
|
|
58
|
+
* };
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```tsx
|
|
62
|
+
* const Component = () => {
|
|
63
|
+
* const [bind, unbind] = useEventListener({
|
|
64
|
+
* target: () => document.querySelector('.element'),
|
|
65
|
+
* type: 'mouseover',
|
|
66
|
+
* listener: (event) => {
|
|
67
|
+
* console.log(event);
|
|
68
|
+
* }
|
|
69
|
+
* });
|
|
70
|
+
*
|
|
71
|
+
* return <div>MouseOver to `.element`</div>;
|
|
72
|
+
* };
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare function useEventListener({ target, type, listener, options, when }: UseEventListenerOptions): UseEventListenerReturnType;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{getTargetElement as T,isNotEmpty as g}from"@primeuix/utils";import*as e from"react";import*as E from"react";function m(u){let r=E.useRef(null);return E.useEffect(()=>(r.current=u,()=>{r.current=null}),[u]),r.current}function H({target:u="document",type:r,listener:i,options:c,when:o=!0}){let s=e.useRef(null),t=e.useRef(null),p=m(i),v=m(c),L=(n={})=>{let{target:a}=n;g(a)&&(l(),(n.when||o)&&(s.current=T(a))),!t.current&&s.current&&(t.current=f=>i&&i(f),s.current.addEventListener(r,t.current,c))},l=()=>{var n;t.current&&((n=s.current)==null||n.removeEventListener(r,t.current,c),t.current=null)},R=()=>{l(),p=null,v=null},d=e.useCallback(()=>{o?s.current=T(u):(l(),s.current=null)},[u,o]);return e.useEffect(()=>()=>{R()},[]),e.useEffect(()=>{d()},[d]),e.useEffect(()=>{let n=`${p}`!=`${i}`,a=v!==c,f=t.current;f&&(n||a)?(l(),o&&L()):f||R()},[i,c,o]),[L,l]}export{H as useEventListener};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-event-listener/index.ts","../../src/use-previous/index.ts"],"sourcesContent":["import { getTargetElement, isNotEmpty } from '@primeuix/utils';\nimport * as React from 'react';\nimport { usePrevious } from '../use-previous';\n\n/**\n * The options for the `useEventListener` hook.\n */\nexport interface UseEventListenerOptions<T = keyof HTMLElementEventMap> {\n /**\n * The target element to bind the event listener to.\n * @default 'document'\n */\n target?: 'document' | 'window' | 'body' | HTMLElement | (() => HTMLElement) | React.Ref<HTMLElement> | string | null;\n /**\n * The event type to listen for.\n */\n type: T | string;\n /**\n * The event listener callback.\n */\n listener: EventListener;\n /**\n * The event listener options\n */\n options?: AddEventListenerOptions | boolean;\n /**\n * A boolean indicating whether the event listener should be active.\n */\n when?: boolean;\n}\n\n/**\n * The return type of the `useEventListener` hook.\n * A tuple containing functions;\n * 1. `bind` function is used to bind the event listener.\n * 2. `unbind` function is used to unbind the event listener.\n */\nexport type UseEventListenerReturnType = [(options?: Partial<Omit<UseEventListenerOptions, 'target'>> & { target?: UseEventListenerOptions['target'] | Document | null }) => void, () => void];\n\n/**\n * Listens for the specified event type on the target element.\n *\n * @param {UseEventListenerOptions} options - The options for the event listener.\n * @returns A tuple containing functions;\n * 1. `bind` function is used to bind the event listener.\n * 2. `unbind` function is used to unbind the event listener.\n *\n * @example\n * ```tsx\n * const Component = () => {\n * const [bind, unbind] = useEventListener({\n * target: 'document',\n * type: 'click',\n * listener: (event) => {\n * console.log(event);\n * },\n * when: true\n * });\n *\n * return <div>Click me</div>;\n * };\n *\n * @example\n * ```tsx\n * const Component = () => {\n * const [bind, unbind] = useEventListener({\n * target: () => document.querySelector('.element'),\n * type: 'mouseover',\n * listener: (event) => {\n * console.log(event);\n * }\n * });\n *\n * return <div>MouseOver to `.element`</div>;\n * };\n * ```\n */\nexport function useEventListener({ target = 'document', type, listener, options, when = true }: UseEventListenerOptions): UseEventListenerReturnType {\n const targetRef = React.useRef<HTMLElement | null>(null);\n const listenerRef = React.useRef<EventListener | null>(null);\n let prevListener = usePrevious(listener);\n let prevOptions = usePrevious(options);\n\n const bind = (bindOptions: Partial<Omit<UseEventListenerOptions, 'target'>> & { target?: UseEventListenerOptions['target'] | Document | null } = {}) => {\n const { target: bindTarget } = bindOptions;\n\n if (isNotEmpty(bindTarget)) {\n unbind();\n\n if (bindOptions.when || when) {\n targetRef.current = getTargetElement(bindTarget) as HTMLElement;\n }\n }\n\n if (!listenerRef.current && targetRef.current) {\n listenerRef.current = (event: Event) => listener && listener(event);\n targetRef.current.addEventListener(type, listenerRef.current, options);\n }\n };\n\n const unbind = () => {\n if (listenerRef.current) {\n targetRef.current?.removeEventListener(type, listenerRef.current, options);\n listenerRef.current = null;\n }\n };\n\n const dispose = () => {\n unbind();\n // Prevent memory leak by releasing\n prevListener = null;\n prevOptions = null;\n };\n\n const updateTarget = React.useCallback(() => {\n if (when) {\n targetRef.current = getTargetElement(target) as HTMLElement;\n } else {\n unbind();\n targetRef.current = null;\n }\n }, [target, when]);\n\n React.useEffect(() => {\n return () => {\n dispose();\n };\n }, []);\n\n React.useEffect(() => {\n updateTarget();\n }, [updateTarget]);\n\n React.useEffect(() => {\n const listenerChanged = `${prevListener}` !== `${listener}`;\n const optionsChanged = prevOptions !== options;\n const listenerExists = listenerRef.current;\n\n if (listenerExists && (listenerChanged || optionsChanged)) {\n unbind();\n\n if (when) {\n bind();\n }\n } else if (!listenerExists) {\n dispose();\n }\n }, [listener, options, when]);\n\n return [bind, unbind];\n}\n","import * as React from 'react';\n\n/**\n * Returns the previous value of the provided value.\n *\n * @param value - The value to track.\n * @returns The previous value of the provided value.\n *\n * @example\n * ```tsx\n * const [count, setCount] = React.useState(5);\n * const prevCount = usePrevious(count);\n *\n * console.log(prevCount); // undefined\n *\n * setCount(10);\n *\n * console.log(prevCount); // 5\n * ```\n */\nexport function usePrevious<T>(value: T): T | null | undefined {\n const ref = React.useRef<T | null>(null);\n\n React.useEffect(() => {\n ref.current = value;\n\n return () => {\n ref.current = null;\n };\n }, [value]);\n\n return ref.current;\n}\n"],"mappings":"AAAA,OAAS,oBAAAA,EAAkB,cAAAC,MAAkB,kBAC7C,UAAYC,MAAW,QCDvB,UAAYC,MAAW,QAoBhB,SAASC,EAAeC,EAAgC,CAC3D,IAAMC,EAAY,SAAiB,IAAI,EAEvC,OAAM,YAAU,KACZA,EAAI,QAAUD,EAEP,IAAM,CACTC,EAAI,QAAU,IAClB,GACD,CAACD,CAAK,CAAC,EAEHC,EAAI,OACf,CD6CO,SAASC,EAAiB,CAAE,OAAAC,EAAS,WAAY,KAAAC,EAAM,SAAAC,EAAU,QAAAC,EAAS,KAAAC,EAAO,EAAK,EAAwD,CACjJ,IAAMC,EAAkB,SAA2B,IAAI,EACjDC,EAAoB,SAA6B,IAAI,EACvDC,EAAeC,EAAYN,CAAQ,EACnCO,EAAcD,EAAYL,CAAO,EAE/BO,EAAO,CAACC,EAAmI,CAAC,IAAM,CACpJ,GAAM,CAAE,OAAQC,CAAW,EAAID,EAE3BE,EAAWD,CAAU,IACrBE,EAAO,GAEHH,EAAY,MAAQP,KACpBC,EAAU,QAAUU,EAAiBH,CAAU,IAInD,CAACN,EAAY,SAAWD,EAAU,UAClCC,EAAY,QAAWU,GAAiBd,GAAYA,EAASc,CAAK,EAClEX,EAAU,QAAQ,iBAAiBJ,EAAMK,EAAY,QAASH,CAAO,EAE7E,EAEMW,EAAS,IAAM,CApGzB,IAAAG,EAqGYX,EAAY,WACZW,EAAAZ,EAAU,UAAV,MAAAY,EAAmB,oBAAoBhB,EAAMK,EAAY,QAASH,GAClEG,EAAY,QAAU,KAE9B,EAEMY,EAAU,IAAM,CAClBJ,EAAO,EAEPP,EAAe,KACfE,EAAc,IAClB,EAEMU,EAAqB,cAAY,IAAM,CACrCf,EACAC,EAAU,QAAUU,EAAiBf,CAAM,GAE3Cc,EAAO,EACPT,EAAU,QAAU,KAE5B,EAAG,CAACL,EAAQI,CAAI,CAAC,EAEjB,OAAM,YAAU,IACL,IAAM,CACTc,EAAQ,CACZ,EACD,CAAC,CAAC,EAEC,YAAU,IAAM,CAClBC,EAAa,CACjB,EAAG,CAACA,CAAY,CAAC,EAEX,YAAU,IAAM,CAClB,IAAMC,EAAkB,GAAGb,CAAY,IAAO,GAAGL,CAAQ,GACnDmB,EAAiBZ,IAAgBN,EACjCmB,EAAiBhB,EAAY,QAE/BgB,IAAmBF,GAAmBC,IACtCP,EAAO,EAEHV,GACAM,EAAK,GAEDY,GACRJ,EAAQ,CAEhB,EAAG,CAAChB,EAAUC,EAASC,CAAI,CAAC,EAErB,CAACM,EAAMI,CAAM,CACxB","names":["getTargetElement","isNotEmpty","React","React","usePrevious","value","ref","useEventListener","target","type","listener","options","when","targetRef","listenerRef","prevListener","usePrevious","prevOptions","bind","bindOptions","bindTarget","isNotEmpty","unbind","getTargetElement","event","_a","dispose","updateTarget","listenerChanged","optionsChanged","listenerExists"]}
|
|
File without changes
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a unique identifier.
|
|
3
|
+
*
|
|
4
|
+
* @param initialValue Optional initial value for the ID.
|
|
5
|
+
* @returns The generated or provided ID.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* const id = useId('foo');
|
|
10
|
+
* console.log(id); // 'foo'
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* const defaultId = useId();
|
|
16
|
+
* console.log(defaultId); // e.g., 'pr_id_0'
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function useId(initialValue?: string): string;
|
package/use-id/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-id/index.ts"],"sourcesContent":["import * as React from 'react';\n\n/**\n * Generates a unique identifier.\n *\n * @param initialValue Optional initial value for the ID.\n * @returns The generated or provided ID.\n *\n * @example\n * ```tsx\n * const id = useId('foo');\n * console.log(id); // 'foo'\n * ```\n *\n * @example\n * ```tsx\n * const defaultId = useId();\n * console.log(defaultId); // e.g., 'pr_id_0'\n * ```\n */\nexport function useId(initialValue?: string): string {\n const idx = React.useId();\n const [idState, setIdState] = React.useState<string>(initialValue || `pr_id_${idx.replaceAll(/:|«|»/g, '')}`);\n\n React.useEffect(() => {\n setIdState(initialValue || `pr_id_${idx.replaceAll(/:|«|»/g, '')}`);\n }, [initialValue, idx]);\n\n return idState;\n}\n"],"mappings":"AAAA,UAAYA,MAAW,QAoBhB,SAASC,EAAMC,EAA+B,CACjD,IAAMC,EAAY,QAAM,EAClB,CAACC,EAASC,CAAU,EAAU,WAAiBH,GAAgB,SAASC,EAAI,WAAW,SAAU,EAAE,CAAC,EAAE,EAE5G,OAAM,YAAU,IAAM,CAClBE,EAAWH,GAAgB,SAASC,EAAI,WAAW,SAAU,EAAE,CAAC,EAAE,CACtE,EAAG,CAACD,EAAcC,CAAG,CAAC,EAEfC,CACX","names":["React","useId","initialValue","idx","idState","setIdState"]}
|
|
File without changes
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Listens for the specified media query.
|
|
3
|
+
*
|
|
4
|
+
* @param query - The media query to match.
|
|
5
|
+
* @param when - A boolean indicating whether the media query should be active.
|
|
6
|
+
* @returns A boolean indicating whether the media query matches.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```tsx
|
|
10
|
+
* const Component = () => {
|
|
11
|
+
* const isMobile = useMatchMedia('(max-width: 768px)');
|
|
12
|
+
*
|
|
13
|
+
* return <div>{isMobile ? 'Mobile' : 'Desktop'}</div>;
|
|
14
|
+
* };
|
|
15
|
+
*/
|
|
16
|
+
export declare function useMatchMedia(query: string, when?: boolean): boolean;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import*as t from"react";function d(a,c=!0){let[u,r]=t.useState(!1),n=t.useRef(null),s=e=>r(e.matches),i=()=>{var e;return(e=n.current)==null?void 0:e.addEventListener("change",s)},o=()=>{var e;return((e=n.current)==null?void 0:e.removeEventListener("change",s))&&(n.current=null)};return t.useEffect(()=>(c&&(n.current=window.matchMedia(a),r(n.current.matches),i()),()=>{o()}),[a,c]),u}export{d as useMatchMedia};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-match-media/index.ts"],"sourcesContent":["import * as React from 'react';\n\n/**\n * Listens for the specified media query.\n *\n * @param query - The media query to match.\n * @param when - A boolean indicating whether the media query should be active.\n * @returns A boolean indicating whether the media query matches.\n *\n * @example\n * ```tsx\n * const Component = () => {\n * const isMobile = useMatchMedia('(max-width: 768px)');\n *\n * return <div>{isMobile ? 'Mobile' : 'Desktop'}</div>;\n * };\n */\nexport function useMatchMedia(query: string, when: boolean = true): boolean {\n const [matches, setMatches] = React.useState(false);\n const matchMedia = React.useRef<MediaQueryList | null>(null);\n\n const handleChange = (event: MediaQueryListEvent) => setMatches(event.matches);\n const bind = () => matchMedia.current?.addEventListener('change', handleChange);\n const unbind = () => matchMedia.current?.removeEventListener('change', handleChange) && (matchMedia.current = null);\n\n React.useEffect(() => {\n if (when) {\n matchMedia.current = window.matchMedia(query);\n setMatches(matchMedia.current.matches);\n\n bind();\n }\n\n return () => {\n unbind();\n };\n }, [query, when]);\n\n return matches;\n}\n"],"mappings":"AAAA,UAAYA,MAAW,QAiBhB,SAASC,EAAcC,EAAeC,EAAgB,GAAe,CACxE,GAAM,CAACC,EAASC,CAAU,EAAU,WAAS,EAAK,EAC5CC,EAAmB,SAA8B,IAAI,EAErDC,EAAgBC,GAA+BH,EAAWG,EAAM,OAAO,EACvEC,EAAO,IAAG,CAtBpB,IAAAC,EAsBuB,OAAAA,EAAAJ,EAAW,UAAX,YAAAI,EAAoB,iBAAiB,SAAUH,IAC5DI,EAAS,IAAG,CAvBtB,IAAAD,EAuByB,QAAAA,EAAAJ,EAAW,UAAX,YAAAI,EAAoB,oBAAoB,SAAUH,MAAkBD,EAAW,QAAU,OAE9G,OAAM,YAAU,KACRH,IACAG,EAAW,QAAU,OAAO,WAAWJ,CAAK,EAC5CG,EAAWC,EAAW,QAAQ,OAAO,EAErCG,EAAK,GAGF,IAAM,CACTE,EAAO,CACX,GACD,CAACT,EAAOC,CAAI,CAAC,EAETC,CACX","names":["React","useMatchMedia","query","when","matches","setMatches","matchMedia","handleChange","event","bind","_a","unbind"]}
|
|
File without changes
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Custom hook that runs a mount effect only once.
|
|
4
|
+
* This is similar to `useEffect`, but it only runs on mount.
|
|
5
|
+
*
|
|
6
|
+
* @param {React.EffectCallback} effect the callback function
|
|
7
|
+
* @returns {void}
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* const MyComponent = () => {
|
|
12
|
+
* useMountEffect(() => {
|
|
13
|
+
* console.log('Mounted');
|
|
14
|
+
* });
|
|
15
|
+
* };
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function useMountEffect(effect: React.EffectCallback): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-mount-effect/index.ts"],"sourcesContent":["import * as React from 'react';\n\n/**\n * Custom hook that runs a mount effect only once.\n * This is similar to `useEffect`, but it only runs on mount.\n *\n * @param {React.EffectCallback} effect the callback function\n * @returns {void}\n *\n * @example\n * ```tsx\n * const MyComponent = () => {\n * useMountEffect(() => {\n * console.log('Mounted');\n * });\n * };\n * ```\n */\nexport function useMountEffect(effect: React.EffectCallback): void {\n const mounted = React.useRef<boolean>(false);\n\n React.useEffect(() => {\n if (!mounted.current) {\n mounted.current = true;\n\n effect?.();\n }\n }, []);\n}\n"],"mappings":"AAAA,UAAYA,MAAW,QAkBhB,SAASC,EAAeC,EAAoC,CAC/D,IAAMC,EAAgB,SAAgB,EAAK,EAErC,YAAU,IAAM,CACbA,EAAQ,UACTA,EAAQ,QAAU,GAElBD,GAAA,MAAAA,IAER,EAAG,CAAC,CAAC,CACT","names":["React","useMountEffect","effect","mounted"]}
|
|
File without changes
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the previous value of the provided value.
|
|
3
|
+
*
|
|
4
|
+
* @param value - The value to track.
|
|
5
|
+
* @returns The previous value of the provided value.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* const [count, setCount] = React.useState(5);
|
|
10
|
+
* const prevCount = usePrevious(count);
|
|
11
|
+
*
|
|
12
|
+
* console.log(prevCount); // undefined
|
|
13
|
+
*
|
|
14
|
+
* setCount(10);
|
|
15
|
+
*
|
|
16
|
+
* console.log(prevCount); // 5
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function usePrevious<T>(value: T): T | null | undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-previous/index.ts"],"sourcesContent":["import * as React from 'react';\n\n/**\n * Returns the previous value of the provided value.\n *\n * @param value - The value to track.\n * @returns The previous value of the provided value.\n *\n * @example\n * ```tsx\n * const [count, setCount] = React.useState(5);\n * const prevCount = usePrevious(count);\n *\n * console.log(prevCount); // undefined\n *\n * setCount(10);\n *\n * console.log(prevCount); // 5\n * ```\n */\nexport function usePrevious<T>(value: T): T | null | undefined {\n const ref = React.useRef<T | null>(null);\n\n React.useEffect(() => {\n ref.current = value;\n\n return () => {\n ref.current = null;\n };\n }, [value]);\n\n return ref.current;\n}\n"],"mappings":"AAAA,UAAYA,MAAW,QAoBhB,SAASC,EAAeC,EAAgC,CAC3D,IAAMC,EAAY,SAAiB,IAAI,EAEvC,OAAM,YAAU,KACZA,EAAI,QAAUD,EAEP,IAAM,CACTC,EAAI,QAAU,IAClB,GACD,CAACD,CAAK,CAAC,EAEHC,EAAI,OACf","names":["React","usePrevious","value","ref"]}
|
|
File without changes
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Used to merge and differentiate incoming props with the default props.
|
|
3
|
+
* - Keys that exist in both `props1` and `props2` are placed in `props`, with values from `props1`.
|
|
4
|
+
* - Keys that exist in `props1` but not in `props2` are placed in `attrs`.
|
|
5
|
+
*
|
|
6
|
+
* @template P1 The type of the incoming set of props.
|
|
7
|
+
* @template P2 The type of the default set of props.
|
|
8
|
+
*
|
|
9
|
+
* @param props1 The incoming set of props (e.g., user-defined or dynamic props).
|
|
10
|
+
* @param props2 The default set of props (e.g., default props).
|
|
11
|
+
* @returns An object containing:
|
|
12
|
+
* - `props`: A new object containing keys that exist in both `props1` and `props2`, using values from `props1`.
|
|
13
|
+
* - `attrs`: A new object containing keys that exist only in `props1`, excluding any keys from `props2`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const { props, attrs } = useProps({ id: 'foo', className: 'bar' }, { className: 'baz' });
|
|
18
|
+
*
|
|
19
|
+
* console.log(props); // { className: 'bar' }
|
|
20
|
+
* console.log(attrs); // { id: 'foo' }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function useProps<P1, P2>(props1?: P1, props2?: P2): {
|
|
24
|
+
props: Pick<P1 & P2, keyof P2>;
|
|
25
|
+
attrs: Omit<P1, keyof P2>;
|
|
26
|
+
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var i=Object.defineProperty;var o=Object.getOwnPropertySymbols;var u=Object.prototype.hasOwnProperty,p=Object.prototype.propertyIsEnumerable;var P=(r,t,s)=>t in r?i(r,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):r[t]=s,a=(r,t)=>{for(var s in t||(t={}))u.call(t,s)&&P(r,s,t[s]);if(o)for(var s of o(t))p.call(t,s)&&P(r,s,t[s]);return r};import*as c from"react";function f(r={},t={}){return c.useMemo(()=>{let s={props:a({},t),attrs:{}};return Object.entries(r).forEach(([n,e])=>{n in t?s.props[n]=e:s.attrs[n]=e}),s},[r,t])}export{f as useProps};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-props/index.ts"],"sourcesContent":["import * as React from 'react';\n\n/**\n * Used to merge and differentiate incoming props with the default props.\n * - Keys that exist in both `props1` and `props2` are placed in `props`, with values from `props1`.\n * - Keys that exist in `props1` but not in `props2` are placed in `attrs`.\n *\n * @template P1 The type of the incoming set of props.\n * @template P2 The type of the default set of props.\n *\n * @param props1 The incoming set of props (e.g., user-defined or dynamic props).\n * @param props2 The default set of props (e.g., default props).\n * @returns An object containing:\n * - `props`: A new object containing keys that exist in both `props1` and `props2`, using values from `props1`.\n * - `attrs`: A new object containing keys that exist only in `props1`, excluding any keys from `props2`.\n *\n * @example\n * ```ts\n * const { props, attrs } = useProps({ id: 'foo', className: 'bar' }, { className: 'baz' });\n *\n * console.log(props); // { className: 'bar' }\n * console.log(attrs); // { id: 'foo' }\n * ```\n */\nexport function useProps<P1, P2>(props1: P1 = {} as P1, props2: P2 = {} as P2) {\n type Props = Pick<P1 & P2, keyof P2>;\n type Attrs = Omit<P1, keyof P2>;\n type Result = { props: Props; attrs: Attrs };\n\n return React.useMemo(() => {\n const result: Result = { props: { ...props2 } as Props, attrs: {} as Attrs };\n\n Object.entries(props1 as Record<string, unknown>).forEach(([key, value]) => {\n if (key in (props2 as Record<string, unknown>)) {\n (result.props as Record<string, unknown>)[key] = value;\n } else {\n (result.attrs as Record<string, unknown>)[key] = value;\n }\n });\n\n return result;\n }, [props1, props2]);\n}\n"],"mappings":"yVAAA,UAAYA,MAAW,QAwBhB,SAASC,EAAiBC,EAAa,CAAC,EAASC,EAAa,CAAC,EAAS,CAK3E,OAAa,UAAQ,IAAM,CACvB,IAAMC,EAAiB,CAAE,MAAOC,EAAA,GAAKF,GAAmB,MAAO,CAAC,CAAW,EAE3E,cAAO,QAAQD,CAAiC,EAAE,QAAQ,CAAC,CAACI,EAAKC,CAAK,IAAM,CACpED,KAAQH,EACPC,EAAO,MAAkCE,CAAG,EAAIC,EAEhDH,EAAO,MAAkCE,CAAG,EAAIC,CAEzD,CAAC,EAEMH,CACX,EAAG,CAACF,EAAQC,CAAM,CAAC,CACvB","names":["React","useProps","props1","props2","result","__spreadValues","key","value"]}
|
|
File without changes
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Custom hook that runs an unmount effect only once.
|
|
4
|
+
* This is similar to `useEffect`, but it only runs on unmount.
|
|
5
|
+
*
|
|
6
|
+
* @param {React.EffectCallback} effect the callback function
|
|
7
|
+
* @returns {void}
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* const Component = () => {
|
|
12
|
+
* useUnmountEffect(() => {
|
|
13
|
+
* console.log('Unmounted');
|
|
14
|
+
* });
|
|
15
|
+
* };
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function useUnmountEffect(effect: React.EffectCallback): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-unmount-effect/index.ts"],"sourcesContent":["import * as React from 'react';\n\n/**\n * Custom hook that runs an unmount effect only once.\n * This is similar to `useEffect`, but it only runs on unmount.\n *\n * @param {React.EffectCallback} effect the callback function\n * @returns {void}\n *\n * @example\n * ```tsx\n * const Component = () => {\n * useUnmountEffect(() => {\n * console.log('Unmounted');\n * });\n * };\n * ```\n */\nexport function useUnmountEffect(effect: React.EffectCallback): void {\n React.useEffect(() => {\n return () => {\n effect?.();\n };\n }, []);\n}\n"],"mappings":"AAAA,UAAYA,MAAW,QAkBhB,SAASC,EAAiBC,EAAoC,CAC3D,YAAU,IACL,IAAM,CACTA,GAAA,MAAAA,GACJ,EACD,CAAC,CAAC,CACT","names":["React","useUnmountEffect","effect"]}
|
|
File without changes
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Custom hook that runs an update effect whenever dependencies change.
|
|
4
|
+
* This is similar to `useEffect`, but it does not run on mount.
|
|
5
|
+
*
|
|
6
|
+
* @param {React.EffectCallback} effect the callback function
|
|
7
|
+
* @param {React.DependencyList} dependencies the dependencies
|
|
8
|
+
* @returns {void}
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```tsx
|
|
12
|
+
* const Component = ({ value }) => {
|
|
13
|
+
* useUpdateEffect(() => {
|
|
14
|
+
* console.log('Updated');
|
|
15
|
+
* }, [value]);
|
|
16
|
+
* };
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare function useUpdateEffect(effect: React.EffectCallback, dependencies?: React.DependencyList): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-update-effect/index.ts"],"sourcesContent":["import * as React from 'react';\n\n/**\n * Custom hook that runs an update effect whenever dependencies change.\n * This is similar to `useEffect`, but it does not run on mount.\n *\n * @param {React.EffectCallback} effect the callback function\n * @param {React.DependencyList} dependencies the dependencies\n * @returns {void}\n *\n * @example\n * ```tsx\n * const Component = ({ value }) => {\n * useUpdateEffect(() => {\n * console.log('Updated');\n * }, [value]);\n * };\n * ```\n */\nexport function useUpdateEffect(effect: React.EffectCallback, dependencies?: React.DependencyList): void {\n const mounted = React.useRef<boolean>(false);\n\n React.useEffect(() => {\n if (!mounted.current) {\n mounted.current = true;\n\n return;\n }\n\n return effect?.();\n }, dependencies);\n}\n"],"mappings":"AAAA,UAAYA,MAAW,QAmBhB,SAASC,EAAgBC,EAA8BC,EAA2C,CACrG,IAAMC,EAAgB,SAAgB,EAAK,EAErC,YAAU,IAAM,CAClB,GAAI,CAACA,EAAQ,QAAS,CAClBA,EAAQ,QAAU,GAElB,MACJ,CAEA,OAAOF,GAAA,YAAAA,GACX,EAAGC,CAAY,CACnB","names":["React","useUpdateEffect","effect","dependencies","mounted"]}
|
|
File without changes
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Starts a view transition.
|
|
3
|
+
*
|
|
4
|
+
* @returns A function that starts a view transition.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* const Component = () => {
|
|
9
|
+
* const startTransition = useViewTransition();
|
|
10
|
+
*
|
|
11
|
+
* const handleClick = () => {
|
|
12
|
+
* startTransition(() => {
|
|
13
|
+
* // Perform some work
|
|
14
|
+
* });
|
|
15
|
+
* };
|
|
16
|
+
*
|
|
17
|
+
* return <button onClick={handleClick}>Click me</button>;
|
|
18
|
+
* };
|
|
19
|
+
*/
|
|
20
|
+
export declare function useViewTransition(): (callback: () => void) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/use-view-transition/index.ts"],"sourcesContent":["import * as React from 'react';\n\n/**\n * Starts a view transition.\n *\n * @returns A function that starts a view transition.\n *\n * @example\n * ```tsx\n * const Component = () => {\n * const startTransition = useViewTransition();\n *\n * const handleClick = () => {\n * startTransition(() => {\n * // Perform some work\n * });\n * };\n *\n * return <button onClick={handleClick}>Click me</button>;\n * };\n */\nexport function useViewTransition() {\n const startTransition = React.useCallback((callback: () => void) => {\n if (document.startViewTransition) {\n document.startViewTransition(() => {\n callback();\n });\n } else {\n callback();\n }\n }, []);\n\n return startTransition;\n}\n"],"mappings":"AAAA,UAAYA,MAAW,QAqBhB,SAASC,GAAoB,CAWhC,OAV8B,cAAaC,GAAyB,CAC5D,SAAS,oBACT,SAAS,oBAAoB,IAAM,CAC/BA,EAAS,CACb,CAAC,EAEDA,EAAS,CAEjB,EAAG,CAAC,CAAC,CAGT","names":["React","useViewTransition","callback"]}
|
|
File without changes
|