UNPKG

react-use-anywhere

Version:

Use React hooks anywhere in your codebase - in services, utilities, and business logic. Router-agnostic, hook-agnostic, and works with any React hooks (auth, navigation, state, custom hooks, etc.).

44 lines (40 loc) 16.8 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const x=require("react");var N={exports:{}},L={};/** * @license React * react-jsx-runtime.production.min.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */var je;function _r(){if(je)return L;je=1;var n=x,a=Symbol.for("react.element"),p=Symbol.for("react.fragment"),d=Object.prototype.hasOwnProperty,l=n.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,S={key:!0,ref:!0,__self:!0,__source:!0};function m(b,g,j){var R,T={},w=null,Y=null;j!==void 0&&(w=""+j),g.key!==void 0&&(w=""+g.key),g.ref!==void 0&&(Y=g.ref);for(R in g)d.call(g,R)&&!S.hasOwnProperty(R)&&(T[R]=g[R]);if(b&&b.defaultProps)for(R in g=b.defaultProps,g)T[R]===void 0&&(T[R]=g[R]);return{$$typeof:a,type:b,key:w,ref:Y,props:T,_owner:l.current}}return L.Fragment=p,L.jsx=m,L.jsxs=m,L}var W={};/** * @license React * react-jsx-runtime.development.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */var He;function Sr(){return He||(He=1,process.env.NODE_ENV!=="production"&&function(){var n=x,a=Symbol.for("react.element"),p=Symbol.for("react.portal"),d=Symbol.for("react.fragment"),l=Symbol.for("react.strict_mode"),S=Symbol.for("react.profiler"),m=Symbol.for("react.provider"),b=Symbol.for("react.context"),g=Symbol.for("react.forward_ref"),j=Symbol.for("react.suspense"),R=Symbol.for("react.suspense_list"),T=Symbol.for("react.memo"),w=Symbol.for("react.lazy"),Y=Symbol.for("react.offscreen"),ae=Symbol.iterator,Le="@@iterator";function We(e){if(e===null||typeof e!="object")return null;var r=ae&&e[ae]||e[Le];return typeof r=="function"?r:null}var H=n.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function y(e){{for(var r=arguments.length,t=new Array(r>1?r-1:0),o=1;o<r;o++)t[o-1]=arguments[o];Ye("error",e,t)}}function Ye(e,r,t){{var o=H.ReactDebugCurrentFrame,s=o.getStackAddendum();s!==""&&(r+="%s",t=t.concat([s]));var c=t.map(function(u){return String(u)});c.unshift("Warning: "+r),Function.prototype.apply.call(console[e],console,c)}}var Me=!1,Ve=!1,Ue=!1,Je=!1,qe=!1,ie;ie=Symbol.for("react.module.reference");function Be(e){return!!(typeof e=="string"||typeof e=="function"||e===d||e===S||qe||e===l||e===j||e===R||Je||e===Y||Me||Ve||Ue||typeof e=="object"&&e!==null&&(e.$$typeof===w||e.$$typeof===T||e.$$typeof===m||e.$$typeof===b||e.$$typeof===g||e.$$typeof===ie||e.getModuleId!==void 0))}function Ke(e,r,t){var o=e.displayName;if(o)return o;var s=r.displayName||r.name||"";return s!==""?t+"("+s+")":t}function ue(e){return e.displayName||"Context"}function C(e){if(e==null)return null;if(typeof e.tag=="number"&&y("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),typeof e=="function")return e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case d:return"Fragment";case p:return"Portal";case S:return"Profiler";case l:return"StrictMode";case j:return"Suspense";case R:return"SuspenseList"}if(typeof e=="object")switch(e.$$typeof){case b:var r=e;return ue(r)+".Consumer";case m:var t=e;return ue(t._context)+".Provider";case g:return Ke(e,e.render,"ForwardRef");case T:var o=e.displayName||null;return o!==null?o:C(e.type)||"Memo";case w:{var s=e,c=s._payload,u=s._init;try{return C(u(c))}catch{return null}}}return null}var O=Object.assign,F=0,se,ce,le,fe,de,ve,pe;function ge(){}ge.__reactDisabledLog=!0;function Ge(){{if(F===0){se=console.log,ce=console.info,le=console.warn,fe=console.error,de=console.group,ve=console.groupCollapsed,pe=console.groupEnd;var e={configurable:!0,enumerable:!0,value:ge,writable:!0};Object.defineProperties(console,{info:e,log:e,warn:e,error:e,group:e,groupCollapsed:e,groupEnd:e})}F++}}function ze(){{if(F--,F===0){var e={configurable:!0,enumerable:!0,writable:!0};Object.defineProperties(console,{log:O({},e,{value:se}),info:O({},e,{value:ce}),warn:O({},e,{value:le}),error:O({},e,{value:fe}),group:O({},e,{value:de}),groupCollapsed:O({},e,{value:ve}),groupEnd:O({},e,{value:pe})})}F<0&&y("disabledDepth fell below zero. This is a bug in React. Please file an issue.")}}var q=H.ReactCurrentDispatcher,B;function M(e,r,t){{if(B===void 0)try{throw Error()}catch(s){var o=s.stack.trim().match(/\n( *(at )?)/);B=o&&o[1]||""}return` `+B+e}}var K=!1,V;{var Xe=typeof WeakMap=="function"?WeakMap:Map;V=new Xe}function ye(e,r){if(!e||K)return"";{var t=V.get(e);if(t!==void 0)return t}var o;K=!0;var s=Error.prepareStackTrace;Error.prepareStackTrace=void 0;var c;c=q.current,q.current=null,Ge();try{if(r){var u=function(){throw Error()};if(Object.defineProperty(u.prototype,"props",{set:function(){throw Error()}}),typeof Reflect=="object"&&Reflect.construct){try{Reflect.construct(u,[])}catch(E){o=E}Reflect.construct(e,[],u)}else{try{u.call()}catch(E){o=E}e.call(u.prototype)}}else{try{throw Error()}catch(E){o=E}e()}}catch(E){if(E&&o&&typeof E.stack=="string"){for(var i=E.stack.split(` `),h=o.stack.split(` `),f=i.length-1,v=h.length-1;f>=1&&v>=0&&i[f]!==h[v];)v--;for(;f>=1&&v>=0;f--,v--)if(i[f]!==h[v]){if(f!==1||v!==1)do if(f--,v--,v<0||i[f]!==h[v]){var _=` `+i[f].replace(" at new "," at ");return e.displayName&&_.includes("<anonymous>")&&(_=_.replace("<anonymous>",e.displayName)),typeof e=="function"&&V.set(e,_),_}while(f>=1&&v>=0);break}}}finally{K=!1,q.current=c,ze(),Error.prepareStackTrace=s}var $=e?e.displayName||e.name:"",P=$?M($):"";return typeof e=="function"&&V.set(e,P),P}function Ze(e,r,t){return ye(e,!1)}function Qe(e){var r=e.prototype;return!!(r&&r.isReactComponent)}function U(e,r,t){if(e==null)return"";if(typeof e=="function")return ye(e,Qe(e));if(typeof e=="string")return M(e);switch(e){case j:return M("Suspense");case R:return M("SuspenseList")}if(typeof e=="object")switch(e.$$typeof){case g:return Ze(e.render);case T:return U(e.type,r,t);case w:{var o=e,s=o._payload,c=o._init;try{return U(c(s),r,t)}catch{}}}return""}var I=Object.prototype.hasOwnProperty,he={},be=H.ReactDebugCurrentFrame;function J(e){if(e){var r=e._owner,t=U(e.type,e._source,r?r.type:null);be.setExtraStackFrame(t)}else be.setExtraStackFrame(null)}function Ne(e,r,t,o,s){{var c=Function.call.bind(I);for(var u in e)if(c(e,u)){var i=void 0;try{if(typeof e[u]!="function"){var h=Error((o||"React class")+": "+t+" type `"+u+"` is invalid; it must be a function, usually from the `prop-types` package, but received `"+typeof e[u]+"`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`.");throw h.name="Invariant Violation",h}i=e[u](r,u,o,t,null,"SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED")}catch(f){i=f}i&&!(i instanceof Error)&&(J(s),y("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).",o||"React class",t,u,typeof i),J(null)),i instanceof Error&&!(i.message in he)&&(he[i.message]=!0,J(s),y("Failed %s type: %s",t,i.message),J(null))}}}var er=Array.isArray;function G(e){return er(e)}function rr(e){{var r=typeof Symbol=="function"&&Symbol.toStringTag,t=r&&e[Symbol.toStringTag]||e.constructor.name||"Object";return t}}function tr(e){try{return Ee(e),!1}catch{return!0}}function Ee(e){return""+e}function Re(e){if(tr(e))return y("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.",rr(e)),Ee(e)}var _e=H.ReactCurrentOwner,nr={key:!0,ref:!0,__self:!0,__source:!0},Se,Te;function or(e){if(I.call(e,"ref")){var r=Object.getOwnPropertyDescriptor(e,"ref").get;if(r&&r.isReactWarning)return!1}return e.ref!==void 0}function ar(e){if(I.call(e,"key")){var r=Object.getOwnPropertyDescriptor(e,"key").get;if(r&&r.isReactWarning)return!1}return e.key!==void 0}function ir(e,r){typeof e.ref=="string"&&_e.current}function ur(e,r){{var t=function(){Se||(Se=!0,y("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",r))};t.isReactWarning=!0,Object.defineProperty(e,"key",{get:t,configurable:!0})}}function sr(e,r){{var t=function(){Te||(Te=!0,y("%s: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",r))};t.isReactWarning=!0,Object.defineProperty(e,"ref",{get:t,configurable:!0})}}var cr=function(e,r,t,o,s,c,u){var i={$$typeof:a,type:e,key:r,ref:t,props:u,_owner:c};return i._store={},Object.defineProperty(i._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:!1}),Object.defineProperty(i,"_self",{configurable:!1,enumerable:!1,writable:!1,value:o}),Object.defineProperty(i,"_source",{configurable:!1,enumerable:!1,writable:!1,value:s}),Object.freeze&&(Object.freeze(i.props),Object.freeze(i)),i};function lr(e,r,t,o,s){{var c,u={},i=null,h=null;t!==void 0&&(Re(t),i=""+t),ar(r)&&(Re(r.key),i=""+r.key),or(r)&&(h=r.ref,ir(r,s));for(c in r)I.call(r,c)&&!nr.hasOwnProperty(c)&&(u[c]=r[c]);if(e&&e.defaultProps){var f=e.defaultProps;for(c in f)u[c]===void 0&&(u[c]=f[c])}if(i||h){var v=typeof e=="function"?e.displayName||e.name||"Unknown":e;i&&ur(u,v),h&&sr(u,v)}return cr(e,i,h,s,o,_e.current,u)}}var z=H.ReactCurrentOwner,ke=H.ReactDebugCurrentFrame;function A(e){if(e){var r=e._owner,t=U(e.type,e._source,r?r.type:null);ke.setExtraStackFrame(t)}else ke.setExtraStackFrame(null)}var X;X=!1;function Z(e){return typeof e=="object"&&e!==null&&e.$$typeof===a}function Ce(){{if(z.current){var e=C(z.current.type);if(e)return` Check the render method of \``+e+"`."}return""}}function fr(e){return""}var me={};function dr(e){{var r=Ce();if(!r){var t=typeof e=="string"?e:e.displayName||e.name;t&&(r=` Check the top-level render call using <`+t+">.")}return r}}function we(e,r){{if(!e._store||e._store.validated||e.key!=null)return;e._store.validated=!0;var t=dr(r);if(me[t])return;me[t]=!0;var o="";e&&e._owner&&e._owner!==z.current&&(o=" It was passed a child from "+C(e._owner.type)+"."),A(e),y('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.',t,o),A(null)}}function Oe(e,r){{if(typeof e!="object")return;if(G(e))for(var t=0;t<e.length;t++){var o=e[t];Z(o)&&we(o,r)}else if(Z(e))e._store&&(e._store.validated=!0);else if(e){var s=We(e);if(typeof s=="function"&&s!==e.entries)for(var c=s.call(e),u;!(u=c.next()).done;)Z(u.value)&&we(u.value,r)}}}function vr(e){{var r=e.type;if(r==null||typeof r=="string")return;var t;if(typeof r=="function")t=r.propTypes;else if(typeof r=="object"&&(r.$$typeof===g||r.$$typeof===T))t=r.propTypes;else return;if(t){var o=C(r);Ne(t,e.props,"prop",o,e)}else if(r.PropTypes!==void 0&&!X){X=!0;var s=C(r);y("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?",s||"Unknown")}typeof r.getDefaultProps=="function"&&!r.getDefaultProps.isReactClassApproved&&y("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead.")}}function pr(e){{for(var r=Object.keys(e.props),t=0;t<r.length;t++){var o=r[t];if(o!=="children"&&o!=="key"){A(e),y("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.",o),A(null);break}}e.ref!==null&&(A(e),y("Invalid attribute `ref` supplied to `React.Fragment`."),A(null))}}var Pe={};function xe(e,r,t,o,s,c){{var u=Be(e);if(!u){var i="";(e===void 0||typeof e=="object"&&e!==null&&Object.keys(e).length===0)&&(i+=" You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.");var h=fr();h?i+=h:i+=Ce();var f;e===null?f="null":G(e)?f="array":e!==void 0&&e.$$typeof===a?(f="<"+(C(e.type)||"Unknown")+" />",i=" Did you accidentally export a JSX literal instead of a component?"):f=typeof e,y("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s",f,i)}var v=lr(e,r,t,s,c);if(v==null)return v;if(u){var _=r.children;if(_!==void 0)if(o)if(G(_)){for(var $=0;$<_.length;$++)Oe(_[$],e);Object.freeze&&Object.freeze(_)}else y("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else Oe(_,e)}if(I.call(r,"key")){var P=C(e),E=Object.keys(r).filter(function(Rr){return Rr!=="key"}),Q=E.length>0?"{key: someKey, "+E.join(": ..., ")+": ...}":"{key: someKey}";if(!Pe[P+Q]){var Er=E.length>0?"{"+E.join(": ..., ")+": ...}":"{}";y(`A props object containing a "key" prop is being spread into JSX: let props = %s; <%s {...props} /> React keys must be passed directly to JSX without using spread: let props = %s; <%s key={someKey} {...props} />`,Q,P,Er,P),Pe[P+Q]=!0}}return e===d?pr(v):vr(v),v}}function gr(e,r,t){return xe(e,r,t,!0)}function yr(e,r,t){return xe(e,r,t,!1)}var hr=yr,br=gr;W.Fragment=d,W.jsx=hr,W.jsxs=br}()),W}process.env.NODE_ENV==="production"?N.exports=_r():N.exports=Sr();var Tr=N.exports;const Ae=x.createContext(null);let ee={};const $e=({children:n,hooks:a})=>{ee=a;const p={};return Object.entries(a).forEach(([d,l])=>{try{p[d]=l()}catch(S){console.warn(`Failed to execute hook "${d}":`,S),p[d]=void 0}}),Tr.jsx(Ae.Provider,{value:p,children:n})},kr=({children:n,hooks:a})=>$e({children:n,hooks:a}),D=()=>{const n=x.useContext(Ae);if(n===null)throw new Error("useHookContext must be used within a HookProvider");return n};function Cr(){return D()}const De=n=>n in ee,Fe=()=>Object.keys(ee);function re(n,a){const p=D(),d=x.useRef();x.useEffect(()=>{if(!De(a)){const l=Fe(),S=l.filter(b=>b.toLowerCase().includes(a.toLowerCase())||a.toLowerCase().includes(b.toLowerCase())).slice(0,3),m=S.length>0?` Did you mean one of these? ${S.map(b=>` • "${b}"`).join(` `)}`:"";console.error(`🚨 useHookService: Hook "${a}" is not registered in HookProvider. Available hooks: ${l.map(b=>`"${b}"`).join(", ")}${m}`)}},[a]),x.useEffect(()=>{const l=p[a];l!==d.current&&(l!==d.current&&JSON.stringify(l)!==JSON.stringify(d.current)||l!==void 0)&&(n._setValue(l),d.current=l)},[p[a],n])}function mr(n,a){re(n,a)}function wr(n,a){re(n,a)}function Or(n){return D()[n]}function Pr(n){return Ie(n)}function Ie(n){const a=D();return Object.prototype.hasOwnProperty.call(a,n)?a[n]:void 0}function xr(){return D()}function te(){let n=null,a=!1;return{_setValue(p){(p!==n&&JSON.stringify(p)!==JSON.stringify(n)||!a)&&(n=p,a=!0)},get(){return n},isReady(){return a},use(p){if(!a)return console.warn("Hook service not ready. Make sure you're using useHookService in a React component."),null;try{return p(n)}catch(d){return console.error("Error using hook service:",d),null}},_reset(){n=null,a=!1}}}const k=new Map;function ne(n){const a=Fe();if(a.length===0){console.warn(`🚨 No hooks registered yet. Make sure to wrap your app with HookProvider first. Example: <HookProvider hooks={{ ${n}: your${n.charAt(0).toUpperCase()+n.slice(1)}Hook }}>`);return}if(!De(n)){const p=a.filter(l=>l.toLowerCase().includes(n.toLowerCase())||n.toLowerCase().includes(l.toLowerCase())).slice(0,3),d=p.length>0?` Did you mean one of these? ${p.map(l=>` • "${l}"`).join(` `)}`:"";console.error(`🚨 Hook "${n}" is not registered in HookProvider. Available hooks: ${a.map(l=>`"${l}"`).join(", ")}${d} 💡 Make sure your HookProvider includes: <HookProvider hooks={{ ${n}: your${n.charAt(0).toUpperCase()+n.slice(1)}Hook, ...other hooks }}>`)}}function oe(n){return ne(n),k.has(n)||k.set(n,te()),k.get(n)}function jr(n){ne(n);const a=k.get(n);return a||null}function Hr(){k.forEach(n=>{n._reset()}),k.clear()}function Ar(n){return oe(n)}function $r(n){return ne(n),k.has(n)||k.set(n,te()),k.get(n)}function Dr(n){return oe(n)}exports.HookProvider=$e;exports.TypedHookProvider=kr;exports.createHookService=te;exports.createInferredSingletonService=Dr;exports.createSingletonService=oe;exports.createStrictSingletonService=$r;exports.createTypedSingletonService=Ar;exports.getSingletonService=jr;exports.resetAllServices=Hr;exports.useAllHooks=xr;exports.useHook=Ie;exports.useHookContext=D;exports.useHookService=re;exports.useStrictHook=Pr;exports.useStrictHookService=wr;exports.useTypedHook=Or;exports.useTypedHookContext=Cr;exports.useTypedHookService=mr; //# sourceMappingURL=index.cjs.map