@rimori/react-client 0.4.6-next.1 → 0.4.6-next.3
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/hooks/I18nHooks.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect, useState } from 'react';
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
2
2
|
import { useRimori } from '../providers/PluginProvider';
|
|
3
3
|
/**
|
|
4
4
|
* Custom useTranslation hook that provides a translation function and indicates readiness
|
|
@@ -7,19 +7,22 @@ import { useRimori } from '../providers/PluginProvider';
|
|
|
7
7
|
export function useTranslation() {
|
|
8
8
|
const { plugin } = useRimori();
|
|
9
9
|
const [translatorInstance, setTranslatorInstance] = useState(null);
|
|
10
|
+
const [updateCount, setUpdateCount] = useState(0);
|
|
10
11
|
useEffect(() => {
|
|
11
|
-
void plugin.getTranslator().then(
|
|
12
|
+
void plugin.getTranslator().then((translator) => {
|
|
13
|
+
setTranslatorInstance(translator);
|
|
14
|
+
translator.onLanguageChanged(() => setUpdateCount(updateCount + 1));
|
|
15
|
+
});
|
|
12
16
|
}, [plugin]);
|
|
13
|
-
const safeT = (key, options) => {
|
|
17
|
+
const safeT = useCallback((key, options) => {
|
|
14
18
|
// return zero-width space if translator is not initialized to keep text space occupied
|
|
15
19
|
if (!translatorInstance)
|
|
16
20
|
return '\u200B'; // zero-width space
|
|
17
21
|
const result = translatorInstance.t(key, options);
|
|
18
22
|
if (!result) {
|
|
19
|
-
console.error(`Translation key not found: ${key}`);
|
|
20
23
|
return '\u200B'; // zero-width space
|
|
21
24
|
}
|
|
22
25
|
return result;
|
|
23
|
-
};
|
|
26
|
+
}, [translatorInstance, updateCount]);
|
|
24
27
|
return { t: safeT, ready: translatorInstance !== null };
|
|
25
28
|
}
|
|
@@ -3,6 +3,8 @@ export function useTheme(theme = 'system') {
|
|
|
3
3
|
const dom = document.documentElement;
|
|
4
4
|
dom.style.background = 'hsl(var(--background))';
|
|
5
5
|
dom.classList.add('text-gray-900', 'dark:text-gray-200', 'bg-gray-50', 'dark:bg-gray-950');
|
|
6
|
+
dom.dataset.theme = isDark ? 'dark' : 'light';
|
|
7
|
+
dom.style.colorScheme = isDark ? 'dark' : 'light';
|
|
6
8
|
const root = document.querySelector('#root');
|
|
7
9
|
root.style.background = 'hsl(var(--background))';
|
|
8
10
|
dom.classList[isDark ? 'add' : 'remove']('dark');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rimori/react-client",
|
|
3
|
-
"version": "0.4.6-next.
|
|
3
|
+
"version": "0.4.6-next.3",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"format": "prettier --write ."
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
|
-
"@rimori/client": "2.5.
|
|
26
|
+
"@rimori/client": "2.5.9-next.2",
|
|
27
27
|
"react": "^18.1.0",
|
|
28
28
|
"react-dom": "^18.1.0"
|
|
29
29
|
},
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@eslint/js": "^9.37.0",
|
|
37
|
-
"@rimori/client": "2.5.
|
|
37
|
+
"@rimori/client": "2.5.9-next.2",
|
|
38
38
|
"@types/react": "^18.3.21",
|
|
39
39
|
"eslint-config-prettier": "^10.1.8",
|
|
40
40
|
"eslint-plugin-prettier": "^5.5.4",
|
package/src/hooks/I18nHooks.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TOptions } from '@rimori/client';
|
|
2
|
-
import { useEffect, useState } from 'react';
|
|
2
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
3
3
|
import { Translator } from '@rimori/client';
|
|
4
4
|
import { useRimori } from '../providers/PluginProvider';
|
|
5
5
|
|
|
@@ -12,22 +12,28 @@ type TranslatorFn = (key: string, options?: TOptions) => string;
|
|
|
12
12
|
export function useTranslation(): { t: TranslatorFn; ready: boolean } {
|
|
13
13
|
const { plugin } = useRimori();
|
|
14
14
|
const [translatorInstance, setTranslatorInstance] = useState<Translator | null>(null);
|
|
15
|
+
const [updateCount, setUpdateCount] = useState(0);
|
|
15
16
|
|
|
16
17
|
useEffect(() => {
|
|
17
|
-
void plugin.getTranslator().then(
|
|
18
|
+
void plugin.getTranslator().then((translator) => {
|
|
19
|
+
setTranslatorInstance(translator);
|
|
20
|
+
translator.onLanguageChanged(() => setUpdateCount(updateCount + 1));
|
|
21
|
+
});
|
|
18
22
|
}, [plugin]);
|
|
19
23
|
|
|
20
|
-
const safeT = (
|
|
21
|
-
|
|
22
|
-
|
|
24
|
+
const safeT = useCallback(
|
|
25
|
+
(key: string, options?: TOptions): string => {
|
|
26
|
+
// return zero-width space if translator is not initialized to keep text space occupied
|
|
27
|
+
if (!translatorInstance) return '\u200B'; // zero-width space
|
|
23
28
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
const result = translatorInstance.t(key, options);
|
|
30
|
+
if (!result) {
|
|
31
|
+
return '\u200B'; // zero-width space
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
},
|
|
35
|
+
[translatorInstance, updateCount],
|
|
36
|
+
);
|
|
31
37
|
|
|
32
38
|
return { t: safeT, ready: translatorInstance !== null };
|
|
33
39
|
}
|
package/src/hooks/ThemeSetter.ts
CHANGED
|
@@ -6,6 +6,8 @@ export function useTheme(theme: Theme = 'system'): { isDark: boolean; theme: The
|
|
|
6
6
|
const dom = document.documentElement;
|
|
7
7
|
dom.style.background = 'hsl(var(--background))';
|
|
8
8
|
dom.classList.add('text-gray-900', 'dark:text-gray-200', 'bg-gray-50', 'dark:bg-gray-950');
|
|
9
|
+
dom.dataset.theme = isDark ? 'dark' : 'light';
|
|
10
|
+
dom.style.colorScheme = isDark ? 'dark' : 'light';
|
|
9
11
|
|
|
10
12
|
const root = document.querySelector('#root') as HTMLDivElement;
|
|
11
13
|
root.style.background = 'hsl(var(--background))';
|