@saykit/react 0.0.0-beta-20260906064714 → 0.0.0-beta-20260912200031

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 CHANGED
@@ -12,30 +12,66 @@ A `<Say>` component for rendering translated content in server and client compon
12
12
  pnpm add @saykit/react saykit
13
13
  ```
14
14
 
15
- You will also need a SayKit build-tool plugin and a `saykit.config.ts`.
15
+ You will also need a SayKit build-tool plugin ([`unplugin-saykit`](https://github.com/k0d13/saykit/tree/main/packages/plugin-unplugin) or [`babel-plugin-saykit`](https://github.com/k0d13/saykit/tree/main/packages/plugin-babel)) and a `saykit.config.ts` with `@saykit/transform-jsx` in the bucket. The lazy `() => import(...)` catalogues below need `unplugin-saykit` or `babel-plugin-saykit` with `catalogues: 'module'`; Babel's default inline mode only handles static imports.
16
16
 
17
17
  ## Usage
18
18
 
19
- ```tsx
20
- import { Say } from '@saykit/react';
21
- import { SayProvider } from '@saykit/react/client';
19
+ ```ts title="src/i18n.ts"
22
20
  import { createCatalogue, createStore } from 'saykit';
23
21
 
24
- const en = { greeting: 'Hello, {name}!' };
25
- const fr = { greeting: 'Bonjour, {name} !' };
22
+ export const catalogue = createCatalogue({
23
+ en: () => import('./locales/en.po'),
24
+ fr: () => import('./locales/fr.po'),
25
+ });
26
+
27
+ const initial = catalogue.match(navigator.languages);
28
+ await catalogue.load(initial);
29
+
30
+ export const store = createStore(catalogue, initial);
31
+ export type Locale = (typeof catalogue.locales)[number];
32
+ ```
33
+
34
+ ```tsx title="src/app.tsx"
35
+ import { Say } from '@saykit/react';
36
+ import { SayProvider, useSay } from '@saykit/react/client';
37
+ import { type Locale, store } from './i18n.js';
38
+
39
+ function Cart({ name, items }: { name: string; items: string[] }) {
40
+ return (
41
+ <p>
42
+ <Say>Hello, {name}!</Say>{' '}
43
+ <Say.Plural
44
+ _={items.length}
45
+ _0="Your cart is empty."
46
+ one="You have 1 item."
47
+ other={<>You have {items.length} items.</>}
48
+ />
49
+ </p>
50
+ );
51
+ }
26
52
 
27
- const store = createStore(createCatalogue({ en, fr }), 'fr');
53
+ function LocalePicker() {
54
+ const say = useSay();
55
+ return (
56
+ <select value={say.locale} onChange={(event) => store.set(event.target.value as Locale)}>
57
+ <option value="en">English</option>
58
+ <option value="fr">Français</option>
59
+ </select>
60
+ );
61
+ }
28
62
 
29
- function App() {
63
+ export function App() {
30
64
  return (
31
65
  <SayProvider store={store}>
32
- <Say>Hello, {name}!</Say>
33
- <Say.Plural _={count} one={<>{count} item</>} other={<>{count} items</>} />
66
+ <LocalePicker />
67
+ <Cart name="Ada" items={[]} />
34
68
  </SayProvider>
35
69
  );
36
70
  }
37
71
  ```
38
72
 
73
+ Elements inside a message survive translation as numbered tags: `<Say>Read the <a href="/docs">docs</a></Say>` extracts as `Read the <0>docs</0>`.
74
+
39
75
  ## Documentation
40
76
 
41
77
  [React integration guide](https://saykit.js.org/integrations/react) at [saykit.js.org](https://saykit.js.org).
package/dist/index.d.mts CHANGED
@@ -22,10 +22,11 @@ type PropsWithJSXSafeKeys<T> = { [K in keyof T as K extends number | `${number}$
22
22
  * @returns The translation node for the descriptor
23
23
  * @remark This is a macro and must be used with the relevant saykit plugin
24
24
  */
25
- declare function Say(props: PropsWithSayChildren<Disallow<{
25
+ declare function Say(props: PropsWithSayChildren<{
26
+ id?: string;
26
27
  context?: string;
27
28
  whitespace?: boolean;
28
- }, 'id'>>): ReactElement;
29
+ }>): ReactElement;
29
30
  declare namespace Say {
30
31
  /**
31
32
  * Define a pluralised message.
package/dist/index.mjs CHANGED
@@ -56,14 +56,17 @@ function resolveValuePropKeys(props) {
56
56
  //#endregion
57
57
  //#region src/runtime/index.ts
58
58
  function Say(props) {
59
- if (!("id" in props)) throw new Error("'Say' is a macro and must be used with the relevant saykit plugin", { cause: /* @__PURE__ */ new Error("The 'id' property is required for a descriptor") });
59
+ if (!("id" in props) && !("message" in props)) throw new Error("'Say' is a macro and must be used with the relevant saykit plugin", { cause: /* @__PURE__ */ new Error("The 'id' property is required for a descriptor") });
60
60
  const say = GET_SAY();
61
- const { id, whitespace, ...rest } = props;
61
+ const { id, message, whitespace, ...rest } = props;
62
62
  const values = resolveValuePropKeys(rest);
63
63
  return createElement(Renderer, {
64
- html: say.call({
64
+ html: say.call(message === void 0 ? {
65
65
  ...rest,
66
66
  id
67
+ } : {
68
+ ...rest,
69
+ message
67
70
  }),
68
71
  whitespace,
69
72
  components(tag) {
@@ -56,14 +56,17 @@ function resolveValuePropKeys(props) {
56
56
  //#endregion
57
57
  //#region src/runtime/index.ts
58
58
  function Say(props) {
59
- if (!("id" in props)) throw new Error("'Say' is a macro and must be used with the relevant saykit plugin", { cause: /* @__PURE__ */ new Error("The 'id' property is required for a descriptor") });
59
+ if (!("id" in props) && !("message" in props)) throw new Error("'Say' is a macro and must be used with the relevant saykit plugin", { cause: /* @__PURE__ */ new Error("The 'id' property is required for a descriptor") });
60
60
  const say = GET_SAY();
61
- const { id, whitespace, ...rest } = props;
61
+ const { id, message, whitespace, ...rest } = props;
62
62
  const values = resolveValuePropKeys(rest);
63
63
  return createElement(Renderer, {
64
- html: say.call({
64
+ html: say.call(message === void 0 ? {
65
65
  ...rest,
66
66
  id
67
+ } : {
68
+ ...rest,
69
+ message
67
70
  }),
68
71
  whitespace,
69
72
  components(tag) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saykit/react",
3
- "version": "0.0.0-beta-20260906064714",
3
+ "version": "0.0.0-beta-20260912200031",
4
4
  "description": "React integration for saykit, i18n hooks and components",
5
5
  "keywords": [
6
6
  "i18n",
@@ -53,11 +53,11 @@
53
53
  "jsdom": "^29.1.1",
54
54
  "react": "^19.2.8",
55
55
  "react-dom": "^19.2.8",
56
- "saykit": "^0.0.0-beta-20260906064714"
56
+ "saykit": "^0.0.0-beta-20260912200031"
57
57
  },
58
58
  "peerDependencies": {
59
59
  "react": "*",
60
- "saykit": "0.0.0-beta-20260906064714"
60
+ "saykit": "0.0.0-beta-20260912200031"
61
61
  },
62
62
  "scripts": {
63
63
  "check": "tsc --noEmit",