@ohos-ports/hooks 0.0.0-stage → 11.2.0-beta.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.md +45 -0
- package/README.md +50 -2
- package/index.d.ts +27 -0
- package/index.mjs +3 -0
- package/package.json +40 -4
- package/use-attr-selector/index.d.ts +13 -0
- package/use-attr-selector/index.mjs +1 -0
- package/use-controlled-state/index.d.ts +59 -0
- package/use-controlled-state/index.mjs +1 -0
- package/use-direction/index.d.ts +19 -0
- package/use-direction/index.mjs +1 -0
- package/use-escape-listener/index.d.ts +71 -0
- package/use-escape-listener/index.mjs +1 -0
- package/use-event-listener/index.d.ts +75 -0
- package/use-event-listener/index.mjs +1 -0
- package/use-filter/_matchers.d.ts +23 -0
- package/use-filter/index.d.ts +94 -0
- package/use-filter/index.mjs +3 -0
- package/use-hot-key/index.d.ts +25 -0
- package/use-hot-key/index.mjs +1 -0
- package/use-id/index.d.ts +19 -0
- package/use-id/index.mjs +1 -0
- package/use-intersection-observer/index.d.ts +58 -0
- package/use-intersection-observer/index.mjs +1 -0
- package/use-is-mobile/index.d.ts +18 -0
- package/use-is-mobile/index.mjs +1 -0
- package/use-key-filter/index.d.ts +42 -0
- package/use-key-filter/index.mjs +1 -0
- package/use-local-storage/index.d.ts +56 -0
- package/use-local-storage/index.mjs +1 -0
- package/use-mask/index.d.ts +138 -0
- package/use-mask/index.mjs +1 -0
- package/use-match-media/index.d.ts +16 -0
- package/use-match-media/index.mjs +1 -0
- package/use-mount-effect/index.d.ts +18 -0
- package/use-mount-effect/index.mjs +1 -0
- package/use-mounted/index.d.ts +36 -0
- package/use-mounted/index.mjs +1 -0
- package/use-number-formatter/index.d.ts +194 -0
- package/use-number-formatter/index.mjs +1 -0
- package/use-presence/index.d.ts +20 -0
- package/use-presence/index.mjs +1 -0
- package/use-previous/index.d.ts +19 -0
- package/use-previous/index.mjs +1 -0
- package/use-props/index.d.ts +31 -0
- package/use-props/index.mjs +1 -0
- package/use-queue-task/index.d.ts +1 -0
- package/use-queue-task/index.mjs +1 -0
- package/use-scrolltop/index.d.ts +59 -0
- package/use-scrolltop/index.mjs +1 -0
- package/use-sortable-list/index.d.ts +43 -0
- package/use-sortable-list/index.mjs +3 -0
- package/use-tree-filter/index.d.ts +52 -0
- package/use-tree-filter/index.mjs +3 -0
- package/use-unmount-effect/index.d.ts +18 -0
- package/use-unmount-effect/index.mjs +1 -0
- package/use-update-effect/index.d.ts +19 -0
- package/use-update-effect/index.mjs +1 -0
- package/use-view-transition/index.d.ts +20 -0
- package/use-view-transition/index.mjs +1 -0
- package/use-visibility-change/index.d.ts +14 -0
- package/use-visibility-change/index.mjs +1 -0
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
"use client";import{resolveFieldData as P}from"@primeuix/utils/object";import*as s from"react";import{equals as oe,removeAccents as ae}from"@primeuix/utils/object";function f(e){return!!e&&typeof e.getTime=="function"}function T(e,n){return ae(String(e)).toLocaleLowerCase(n)}var K={startsWith(e,n,o){return n==null||n===""?!0:e==null?!1:T(e,o).startsWith(T(n,o))},contains(e,n,o){return n==null||n===""?!0:e==null?!1:T(e,o).indexOf(T(n,o))!==-1},notContains(e,n,o){return n==null||n===""?!0:e==null?!1:T(e,o).indexOf(T(n,o))===-1},endsWith(e,n,o){return n==null||n===""?!0:e==null?!1:T(e,o).endsWith(T(n,o))},equals(e,n,o){return n==null||n===""?!0:e==null?!1:f(e)&&f(n)?e.getTime()===n.getTime():T(e,o)===T(n,o)},notEquals(e,n,o){return n==null||n===""?!1:e==null?!0:f(e)&&f(n)?e.getTime()!==n.getTime():T(e,o)!==T(n,o)},in(e,n){if(!Array.isArray(n)||n.length===0)return!0;for(let o of n)if(oe(e,o))return!0;return!1},between(e,n){return!Array.isArray(n)||n[0]==null||n[1]==null?!0:e==null?!1:f(e)&&f(n[0])&&f(n[1])?n[0].getTime()<=e.getTime()&&e.getTime()<=n[1].getTime():n[0]<=e&&e<=n[1]},lt(e,n){return n==null?!0:e==null?!1:f(e)&&f(n)?e.getTime()<n.getTime():e<n},lte(e,n){return n==null?!0:e==null?!1:f(e)&&f(n)?e.getTime()<=n.getTime():e<=n},gt(e,n){return n==null?!0:e==null?!1:f(e)&&f(n)?e.getTime()>n.getTime():e>n},gte(e,n){return n==null?!0:e==null?!1:f(e)&&f(n)?e.getTime()>=n.getTime():e>=n},dateIs(e,n){return n==null?!0:e==null?!1:e.toDateString()===n.toDateString()},dateIsNot(e,n){return n==null?!0:e==null?!1:e.toDateString()!==n.toDateString()},dateBefore(e,n){return n==null?!0:e==null?!1:e.getTime()<n.getTime()},dateAfter(e,n){return n==null?!0:e==null?!1:e.getTime()>n.getTime()}},N={};function se(e,n){N[e]=n}function ie(e){delete N[e]}function J(){return N}function I(){return"contains"}function m(e){return Array.isArray(e.constraints)}function H(e,n,o,M,h,x){let R=x[M];return R?Array.isArray(n)?o==null||o===""?!0:n.some(w=>R(P(e,w),o,h)):R(typeof e=="object"?P(e,n):e,o,h):!0}function de(e,n,o,M){if(m(n)){let h=n.constraints??[];return h.length===0?!0:(n.operator??"and")==="and"?h.every(R=>H(e,n.field,R.value,R.matchMode??I(),M,o)):h.some(R=>H(e,n.field,R.value,R.matchMode??I(),M,o))}return H(e,n.field,n.value,n.matchMode??I(),M,o)}function Re(e){let n=e.field,o=e.matchMode,M=n!==void 0,h=M&&e.value!==void 0,x=s.useMemo(()=>M?[{field:n,value:e.value??null,matchMode:o}]:[],[M,n,o,e.value]),R=e.rules!==void 0||h,[w,Q]=s.useState(e.defaultRules??(M?x:[])),X=s.useMemo(()=>[{field:n,value:e.value,matchMode:o}],[n,e.value,o]),i=e.rules!==void 0?e.rules:h?X:w,W=s.useRef(e.onRulesChange),q=s.useRef(e.onValueChange);W.current=e.onRulesChange,q.current=e.onValueChange;let c=s.useCallback(r=>{if(R||Q(r),W.current?.(r),M){let t=r[0];t&&!("constraints"in t)&&q.current?.(t.value)}},[R,M]),v=e.filterLocale,y=e.filterDelay??0,L=s.useMemo(()=>({...K,...J(),...e.matchers??{}}),[e.matchers]),[Y,Z]=s.useState(i);s.useEffect(()=>{if(y<=0)return;let r=setTimeout(()=>Z(i),y);return()=>clearTimeout(r)},[i,y]);let p=y>0?Y:i,C=s.useMemo(()=>p.filter(r=>le(r)),[p]),D=s.useCallback(r=>C.every(t=>de(r,t,L,v)),[C,L,v]),k=!!e.lazy,$=s.useMemo(()=>k||C.length===0?e.data:e.data.filter(D),[k,e.data,C,D]),ee=C.length>0,G=s.useRef(e.onLazyLoad);G.current=e.onLazyLoad;let j=s.useRef(null);s.useEffect(()=>{if(!k)return;let r=JSON.stringify(p);j.current!==r&&(j.current=r,G.current?.({rules:p,filterLocale:v}))},[k,p,v]);let ne=s.useCallback(r=>c(r),[c]),A=s.useCallback((r,t)=>{let a=i.map((u,d)=>d===r?{...u,value:t}:u);c(a)},[i,c]),S=s.useCallback((r,t)=>{let a=i.map((u,d)=>d===r?{...u,matchMode:t}:u);c(a)},[i,c]),O=s.useCallback((r,t)=>{let a=i.map((u,d)=>d!==r?u:m(u)?{...u,operator:t}:{field:u.field,operator:t,constraints:[{value:u.value,matchMode:u.matchMode}]});c(a)},[i,c]),z=s.useCallback((r,t,a)=>{let u=i.map((d,l)=>{if(l!==r)return d;let b=m(d)?d.constraints.map((g,F)=>F===t?{...g,value:a}:g):t===0?[{value:a,matchMode:d.matchMode}]:d.constraints??[];return{field:d.field,operator:m(d)?d.operator:void 0,constraints:b}});c(u)},[i,c]),B=s.useCallback((r,t,a)=>{let u=i.map((d,l)=>{if(l!==r)return d;let b=m(d)?d.constraints.map((g,F)=>F===t?{...g,matchMode:a}:g):t===0?[{value:d.value,matchMode:a}]:d.constraints??[];return{field:d.field,operator:m(d)?d.operator:void 0,constraints:b}});c(u)},[i,c]),te=s.useCallback(r=>c([...i,r]),[i,c]),E=s.useCallback(r=>{let t=i.filter((a,u)=>u!==r);c(t)},[i,c]),V=s.useCallback(r=>{let t=i.map((a,u)=>u!==r?a:m(a)?{...a,constraints:a.constraints.map(d=>({...d,value:null}))}:{...a,value:null});c(t)},[i,c]),_=s.useCallback((r,t)=>{let a=i.map((u,d)=>{if(d!==r)return u;let l=m(u)?u.constraints:[{value:u.value,matchMode:u.matchMode}];return{field:u.field,operator:m(u)?u.operator:"and",constraints:[...l,{value:null,matchMode:t??I()}]}});c(a)},[i,c]),U=s.useCallback((r,t)=>{let a=i.map((u,d)=>{if(d!==r||!m(u))return u;let l=u.constraints.filter((b,g)=>g!==t);return l.length===0?u:{...u,constraints:l}});c(a)},[i,c]),re=s.useCallback(()=>{let r=i.map(t=>m(t)?{...t,constraints:t.constraints.map(a=>({...a,value:null}))}:{...t,value:null});c(r)},[i,c]),ue=s.useCallback(r=>{let t=typeof r=="number"?r:i.findIndex(l=>Array.isArray(l.field)?l.field.join(",")===r:l.field===r);if(t<0||!i[t])return;let a=i[t],u=m(a),d=l=>{let b=u?a.constraints:[{value:a.value,matchMode:a.matchMode}];if(l<0||l>=b.length)return;let g=b[l];return{value:g.value,matchMode:g.matchMode,setValue:F=>z(t,l,F),setMatchMode:F=>B(t,l,F),remove:()=>U(t,l)}};return{field:a.field,value:u?void 0:a.value,matchMode:u?void 0:a.matchMode,operator:u?a.operator:void 0,constraints:u?a.constraints:void 0,setValue:l=>A(t,l),setMatchMode:l=>S(t,l),setOperator:l=>O(t,l),addConstraint:l=>_(t,l),constraint:d,remove:()=>E(t),clear:()=>V(t)}},[i,A,S,O,z,B,_,U,E,V]);return{rules:i,filteredData:$,isFiltered:ee,setRules:ne,setValue:A,setMatchMode:S,setOperator:O,setConstraintValue:z,setConstraintMatchMode:B,addRule:te,removeRule:E,clearRule:V,addConstraint:_,removeConstraint:U,clearAll:re,rule:ue,match:D,matchers:L}}function le(e){return m(e)?e.constraints.some(n=>n.value!==null&&n.value!==void 0&&n.value!==""):e.value!==null&&e.value!==void 0&&e.value!==""}export{K as BUILT_IN_MATCHERS,J as getGlobalMatchers,de as matchRule,se as registerMatcher,le as ruleHasValue,ie as unregisterMatcher,Re as useFilter};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type UseHotKeyTarget = Document | HTMLElement | null | undefined;
|
|
2
|
+
export interface UseHotKeyOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Target to listen on. Defaults to `document` when available.
|
|
5
|
+
*/
|
|
6
|
+
target?: UseHotKeyTarget;
|
|
7
|
+
/**
|
|
8
|
+
* Keyboard event type.
|
|
9
|
+
* @default 'keydown'
|
|
10
|
+
*/
|
|
11
|
+
event?: 'keydown' | 'keyup' | 'keypress';
|
|
12
|
+
/**
|
|
13
|
+
* Enables or disables the listener.
|
|
14
|
+
* @default true
|
|
15
|
+
*/
|
|
16
|
+
when?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Runs the provided handler when any of the given keys is pressed.
|
|
20
|
+
*
|
|
21
|
+
* @param keys A single key or an array of keys to listen for (e.g. 'k' or 'ctrl+k').
|
|
22
|
+
* @param handler Callback fired when a matching key event occurs.
|
|
23
|
+
* @param options Optional configuration for target, event type, and enable flag.
|
|
24
|
+
*/
|
|
25
|
+
export declare function useHotKey(keys: string | string[], handler: (event: KeyboardEvent) => void, options?: UseHotKeyOptions): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import*as a from"react";var l=t=>{let e=t.toLowerCase();return e==="space"||e==="spacebar"?" ":e},p=t=>{let e=t.split("+").map(s=>s.trim().toLowerCase()).filter(Boolean),r,n={ctrl:!1,meta:!1,alt:!1,shift:!1};return e.forEach(s=>{switch(s){case"ctrl":case"control":n.ctrl=!0;break;case"meta":case"cmd":case"command":n.meta=!0;break;case"alt":case"option":n.alt=!0;break;case"shift":n.shift=!0;break;default:r=l(s)}}),{key:r,required:n}},K=(t,e)=>{if(!t.key)return!1;let{required:r}=t;return r.ctrl!==e.ctrlKey||r.meta!==e.metaKey||r.alt!==e.altKey||r.shift!==e.shiftKey?!1:l(e.key)===t.key};function E(t,e,r={}){let{target:n,event:s="keydown",when:c=!0}=r,i=a.useRef(e);a.useEffect(()=>{i.current=e},[e]),a.useEffect(()=>{if(!c)return;let o=n??(typeof document<"u"?document:null);if(!o)return;let y=(Array.isArray(t)?t:[t]).map(p),f=m=>{let u=m;y.some(d=>K(d,u))&&i.current?.(u)};return o.addEventListener(s,f),()=>{o.removeEventListener(s,f)}},[s,t,n,c])}export{E as useHotKey};
|
|
@@ -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
|
+
import*as t from"react";function d(e){let r=t.useId(),[s,c]=t.useState(e||`pr_id_${r.replaceAll(/:|«|»/g,"")}`);return t.useEffect(()=>{c(e||`pr_id_${r.replaceAll(/:|«|»/g,"")}`)},[e,r]),s}export{d as useId};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export interface UseIntersectionObserverOptions extends IntersectionObserverInit {
|
|
2
|
+
/**
|
|
3
|
+
* Skip creating the IntersectionObserver
|
|
4
|
+
* @default false
|
|
5
|
+
*/
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Callback function that will be called when intersection changes
|
|
9
|
+
*/
|
|
10
|
+
onIntersect?: (entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void;
|
|
11
|
+
/**
|
|
12
|
+
* If the IntersectionObserver API isn't available, set this fallback behavior
|
|
13
|
+
*/
|
|
14
|
+
fallback?: () => void;
|
|
15
|
+
}
|
|
16
|
+
export interface UseIntersectionObserverResult {
|
|
17
|
+
/** The current intersection entries */
|
|
18
|
+
entries: IntersectionObserverEntry[];
|
|
19
|
+
/** Whether any of the observed elements are intersecting */
|
|
20
|
+
isIntersecting: boolean;
|
|
21
|
+
/** Function to start observing an element */
|
|
22
|
+
observe: (element: Element) => void;
|
|
23
|
+
/** Function to stop observing an element */
|
|
24
|
+
unobserve: (element: Element) => void;
|
|
25
|
+
/** Function to stop observing all elements */
|
|
26
|
+
disconnect: () => void;
|
|
27
|
+
/** The IntersectionObserver instance */
|
|
28
|
+
observer: IntersectionObserver | null;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Low-level hook for IntersectionObserver API.
|
|
32
|
+
* Provides direct access to observe/unobserve methods and entries.
|
|
33
|
+
*
|
|
34
|
+
* @param options - Configuration options for the IntersectionObserver
|
|
35
|
+
* @returns Object containing observer methods and current entries
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```tsx
|
|
39
|
+
* const Component = () => {
|
|
40
|
+
* const ref = useRef(null);
|
|
41
|
+
* const { observe, isIntersecting, entries } = useIntersectionObserver({
|
|
42
|
+
* threshold: 0.5,
|
|
43
|
+
* onIntersect: (entries) => {
|
|
44
|
+
* console.log('Intersection changed:', entries);
|
|
45
|
+
* }
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* useEffect(() => {
|
|
49
|
+
* if (ref.current) {
|
|
50
|
+
* observe(ref.current);
|
|
51
|
+
* }
|
|
52
|
+
* }, [observe]);
|
|
53
|
+
*
|
|
54
|
+
* return <div ref={ref}>Content</div>;
|
|
55
|
+
* };
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare function useIntersectionObserver(options?: UseIntersectionObserverOptions): UseIntersectionObserverResult;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import*as r from"react";function E(f={}){let{disabled:l=!1,onIntersect:c,fallback:o,...n}=f,[i,u]=r.useState([]),t=r.useRef(null),s=r.useRef(new Set),a=r.useRef(c);r.useEffect(()=>{a.current=c},[c]);let v=r.useMemo(()=>i.some(e=>e.isIntersecting),[i]);r.useEffect(()=>{if(!l){if(typeof window.IntersectionObserver>"u"){o&&o();return}return t.current=new IntersectionObserver((e,b)=>{u(e),a.current&&a.current(e,b)},n),s.current.forEach(e=>{t.current&&t.current.observe(e)}),()=>{t.current&&(t.current.disconnect(),t.current=null)}}},[l,o,n.root,n.rootMargin,n.threshold]);let d=r.useCallback(e=>{e&&(s.current.add(e),t.current&&t.current.observe(e))},[]),I=r.useCallback(e=>{e&&(s.current.delete(e),t.current&&t.current.unobserve(e),u(b=>b.filter(O=>O.target!==e)))},[]),R=r.useCallback(()=>{t.current&&t.current.disconnect(),s.current.clear(),u([])},[]);return{entries:i,isIntersecting:v,observe:d,unobserve:I,disconnect:R,observer:t.current}}export{E as useIntersectionObserver};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detects whether the viewport matches a mobile breakpoint.
|
|
3
|
+
*
|
|
4
|
+
* @param breakpoint - Max-width in pixels to consider as mobile. Defaults to 768.
|
|
5
|
+
* @returns A boolean indicating whether the viewport is at or below the breakpoint.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* const Component = () => {
|
|
10
|
+
* const isMobile = useIsMobile();
|
|
11
|
+
* return <div>{isMobile ? 'Mobile' : 'Desktop'}</div>;
|
|
12
|
+
* };
|
|
13
|
+
*
|
|
14
|
+
* // Custom breakpoint
|
|
15
|
+
* const isTablet = useIsMobile(1024);
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function useIsMobile(breakpoint?: number): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import*as e from"react";function o(a,c=!0){let[i,r]=e.useState(!1),t=e.useRef(null),n=e.useCallback(l=>r(l.matches),[]),s=e.useCallback(()=>t.current?.addEventListener("change",n),[n]),u=e.useCallback(()=>t.current?.removeEventListener("change",n)&&(t.current=null),[n]);return e.useEffect(()=>(c&&(t.current=window.matchMedia(a),r(t.current.matches),s()),()=>{u()}),[a,c,s,u]),i}var d=768;function M(a=d){return o(`(max-width: ${a-1}px)`)}export{M as useIsMobile};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The options for the `useKeyFilter` hook.
|
|
3
|
+
*/
|
|
4
|
+
export interface UseKeyFilterOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Sets the pattern for key filtering.
|
|
7
|
+
* @default /./
|
|
8
|
+
*/
|
|
9
|
+
pattern?: 'pint' | 'int' | 'pnum' | 'money' | 'num' | 'hex' | 'email' | 'alpha' | 'alphanum' | RegExp;
|
|
10
|
+
/**
|
|
11
|
+
* When enabled, instead of blocking keys, input is validated internally to test against the regular expression.
|
|
12
|
+
* @default false
|
|
13
|
+
*/
|
|
14
|
+
validateOnly?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface UseKeyFilterExposes {
|
|
17
|
+
/**
|
|
18
|
+
* Handles input events for key filter.
|
|
19
|
+
* Processes character input and composition events while applying the filter pattern.
|
|
20
|
+
* @param event - The form or composition event from the input element
|
|
21
|
+
*/
|
|
22
|
+
onBeforeInput: (event: React.CompositionEvent<HTMLInputElement>) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Handles keypress events for character input validation.
|
|
25
|
+
* Validates and places characters according to the filter pattern.
|
|
26
|
+
* @param event - The keyboard event from the input element
|
|
27
|
+
*/
|
|
28
|
+
onKeyPress: (event: React.KeyboardEvent<HTMLInputElement>) => void;
|
|
29
|
+
/**
|
|
30
|
+
* Handles paste events for clipboard content insertion.
|
|
31
|
+
* Processes pasted content according to the mask pattern.
|
|
32
|
+
* @param event - The clipboard event from the input element
|
|
33
|
+
*/
|
|
34
|
+
onPaste: (event: React.ClipboardEvent<HTMLInputElement>) => void;
|
|
35
|
+
/**
|
|
36
|
+
* Validates the current input value against the filter pattern.
|
|
37
|
+
* @param event - The form event from the input element
|
|
38
|
+
* @returns true if the value matches the pattern, false otherwise
|
|
39
|
+
*/
|
|
40
|
+
validate: (event: React.FormEvent<HTMLInputElement>) => boolean;
|
|
41
|
+
}
|
|
42
|
+
export declare function useKeyFilter(options: UseKeyFilterOptions): UseKeyFilterExposes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{isAndroid as s}from"@primeuix/utils";function v(p){let{pattern:r=/./,validateOnly:o=!1}=p,u={pint:/[\d]/,int:/[\d-]/,pnum:/[\d.]/,money:/[\d.\s,]/,num:/[\d-.]/,hex:/[0-9a-f]/i,email:/[a-z0-9_.-@]/i,alpha:/[a-z_]/i,alphanum:/[a-z0-9_]/},i=()=>typeof r=="string"?u[r]:r,c=e=>{o||!s()||l(e,e.data)},m=e=>{o||s()||e.ctrlKey||e.altKey||e.metaKey||l(e,e.key)},d=e=>{if(o)return;let t=i();[...e.clipboardData.getData("text")].forEach(a=>{if(!t.test(a))return e.preventDefault(),!1})},l=(e,t)=>{if(t==null||!(t.length<=2))return;i().test(t)||e.preventDefault()};return{onBeforeInput:c,onKeyPress:m,onPaste:d,validate:e=>{let t=e.target.value,n=!0,a=i();return t&&!a.test(t)&&(n=!1),n}}}export{v as useKeyFilter};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* The options for the `useLocalStorage` hook.
|
|
4
|
+
*/
|
|
5
|
+
export interface UseLocalStorageOptions<T = unknown> {
|
|
6
|
+
/**
|
|
7
|
+
* The key to use in localStorage.
|
|
8
|
+
*/
|
|
9
|
+
key: string;
|
|
10
|
+
/**
|
|
11
|
+
* The initial value to use when no value is found in localStorage.
|
|
12
|
+
*/
|
|
13
|
+
initialValue: T;
|
|
14
|
+
/**
|
|
15
|
+
* Custom serializer function. Defaults to `JSON.stringify`.
|
|
16
|
+
*/
|
|
17
|
+
serializer?: (value: T) => string;
|
|
18
|
+
/**
|
|
19
|
+
* Custom deserializer function. Defaults to `JSON.parse`.
|
|
20
|
+
*/
|
|
21
|
+
deserializer?: (value: string) => T;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The return type of the `useLocalStorage` hook.
|
|
25
|
+
* A tuple containing:
|
|
26
|
+
* 1. The current stored value.
|
|
27
|
+
* 2. A setter function to update the value.
|
|
28
|
+
* 3. A function to remove the value from localStorage.
|
|
29
|
+
*/
|
|
30
|
+
export type UseLocalStorageReturnType<T = unknown> = [T, React.Dispatch<React.SetStateAction<T>>, () => void];
|
|
31
|
+
/**
|
|
32
|
+
* A custom hook that syncs state with localStorage.
|
|
33
|
+
* Uses initialValue for SSR and first client render to avoid hydration mismatch,
|
|
34
|
+
* then reads the stored value in useLayoutEffect (before paint) so no flash occurs.
|
|
35
|
+
*
|
|
36
|
+
* @param {UseLocalStorageOptions} options - The options for the local storage hook.
|
|
37
|
+
* @returns A tuple containing the current value, a setter function, and a remove function.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```tsx
|
|
41
|
+
* const MyComponent = () => {
|
|
42
|
+
* const [sizes, setSizes, removeSizes] = useLocalStorage({
|
|
43
|
+
* key: 'splitter-sizes',
|
|
44
|
+
* initialValue: [50, 50]
|
|
45
|
+
* });
|
|
46
|
+
*
|
|
47
|
+
* return (
|
|
48
|
+
* <Splitter.Root sizes={sizes} onResizeEnd={(e) => setSizes(e.sizes)}>
|
|
49
|
+
* <Splitter.Panel />
|
|
50
|
+
* <Splitter.Panel />
|
|
51
|
+
* </Splitter.Root>
|
|
52
|
+
* );
|
|
53
|
+
* };
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function useLocalStorage<T>({ key, initialValue, serializer, deserializer }: UseLocalStorageOptions<T>): UseLocalStorageReturnType<T>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import*as t from"react";function i({key:e,initialValue:a,serializer:c=JSON.stringify,deserializer:n=JSON.parse}){let[o,r]=t.useState(a),s=t.useRef(!1);t.useLayoutEffect(()=>{try{let u=window.localStorage.getItem(e);u!==null&&r(n(u))}catch{}s.current=!0},[e,n]),t.useEffect(()=>{if(s.current)try{window.localStorage.setItem(e,c(o))}catch{}},[e,o,c]);let l=t.useCallback(()=>{try{window.localStorage.removeItem(e)}catch{}r(a)},[e,a]);return[o,r,l]}export{i as useLocalStorage};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event payload for `useMask` value/complete callbacks.
|
|
3
|
+
*/
|
|
4
|
+
export interface UseMaskValueChangeEvent {
|
|
5
|
+
/**
|
|
6
|
+
* The DOM event that triggered the change, or `null` for programmatic
|
|
7
|
+
* updates (`setValue`, `reset`).
|
|
8
|
+
*/
|
|
9
|
+
originalEvent: Event | null;
|
|
10
|
+
/**
|
|
11
|
+
* Masked value (e.g. `"12/05/2026"`). Always in masked form.
|
|
12
|
+
*/
|
|
13
|
+
value: string;
|
|
14
|
+
/**
|
|
15
|
+
* Unmasked (raw) value (e.g. `"12052026"`). Separators and placeholder
|
|
16
|
+
* characters are stripped.
|
|
17
|
+
*/
|
|
18
|
+
rawValue: string;
|
|
19
|
+
/**
|
|
20
|
+
* Whether all required slots are filled.
|
|
21
|
+
*/
|
|
22
|
+
isComplete: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Options for the `useMask` hook.
|
|
26
|
+
*/
|
|
27
|
+
export interface UseMaskOptions {
|
|
28
|
+
/**
|
|
29
|
+
* Mask pattern. Built-in tokens:
|
|
30
|
+
* - `9` → [0-9]
|
|
31
|
+
* - `a` → [A-Za-z]
|
|
32
|
+
* - `A` → [A-Z]
|
|
33
|
+
* - `*` → [A-Za-z0-9]
|
|
34
|
+
* Append `?` to mark the remainder of the mask as optional. Extend with
|
|
35
|
+
* the `tokens` option.
|
|
36
|
+
*/
|
|
37
|
+
mask: string;
|
|
38
|
+
/**
|
|
39
|
+
* Custom token map merged on top of the built-in tokens. Each token
|
|
40
|
+
* character is matched against the supplied RegExp.
|
|
41
|
+
* @example { '#': /[0-9]/, H: /[A-Fa-f0-9]/ }
|
|
42
|
+
*/
|
|
43
|
+
tokens?: Record<string, RegExp>;
|
|
44
|
+
/**
|
|
45
|
+
* Controlled masked value. When provided, the hook operates in
|
|
46
|
+
* controlled mode.
|
|
47
|
+
*/
|
|
48
|
+
value?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Initial masked value for uncontrolled mode. Ignored when `value` is
|
|
51
|
+
* provided.
|
|
52
|
+
*/
|
|
53
|
+
defaultValue?: string;
|
|
54
|
+
/**
|
|
55
|
+
* Fires whenever the value changes (typing, paste, programmatic).
|
|
56
|
+
*/
|
|
57
|
+
onValueChange?: (event: UseMaskValueChangeEvent) => void;
|
|
58
|
+
/**
|
|
59
|
+
* Fires when the value transitions to complete (all required slots
|
|
60
|
+
* filled). Re-fires if the value becomes incomplete and is completed
|
|
61
|
+
* again.
|
|
62
|
+
*/
|
|
63
|
+
onComplete?: (event: UseMaskValueChangeEvent) => void;
|
|
64
|
+
/**
|
|
65
|
+
* Placeholder character for empty slots. Pass `null` to skip filling
|
|
66
|
+
* empty slots visually.
|
|
67
|
+
* @default '_'
|
|
68
|
+
*/
|
|
69
|
+
slotChar?: string | null;
|
|
70
|
+
/**
|
|
71
|
+
* Whether to render the mask skeleton on focus.
|
|
72
|
+
* @default false
|
|
73
|
+
*/
|
|
74
|
+
showMaskOnFocus?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Always render the mask skeleton, regardless of focus state.
|
|
77
|
+
* @default false
|
|
78
|
+
*/
|
|
79
|
+
alwaysShowMask?: boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Clear an incomplete value on blur.
|
|
82
|
+
* @default true
|
|
83
|
+
*/
|
|
84
|
+
autoClear?: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Transform every character before token validation (e.g. uppercase).
|
|
87
|
+
*/
|
|
88
|
+
transform?: (char: string) => string;
|
|
89
|
+
/**
|
|
90
|
+
* Read-only mode. Typing is blocked; programmatic `setValue` still
|
|
91
|
+
* works.
|
|
92
|
+
* @default false
|
|
93
|
+
*/
|
|
94
|
+
readOnly?: boolean;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Return type of the `useMask` hook.
|
|
98
|
+
*/
|
|
99
|
+
export interface UseMaskReturn {
|
|
100
|
+
/**
|
|
101
|
+
* Ref callback for the target `<input>` (or `<textarea>`). Attaches
|
|
102
|
+
* native event listeners on mount and tears them down on unmount or
|
|
103
|
+
* when the ref switches to a new element.
|
|
104
|
+
*/
|
|
105
|
+
ref: (element: HTMLInputElement | null) => void;
|
|
106
|
+
/**
|
|
107
|
+
* Current masked value (always reflects what is in the input).
|
|
108
|
+
*/
|
|
109
|
+
value: string;
|
|
110
|
+
/**
|
|
111
|
+
* Current unmasked (raw) value.
|
|
112
|
+
*/
|
|
113
|
+
rawValue: string;
|
|
114
|
+
/**
|
|
115
|
+
* Whether all required slots are filled.
|
|
116
|
+
*/
|
|
117
|
+
isComplete: boolean;
|
|
118
|
+
/**
|
|
119
|
+
* Programmatically set the masked value. The value is normalized
|
|
120
|
+
* through the mask before being applied.
|
|
121
|
+
*/
|
|
122
|
+
setValue: (next: string) => void;
|
|
123
|
+
/**
|
|
124
|
+
* Reset the input to an empty mask skeleton.
|
|
125
|
+
*/
|
|
126
|
+
reset: () => void;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* React hook for masked text input. Attach the returned `ref` to any
|
|
130
|
+
* component that forwards a ref to a real `<input>` (e.g. `InputText`,
|
|
131
|
+
* `Textarea`, `DatePicker.Input`). The hook installs DOM event listeners
|
|
132
|
+
* directly on the element, so no other props need to be wired.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* const { ref, value, rawValue, isComplete } = useMask({ mask: '99/99/9999' });
|
|
136
|
+
* return <InputText ref={ref} placeholder="DD/MM/YYYY" />;
|
|
137
|
+
*/
|
|
138
|
+
export declare function useMask(options: UseMaskOptions): UseMaskReturn;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{getUserAgent as _}from"@primeuix/utils";import*as u from"react";var Ie={9:/[0-9]/,a:/[A-Za-z]/,A:/[A-Z]/,"*":/[A-Za-z0-9]/};function Ae(Ce){let{mask:E,tokens:O,value:M,defaultValue:P,onValueChange:q,onComplete:z,slotChar:W="_",showMaskOnFocus:G=!1,alwaysShowMask:J=!1,autoClear:Q=!0,transform:X,readOnly:Y=!1}=Ce,$=u.useRef(q);$.current=q;let ee=u.useRef(z);ee.current=z;let D=u.useRef(X);D.current=X;let F=M!==void 0,[ye,we]=u.useState(P??""),V=F?M??"":ye,te=u.useCallback(e=>{F||we(e)},[F]),R=u.useRef(null),g=u.useRef(0),o=u.useRef([]),b=u.useRef([]),C=u.useRef(""),L=u.useRef(0),re=u.useRef(!1),U=u.useRef(!1),j=u.useRef(null),y=u.useRef(null),H=u.useRef(0),B=u.useRef(null),I=u.useRef(null),S=u.useRef(null),N=u.useRef(!1),ne=u.useRef(null),p=u.useRef({slotChar:W,showMaskOnFocus:G,alwaysShowMask:J,autoClear:Q,readOnly:Y,mask:E,userTokens:O});p.current={slotChar:W,showMaskOnFocus:G,alwaysShowMask:J,autoClear:Q,readOnly:Y,mask:E,userTokens:O};let h=u.useCallback(e=>{let t=p.current.slotChar;return t==null?"":e<t.length?t.charAt(e):t.charAt(0)},[]),l=u.useCallback((e,t)=>{let n=R.current,r=0,c=0;return n?(typeof e=="number"?(r=e,c=typeof t=="number"?t:r,n.setSelectionRange(r,c)):(r=n.selectionStart??0,c=n.selectionEnd??0),{begin:r,end:c}):{begin:r,end:c}},[]),m=u.useCallback(e=>{for(;++e<g.current&&!o.current[e];);return e},[]),ue=u.useCallback(e=>{for(;--e>=0&&!o.current[e];);return e},[]),Z=u.useRef(!1),a=u.useCallback((e,t)=>{let n=window.HTMLInputElement.prototype,r=Object.getOwnPropertyDescriptor(n,"value")?.set;r?r.call(e,t):e.value=t},[]),ce=u.useCallback(e=>{Z.current=!0;try{e.dispatchEvent(new Event("input",{bubbles:!0}))}finally{Z.current=!1}},[]),v=u.useCallback(()=>{let e=R.current;e&&a(e,b.current.join(""))},[a]),w=u.useCallback((e,t)=>{for(let n=e;n<t&&n<g.current;n++)o.current[n]&&(b.current[n]=h(n))},[h]),A=u.useCallback((e,t)=>{if(e<0)return;let n=m(t);for(let r=e;r<g.current;r++)if(o.current[r]){if(n<g.current&&o.current[r]?.test(b.current[n]))b.current[r]=b.current[n],b.current[n]=h(n);else break;n=m(n)}v(),l(Math.max(y.current??0,e))},[m,h,v,l]),se=u.useCallback(e=>{let t=h(e);for(let n=e;n<g.current;n++)if(o.current[n]){let r=m(n),c=b.current[n];if(b.current[n]=t,r<g.current&&o.current[r]?.test(c))t=c;else break}},[h,m]),i=u.useCallback((e=!1)=>{let t=R.current;if(!t)return y.current??0;let n=t.value,r=-1,c,s,k;for(c=0,k=0;c<g.current;c++){let T=o.current[c];if(T!==null){for(b.current[c]=h(c);k++<(n?.length??0);)if(s=n?.charAt(k-1)??"",T.test(s)){b.current[c]=s,r=c;break}if(k>(n?.length??0)){w(c+1,g.current);break}}else b.current[c]===n?.charAt(k)&&k++,c<L.current&&(r=c)}return e?v():r+1<L.current?p.current.autoClear||b.current.join("")===C.current?(t.value&&a(t,""),w(0,g.current)):v():(v(),a(t,t.value.substring(0,r+1))),L.current?c:y.current??0},[h,w,v,a]),K=u.useCallback(e=>{let t=[],n=e.split("");for(let r=0;r<n.length;r++){let c=n[r];o.current[r]&&c!==h(r)&&t.push(c)}return t.join("")},[h]),x=u.useCallback(e=>{let t=e.split("");for(let n=0;n<=H.current&&n<g.current;n++)if(o.current[n]){let r=t[n];if(!r||!o.current[n]?.test(r))return!1}return!0},[]),f=u.useCallback(e=>{let t=R.current;if(!t)return;let n=t.value,r=n===C.current,c=r?"":n;if(ne.current===c)return;ne.current=c,e?.type!=="input"&&ce(t);let s=r?"":K(n),k=!r&&x(n),T={originalEvent:e,value:c,rawValue:s,isComplete:k};te(c),$.current?.(T),k&&!N.current&&ee.current?.(T),N.current=k,S.current=n},[K,x,te,ce]),ae=u.useCallback(e=>{if(p.current.readOnly||e.type==="paste")return;let t=i(!0);l(t),f(e)},[i,l,f]),le=u.useCallback(e=>{let t=R.current;if(!t)return;let n=t.value,r=l();if(r){if(I.current&&I.current.length&&I.current.length>n.length){for(i(!0);r.begin>0&&!o.current[r.begin-1];)r.begin--;if(r.begin===0)for(;r.begin<(y.current??0)&&!o.current[r.begin];)r.begin++;l(r.begin,r.begin)}else{for(i(!0);r.begin<g.current&&!o.current[r.begin];)r.begin++;l(r.begin,r.begin)}f(e)}},[l,i,f]),oe=u.useCallback(e=>{R.current&&(Z.current||"isComposing"in e&&e.isComposing||(re.current?le(e):ae(e)))},[le,ae]),ie=u.useCallback(e=>{let t=R.current;if(!t||p.current.readOnly)return;let n=e.code,r,c,s,k=/iphone/i.test(_());if(I.current=t.value,n==="Backspace"||n==="Delete"||k&&n==="Escape"){if(r=l(),!r)return;c=r.begin,s=r.end,s-c===0&&(c=n!=="Delete"?ue(c):s=m(c-1),s=n==="Delete"?m(s):s),w(c,s),A(c,s-1),f(e),e.preventDefault()}else n==="Enter"?(t.blur(),f(e)):n==="Escape"&&(a(t,j.current??""),l(0,i()),f(e),e.preventDefault())},[l,ue,m,w,A,f,a,i]),fe=u.useCallback(e=>{if(!R.current||p.current.readOnly)return;let n=e.code,r=l();if(r&&!(e.ctrlKey||e.altKey||e.metaKey||e.shiftKey||e.key==="CapsLock"||e.key==="Escape"||e.key==="Tab")){if(n&&n!=="Enter"){r.end-r.begin!==0&&(w(r.begin,r.end),A(r.begin,r.end-1));let c=m(r.begin-1);if(c<g.current){let s=e.key;if(D.current&&(s=D.current(s)),o.current[c]?.test(s)){se(c),b.current[c]=s,v();let k=m(c);/android/i.test(_())?setTimeout(()=>l(k),0):l(k)}}e.preventDefault()}f(e)}},[l,w,A,m,se,v,f]),Re=u.useCallback(e=>{let t=R.current;if(!t||p.current.readOnly)return;B.current&&clearTimeout(B.current),U.current=!0,j.current=t.value;let n=p.current.showMaskOnFocus||p.current.alwaysShowMask;if(!t.value||t.value===C.current)n&&a(t,C.current),requestAnimationFrame(()=>{t===document.activeElement&&l(y.current??0,y.current??0)});else{let r=i();B.current=setTimeout(()=>{t===document.activeElement&&(v(),r===p.current.mask?.replace("?","").length?l(0,r):l(r))},10)}},[l,i,v,a]),be=u.useCallback(e=>{let t=R.current;t&&(U.current=!1,i(),!p.current.alwaysShowMask&&t.value===C.current&&a(t,""),f(e),t.value!==j.current&&t.dispatchEvent(new Event("change",{bubbles:!0,cancelable:!1})))},[i,f,a]),ge=u.useCallback(e=>{!R.current||p.current.readOnly||setTimeout(()=>{let n=i(!0);l(n),f(e)},0)},[i,l,f]),pe=u.useCallback(()=>{o.current=[],L.current=E?E.length:0,g.current=E?E.length:0,y.current=null,H.current=0;let e={...Ie,...O},t=_();re.current=/chrome/i.test(t)&&/android/i.test(t);let n=E?E.split(""):[];for(let c=0;c<n.length;c++){let s=n[c];s==="?"?(g.current--,L.current=c):e[s]?(o.current.push(e[s]),y.current===null&&(y.current=o.current.length-1),c<L.current&&(H.current=o.current.length-1)):o.current.push(null)}b.current=[];for(let c=0;c<n.length;c++){let s=n[c];s!=="?"&&(e[s]?b.current.push(h(c)):b.current.push(s))}C.current=b.current.join("");let r=R.current;r&&!r.value&&p.current.alwaysShowMask&&a(r,C.current)},[E,O,h,a]);u.useEffect(()=>{pe()},[pe]),u.useEffect(()=>{let e=R.current;e&&M!==void 0&&M!==S.current&&(U.current||(a(e,M),i(),S.current=e.value))},[M,a,i]);let ke=u.useRef(oe);ke.current=oe;let de=u.useRef(ie);de.current=ie;let he=u.useRef(fe);he.current=fe;let me=u.useRef(Re);me.current=Re;let ve=u.useRef(be);ve.current=be;let Ee=u.useRef(ge);Ee.current=ge;let d=u.useMemo(()=>({input:e=>ke.current(e),keydown:e=>de.current(e),keypress:e=>he.current(e),focus:e=>me.current(e),blur:e=>ve.current(e),paste:e=>Ee.current(e)}),[]),Me=u.useCallback(e=>{let t=R.current;t&&t!==e&&(t.removeEventListener("input",d.input),t.removeEventListener("keydown",d.keydown),t.removeEventListener("keypress",d.keypress),t.removeEventListener("focus",d.focus),t.removeEventListener("blur",d.blur),t.removeEventListener("paste",d.paste)),R.current=e,e&&e!==t&&(e.addEventListener("input",d.input),e.addEventListener("keydown",d.keydown),e.addEventListener("keypress",d.keypress),e.addEventListener("focus",d.focus),e.addEventListener("blur",d.blur),e.addEventListener("paste",d.paste),M?(a(e,M),i()):P&&!e.value?(a(e,P),i()):p.current.alwaysShowMask&&!e.value&&a(e,C.current))},[d]),Ve=u.useMemo(()=>V?K(V):"",[V,K]),Le=u.useMemo(()=>V?x(V):!1,[V,x]),Te=u.useCallback(e=>{let t=R.current;t&&(a(t,e??""),i(!0),f(null))},[a,i,f]),Oe=u.useCallback(()=>{let e=R.current;e&&(a(e,""),w(0,g.current),p.current.alwaysShowMask&&a(e,C.current),S.current=e.value,N.current=!1,f(null))},[a,w,f]);return{ref:Me,value:V,rawValue:Ve,isComplete:Le,setValue:Te,reset:Oe}}export{Ae as useMask};
|
|
@@ -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 @@
|
|
|
1
|
+
import*as e from"react";function o(n,c=!0){let[i,s]=e.useState(!1),t=e.useRef(null),a=e.useCallback(l=>s(l.matches),[]),r=e.useCallback(()=>t.current?.addEventListener("change",a),[a]),u=e.useCallback(()=>t.current?.removeEventListener("change",a)&&(t.current=null),[a]);return e.useEffect(()=>(c&&(t.current=window.matchMedia(n),s(t.current.matches),r()),()=>{u()}),[n,c,r,u]),i}export{o as useMatchMedia};
|
|
@@ -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
|
+
import*as e from"react";function f(c){let t=e.useRef(!1);e.useEffect(()=>{t.current||(t.current=!0,c?.())},[])}export{f as useMountEffect};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for the useMounted hook.
|
|
3
|
+
*/
|
|
4
|
+
export interface UseMountedOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Callback invoked when the component transitions to mounted state.
|
|
7
|
+
*/
|
|
8
|
+
onMounted?: () => void;
|
|
9
|
+
/**
|
|
10
|
+
* Callback invoked when the component transitions to unmounted state
|
|
11
|
+
* or when the component is destroyed while mounted.
|
|
12
|
+
*/
|
|
13
|
+
onUnmounted?: () => void;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Custom hook that tracks whether a component is mounted on the client.
|
|
17
|
+
* Provides SSR-safe mounting detection with optional lifecycle callbacks.
|
|
18
|
+
*
|
|
19
|
+
* @param {UseMountedOptions} options configuration options
|
|
20
|
+
* @returns {boolean} whether the component is currently mounted
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* const MyComponent = () => {
|
|
25
|
+
* const mounted = useMounted({
|
|
26
|
+
* onMounted: () => console.log('Mounted'),
|
|
27
|
+
* onUnmounted: () => console.log('Unmounted')
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* if (!mounted) return null;
|
|
31
|
+
*
|
|
32
|
+
* return <div>Content</div>;
|
|
33
|
+
* };
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function useMounted(options?: UseMountedOptions): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import*as e from"react";function u(t={}){let[n,o]=e.useState(!1);return e.useEffect(()=>(o(!0),t.onMounted?.(),()=>{t.onUnmounted?.()}),[]),n}export{u as useMounted};
|