@verbaly/react 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.cjs +41 -84
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -11
- package/dist/index.d.ts +12 -11
- package/dist/index.js +36 -65
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<img src="https://raw.githubusercontent.com/AronSoto/verbaly/develop/assets/logo
|
|
2
|
+
<img src="https://raw.githubusercontent.com/AronSoto/verbaly/develop/assets/logo.png" alt="Verbaly" width="300" />
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
5
|
<p align="center"><em>React bindings for Verbaly — hooks over the reactive core.</em></p>
|
package/dist/index.cjs
CHANGED
|
@@ -1,99 +1,56 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
Trans: () => Trans,
|
|
24
|
-
VerbalyProvider: () => VerbalyProvider,
|
|
25
|
-
useLocale: () => useLocale,
|
|
26
|
-
useT: () => useT,
|
|
27
|
-
useVerbaly: () => useVerbaly
|
|
28
|
-
});
|
|
29
|
-
module.exports = __toCommonJS(index_exports);
|
|
30
|
-
var import_react = require("react");
|
|
31
|
-
var import_verbaly = require("verbaly");
|
|
32
|
-
var VerbalyContext = (0, import_react.createContext)(null);
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let react = require("react");
|
|
3
|
+
let verbaly = require("verbaly");
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
const VerbalyContext = (0, react.createContext)(null);
|
|
33
6
|
function VerbalyProvider(props) {
|
|
34
|
-
|
|
35
|
-
VerbalyContext.Provider,
|
|
36
|
-
{ value: props.instance },
|
|
37
|
-
props.children
|
|
38
|
-
);
|
|
7
|
+
return (0, react.createElement)(VerbalyContext.Provider, { value: props.instance }, props.children);
|
|
39
8
|
}
|
|
40
9
|
function useVerbaly() {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
return instance;
|
|
10
|
+
const instance = (0, react.useContext)(VerbalyContext);
|
|
11
|
+
if (!instance) throw new Error("[verbaly] useVerbaly requires a <VerbalyProvider>");
|
|
12
|
+
return instance;
|
|
46
13
|
}
|
|
47
14
|
function useT() {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
15
|
+
const instance = useVerbaly();
|
|
16
|
+
useVersion(instance);
|
|
17
|
+
return instance.t;
|
|
51
18
|
}
|
|
52
19
|
function useLocale() {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
20
|
+
const instance = useVerbaly();
|
|
21
|
+
useVersion(instance);
|
|
22
|
+
return [instance.locale, instance.setLocale];
|
|
56
23
|
}
|
|
57
24
|
function useVersion(instance) {
|
|
58
|
-
|
|
59
|
-
instance.subscribe,
|
|
60
|
-
() => instance.version,
|
|
61
|
-
() => instance.version
|
|
62
|
-
);
|
|
25
|
+
(0, react.useSyncExternalStore)(instance.subscribe, () => instance.version, () => instance.version);
|
|
63
26
|
}
|
|
64
27
|
function Trans(props) {
|
|
65
|
-
|
|
66
|
-
const text = t(props.id, props.values);
|
|
67
|
-
return (0, import_react.createElement)(
|
|
68
|
-
import_react.Fragment,
|
|
69
|
-
null,
|
|
70
|
-
...toNodes((0, import_verbaly.parseTags)(text), props.components ?? {}, props.links ?? {})
|
|
71
|
-
);
|
|
28
|
+
return (0, react.createElement)(react.Fragment, null, ...toNodes((0, verbaly.parseTags)(useT()(props.id, props.values)), props.components ?? {}, props.links ?? {}));
|
|
72
29
|
}
|
|
73
30
|
function toNodes(nodes, components, links) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
31
|
+
return nodes.map((node, i) => {
|
|
32
|
+
if (typeof node === "string") return node;
|
|
33
|
+
const children = toNodes(node.children, components, links);
|
|
34
|
+
const el = components[node.name];
|
|
35
|
+
if (el) return (0, react.cloneElement)(el, { key: i }, ...children);
|
|
36
|
+
const link = links[node.name];
|
|
37
|
+
if (link !== void 0) {
|
|
38
|
+
const def = typeof link === "string" ? { href: link } : link;
|
|
39
|
+
return (0, react.createElement)("a", {
|
|
40
|
+
key: i,
|
|
41
|
+
href: (0, verbaly.safeHref)(def.href),
|
|
42
|
+
target: def.target,
|
|
43
|
+
rel: def.rel
|
|
44
|
+
}, ...children);
|
|
45
|
+
}
|
|
46
|
+
return (0, react.createElement)(react.Fragment, { key: i }, ...children);
|
|
47
|
+
});
|
|
90
48
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
});
|
|
49
|
+
//#endregion
|
|
50
|
+
exports.Trans = Trans;
|
|
51
|
+
exports.VerbalyProvider = VerbalyProvider;
|
|
52
|
+
exports.useLocale = useLocale;
|
|
53
|
+
exports.useT = useT;
|
|
54
|
+
exports.useVerbaly = useVerbaly;
|
|
55
|
+
|
|
99
56
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n cloneElement,\n createContext,\n createElement,\n Fragment,\n useContext,\n useSyncExternalStore,\n type ReactElement,\n type ReactNode,\n} from 'react';\nimport {\n parseTags,\n safeHref,\n type DictionaryInput,\n type Params,\n type RichLink,\n type TagNode,\n type TFunction,\n type Verbaly,\n} from 'verbaly';\n\nconst VerbalyContext = createContext<Verbaly | null>(null);\n\nexport interface VerbalyProviderProps<D extends DictionaryInput> {\n instance: Verbaly<D>;\n children?: ReactNode;\n}\n\nexport function VerbalyProvider<D extends DictionaryInput>(\n props: VerbalyProviderProps<D>,\n): ReactElement {\n return createElement(\n VerbalyContext.Provider,\n { value: props.instance as unknown as Verbaly },\n props.children,\n );\n}\n\nexport function useVerbaly<D extends DictionaryInput = DictionaryInput>(): Verbaly<D> {\n const instance = useContext(VerbalyContext);\n if (!instance) {\n throw new Error('[verbaly] useVerbaly requires a <VerbalyProvider>');\n }\n return instance as unknown as Verbaly<D>;\n}\n\nexport function useT<D extends DictionaryInput = DictionaryInput>(): TFunction<D> {\n const instance = useVerbaly<D>();\n useVersion(instance);\n return instance.t;\n}\n\nexport function useLocale(): [string, (locale: string) => void] {\n const instance = useVerbaly();\n useVersion(instance);\n return [instance.locale, instance.setLocale];\n}\n\nfunction useVersion<D extends DictionaryInput>(instance: Verbaly<D>): void {\n useSyncExternalStore(\n instance.subscribe,\n () => instance.version,\n () => instance.version,\n );\n}\n\nexport interface TransProps {\n id: string;\n values?: Params;\n components?: Record<string, ReactElement>;\n links?: Record<string, RichLink>;\n}\n\n// translated message + element interpolation\nexport function Trans(props: TransProps): ReactElement {\n const t = useT();\n const text = (t as unknown as (id: string, values?: Params) => string)(props.id, props.values);\n return createElement(\n Fragment,\n null,\n ...toNodes(parseTags(text), props.components ?? {}, props.links ?? {}),\n );\n}\n\nfunction toNodes(\n nodes: TagNode[],\n components: Record<string, ReactElement>,\n links: Record<string, RichLink>,\n): ReactNode[] {\n return nodes.map((node, i) => {\n if (typeof node === 'string') return node;\n const children = toNodes(node.children, components, links);\n const el = components[node.name];\n if (el) return cloneElement(el, { key: i }, ...children);\n const link = links[node.name];\n if (link !== undefined) {\n const def: Exclude<RichLink, string> = typeof link === 'string' ? { href: link } : link;\n return createElement(\n 'a',\n { key: i, href: safeHref(def.href), target: def.target, rel: def.rel },\n ...children,\n );\n }\n return createElement(Fragment, { key: i }, ...children);\n });\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["Fragment"],"sources":["../src/index.ts"],"sourcesContent":["import {\n cloneElement,\n createContext,\n createElement,\n Fragment,\n useContext,\n useSyncExternalStore,\n type ReactElement,\n type ReactNode,\n} from 'react';\nimport {\n parseTags,\n safeHref,\n type DictionaryInput,\n type Params,\n type RichLink,\n type TagNode,\n type TFunction,\n type Verbaly,\n} from 'verbaly';\n\nconst VerbalyContext = createContext<Verbaly | null>(null);\n\nexport interface VerbalyProviderProps<D extends DictionaryInput> {\n instance: Verbaly<D>;\n children?: ReactNode;\n}\n\nexport function VerbalyProvider<D extends DictionaryInput>(\n props: VerbalyProviderProps<D>,\n): ReactElement {\n return createElement(\n VerbalyContext.Provider,\n { value: props.instance as unknown as Verbaly },\n props.children,\n );\n}\n\nexport function useVerbaly<D extends DictionaryInput = DictionaryInput>(): Verbaly<D> {\n const instance = useContext(VerbalyContext);\n if (!instance) {\n throw new Error('[verbaly] useVerbaly requires a <VerbalyProvider>');\n }\n return instance as unknown as Verbaly<D>;\n}\n\nexport function useT<D extends DictionaryInput = DictionaryInput>(): TFunction<D> {\n const instance = useVerbaly<D>();\n useVersion(instance);\n return instance.t;\n}\n\nexport function useLocale(): [string, (locale: string) => void] {\n const instance = useVerbaly();\n useVersion(instance);\n return [instance.locale, instance.setLocale];\n}\n\nfunction useVersion<D extends DictionaryInput>(instance: Verbaly<D>): void {\n useSyncExternalStore(\n instance.subscribe,\n () => instance.version,\n () => instance.version,\n );\n}\n\nexport interface TransProps {\n id: string;\n values?: Params;\n components?: Record<string, ReactElement>;\n links?: Record<string, RichLink>;\n}\n\n// translated message + element interpolation\nexport function Trans(props: TransProps): ReactElement {\n const t = useT();\n const text = (t as unknown as (id: string, values?: Params) => string)(props.id, props.values);\n return createElement(\n Fragment,\n null,\n ...toNodes(parseTags(text), props.components ?? {}, props.links ?? {}),\n );\n}\n\nfunction toNodes(\n nodes: TagNode[],\n components: Record<string, ReactElement>,\n links: Record<string, RichLink>,\n): ReactNode[] {\n return nodes.map((node, i) => {\n if (typeof node === 'string') return node;\n const children = toNodes(node.children, components, links);\n const el = components[node.name];\n if (el) return cloneElement(el, { key: i }, ...children);\n const link = links[node.name];\n if (link !== undefined) {\n const def: Exclude<RichLink, string> = typeof link === 'string' ? { href: link } : link;\n return createElement(\n 'a',\n { key: i, href: safeHref(def.href), target: def.target, rel: def.rel },\n ...children,\n );\n }\n return createElement(Fragment, { key: i }, ...children);\n });\n}\n"],"mappings":";;;;AAqBA,MAAM,kBAAA,GAAA,MAAA,cAAA,CAA+C,IAAI;AAOzD,SAAgB,gBACd,OACc;CACd,QAAA,GAAA,MAAA,cAAA,CACE,eAAe,UACf,EAAE,OAAO,MAAM,SAA+B,GAC9C,MAAM,QACR;AACF;AAEA,SAAgB,aAAsE;CACpF,MAAM,YAAA,GAAA,MAAA,WAAA,CAAsB,cAAc;CAC1C,IAAI,CAAC,UACH,MAAM,IAAI,MAAM,mDAAmD;CAErE,OAAO;AACT;AAEA,SAAgB,OAAkE;CAChF,MAAM,WAAW,WAAc;CAC/B,WAAW,QAAQ;CACnB,OAAO,SAAS;AAClB;AAEA,SAAgB,YAAgD;CAC9D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ReactNode } from "react";
|
|
2
|
+
import { DictionaryInput, Params, RichLink, TFunction, Verbaly } from "verbaly";
|
|
3
|
+
//#region src/index.d.ts
|
|
4
4
|
interface VerbalyProviderProps<D extends DictionaryInput> {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
instance: Verbaly<D>;
|
|
6
|
+
children?: ReactNode;
|
|
7
7
|
}
|
|
8
8
|
declare function VerbalyProvider<D extends DictionaryInput>(props: VerbalyProviderProps<D>): ReactElement;
|
|
9
9
|
declare function useVerbaly<D extends DictionaryInput = DictionaryInput>(): Verbaly<D>;
|
|
10
10
|
declare function useT<D extends DictionaryInput = DictionaryInput>(): TFunction<D>;
|
|
11
11
|
declare function useLocale(): [string, (locale: string) => void];
|
|
12
12
|
interface TransProps {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
id: string;
|
|
14
|
+
values?: Params;
|
|
15
|
+
components?: Record<string, ReactElement>;
|
|
16
|
+
links?: Record<string, RichLink>;
|
|
17
17
|
}
|
|
18
18
|
declare function Trans(props: TransProps): ReactElement;
|
|
19
|
-
|
|
20
|
-
export { Trans,
|
|
19
|
+
//#endregion
|
|
20
|
+
export { Trans, TransProps, VerbalyProvider, VerbalyProviderProps, useLocale, useT, useVerbaly };
|
|
21
|
+
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
import { ReactElement, ReactNode } from
|
|
2
|
-
import { Params, RichLink,
|
|
3
|
-
|
|
1
|
+
import { ReactElement, ReactNode } from "react";
|
|
2
|
+
import { DictionaryInput, Params, RichLink, TFunction, Verbaly } from "verbaly";
|
|
3
|
+
//#region src/index.d.ts
|
|
4
4
|
interface VerbalyProviderProps<D extends DictionaryInput> {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
instance: Verbaly<D>;
|
|
6
|
+
children?: ReactNode;
|
|
7
7
|
}
|
|
8
8
|
declare function VerbalyProvider<D extends DictionaryInput>(props: VerbalyProviderProps<D>): ReactElement;
|
|
9
9
|
declare function useVerbaly<D extends DictionaryInput = DictionaryInput>(): Verbaly<D>;
|
|
10
10
|
declare function useT<D extends DictionaryInput = DictionaryInput>(): TFunction<D>;
|
|
11
11
|
declare function useLocale(): [string, (locale: string) => void];
|
|
12
12
|
interface TransProps {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
id: string;
|
|
14
|
+
values?: Params;
|
|
15
|
+
components?: Record<string, ReactElement>;
|
|
16
|
+
links?: Record<string, RichLink>;
|
|
17
17
|
}
|
|
18
18
|
declare function Trans(props: TransProps): ReactElement;
|
|
19
|
-
|
|
20
|
-
export { Trans,
|
|
19
|
+
//#endregion
|
|
20
|
+
export { Trans, TransProps, VerbalyProvider, VerbalyProviderProps, useLocale, useT, useVerbaly };
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,80 +1,51 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
createElement,
|
|
6
|
-
Fragment,
|
|
7
|
-
useContext,
|
|
8
|
-
useSyncExternalStore
|
|
9
|
-
} from "react";
|
|
10
|
-
import {
|
|
11
|
-
parseTags,
|
|
12
|
-
safeHref
|
|
13
|
-
} from "verbaly";
|
|
14
|
-
var VerbalyContext = createContext(null);
|
|
1
|
+
import { Fragment, cloneElement, createContext, createElement, useContext, useSyncExternalStore } from "react";
|
|
2
|
+
import { parseTags, safeHref } from "verbaly";
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
const VerbalyContext = createContext(null);
|
|
15
5
|
function VerbalyProvider(props) {
|
|
16
|
-
|
|
17
|
-
VerbalyContext.Provider,
|
|
18
|
-
{ value: props.instance },
|
|
19
|
-
props.children
|
|
20
|
-
);
|
|
6
|
+
return createElement(VerbalyContext.Provider, { value: props.instance }, props.children);
|
|
21
7
|
}
|
|
22
8
|
function useVerbaly() {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
return instance;
|
|
9
|
+
const instance = useContext(VerbalyContext);
|
|
10
|
+
if (!instance) throw new Error("[verbaly] useVerbaly requires a <VerbalyProvider>");
|
|
11
|
+
return instance;
|
|
28
12
|
}
|
|
29
13
|
function useT() {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
14
|
+
const instance = useVerbaly();
|
|
15
|
+
useVersion(instance);
|
|
16
|
+
return instance.t;
|
|
33
17
|
}
|
|
34
18
|
function useLocale() {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
19
|
+
const instance = useVerbaly();
|
|
20
|
+
useVersion(instance);
|
|
21
|
+
return [instance.locale, instance.setLocale];
|
|
38
22
|
}
|
|
39
23
|
function useVersion(instance) {
|
|
40
|
-
|
|
41
|
-
instance.subscribe,
|
|
42
|
-
() => instance.version,
|
|
43
|
-
() => instance.version
|
|
44
|
-
);
|
|
24
|
+
useSyncExternalStore(instance.subscribe, () => instance.version, () => instance.version);
|
|
45
25
|
}
|
|
46
26
|
function Trans(props) {
|
|
47
|
-
|
|
48
|
-
const text = t(props.id, props.values);
|
|
49
|
-
return createElement(
|
|
50
|
-
Fragment,
|
|
51
|
-
null,
|
|
52
|
-
...toNodes(parseTags(text), props.components ?? {}, props.links ?? {})
|
|
53
|
-
);
|
|
27
|
+
return createElement(Fragment, null, ...toNodes(parseTags(useT()(props.id, props.values)), props.components ?? {}, props.links ?? {}));
|
|
54
28
|
}
|
|
55
29
|
function toNodes(nodes, components, links) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
30
|
+
return nodes.map((node, i) => {
|
|
31
|
+
if (typeof node === "string") return node;
|
|
32
|
+
const children = toNodes(node.children, components, links);
|
|
33
|
+
const el = components[node.name];
|
|
34
|
+
if (el) return cloneElement(el, { key: i }, ...children);
|
|
35
|
+
const link = links[node.name];
|
|
36
|
+
if (link !== void 0) {
|
|
37
|
+
const def = typeof link === "string" ? { href: link } : link;
|
|
38
|
+
return createElement("a", {
|
|
39
|
+
key: i,
|
|
40
|
+
href: safeHref(def.href),
|
|
41
|
+
target: def.target,
|
|
42
|
+
rel: def.rel
|
|
43
|
+
}, ...children);
|
|
44
|
+
}
|
|
45
|
+
return createElement(Fragment, { key: i }, ...children);
|
|
46
|
+
});
|
|
72
47
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
useLocale,
|
|
77
|
-
useT,
|
|
78
|
-
useVerbaly
|
|
79
|
-
};
|
|
48
|
+
//#endregion
|
|
49
|
+
export { Trans, VerbalyProvider, useLocale, useT, useVerbaly };
|
|
50
|
+
|
|
80
51
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n cloneElement,\n createContext,\n createElement,\n Fragment,\n useContext,\n useSyncExternalStore,\n type ReactElement,\n type ReactNode,\n} from 'react';\nimport {\n parseTags,\n safeHref,\n type DictionaryInput,\n type Params,\n type RichLink,\n type TagNode,\n type TFunction,\n type Verbaly,\n} from 'verbaly';\n\nconst VerbalyContext = createContext<Verbaly | null>(null);\n\nexport interface VerbalyProviderProps<D extends DictionaryInput> {\n instance: Verbaly<D>;\n children?: ReactNode;\n}\n\nexport function VerbalyProvider<D extends DictionaryInput>(\n props: VerbalyProviderProps<D>,\n): ReactElement {\n return createElement(\n VerbalyContext.Provider,\n { value: props.instance as unknown as Verbaly },\n props.children,\n );\n}\n\nexport function useVerbaly<D extends DictionaryInput = DictionaryInput>(): Verbaly<D> {\n const instance = useContext(VerbalyContext);\n if (!instance) {\n throw new Error('[verbaly] useVerbaly requires a <VerbalyProvider>');\n }\n return instance as unknown as Verbaly<D>;\n}\n\nexport function useT<D extends DictionaryInput = DictionaryInput>(): TFunction<D> {\n const instance = useVerbaly<D>();\n useVersion(instance);\n return instance.t;\n}\n\nexport function useLocale(): [string, (locale: string) => void] {\n const instance = useVerbaly();\n useVersion(instance);\n return [instance.locale, instance.setLocale];\n}\n\nfunction useVersion<D extends DictionaryInput>(instance: Verbaly<D>): void {\n useSyncExternalStore(\n instance.subscribe,\n () => instance.version,\n () => instance.version,\n );\n}\n\nexport interface TransProps {\n id: string;\n values?: Params;\n components?: Record<string, ReactElement>;\n links?: Record<string, RichLink>;\n}\n\n// translated message + element interpolation\nexport function Trans(props: TransProps): ReactElement {\n const t = useT();\n const text = (t as unknown as (id: string, values?: Params) => string)(props.id, props.values);\n return createElement(\n Fragment,\n null,\n ...toNodes(parseTags(text), props.components ?? {}, props.links ?? {}),\n );\n}\n\nfunction toNodes(\n nodes: TagNode[],\n components: Record<string, ReactElement>,\n links: Record<string, RichLink>,\n): ReactNode[] {\n return nodes.map((node, i) => {\n if (typeof node === 'string') return node;\n const children = toNodes(node.children, components, links);\n const el = components[node.name];\n if (el) return cloneElement(el, { key: i }, ...children);\n const link = links[node.name];\n if (link !== undefined) {\n const def: Exclude<RichLink, string> = typeof link === 'string' ? { href: link } : link;\n return createElement(\n 'a',\n { key: i, href: safeHref(def.href), target: def.target, rel: def.rel },\n ...children,\n );\n }\n return createElement(Fragment, { key: i }, ...children);\n });\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import {\n cloneElement,\n createContext,\n createElement,\n Fragment,\n useContext,\n useSyncExternalStore,\n type ReactElement,\n type ReactNode,\n} from 'react';\nimport {\n parseTags,\n safeHref,\n type DictionaryInput,\n type Params,\n type RichLink,\n type TagNode,\n type TFunction,\n type Verbaly,\n} from 'verbaly';\n\nconst VerbalyContext = createContext<Verbaly | null>(null);\n\nexport interface VerbalyProviderProps<D extends DictionaryInput> {\n instance: Verbaly<D>;\n children?: ReactNode;\n}\n\nexport function VerbalyProvider<D extends DictionaryInput>(\n props: VerbalyProviderProps<D>,\n): ReactElement {\n return createElement(\n VerbalyContext.Provider,\n { value: props.instance as unknown as Verbaly },\n props.children,\n );\n}\n\nexport function useVerbaly<D extends DictionaryInput = DictionaryInput>(): Verbaly<D> {\n const instance = useContext(VerbalyContext);\n if (!instance) {\n throw new Error('[verbaly] useVerbaly requires a <VerbalyProvider>');\n }\n return instance as unknown as Verbaly<D>;\n}\n\nexport function useT<D extends DictionaryInput = DictionaryInput>(): TFunction<D> {\n const instance = useVerbaly<D>();\n useVersion(instance);\n return instance.t;\n}\n\nexport function useLocale(): [string, (locale: string) => void] {\n const instance = useVerbaly();\n useVersion(instance);\n return [instance.locale, instance.setLocale];\n}\n\nfunction useVersion<D extends DictionaryInput>(instance: Verbaly<D>): void {\n useSyncExternalStore(\n instance.subscribe,\n () => instance.version,\n () => instance.version,\n );\n}\n\nexport interface TransProps {\n id: string;\n values?: Params;\n components?: Record<string, ReactElement>;\n links?: Record<string, RichLink>;\n}\n\n// translated message + element interpolation\nexport function Trans(props: TransProps): ReactElement {\n const t = useT();\n const text = (t as unknown as (id: string, values?: Params) => string)(props.id, props.values);\n return createElement(\n Fragment,\n null,\n ...toNodes(parseTags(text), props.components ?? {}, props.links ?? {}),\n );\n}\n\nfunction toNodes(\n nodes: TagNode[],\n components: Record<string, ReactElement>,\n links: Record<string, RichLink>,\n): ReactNode[] {\n return nodes.map((node, i) => {\n if (typeof node === 'string') return node;\n const children = toNodes(node.children, components, links);\n const el = components[node.name];\n if (el) return cloneElement(el, { key: i }, ...children);\n const link = links[node.name];\n if (link !== undefined) {\n const def: Exclude<RichLink, string> = typeof link === 'string' ? { href: link } : link;\n return createElement(\n 'a',\n { key: i, href: safeHref(def.href), target: def.target, rel: def.rel },\n ...children,\n );\n }\n return createElement(Fragment, { key: i }, ...children);\n });\n}\n"],"mappings":";;;AAqBA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AAO,cAAc,UAAU,EAAE,KAAK,EAAE,GAAG,GAAG,QAAQ;CACxD,CAAC;AACH"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@verbaly/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "React bindings for Verbaly — hooks over the reactive core.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"react": "^18.0.0 || ^19.0.0",
|
|
51
|
-
"verbaly": "^0.
|
|
51
|
+
"verbaly": "^0.12.0"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@types/react": "^19.2.17",
|
|
@@ -56,11 +56,11 @@
|
|
|
56
56
|
"happy-dom": "^20.10.6",
|
|
57
57
|
"react": "^19.2.7",
|
|
58
58
|
"react-dom": "^19.2.7",
|
|
59
|
-
"verbaly": "0.
|
|
59
|
+
"verbaly": "0.12.0"
|
|
60
60
|
},
|
|
61
61
|
"scripts": {
|
|
62
|
-
"build": "
|
|
63
|
-
"dev": "
|
|
62
|
+
"build": "tsdown",
|
|
63
|
+
"dev": "tsdown --watch",
|
|
64
64
|
"test": "vitest run --passWithNoTests",
|
|
65
65
|
"typecheck": "tsc --noEmit"
|
|
66
66
|
}
|