@shohojdhara/atomix 0.5.2 → 0.5.4

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.
Files changed (39) hide show
  1. package/atomix.config.ts +33 -33
  2. package/dist/config.d.ts +187 -112
  3. package/dist/config.js +7 -49
  4. package/dist/config.js.map +1 -1
  5. package/dist/index.d.ts +1958 -900
  6. package/dist/index.esm.js +2275 -383
  7. package/dist/index.esm.js.map +1 -1
  8. package/dist/index.js +2327 -417
  9. package/dist/index.js.map +1 -1
  10. package/dist/index.min.js +1 -1
  11. package/dist/index.min.js.map +1 -1
  12. package/dist/theme.d.ts +1390 -276
  13. package/dist/theme.js +2129 -621
  14. package/dist/theme.js.map +1 -1
  15. package/package.json +1 -1
  16. package/scripts/cli/internal/config-loader.js +30 -20
  17. package/src/lib/config/index.ts +38 -362
  18. package/src/lib/config/loader.ts +419 -0
  19. package/src/lib/config/public-api.ts +43 -0
  20. package/src/lib/config/types.ts +389 -0
  21. package/src/lib/config/validator.ts +305 -0
  22. package/src/lib/theme/adapters/index.ts +1 -1
  23. package/src/lib/theme/adapters/themeAdapter.ts +358 -229
  24. package/src/lib/theme/components/ThemeToggle.tsx +276 -0
  25. package/src/lib/theme/config/configLoader.ts +351 -0
  26. package/src/lib/theme/config/loader.ts +221 -0
  27. package/src/lib/theme/core/createTheme.ts +126 -50
  28. package/src/lib/theme/core/createThemeObject.ts +7 -4
  29. package/src/lib/theme/hooks/useThemeSwitcher.ts +164 -0
  30. package/src/lib/theme/index.ts +322 -38
  31. package/src/lib/theme/runtime/ThemeProvider.tsx +44 -10
  32. package/src/lib/theme/runtime/__tests__/ThemeProvider.test.tsx +44 -393
  33. package/src/lib/theme/runtime/useTheme.ts +1 -0
  34. package/src/lib/theme/tokens/tokens.ts +101 -1
  35. package/src/lib/theme/types.ts +91 -0
  36. package/src/lib/theme/utils/performanceMonitor.ts +315 -0
  37. package/src/lib/theme/utils/responsive.ts +280 -0
  38. package/src/lib/theme/utils/themeUtils.ts +531 -117
  39. package/src/styles/05-objects/_objects.masonry-grid.scss +3 -3
@@ -22,14 +22,27 @@
22
22
  // Core Theme Functions
23
23
  // ============================================================================
24
24
 
25
- // Create theme CSS from DesignTokens
26
- export { createTheme } from './core';
27
-
28
- // Theme composition
29
- export { deepMerge, mergeTheme, extendTheme } from './core';
25
+ import {
26
+ createTheme,
27
+ deepMerge,
28
+ mergeTheme,
29
+ extendTheme,
30
+ createThemeRegistry,
31
+ registerTheme,
32
+ unregisterTheme,
33
+ hasTheme,
34
+ getTheme,
35
+ getAllThemes,
36
+ getThemeIds,
37
+ clearThemes,
38
+ getThemeCount,
39
+ } from './core';
30
40
 
31
- // Simplified Theme Registry
32
41
  export {
42
+ createTheme,
43
+ deepMerge,
44
+ mergeTheme,
45
+ extendTheme,
33
46
  createThemeRegistry,
34
47
  registerTheme,
35
48
  unregisterTheme,
@@ -39,14 +52,14 @@ export {
39
52
  getThemeIds,
40
53
  clearThemes,
41
54
  getThemeCount,
42
- } from './core';
55
+ };
43
56
 
44
57
  // ============================================================================
45
58
  // Theme Injection and Management
46
59
  // ============================================================================
47
60
 
48
61
  import { injectCSS, removeCSS, isCSSInjected } from './utils/injectCSS';
49
- // File saving utilities removed to prevent bundling Node.js modules in browser
62
+ export { injectCSS, removeCSS, isCSSInjected };
50
63
 
51
64
  /**
52
65
  * Inject theme CSS into DOM
@@ -66,23 +79,30 @@ export function removeTheme(id: string = 'atomix-theme'): void {
66
79
  // Token Utilities
67
80
  // ============================================================================
68
81
 
69
- export { createTokens, defaultTokens, type DesignTokens } from './tokens';
82
+ import { createTokens, defaultTokens, type DesignTokens } from './tokens';
83
+ export { createTokens, defaultTokens, type DesignTokens };
70
84
 
71
85
  // ============================================================================
72
86
  // CSS Generation
73
87
  // ============================================================================
74
88
 
75
- export {
89
+ import {
76
90
  generateCSSVariables,
77
91
  generateCSSVariablesForSelector,
78
92
  type GenerateCSSVariablesOptions,
79
93
  } from './generators';
80
94
 
95
+ export {
96
+ generateCSSVariables,
97
+ generateCSSVariablesForSelector,
98
+ type GenerateCSSVariablesOptions,
99
+ };
100
+
81
101
  // ============================================================================
82
102
  // Naming and Component Theming Utilities
83
103
  // ============================================================================
84
104
 
85
- export {
105
+ import {
86
106
  generateClassName,
87
107
  generateCSSVariableName,
88
108
  normalizeThemeTokens,
@@ -92,6 +112,15 @@ export {
92
112
  } from './utils/naming';
93
113
 
94
114
  export {
115
+ generateClassName,
116
+ generateCSSVariableName,
117
+ normalizeThemeTokens,
118
+ camelToKebab,
119
+ themePropertyToCSSVar,
120
+ type NamingOptions,
121
+ };
122
+
123
+ import {
95
124
  getComponentThemeValue,
96
125
  generateComponentCSSVars,
97
126
  applyComponentTheme,
@@ -99,39 +128,159 @@ export {
99
128
  type ComponentThemeOptions,
100
129
  } from './utils/componentTheming';
101
130
 
131
+ export {
132
+ getComponentThemeValue,
133
+ generateComponentCSSVars,
134
+ applyComponentTheme,
135
+ useComponentTheme,
136
+ type ComponentThemeOptions,
137
+ };
138
+
102
139
  // ============================================================================
103
- // Injection Utilities
140
+ // Theme Utilities (Switching, Persistence, Colors)
104
141
  // ============================================================================
105
142
 
106
- export { injectCSS, removeCSS, isCSSInjected } from './utils/injectCSS';
143
+ import {
144
+ // Theme Mode Switching
145
+ switchTheme,
146
+ toggleTheme,
147
+ getCurrentTheme,
148
+ getSystemTheme,
149
+ initializeTheme,
150
+ listenToSystemTheme,
151
+
152
+ // Theme Persistence
153
+ persistTheme,
154
+ clearThemePreference,
155
+
156
+ // Token Manipulation
157
+ mergeTokens,
158
+ overrideTokens,
159
+ pickTokens,
160
+ omitTokens,
161
+
162
+ // Color Utilities
163
+ hexToRgb,
164
+ rgbToHex,
165
+ getLuminance,
166
+ getContrastRatio,
167
+ isAccessible,
168
+ getContrastText,
169
+ lighten,
170
+ darken,
171
+ alpha,
172
+ emphasize,
173
+ createSpacing,
174
+
175
+ // Types
176
+ type ThemeMode,
177
+ type ThemeSwitcherOptions,
178
+ type ThemePersistenceOptions,
179
+ } from './utils/themeUtils';
107
180
 
108
- // Config loader removed to prevent bundling Node.js modules in browser
181
+ export {
182
+ switchTheme,
183
+ toggleTheme,
184
+ getCurrentTheme,
185
+ getSystemTheme,
186
+ initializeTheme,
187
+ listenToSystemTheme,
188
+ persistTheme,
189
+ clearThemePreference,
190
+ mergeTokens,
191
+ overrideTokens,
192
+ pickTokens,
193
+ omitTokens,
194
+ hexToRgb,
195
+ rgbToHex,
196
+ getLuminance,
197
+ getContrastRatio,
198
+ isAccessible,
199
+ getContrastText,
200
+ lighten,
201
+ darken,
202
+ alpha,
203
+ emphasize,
204
+ createSpacing,
205
+ type ThemeMode,
206
+ type ThemeSwitcherOptions,
207
+ type ThemePersistenceOptions,
208
+ };
109
209
 
110
210
  // ============================================================================
111
211
  // React Integration
112
212
  // ============================================================================
113
213
 
114
- // Core React components and hooks
115
- export { ThemeProvider } from './runtime/ThemeProvider';
116
- export { useTheme } from './runtime/useTheme';
117
- export { useThemeTokens } from './runtime/useThemeTokens';
118
- export { ThemeContext } from './runtime/ThemeContext';
119
- export { ThemeErrorBoundary } from './runtime/ThemeErrorBoundary';
214
+ import { ThemeProvider } from './runtime/ThemeProvider';
215
+ import { useTheme } from './runtime/useTheme';
216
+ import { useThemeTokens } from './runtime/useThemeTokens';
217
+ import { ThemeContext } from './runtime/ThemeContext';
218
+ import { ThemeErrorBoundary, type ThemeErrorBoundaryProps } from './runtime/ThemeErrorBoundary';
120
219
 
121
- // Theme application
122
- export { ThemeApplicator, getThemeApplicator, applyTheme } from './runtime/ThemeApplicator';
220
+ export {
221
+ ThemeProvider,
222
+ useTheme,
223
+ useThemeTokens,
224
+ ThemeContext,
225
+ ThemeErrorBoundary,
226
+ type ThemeErrorBoundaryProps,
227
+ };
228
+
229
+ import {
230
+ useThemeSwitcher,
231
+ type UseThemeSwitcherOptions,
232
+ type UseThemeSwitcherReturn,
233
+ } from './hooks/useThemeSwitcher';
234
+
235
+ export {
236
+ useThemeSwitcher,
237
+ type UseThemeSwitcherOptions,
238
+ type UseThemeSwitcherReturn,
239
+ };
240
+
241
+ import { ThemeToggle, type ThemeToggleProps } from './components/ThemeToggle';
242
+ export { ThemeToggle, type ThemeToggleProps };
243
+
244
+ import { ThemeApplicator, getThemeApplicator, applyTheme } from './runtime/ThemeApplicator';
245
+ export { ThemeApplicator, getThemeApplicator, applyTheme };
123
246
 
124
247
  // DevTools (for development and debugging)
125
248
  export * from './devtools';
126
249
 
127
- // CSS variable utilities
128
- export { designTokensToCSSVars } from './adapters';
250
+ // Theme adapters
251
+ import { designTokensToCSSVars, configToTokens } from './adapters';
252
+ export { designTokensToCSSVars, configToTokens };
129
253
 
130
- // Theme helpers (utilities for working with DesignTokens)
131
- export { isDesignTokens } from './utils/themeHelpers';
254
+ // Theme helpers
255
+ import { isDesignTokens } from './utils/themeHelpers';
256
+ export { isDesignTokens };
257
+
258
+ // Performance utilities
259
+ import {
260
+ createPerformanceMonitor,
261
+ usePerformanceMonitor,
262
+ type PerformanceMetrics,
263
+ } from './utils/performanceMonitor';
264
+
265
+ export {
266
+ createPerformanceMonitor,
267
+ usePerformanceMonitor,
268
+ type PerformanceMetrics,
269
+ };
270
+
271
+ // Responsive utilities
272
+ import {
273
+ createResponsiveUtil,
274
+ useResponsive,
275
+ } from './utils/responsive';
132
276
 
133
- // CSS variable utilities
134
277
  export {
278
+ createResponsiveUtil,
279
+ useResponsive,
280
+ };
281
+
282
+ // CSS variable utilities
283
+ import {
135
284
  mapSCSSTokensToCSSVars,
136
285
  applyCSSVariables,
137
286
  removeCSSVariables,
@@ -140,10 +289,25 @@ export {
140
289
  mergeCSSVars,
141
290
  isValidCSSVariableName,
142
291
  extractComponentName,
292
+ type CSSVariableConfig,
293
+ type CSSVariableNamingOptions,
143
294
  } from './adapters/cssVariableMapper';
144
295
 
145
- // RTL Support
146
- export { RTLManager } from './i18n/rtl';
296
+ export {
297
+ mapSCSSTokensToCSSVars,
298
+ applyCSSVariables,
299
+ removeCSSVariables,
300
+ getCSSVariable,
301
+ cssVarsToStyle,
302
+ mergeCSSVars,
303
+ isValidCSSVariableName,
304
+ extractComponentName,
305
+ type CSSVariableConfig,
306
+ type CSSVariableNamingOptions,
307
+ };
308
+
309
+ import { RTLManager, createRTLManager, isRTLLocale, getDirectionFromLocale, rtlCSS, type RTLConfig } from './i18n/rtl';
310
+ export { RTLManager, createRTLManager, isRTLLocale, getDirectionFromLocale, rtlCSS, type RTLConfig };
147
311
 
148
312
  // Types
149
313
  export type {
@@ -155,14 +319,134 @@ export type {
155
319
  UseThemeReturn,
156
320
  ComponentThemeOverride,
157
321
  ThemeComponentOverrides,
322
+ Theme,
158
323
  } from './types';
159
324
 
160
- // Note: Theme type is deprecated - use DesignTokens instead
161
- // Keeping for backward compatibility with devtools and internal use only
162
- export type { Theme } from './types';
163
-
164
- export type { ThemeErrorBoundaryProps } from './runtime/ThemeErrorBoundary';
165
-
166
- export type { CSSVariableConfig, CSSVariableNamingOptions } from './adapters/cssVariableMapper';
167
-
168
- export type { RTLConfig } from './i18n/rtl';
325
+ /**
326
+ * Main theme module interface
327
+ */
328
+ export default {
329
+ // Core
330
+ createTheme,
331
+ injectTheme,
332
+ removeTheme,
333
+
334
+ // Context and Provider
335
+ ThemeProvider,
336
+ useTheme,
337
+ useThemeTokens,
338
+ ThemeContext,
339
+ ThemeErrorBoundary,
340
+
341
+ // Adapters
342
+ configToTokens,
343
+ designTokensToCSSVars,
344
+
345
+ // Theme Utils
346
+ switchTheme,
347
+ toggleTheme,
348
+ getCurrentTheme,
349
+ getSystemTheme,
350
+ initializeTheme,
351
+ listenToSystemTheme,
352
+ persistTheme,
353
+ clearThemePreference,
354
+
355
+ // Token Manipulation
356
+ mergeTokens,
357
+ overrideTokens,
358
+ pickTokens,
359
+ omitTokens,
360
+
361
+ // Color Utilities
362
+ hexToRgb,
363
+ rgbToHex,
364
+ getLuminance,
365
+ getContrastRatio,
366
+ isAccessible,
367
+ getContrastText,
368
+ lighten,
369
+ darken,
370
+ alpha,
371
+ emphasize,
372
+ createSpacing,
373
+
374
+ // Performance utilities
375
+ createPerformanceMonitor,
376
+ usePerformanceMonitor,
377
+
378
+ // Responsive utilities
379
+ createResponsiveUtil,
380
+ useResponsive,
381
+
382
+ // Components
383
+ ThemeToggle,
384
+ ThemeApplicator,
385
+ applyTheme,
386
+ getThemeApplicator,
387
+
388
+ // Registry
389
+ createThemeRegistry,
390
+ registerTheme,
391
+ unregisterTheme,
392
+ hasTheme,
393
+ getTheme,
394
+ getAllThemes,
395
+ getThemeIds,
396
+ clearThemes,
397
+ getThemeCount,
398
+
399
+ // Composition
400
+ deepMerge,
401
+ mergeTheme,
402
+ extendTheme,
403
+
404
+ // Tokens
405
+ createTokens,
406
+ defaultTokens,
407
+
408
+ // Generators
409
+ generateCSSVariables,
410
+ generateCSSVariablesForSelector,
411
+
412
+ // Naming
413
+ generateClassName,
414
+ generateCSSVariableName,
415
+ normalizeThemeTokens,
416
+ camelToKebab,
417
+ themePropertyToCSSVar,
418
+
419
+ // Component Theming
420
+ getComponentThemeValue,
421
+ generateComponentCSSVars,
422
+ applyComponentTheme,
423
+ useComponentTheme,
424
+
425
+ // Hooks
426
+ useThemeSwitcher,
427
+
428
+ // Helpers
429
+ isDesignTokens,
430
+
431
+ // CSS Variable Mapper
432
+ mapSCSSTokensToCSSVars,
433
+ applyCSSVariables,
434
+ removeCSSVariables,
435
+ getCSSVariable,
436
+ cssVarsToStyle,
437
+ mergeCSSVars,
438
+ isValidCSSVariableName,
439
+ extractComponentName,
440
+
441
+ // Injection Utils
442
+ injectCSS,
443
+ removeCSS,
444
+ isCSSInjected,
445
+
446
+ // I18n
447
+ RTLManager,
448
+ createRTLManager,
449
+ isRTLLocale,
450
+ getDirectionFromLocale,
451
+ rtlCSS,
452
+ };
@@ -80,6 +80,15 @@ export const ThemeProvider: React.FC<ThemeProviderProps> = ({
80
80
  if (enablePersistence && storageAdapter.isAvailable()) {
81
81
  const stored = storageAdapter.getItem(storageKey);
82
82
  if (stored) {
83
+ // If it looks like a JSON object, parse it
84
+ if (stored.trim().startsWith('{')) {
85
+ try {
86
+ return JSON.parse(stored);
87
+ } catch (e) {
88
+ logger.error('Failed to parse stored theme tokens', e as Error);
89
+ return stored;
90
+ }
91
+ }
83
92
  return stored;
84
93
  }
85
94
  }
@@ -103,17 +112,19 @@ export const ThemeProvider: React.FC<ThemeProviderProps> = ({
103
112
  });
104
113
 
105
114
  const [activeTokens, setActiveTokens] = useState<DesignTokens | null>(() => {
106
- // If defaultTheme is DesignTokens, validate and store them
115
+ // 1. Check if initialDefaultTheme (from storage) is an object
116
+ if (initialDefaultTheme && typeof initialDefaultTheme !== 'string') {
117
+ const { tokens, validation } = validateAndMergeTokens(initialDefaultTheme);
118
+ if (validation.valid) {
119
+ return tokens;
120
+ }
121
+ }
122
+
123
+ // 2. Check if defaultTheme prop is an object
107
124
  if (defaultTheme && typeof defaultTheme !== 'string') {
108
125
  const { tokens, validation } = validateAndMergeTokens(defaultTheme);
109
126
  if (validation.valid) {
110
127
  return tokens;
111
- } else {
112
- logger.warn('Invalid default theme tokens, using defaults', {
113
- errors: validation.errors,
114
- warnings: validation.warnings,
115
- });
116
- return createTokens({}); // Use defaults if validation fails
117
128
  }
118
129
  }
119
130
  return null;
@@ -143,12 +154,20 @@ export const ThemeProvider: React.FC<ThemeProviderProps> = ({
143
154
  }
144
155
  }, [currentTheme, dataAttribute]);
145
156
 
146
- // Handle theme persistence
157
+ // Handle persistence
147
158
  useEffect(() => {
148
159
  if (enablePersistence && storageAdapter.isAvailable()) {
149
- storageAdapter.setItem(storageKey, String(currentTheme));
160
+ if (currentTheme === 'tokens-theme') {
161
+ // Only persist if we have actual tokens to store
162
+ if (activeTokens) {
163
+ storageAdapter.setItem(storageKey, JSON.stringify(activeTokens));
164
+ }
165
+ } else {
166
+ // Persist named theme string
167
+ storageAdapter.setItem(storageKey, String(currentTheme));
168
+ }
150
169
  }
151
- }, [currentTheme, storageKey, enablePersistence, storageAdapter]);
170
+ }, [currentTheme, activeTokens, enablePersistence, storageKey, storageAdapter]);
152
171
 
153
172
  // Cleanup: Remove completed promises and abort controllers on unmount
154
173
  useEffect(() => {
@@ -430,6 +449,19 @@ export const ThemeProvider: React.FC<ThemeProviderProps> = ({
430
449
  [themes, isThemeLoaded, handleError, basePath, useMinified, cdnPath]
431
450
  );
432
451
 
452
+ // Update theme section function
453
+ const updateTheme = useCallback(
454
+ async (sectionOrTokens: any, values?: any) => {
455
+ if (typeof sectionOrTokens === 'string' && values) {
456
+ // Legacy support: updateTheme('colors', { primary: '...' })
457
+ return setTheme(values);
458
+ }
459
+ // Direct partial update: updateTheme({ primary: '...' })
460
+ return setTheme(sectionOrTokens);
461
+ },
462
+ [setTheme]
463
+ );
464
+
433
465
  // Create a mock theme manager instance for the context
434
466
  const themeManager = useMemo(() => {
435
467
  // This would normally be a real ThemeManager instance
@@ -455,6 +487,7 @@ export const ThemeProvider: React.FC<ThemeProviderProps> = ({
455
487
  theme: currentTheme,
456
488
  activeTokens,
457
489
  setTheme,
490
+ updateTheme,
458
491
  availableThemes,
459
492
  isLoading,
460
493
  error,
@@ -466,6 +499,7 @@ export const ThemeProvider: React.FC<ThemeProviderProps> = ({
466
499
  currentTheme,
467
500
  activeTokens,
468
501
  setTheme,
502
+ updateTheme,
469
503
  availableThemes, // Use memoized value
470
504
  isLoading,
471
505
  error,