@udixio/ui-react 1.4.0 → 1.5.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/CHANGELOG.md +15 -0
- package/dist/index.cjs +2 -2
- package/dist/index.js +572 -560
- package/dist/lib/effects/ThemeProvider.d.ts +2 -1
- package/dist/lib/effects/ThemeProvider.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/lib/effects/ThemeProvider.tsx +76 -27
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ConfigInterface } from '@udixio/theme';
|
|
2
|
-
export declare const ThemeProvider: ({ config }: {
|
|
2
|
+
export declare const ThemeProvider: ({ config, throttleDelay, }: {
|
|
3
3
|
config: ConfigInterface;
|
|
4
|
+
throttleDelay?: number;
|
|
4
5
|
}) => import("react/jsx-runtime").JSX.Element | null;
|
|
5
6
|
//# sourceMappingURL=ThemeProvider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ThemeProvider.d.ts","sourceRoot":"","sources":["../../../src/lib/effects/ThemeProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,eAAe,EAAU,MAAM,eAAe,CAAC;AAS7D,eAAO,MAAM,aAAa,GAAI,
|
|
1
|
+
{"version":3,"file":"ThemeProvider.d.ts","sourceRoot":"","sources":["../../../src/lib/effects/ThemeProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,eAAe,EAAU,MAAM,eAAe,CAAC;AAS7D,eAAO,MAAM,aAAa,GAAI,4BAG3B;IACD,MAAM,EAAE,eAAe,CAAC;IACxB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,mDA2FA,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@udixio/ui-react",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"react": "^19.1.1",
|
|
37
37
|
"react-dom": "^19.1.1",
|
|
38
38
|
"@udixio/theme": "1.2.0",
|
|
39
|
-
"@udixio/tailwind": "1.
|
|
39
|
+
"@udixio/tailwind": "1.5.0"
|
|
40
40
|
},
|
|
41
41
|
"repository": {
|
|
42
42
|
"type": "git",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type ConfigInterface, loader } from '@udixio/theme';
|
|
2
|
-
import { useEffect, useState } from 'react';
|
|
2
|
+
import { useEffect, useRef, useState } from 'react';
|
|
3
3
|
import { TailwindPlugin } from '@udixio/tailwind';
|
|
4
4
|
|
|
5
5
|
function isValidHexColor(hexColorString: string) {
|
|
@@ -7,52 +7,101 @@ function isValidHexColor(hexColorString: string) {
|
|
|
7
7
|
return regex.test(hexColorString);
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
export const ThemeProvider = ({
|
|
10
|
+
export const ThemeProvider = ({
|
|
11
|
+
config,
|
|
12
|
+
throttleDelay = 100, // Délai par défaut de 300ms
|
|
13
|
+
}: {
|
|
14
|
+
config: ConfigInterface;
|
|
15
|
+
throttleDelay?: number;
|
|
16
|
+
}) => {
|
|
11
17
|
const [error, setError] = useState<string | null>(null);
|
|
12
|
-
|
|
13
18
|
const [outputCss, setOutputCss] = useState<null | string>(null);
|
|
14
19
|
|
|
20
|
+
// Refs pour gérer le throttling
|
|
21
|
+
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
22
|
+
const lastSourceColorRef = useRef<string>(config.sourceColor);
|
|
23
|
+
const isInitialLoadRef = useRef<boolean>(true);
|
|
24
|
+
|
|
15
25
|
useEffect(() => {
|
|
16
|
-
|
|
26
|
+
// Si c'est le premier chargement, on applique immédiatement
|
|
27
|
+
if (isInitialLoadRef.current) {
|
|
28
|
+
isInitialLoadRef.current = false;
|
|
29
|
+
lastSourceColorRef.current = config.sourceColor;
|
|
30
|
+
applyThemeChange(config.sourceColor);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Si la couleur n'a pas changé, on ne fait rien
|
|
35
|
+
if (config.sourceColor === lastSourceColorRef.current) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Annuler le timeout précédent s'il existe
|
|
40
|
+
if (timeoutRef.current) {
|
|
41
|
+
clearTimeout(timeoutRef.current);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Programmer un nouveau changement de thème avec un délai
|
|
45
|
+
timeoutRef.current = setTimeout(() => {
|
|
46
|
+
lastSourceColorRef.current = config.sourceColor;
|
|
47
|
+
applyThemeChange(config.sourceColor);
|
|
48
|
+
timeoutRef.current = null;
|
|
49
|
+
}, throttleDelay);
|
|
50
|
+
|
|
51
|
+
// Cleanup function pour annuler le timeout si le composant se démonte
|
|
52
|
+
return () => {
|
|
53
|
+
if (timeoutRef.current) {
|
|
54
|
+
clearTimeout(timeoutRef.current);
|
|
55
|
+
timeoutRef.current = null;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}, [config.sourceColor, throttleDelay]);
|
|
59
|
+
|
|
60
|
+
const applyThemeChange = async (sourceColor: string) => {
|
|
61
|
+
if (!isValidHexColor(sourceColor)) {
|
|
17
62
|
setError('Invalid hex color');
|
|
18
63
|
return;
|
|
19
64
|
}
|
|
20
65
|
|
|
21
|
-
|
|
22
|
-
try {
|
|
23
|
-
const api = await loader(config);
|
|
66
|
+
setError(null);
|
|
24
67
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
68
|
+
try {
|
|
69
|
+
// Mesure du temps de chargement de l'API
|
|
70
|
+
const api = await loader(config);
|
|
71
|
+
api.themes.update({
|
|
72
|
+
sourceColorHex: sourceColor,
|
|
73
|
+
});
|
|
74
|
+
await api.load();
|
|
28
75
|
|
|
29
|
-
|
|
76
|
+
const generatedCss = api.plugins
|
|
77
|
+
.getPlugin(TailwindPlugin)
|
|
78
|
+
.getInstance().outputCss;
|
|
30
79
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
80
|
+
if (generatedCss) {
|
|
81
|
+
setOutputCss(generatedCss);
|
|
82
|
+
}
|
|
83
|
+
} catch (err) {
|
|
84
|
+
setError(err instanceof Error ? err.message : 'Theme loading failed');
|
|
85
|
+
}
|
|
86
|
+
};
|
|
34
87
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
88
|
+
// Cleanup lors du démontage du composant
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
return () => {
|
|
91
|
+
if (timeoutRef.current) {
|
|
92
|
+
clearTimeout(timeoutRef.current);
|
|
39
93
|
}
|
|
40
94
|
};
|
|
95
|
+
}, []);
|
|
41
96
|
|
|
42
|
-
initTheme();
|
|
43
|
-
}, [config.sourceColor]);
|
|
44
97
|
if (error) {
|
|
45
|
-
console.error('ThemeProvider error:', error);
|
|
46
98
|
return null;
|
|
47
99
|
}
|
|
48
100
|
|
|
49
101
|
if (!outputCss) {
|
|
102
|
+
console.error('ThemeProvider null');
|
|
50
103
|
return null;
|
|
51
104
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
type="text/tailwindcss"
|
|
55
|
-
dangerouslySetInnerHTML={{ __html: outputCss }}
|
|
56
|
-
/>
|
|
57
|
-
);
|
|
105
|
+
|
|
106
|
+
return <style dangerouslySetInnerHTML={{ __html: outputCss }} />;
|
|
58
107
|
};
|