@fanee/react 0.6.0 → 0.6.2

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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/provider.tsx","../src/hooks.ts"],"mappings":";;;;UAKiB,kBAAA;;EAEhB,OAAA,EAAS,YAAA;EAFyB;EAIlC,QAAA,EAAU,SAAS;AAAA;;;;;;AAAA;AAepB;;;;;;iBAAgB,aAAA;EAAgB,OAAA;EAAS;AAAA,GAAY,kBAAA,mBAAkB,GAAA,CAAA,OAAA;;iBAKvD,UAAA,IAAc,YAAY;;;;;AAxB1C;;;;;;;;iBCsBgB,QAAA;EAAc,OAAA,EAAS,UAAA,QAAkB,UAAA;EAAa,MAAA,EAAQ,MAAA;AAAA;;;;;;;;;;;;;;;ADHP;AAKvE;iBCoBgB,IAAA,CAAK,OAAA,GAAU,OAAA,CAAQ,gBAAA,IAAoB,iBAAA;;;ADpBjB;;;iBCyC1B,SAAA,IAAa,MAAM;AA3CnC;;;;;AAAA,iBAsDgB,YAAA,KAAiB,MAAc,EAAN,MAAM"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/provider.tsx","../src/hooks.ts"],"mappings":";;;;UAKiB,kBAAA;;EAEhB,OAAA,EAAS,YAAA;EAFyB;EAIlC,QAAA,EAAU,SAAS;AAAA;;;;;;AAAA;AAepB;;;;;;iBAAgB,aAAA;EAAgB,OAAA;EAAS;AAAA,GAAY,kBAAA,mBAAkB,GAAA,CAAA,OAAA;;iBAKvD,UAAA,IAAc,YAAY;;;;;AAxB1C;;;;;;;;iBCgBgB,QAAA;EAAc,OAAA,EAAS,UAAA,QAAkB,UAAA;EAAa,MAAA,EAAQ,MAAA;AAAA;;;;;;;;;;;;;;;ADGP;AAKvE;iBCcgB,IAAA,CAAK,OAAA,GAAU,OAAA,CAAQ,gBAAA,IAAoB,iBAAA;;;ADdjB;;;iBCuC1B,SAAA,IAAa,MAAM;AA/CnC;;;;;AAAA,iBA6DgB,YAAA,KAAiB,MAAc,EAAN,MAAM"}
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- import{createContext as e,useCallback as t,useContext as n,useEffect as r,useMemo as i,useReducer as a}from"react";import{jsx as o}from"react/jsx-runtime";const s=e(null);function c({runtime:e,children:t}){return o(s.Provider,{value:e,children:t})}function l(){let e=n(s);if(!e)throw Error(`[fanee] Fanee hooks must be used within a FaneeProvider`);return e}function u(){let e=l(),[t,n]=a(e=>e+1,0);return r(()=>e.subscribe(()=>{n()}),[e]),t}function d(){let e=l(),t=p();return i(()=>({runtime:e,locale:t}),[e,t])}function f(e){let n=l(),r=u(),i=e?.locale,a=e?.namespace;return t((e,t)=>n.getT({locale:i,namespace:a})(e,t),[n,i,a,r])}function p(){let e=l();return u(),e.getLocale()}function m(){let e=l();return t(t=>e.setLocale(t),[e])}export{c as FaneeProvider,d as useFanee,p as useLocale,m as useSetLocale,f as useT};
1
+ import{createContext as e,useCallback as t,useContext as n,useMemo as r,useSyncExternalStore as i}from"react";import{jsx as a}from"react/jsx-runtime";const o=e(null);function s({runtime:e,children:t}){return a(o.Provider,{value:e,children:t})}function c(){let e=n(o);if(!e)throw Error(`[fanee] Fanee hooks must be used within a FaneeProvider`);return e}function l(e,t){return e.subscribe(()=>t())}function u(){let e=c(),t=f();return r(()=>({runtime:e,locale:t}),[e,t])}function d(e){let n=c(),r=i(e=>l(n,e),()=>n.getVersion(),()=>n.getVersion()),a=e?.locale,o=e?.namespace;return t((e,t)=>n.getT({locale:a,namespace:o})(e,t),[n,a,o,r])}function f(){let e=c();return i(t=>l(e,t),()=>e.getLocale(),()=>e.getLocale())}function p(){let e=c();return t(t=>e.setLocale(t),[e])}export{s as FaneeProvider,u as useFanee,f as useLocale,p as useSetLocale,d as useT};
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/provider.tsx","../src/hooks.ts"],"sourcesContent":["import { createContext, useContext, type ReactNode } from \"react\";\nimport type { FaneeRuntime } from \"@fanee/core\";\n\nconst FaneeContext = createContext<FaneeRuntime | null>(null);\n\nexport interface FaneeProviderProps {\n\t/** FaneeRuntime instance to expose to the React tree. */\n\truntime: FaneeRuntime;\n\t/** React children. */\n\tchildren: ReactNode;\n}\n\n/**\n * Provides a FaneeRuntime instance to descendant components.\n *\n * @example\n * ```tsx\n * const runtime = new FaneeRuntime().config({ currentLocale: \"en\" });\n *\n * <FaneeProvider runtime={runtime}>\n * <App />\n * </FaneeProvider>\n * ```\n */\nexport function FaneeProvider({ runtime, children }: FaneeProviderProps) {\n\treturn <FaneeContext.Provider value={runtime}>{children}</FaneeContext.Provider>;\n}\n\n/** @internal */\nexport function useRuntime(): FaneeRuntime {\n\tconst runtime = useContext(FaneeContext);\n\tif (!runtime) {\n\t\tthrow new Error(\"[fanee] Fanee hooks must be used within a FaneeProvider\");\n\t}\n\treturn runtime;\n}\n","import { useCallback, useEffect, useReducer, useMemo } from \"react\";\nimport type { Locale, MessageKey, TranslateContext, TranslateFunction } from \"@fanee/core\";\nimport { useRuntime } from \"./provider\";\n\nfunction useRuntimeVersion(): number {\n\tconst runtime = useRuntime();\n\tconst [version, bump] = useReducer((v: number) => v + 1, 0);\n\n\tuseEffect(() => {\n\t\treturn runtime.subscribe(() => {\n\t\t\tbump();\n\t\t});\n\t}, [runtime]);\n\n\treturn version;\n}\n\n/**\n * Subscribe to the Fanee runtime and access it alongside the current locale.\n *\n * @returns An object with the runtime and the active locale.\n *\n * @example\n * ```tsx\n * const { runtime, locale } = useFanee();\n * ```\n */\nexport function useFanee(): { runtime: ReturnType<typeof useRuntime>; locale: Locale } {\n\tconst runtime = useRuntime();\n\tconst locale = useLocale();\n\treturn useMemo(() => ({ runtime, locale }), [runtime, locale]);\n}\n\n/**\n * Return a translation function bound to the current runtime state and\n * an optional namespace/locale context.\n *\n * The returned function is recreated whenever the active locale, namespace,\n * or underlying resources change, so it remains reactive to runtime updates.\n *\n * @param context - Optional locale/namespace overrides.\n * @returns A `(key, vars?) => string` translate function.\n *\n * @example\n * ```tsx\n * const t = useT({ namespace: \"errors\" });\n * return <p>{t(\"not_found\")}</p>;\n * ```\n */\nexport function useT(context?: Partial<TranslateContext>): TranslateFunction {\n\tconst runtime = useRuntime();\n\tconst version = useRuntimeVersion();\n\tconst locale = context?.locale;\n\tconst namespace = context?.namespace;\n\n\t// biome-ignore lint/correctness/useExhaustiveDependencies: version forces update on resource changes\n\treturn useCallback(\n\t\t(key: MessageKey, vars?: Record<string, unknown>) => {\n\t\t\tconst result = runtime.getT({ locale, namespace })(key, vars);\n\t\t\treturn result;\n\t\t},\n\t\t[runtime, locale, namespace, version]\n\t);\n}\n\n/**\n * Read the currently active locale from the runtime.\n *\n * @returns The BCP-47 locale tag.\n */\nexport function useLocale(): Locale {\n\tconst runtime = useRuntime();\n\tuseRuntimeVersion();\n\treturn runtime.getLocale();\n}\n\n/**\n * Return a setter for the runtime locale.\n *\n * @returns A function that updates the active locale.\n */\nexport function useSetLocale(): (locale: Locale) => void {\n\tconst runtime = useRuntime();\n\treturn useCallback((locale: Locale) => runtime.setLocale(locale), [runtime]);\n}\n"],"mappings":"2JAGA,MAAM,EAAe,EAAmC,IAAI,EAqB5D,SAAgB,EAAc,CAAE,UAAS,YAAgC,CACxE,OAAO,EAAC,EAAa,SAAd,CAAuB,MAAO,EAAU,UAAgC,CAAA,CAChF,CAGA,SAAgB,GAA2B,CAC1C,IAAM,EAAU,EAAW,CAAY,EACvC,GAAI,CAAC,EACJ,MAAU,MAAM,yDAAyD,EAE1E,OAAO,CACR,CC/BA,SAAS,GAA4B,CACpC,IAAM,EAAU,EAAW,EACrB,CAAC,EAAS,GAAQ,EAAY,GAAc,EAAI,EAAG,CAAC,EAQ1D,OANA,MACQ,EAAQ,cAAgB,CAC9B,EAAK,CACN,CAAC,EACC,CAAC,CAAO,CAAC,EAEL,CACR,CAYA,SAAgB,GAAuE,CACtF,IAAM,EAAU,EAAW,EACrB,EAAS,EAAU,EACzB,OAAO,OAAe,CAAE,UAAS,QAAO,GAAI,CAAC,EAAS,CAAM,CAAC,CAC9D,CAkBA,SAAgB,EAAK,EAAwD,CAC5E,IAAM,EAAU,EAAW,EACrB,EAAU,EAAkB,EAC5B,EAAS,GAAS,OAClB,EAAY,GAAS,UAG3B,OAAO,GACL,EAAiB,IACF,EAAQ,KAAK,CAAE,SAAQ,WAAU,CAAC,CAAC,CAAC,EAAK,CAC5C,EAEb,CAAC,EAAS,EAAQ,EAAW,CAAO,CACrC,CACD,CAOA,SAAgB,GAAoB,CACnC,IAAM,EAAU,EAAW,EAE3B,OADA,EAAkB,EACX,EAAQ,UAAU,CAC1B,CAOA,SAAgB,GAAyC,CACxD,IAAM,EAAU,EAAW,EAC3B,OAAO,EAAa,GAAmB,EAAQ,UAAU,CAAM,EAAG,CAAC,CAAO,CAAC,CAC5E"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/provider.tsx","../src/hooks.ts"],"sourcesContent":["import { createContext, useContext, type ReactNode } from \"react\";\nimport type { FaneeRuntime } from \"@fanee/core\";\n\nconst FaneeContext = createContext<FaneeRuntime | null>(null);\n\nexport interface FaneeProviderProps {\n\t/** FaneeRuntime instance to expose to the React tree. */\n\truntime: FaneeRuntime;\n\t/** React children. */\n\tchildren: ReactNode;\n}\n\n/**\n * Provides a FaneeRuntime instance to descendant components.\n *\n * @example\n * ```tsx\n * const runtime = new FaneeRuntime().config({ currentLocale: \"en\" });\n *\n * <FaneeProvider runtime={runtime}>\n * <App />\n * </FaneeProvider>\n * ```\n */\nexport function FaneeProvider({ runtime, children }: FaneeProviderProps) {\n\treturn <FaneeContext.Provider value={runtime}>{children}</FaneeContext.Provider>;\n}\n\n/** @internal */\nexport function useRuntime(): FaneeRuntime {\n\tconst runtime = useContext(FaneeContext);\n\tif (!runtime) {\n\t\tthrow new Error(\"[fanee] Fanee hooks must be used within a FaneeProvider\");\n\t}\n\treturn runtime;\n}\n","import { useCallback, useMemo, useSyncExternalStore } from \"react\";\nimport type { Locale, MessageKey, TranslateContext, TranslateFunction } from \"@fanee/core\";\nimport { useRuntime } from \"./provider\";\n\nfunction subscribeToRuntime(\n\truntime: ReturnType<typeof useRuntime>,\n\tcallback: () => void\n): () => void {\n\treturn runtime.subscribe(() => callback());\n}\n\n/**\n * Subscribe to the Fanee runtime and access it alongside the current locale.\n *\n * @returns An object with the runtime and the active locale.\n *\n * @example\n * ```tsx\n * const { runtime, locale } = useFanee();\n * ```\n */\nexport function useFanee(): { runtime: ReturnType<typeof useRuntime>; locale: Locale } {\n\tconst runtime = useRuntime();\n\tconst locale = useLocale();\n\treturn useMemo(() => ({ runtime, locale }), [runtime, locale]);\n}\n\n/**\n * Return a translation function bound to the current runtime state and\n * an optional namespace/locale context.\n *\n * The returned function is recreated whenever the active locale, namespace,\n * or underlying resources change, so it remains reactive to runtime updates.\n *\n * @param context - Optional locale/namespace overrides.\n * @returns A `(key, vars?) => string` translate function.\n *\n * @example\n * ```tsx\n * const t = useT({ namespace: \"errors\" });\n * return <p>{t(\"not_found\")}</p>;\n * ```\n */\nexport function useT(context?: Partial<TranslateContext>): TranslateFunction {\n\tconst runtime = useRuntime();\n\tconst version = useSyncExternalStore(\n\t\t(callback) => subscribeToRuntime(runtime, callback),\n\t\t() => runtime.getVersion(),\n\t\t() => runtime.getVersion()\n\t);\n\tconst locale = context?.locale;\n\tconst namespace = context?.namespace;\n\n\t// biome-ignore lint/correctness/useExhaustiveDependencies: version forces update on resource changes\n\treturn useCallback(\n\t\t(key: MessageKey, vars?: Record<string, unknown>) => {\n\t\t\tconst result = runtime.getT({ locale, namespace })(key, vars);\n\t\t\treturn result;\n\t\t},\n\t\t[runtime, locale, namespace, version]\n\t);\n}\n\n/**\n * Read the currently active locale from the runtime.\n *\n * @returns The BCP-47 locale tag.\n */\nexport function useLocale(): Locale {\n\tconst runtime = useRuntime();\n\treturn useSyncExternalStore(\n\t\t(callback) => subscribeToRuntime(runtime, callback),\n\t\t() => runtime.getLocale(),\n\t\t() => runtime.getLocale()\n\t);\n}\n\n/**\n * Return a setter for the runtime locale.\n *\n * @returns A function that updates the active locale.\n */\nexport function useSetLocale(): (locale: Locale) => void {\n\tconst runtime = useRuntime();\n\treturn useCallback((locale: Locale) => runtime.setLocale(locale), [runtime]);\n}\n"],"mappings":"sJAGA,MAAM,EAAe,EAAmC,IAAI,EAqB5D,SAAgB,EAAc,CAAE,UAAS,YAAgC,CACxE,OAAO,EAAC,EAAa,SAAd,CAAuB,MAAO,EAAU,UAAgC,CAAA,CAChF,CAGA,SAAgB,GAA2B,CAC1C,IAAM,EAAU,EAAW,CAAY,EACvC,GAAI,CAAC,EACJ,MAAU,MAAM,yDAAyD,EAE1E,OAAO,CACR,CC/BA,SAAS,EACR,EACA,EACa,CACb,OAAO,EAAQ,cAAgB,EAAS,CAAC,CAC1C,CAYA,SAAgB,GAAuE,CACtF,IAAM,EAAU,EAAW,EACrB,EAAS,EAAU,EACzB,OAAO,OAAe,CAAE,UAAS,QAAO,GAAI,CAAC,EAAS,CAAM,CAAC,CAC9D,CAkBA,SAAgB,EAAK,EAAwD,CAC5E,IAAM,EAAU,EAAW,EACrB,EAAU,EACd,GAAa,EAAmB,EAAS,CAAQ,MAC5C,EAAQ,WAAW,MACnB,EAAQ,WAAW,CAC1B,EACM,EAAS,GAAS,OAClB,EAAY,GAAS,UAG3B,OAAO,GACL,EAAiB,IACF,EAAQ,KAAK,CAAE,SAAQ,WAAU,CAAC,CAAC,CAAC,EAAK,CAC5C,EAEb,CAAC,EAAS,EAAQ,EAAW,CAAO,CACrC,CACD,CAOA,SAAgB,GAAoB,CACnC,IAAM,EAAU,EAAW,EAC3B,OAAO,EACL,GAAa,EAAmB,EAAS,CAAQ,MAC5C,EAAQ,UAAU,MAClB,EAAQ,UAAU,CACzB,CACD,CAOA,SAAgB,GAAyC,CACxD,IAAM,EAAU,EAAW,EAC3B,OAAO,EAAa,GAAmB,EAAQ,UAAU,CAAM,EAAG,CAAC,CAAO,CAAC,CAC5E"}
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "main": "dist/index.js",
4
4
  "module": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
- "version": "0.6.0",
6
+ "version": "0.6.2",
7
7
  "type": "module",
8
8
  "private": false,
9
9
  "license": "MIT",
@@ -21,7 +21,7 @@
21
21
  "react": ">=18"
22
22
  },
23
23
  "dependencies": {
24
- "@fanee/core": "workspace:*"
24
+ "@fanee/core": "^0.6.2"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@testing-library/react": "^16.0.0",