lexisync-react 0.1.1 → 0.1.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.
- package/dist/index.cjs +15 -12
- package/dist/index.d.cts +7 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +15 -12
- package/package.json +1 -1
- package/src/provider.test.tsx +14 -1
- package/src/provider.tsx +25 -13
package/dist/index.cjs
CHANGED
|
@@ -94,8 +94,9 @@ var LexiSyncContext = (0, import_react.createContext)(null);
|
|
|
94
94
|
function toError(e) {
|
|
95
95
|
return e instanceof Error ? e : new Error(String(e));
|
|
96
96
|
}
|
|
97
|
+
var STUB_I18N = null;
|
|
97
98
|
function LexiSyncProvider(props) {
|
|
98
|
-
const { children, i18n: externalI18n, ...rawConfig } = props;
|
|
99
|
+
const { children, i18n: externalI18n, suspense = false, ...rawConfig } = props;
|
|
99
100
|
const config = (0, import_react.useMemo)(
|
|
100
101
|
() => resolveConfig(rawConfig),
|
|
101
102
|
// eslint-disable-next-line react-hooks/exhaustive-deps -- stable by field
|
|
@@ -157,26 +158,28 @@ function LexiSyncProvider(props) {
|
|
|
157
158
|
const t = (0, import_react.useCallback)(
|
|
158
159
|
(key, options) => {
|
|
159
160
|
void tick;
|
|
160
|
-
return instance ? instance.t(key, options) : key;
|
|
161
|
+
return instance ? String(instance.t(key, options)) : key;
|
|
161
162
|
},
|
|
162
163
|
[instance, tick]
|
|
163
164
|
);
|
|
164
|
-
const value = (0, import_react.useMemo)(
|
|
165
|
-
|
|
166
|
-
return null;
|
|
167
|
-
}
|
|
168
|
-
return {
|
|
165
|
+
const value = (0, import_react.useMemo)(
|
|
166
|
+
() => ({
|
|
169
167
|
config,
|
|
170
168
|
status,
|
|
171
169
|
error,
|
|
172
|
-
i18n: instance ??
|
|
170
|
+
i18n: instance ?? STUB_I18N,
|
|
173
171
|
t,
|
|
174
172
|
changeLanguage,
|
|
175
173
|
reload
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
174
|
+
}),
|
|
175
|
+
[instance, status, error, config, t, changeLanguage, reload]
|
|
176
|
+
);
|
|
177
|
+
if (suspense && !instance && status !== "error") {
|
|
178
|
+
return (0, import_react.createElement)(
|
|
179
|
+
"div",
|
|
180
|
+
{ "data-lexisync-status": status, role: "status" },
|
|
181
|
+
"Loading translations\u2026"
|
|
182
|
+
);
|
|
180
183
|
}
|
|
181
184
|
return (0, import_react.createElement)(LexiSyncContext.Provider, { value }, children);
|
|
182
185
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -44,9 +44,15 @@ type LexiSyncProviderProps = LexiSyncConfig & {
|
|
|
44
44
|
children: ReactNode;
|
|
45
45
|
/** Optional pre-built i18n instance (tests / advanced). */
|
|
46
46
|
i18n?: i18n;
|
|
47
|
+
/**
|
|
48
|
+
* When true (default), children render immediately while CDN/bootstrap loads.
|
|
49
|
+
* `t()` returns the key until ready — pair with local fallbacks for zero flash.
|
|
50
|
+
* Set false only if you want a hard loading gate.
|
|
51
|
+
*/
|
|
52
|
+
suspense?: boolean;
|
|
47
53
|
};
|
|
48
54
|
declare function LexiSyncProvider(props: LexiSyncProviderProps): react.DetailedReactHTMLElement<{
|
|
49
|
-
'data-lexisync-status':
|
|
55
|
+
'data-lexisync-status': "idle" | "loading" | "ready";
|
|
50
56
|
role: "status";
|
|
51
57
|
}, HTMLElement> | react.FunctionComponentElement<react.ProviderProps<LexiSyncContextValue | null>>;
|
|
52
58
|
declare function useLexiSync(): LexiSyncContextValue;
|
package/dist/index.d.ts
CHANGED
|
@@ -44,9 +44,15 @@ type LexiSyncProviderProps = LexiSyncConfig & {
|
|
|
44
44
|
children: ReactNode;
|
|
45
45
|
/** Optional pre-built i18n instance (tests / advanced). */
|
|
46
46
|
i18n?: i18n;
|
|
47
|
+
/**
|
|
48
|
+
* When true (default), children render immediately while CDN/bootstrap loads.
|
|
49
|
+
* `t()` returns the key until ready — pair with local fallbacks for zero flash.
|
|
50
|
+
* Set false only if you want a hard loading gate.
|
|
51
|
+
*/
|
|
52
|
+
suspense?: boolean;
|
|
47
53
|
};
|
|
48
54
|
declare function LexiSyncProvider(props: LexiSyncProviderProps): react.DetailedReactHTMLElement<{
|
|
49
|
-
'data-lexisync-status':
|
|
55
|
+
'data-lexisync-status': "idle" | "loading" | "ready";
|
|
50
56
|
role: "status";
|
|
51
57
|
}, HTMLElement> | react.FunctionComponentElement<react.ProviderProps<LexiSyncContextValue | null>>;
|
|
52
58
|
declare function useLexiSync(): LexiSyncContextValue;
|
package/dist/index.js
CHANGED
|
@@ -61,8 +61,9 @@ var LexiSyncContext = createContext(null);
|
|
|
61
61
|
function toError(e) {
|
|
62
62
|
return e instanceof Error ? e : new Error(String(e));
|
|
63
63
|
}
|
|
64
|
+
var STUB_I18N = null;
|
|
64
65
|
function LexiSyncProvider(props) {
|
|
65
|
-
const { children, i18n: externalI18n, ...rawConfig } = props;
|
|
66
|
+
const { children, i18n: externalI18n, suspense = false, ...rawConfig } = props;
|
|
66
67
|
const config = useMemo(
|
|
67
68
|
() => resolveConfig(rawConfig),
|
|
68
69
|
// eslint-disable-next-line react-hooks/exhaustive-deps -- stable by field
|
|
@@ -124,26 +125,28 @@ function LexiSyncProvider(props) {
|
|
|
124
125
|
const t = useCallback(
|
|
125
126
|
(key, options) => {
|
|
126
127
|
void tick;
|
|
127
|
-
return instance ? instance.t(key, options) : key;
|
|
128
|
+
return instance ? String(instance.t(key, options)) : key;
|
|
128
129
|
},
|
|
129
130
|
[instance, tick]
|
|
130
131
|
);
|
|
131
|
-
const value = useMemo(
|
|
132
|
-
|
|
133
|
-
return null;
|
|
134
|
-
}
|
|
135
|
-
return {
|
|
132
|
+
const value = useMemo(
|
|
133
|
+
() => ({
|
|
136
134
|
config,
|
|
137
135
|
status,
|
|
138
136
|
error,
|
|
139
|
-
i18n: instance ??
|
|
137
|
+
i18n: instance ?? STUB_I18N,
|
|
140
138
|
t,
|
|
141
139
|
changeLanguage,
|
|
142
140
|
reload
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
141
|
+
}),
|
|
142
|
+
[instance, status, error, config, t, changeLanguage, reload]
|
|
143
|
+
);
|
|
144
|
+
if (suspense && !instance && status !== "error") {
|
|
145
|
+
return createElement(
|
|
146
|
+
"div",
|
|
147
|
+
{ "data-lexisync-status": status, role: "status" },
|
|
148
|
+
"Loading translations\u2026"
|
|
149
|
+
);
|
|
147
150
|
}
|
|
148
151
|
return createElement(LexiSyncContext.Provider, { value }, children);
|
|
149
152
|
}
|
package/package.json
CHANGED
package/src/provider.test.tsx
CHANGED
|
@@ -69,13 +69,26 @@ describe('LexiSyncProvider', () => {
|
|
|
69
69
|
expect(() => render(createElement(Probe))).toThrow(/useLexiSync must be used within/);
|
|
70
70
|
});
|
|
71
71
|
|
|
72
|
-
it('
|
|
72
|
+
it('renders children immediately while loading, then ready', async () => {
|
|
73
73
|
render(createElement(LexiSyncProvider, { projectId: 'p1', version: 'latest' }, createElement(Probe)));
|
|
74
|
+
// No blank "Loading translations…" gate — shell is visible with status=loading
|
|
75
|
+
expect(screen.getByTestId('status').textContent).toBe('loading');
|
|
74
76
|
await waitFor(() => expect(screen.getByTestId('status').textContent).toBe('ready'));
|
|
75
77
|
expect(screen.getByTestId('hello').textContent).toBe('Hello');
|
|
76
78
|
expect(screen.getByTestId('version').textContent).toBe('latest');
|
|
77
79
|
});
|
|
78
80
|
|
|
81
|
+
it('suspense=true keeps legacy loading gate', () => {
|
|
82
|
+
render(
|
|
83
|
+
createElement(
|
|
84
|
+
LexiSyncProvider,
|
|
85
|
+
{ projectId: 'p1', suspense: true },
|
|
86
|
+
createElement(Probe)
|
|
87
|
+
)
|
|
88
|
+
);
|
|
89
|
+
expect(screen.getByRole('status').textContent).toMatch(/Loading translations/i);
|
|
90
|
+
});
|
|
91
|
+
|
|
79
92
|
it('useT returns translations', async () => {
|
|
80
93
|
render(createElement(LexiSyncProvider, { projectId: 'p1' }, createElement(TOnly)));
|
|
81
94
|
await waitFor(() => expect(screen.getByTestId('t-only').textContent).toBe('World'));
|
package/src/provider.tsx
CHANGED
|
@@ -18,14 +18,23 @@ export type LexiSyncProviderProps = LexiSyncConfig & {
|
|
|
18
18
|
children: ReactNode;
|
|
19
19
|
/** Optional pre-built i18n instance (tests / advanced). */
|
|
20
20
|
i18n?: I18nInstance;
|
|
21
|
+
/**
|
|
22
|
+
* When true (default), children render immediately while CDN/bootstrap loads.
|
|
23
|
+
* `t()` returns the key until ready — pair with local fallbacks for zero flash.
|
|
24
|
+
* Set false only if you want a hard loading gate.
|
|
25
|
+
*/
|
|
26
|
+
suspense?: boolean;
|
|
21
27
|
};
|
|
22
28
|
|
|
23
29
|
function toError(e: unknown): Error {
|
|
24
30
|
return e instanceof Error ? e : new Error(String(e));
|
|
25
31
|
}
|
|
26
32
|
|
|
33
|
+
/** Minimal stand-in so consumers never crash before i18n boots. */
|
|
34
|
+
const STUB_I18N = null as unknown as I18nInstance;
|
|
35
|
+
|
|
27
36
|
export function LexiSyncProvider(props: LexiSyncProviderProps) {
|
|
28
|
-
const { children, i18n: externalI18n, ...rawConfig } = props;
|
|
37
|
+
const { children, i18n: externalI18n, suspense = false, ...rawConfig } = props;
|
|
29
38
|
const config = useMemo(
|
|
30
39
|
() => resolveConfig(rawConfig),
|
|
31
40
|
// eslint-disable-next-line react-hooks/exhaustive-deps -- stable by field
|
|
@@ -93,28 +102,31 @@ export function LexiSyncProvider(props: LexiSyncProviderProps) {
|
|
|
93
102
|
const t = useCallback(
|
|
94
103
|
(key: string, options?: Record<string, unknown>) => {
|
|
95
104
|
void tick;
|
|
96
|
-
return instance ? instance.t(key, options) : key;
|
|
105
|
+
return instance ? String(instance.t(key, options)) : key;
|
|
97
106
|
},
|
|
98
107
|
[instance, tick]
|
|
99
108
|
);
|
|
100
109
|
|
|
101
|
-
const value = useMemo<LexiSyncContextValue
|
|
102
|
-
|
|
103
|
-
return null;
|
|
104
|
-
}
|
|
105
|
-
return {
|
|
110
|
+
const value = useMemo<LexiSyncContextValue>(
|
|
111
|
+
() => ({
|
|
106
112
|
config,
|
|
107
113
|
status,
|
|
108
114
|
error,
|
|
109
|
-
i18n:
|
|
115
|
+
i18n: instance ?? STUB_I18N,
|
|
110
116
|
t,
|
|
111
117
|
changeLanguage,
|
|
112
118
|
reload,
|
|
113
|
-
}
|
|
114
|
-
|
|
119
|
+
}),
|
|
120
|
+
[instance, status, error, config, t, changeLanguage, reload]
|
|
121
|
+
);
|
|
115
122
|
|
|
116
|
-
|
|
117
|
-
|
|
123
|
+
// Optional hard gate (legacy). Default: always paint children.
|
|
124
|
+
if (suspense && !instance && status !== 'error') {
|
|
125
|
+
return createElement(
|
|
126
|
+
'div',
|
|
127
|
+
{ 'data-lexisync-status': status, role: 'status' },
|
|
128
|
+
'Loading translations…'
|
|
129
|
+
);
|
|
118
130
|
}
|
|
119
131
|
|
|
120
132
|
return createElement(LexiSyncContext.Provider, { value }, children);
|
|
@@ -133,4 +145,4 @@ export function useT() {
|
|
|
133
145
|
return useLexiSync().t;
|
|
134
146
|
}
|
|
135
147
|
|
|
136
|
-
export { LexiSyncContext };
|
|
148
|
+
export { LexiSyncContext };
|