@chainloyalty/react 1.0.2 → 1.2.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/src/index.ts CHANGED
@@ -14,5 +14,9 @@ export type { RewardsDashboardProps, Badge, RewardHistoryItem, TierInfo, Dashboa
14
14
  export { Leaderboard } from './Leaderboard.js';
15
15
  export type { LeaderboardProps, LeaderboardEntry, LeaderboardTimeframe } from './Leaderboard.js';
16
16
 
17
+ // Export theme utilities
18
+ export { getThemeColors, getThemeStylesheet, createThemeStyles } from './theme.js';
19
+ export type { ThemeColors } from './theme.js';
20
+
17
21
  // Version info
18
22
  export const PACKAGE_VERSION = '1.0.0';
package/src/theme.ts ADDED
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Theme utility for @chainloyalty/react
3
+ * Automatically inherits colors from host application's CSS variables
4
+ * Supports Tailwind CSS theme variables and custom CSS variables
5
+ */
6
+
7
+ export interface ThemeColors {
8
+ primary: string;
9
+ accent: string;
10
+ background: string;
11
+ foreground: string;
12
+ card: string;
13
+ border: string;
14
+ muted: string;
15
+ mutedForeground: string;
16
+ }
17
+
18
+ /**
19
+ * Get the current theme colors from host app's CSS variables
20
+ * Falls back to defaults if variables not found
21
+ */
22
+ export const getThemeColors = (): ThemeColors => {
23
+ const root = document.documentElement;
24
+
25
+ // Helper to get CSS variable value
26
+ const getCSSVar = (varName: string, fallback: string): string => {
27
+ const value = getComputedStyle(root).getPropertyValue(varName).trim();
28
+ return value || fallback;
29
+ };
30
+
31
+ // Try to get Tailwind CSS variables first
32
+ const primary = getCSSVar('--primary', '36 100% 50%'); // Golden
33
+ const accent = getCSSVar('--accent', '190 100% 39%'); // Cyan
34
+ const background = getCSSVar('--background', '220 14% 96%'); // Light bg
35
+ const foreground = getCSSVar('--foreground', '220 20% 10%'); // Dark text
36
+ const card = getCSSVar('--card', '0 0% 100%'); // White
37
+ const border = getCSSVar('--border', '220 13% 91%'); // Light gray
38
+ const muted = getCSSVar('--muted', '220 14% 96%'); // Same as background
39
+ const mutedForeground = getCSSVar('--muted-foreground', '220 9% 46%'); // Gray text
40
+
41
+ return {
42
+ primary: `hsl(${primary})`,
43
+ accent: `hsl(${accent})`,
44
+ background: `hsl(${background})`,
45
+ foreground: `hsl(${foreground})`,
46
+ card: `hsl(${card})`,
47
+ border: `hsl(${border})`,
48
+ muted: `hsl(${muted})`,
49
+ mutedForeground: `hsl(${mutedForeground})`,
50
+ };
51
+ };
52
+
53
+ /**
54
+ * Create inline styles using theme colors
55
+ * Can be merged with component styles
56
+ */
57
+ export const createThemeStyles = (theme: ThemeColors) => ({
58
+ container: {
59
+ backgroundColor: theme.card,
60
+ color: theme.foreground,
61
+ borderColor: theme.border,
62
+ } as React.CSSProperties,
63
+ text: {
64
+ color: theme.foreground,
65
+ } as React.CSSProperties,
66
+ accent: {
67
+ color: theme.accent,
68
+ } as React.CSSProperties,
69
+ button: {
70
+ backgroundColor: theme.primary,
71
+ color: '#fff',
72
+ } as React.CSSProperties,
73
+ });
74
+
75
+ /**
76
+ * Inject theme CSS variables into component wrapper
77
+ * This allows nested components to use CSS variables via context
78
+ */
79
+ export const getThemeStylesheet = (theme: ThemeColors): string => {
80
+ return `
81
+ .chainloyalty-theme {
82
+ --cl-primary: ${theme.primary};
83
+ --cl-accent: ${theme.accent};
84
+ --cl-background: ${theme.background};
85
+ --cl-foreground: ${theme.foreground};
86
+ --cl-card: ${theme.card};
87
+ --cl-border: ${theme.border};
88
+ --cl-muted: ${theme.muted};
89
+ --cl-muted-fg: ${theme.mutedForeground};
90
+ }
91
+ `;
92
+ };