@codeleap/styles 4.2.1 → 4.2.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.
@@ -1,4 +1,29 @@
1
1
  "use strict";
2
+ var __read = (this && this.__read) || function (o, n) {
3
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
4
+ if (!m) return o;
5
+ var i = m.call(o), r, ar = [], e;
6
+ try {
7
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8
+ }
9
+ catch (error) { e = { error: error }; }
10
+ finally {
11
+ try {
12
+ if (r && !r.done && (m = i["return"])) m.call(i);
13
+ }
14
+ finally { if (e) throw e.error; }
15
+ }
16
+ return ar;
17
+ };
18
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
19
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
20
+ if (ar || !(i in from)) {
21
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
22
+ ar[i] = from[i];
23
+ }
24
+ }
25
+ return to.concat(ar || Array.prototype.slice.call(from));
26
+ };
2
27
  Object.defineProperty(exports, "__esModule", { value: true });
3
28
  exports.StyleCache = void 0;
4
29
  var Cacher_1 = require("./Cacher");
@@ -31,7 +56,13 @@ var StyleCache = /** @class */ (function () {
31
56
  StyleCache.prototype.keyFor = function (type, keyData) {
32
57
  var _a;
33
58
  var cache = this[type];
34
- var values = [this.baseKey, keyData];
59
+ var withFunctionsHash = Object.values(keyData).map(function (value) {
60
+ if (typeof value === 'function') {
61
+ return value.toString();
62
+ }
63
+ return value;
64
+ });
65
+ var values = __spreadArray([this.baseKey], __read(withFunctionsHash), false);
35
66
  var cacheKey = (0, hashKey_1.hashKey)(values);
36
67
  var cachedValue = minifier_1.minifier.decompress((_a = cache.cache[cacheKey]) !== null && _a !== void 0 ? _a : null);
37
68
  return {
@@ -1,9 +1,10 @@
1
- import { AnyRecord, AnyStyledComponent, ICSS, ITheme, StyleProp, VariantStyleSheet } from '../types';
1
+ import { AnyRecord, AnyStyledComponent, ICSS, ITheme, StyleAggregator, StyleProp, VariantStyleSheet } from '../types';
2
2
  import { MultiplierFunction } from './spacing';
3
3
  import { StateStorage } from 'zustand/middleware';
4
4
  export declare class CodeleapStyleRegistry {
5
5
  stylesheets: Record<string, VariantStyleSheet>;
6
6
  commonVariants: Record<string, ICSS | MultiplierFunction>;
7
+ aggregators: Record<string, StyleAggregator>;
7
8
  components: Record<string, AnyStyledComponent>;
8
9
  private theme;
9
10
  private styleCache;
@@ -33,7 +34,7 @@ export declare class CodeleapStyleRegistry {
33
34
  getCompositionStyle(componentName: string, composition: Record<string, any>, style: any): any;
34
35
  styleFor<T = unknown>(componentName: string, componentStyle: StyleProp<T>, mergeWithDefaultStyle?: boolean): T;
35
36
  registerCommonVariants(): void;
36
- registerVariants(componentName: string, variants: VariantStyleSheet): void;
37
+ registerVariants<Composition extends string = any>(componentName: string, variants: VariantStyleSheet, aggregator?: StyleAggregator<Composition>): void;
37
38
  registerComponent(component: AnyStyledComponent): void;
38
39
  /**
39
40
  * These should be overwritten by the end-user to support
@@ -63,6 +63,7 @@ var CodeleapStyleRegistry = /** @class */ (function () {
63
63
  var _a, _b, _c, _d, _e;
64
64
  this.stylesheets = {};
65
65
  this.commonVariants = {};
66
+ this.aggregators = {};
66
67
  this.components = {};
67
68
  this.styleCache = new StyleCache_1.StyleCache(storage);
68
69
  this.theme = themeStore_1.themeStore.getState();
@@ -118,7 +119,8 @@ var CodeleapStyleRegistry = /** @class */ (function () {
118
119
  var rootElement = this.getRegisteredComponent(componentName).rootElement;
119
120
  var component = _component !== null && _component !== void 0 ? _component : rootElement;
120
121
  var stylesheet = minifier_1.minifier.decompress(this.stylesheets[componentName]);
121
- var cache = this.styleCache.keyFor('variants', { componentName: componentName, component: component, stylesheet: stylesheet, variants: variants });
122
+ var aggregator = this.aggregators[componentName];
123
+ var cache = this.styleCache.keyFor('variants', { componentName: componentName, component: component, stylesheet: stylesheet, variants: variants, aggregator: aggregator });
122
124
  if (!!cache.value) {
123
125
  return cache.value;
124
126
  }
@@ -142,6 +144,9 @@ var CodeleapStyleRegistry = /** @class */ (function () {
142
144
  return _this.computeCommonVariantStyle(componentName, variant, component);
143
145
  }).filter(function (variantStyle) { return !!variantStyle; });
144
146
  var variantStyle = (0, deepmerge_1.default)({ all: true }).apply(void 0, __spreadArray([], __read(variantStyles), false));
147
+ if (!!aggregator) {
148
+ variantStyle = aggregator(theme, variantStyle);
149
+ }
145
150
  this.styleCache.cacheFor('variants', cache.key, variantStyle);
146
151
  return variantStyle;
147
152
  };
@@ -389,23 +394,31 @@ var CodeleapStyleRegistry = /** @class */ (function () {
389
394
  if (typeof s === 'string') {
390
395
  variants.push(s);
391
396
  }
392
- if (typeof s === 'object') {
397
+ var isObj = typeof s === 'object';
398
+ if (variants.length > 0 && (idx === filteredStyle.length - 1 || isObj)) {
399
+ var computedVariantStyle = this.computeVariantStyle(componentName, variants);
400
+ styles.push(computedVariantStyle);
401
+ variants = [];
402
+ }
403
+ if (isObj) {
393
404
  var _h = this.isCompositionStyle(registeredComponent, s), isComposition = _h.isComposition, composition = _h.composition;
394
- if (isComposition) {
405
+ var _j = this.isResponsiveStyle(s), isResponsive = _j.isResponsive, responsiveStyleKey = _j.responsiveStyleKey;
406
+ if (Array.isArray(s)) {
407
+ var styleComposition = this.styleFor(componentName, s, false);
408
+ styles.push(styleComposition);
409
+ }
410
+ else if (isComposition) {
395
411
  var compositionStyles = this.getCompositionStyle(componentName, composition, s);
396
412
  styles.push.apply(styles, __spreadArray([], __read(compositionStyles), false));
397
413
  }
398
- var _j = this.isResponsiveStyle(s), isResponsive = _j.isResponsive, responsiveStyleKey = _j.responsiveStyleKey;
399
- if (isResponsive) {
414
+ else if (isResponsive) {
400
415
  var responsiveStyles = this.getResponsiveStyle(componentName, responsiveStyleKey, s);
401
416
  styles.push(responsiveStyles);
402
417
  delete s[responsiveStyleKey];
403
418
  }
404
- styles.push((_d = {}, _d[rootElement] = s, _d));
405
- }
406
- if (idx === filteredStyle.length - 1 && variants.length > 0) {
407
- var computedVariantStyle = this.computeVariantStyle(componentName, variants);
408
- styles.push(computedVariantStyle);
419
+ else {
420
+ styles.push((_d = {}, _d[rootElement] = s, _d));
421
+ }
409
422
  }
410
423
  idx++;
411
424
  }
@@ -430,10 +443,11 @@ var CodeleapStyleRegistry = /** @class */ (function () {
430
443
  var commonVariants = (0, deepmerge_1.default)({ all: true })(defaultVariants_1.defaultVariants, appVariants, dynamicVariants_1.dynamicVariants, spacingVariants, insetVariants);
431
444
  this.commonVariants = commonVariants;
432
445
  };
433
- CodeleapStyleRegistry.prototype.registerVariants = function (componentName, variants) {
446
+ CodeleapStyleRegistry.prototype.registerVariants = function (componentName, variants, aggregator) {
434
447
  if (this.stylesheets[componentName]) {
435
448
  throw new Error("Variants for ".concat(componentName, " already registered"));
436
449
  }
450
+ this.aggregators[componentName] = aggregator;
437
451
  this.stylesheets[componentName] = minifier_1.minifier.compress(variants);
438
452
  };
439
453
  CodeleapStyleRegistry.prototype.registerComponent = function (component) {
@@ -10,6 +10,17 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
+ var __rest = (this && this.__rest) || function (s, e) {
14
+ var t = {};
15
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
16
+ t[p] = s[p];
17
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
18
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
19
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
20
+ t[p[i]] = s[p[i]];
21
+ }
22
+ return t;
23
+ };
13
24
  Object.defineProperty(exports, "__esModule", { value: true });
14
25
  exports.createTheme = void 0;
15
26
  var borderCreator_1 = require("./borderCreator");
@@ -19,57 +30,40 @@ var defaultVariants_1 = require("./defaultVariants");
19
30
  var spacing_1 = require("./spacing");
20
31
  var themeStore_1 = require("./themeStore");
21
32
  var createTheme = function (theme, colorSchemaPersistor) {
22
- var _a, _b, _c, _d, _e, _f;
23
- var themeObj = {
24
- get currentColorScheme() {
33
+ var _a;
34
+ var colors = theme.colors, breakpoints = theme.breakpoints, presets = theme.presets, borderRadius = theme.borderRadius, effects = theme.effects, typography = theme.typography, icons = theme.icons, values = theme.values, otherThemeValues = __rest(theme, ["colors", "breakpoints", "presets", "borderRadius", "effects", "typography", "icons", "values"]);
35
+ var themeObj = __assign(__assign({}, otherThemeValues), { get currentColorScheme() {
25
36
  return themeStore_1.themeStore.getState().colorScheme;
26
- },
27
- breakpoints: (_a = theme.breakpoints) !== null && _a !== void 0 ? _a : {},
28
- get colors() {
37
+ }, breakpoints: breakpoints !== null && breakpoints !== void 0 ? breakpoints : {}, get colors() {
29
38
  var _a;
30
39
  var colorScheme = themeStore_1.themeStore.getState().colorScheme;
31
40
  if (colorScheme === 'default')
32
- return theme.colors;
41
+ return colors;
33
42
  return (_a = theme.alternateColors) === null || _a === void 0 ? void 0 : _a[colorScheme];
34
- },
35
- setColorScheme: function (colorScheme) {
43
+ }, setColorScheme: function (colorScheme) {
36
44
  themeStore_1.themeStore.setState({ colorScheme: colorScheme });
37
45
  colorSchemaPersistor.set(colorScheme);
38
- },
39
- baseSpacing: theme.baseSpacing,
40
- value: function (n) {
46
+ }, baseSpacing: theme.baseSpacing, value: function (n) {
41
47
  if (n === void 0) { n = 1; }
42
48
  return theme.baseSpacing * n;
43
- },
44
- spacing: __assign(__assign(__assign(__assign({ value: function (n) {
49
+ }, spacing: __assign(__assign(__assign(__assign({ value: function (n) {
45
50
  if (n === void 0) { n = 1; }
46
51
  return theme.baseSpacing * n;
47
- }, gap: (0, multiplierProperty_1.multiplierProperty)(theme.baseSpacing, 'gap') }, (0, spacing_1.spacingFactory)(theme.baseSpacing, 'padding')), (0, spacing_1.spacingFactory)(theme.baseSpacing, 'margin')), (0, spacing_1.spacingFactory)(theme.baseSpacing, 'p', true)), (0, spacing_1.spacingFactory)(theme.baseSpacing, 'm', true)),
48
- inset: {
52
+ }, gap: (0, multiplierProperty_1.multiplierProperty)(theme.baseSpacing, 'gap') }, (0, spacing_1.spacingFactory)(theme.baseSpacing, 'padding')), (0, spacing_1.spacingFactory)(theme.baseSpacing, 'margin')), (0, spacing_1.spacingFactory)(theme.baseSpacing, 'p', true)), (0, spacing_1.spacingFactory)(theme.baseSpacing, 'm', true)), inset: {
49
53
  top: (0, multiplierProperty_1.multiplierProperty)(theme.baseSpacing, 'top'),
50
54
  bottom: (0, multiplierProperty_1.multiplierProperty)(theme.baseSpacing, 'bottom'),
51
55
  left: (0, multiplierProperty_1.multiplierProperty)(theme.baseSpacing, 'left'),
52
- right: (0, multiplierProperty_1.multiplierProperty)(theme.baseSpacing, 'right')
53
- },
54
- presets: __assign(__assign({}, defaultVariants_1.defaultVariants), theme.presets),
55
- borderRadius: (_b = theme.borderRadius) !== null && _b !== void 0 ? _b : {},
56
- effects: (_c = theme.effects) !== null && _c !== void 0 ? _c : {},
57
- media: (0, mediaQuery_1.createMediaQueries)(theme.breakpoints),
58
- border: borderCreator_1.borderCreator,
59
- typography: (_d = theme.typography) !== null && _d !== void 0 ? _d : {},
60
- icons: theme.icons,
61
- values: (_e = theme.values) !== null && _e !== void 0 ? _e : {},
62
- sized: function (size) {
56
+ right: (0, multiplierProperty_1.multiplierProperty)(theme.baseSpacing, 'right'),
57
+ }, presets: __assign(__assign({}, defaultVariants_1.defaultVariants), presets), borderRadius: borderRadius !== null && borderRadius !== void 0 ? borderRadius : {}, effects: effects !== null && effects !== void 0 ? effects : {}, media: (0, mediaQuery_1.createMediaQueries)(breakpoints), border: borderCreator_1.borderCreator, typography: typography !== null && typography !== void 0 ? typography : {}, icons: icons, values: values !== null && values !== void 0 ? values : {}, sized: function (size) {
63
58
  var value = typeof size == 'number' ? size * theme.baseSpacing : size;
64
59
  return {
65
60
  width: value,
66
61
  height: value,
67
62
  };
68
- },
69
- };
63
+ } });
70
64
  themeStore_1.themeStore.setState({
71
65
  current: themeObj,
72
- colorScheme: (_f = colorSchemaPersistor.get()) !== null && _f !== void 0 ? _f : 'default'
66
+ colorScheme: (_a = colorSchemaPersistor.get()) !== null && _a !== void 0 ? _a : 'default',
73
67
  });
74
68
  return themeObj;
75
69
  };
@@ -1,13 +1,14 @@
1
1
  import { DynamicVariants } from '../lib/dynamicVariants';
2
2
  import { DefaultVariants } from '../lib/defaultVariants';
3
- import { AnyRecord, IAppVariants, IBreakpoints, ICSS, IEffects } from './core';
3
+ import { AnyRecord, IAppVariants, IBreakpoints, ICSS, IEffects, ITheme } from './core';
4
4
  import { Multiplier, Spacing } from './spacing';
5
5
  type Inset = `top:${Multiplier}` | `bottom:${Multiplier}` | `left:${Multiplier}` | `right:${Multiplier}`;
6
6
  export type CommonVariants = Spacing | Inset | DynamicVariants | keyof DefaultVariants | keyof IAppVariants | `effect:${keyof IEffects}`;
7
7
  type StyleAtom<Composition = AnyRecord, Variants = string, HasBreakpoints = string, HasComposition = string> = ICSS | Variants | CommonVariants | boolean | null | '' | (HasBreakpoints extends string ? {
8
8
  'breakpoints': Partial<Record<keyof IBreakpoints, StyleAtom<Composition, Variants, boolean, string> | StyleAtom<Composition, Variants, boolean, string>[]>>;
9
- } : null) | (HasComposition extends string ? Partial<Record<keyof Composition, StyleAtom<AnyRecord, Variants, boolean, boolean> | StyleAtom<AnyRecord, Variants, boolean, boolean>[]>> : null);
9
+ } : null) | (HasComposition extends string ? Partial<Record<keyof Composition, StyleAtom<AnyRecord, Variants, boolean, boolean> | StyleAtom<AnyRecord, Variants, boolean, boolean>[]>> : null) | Array<Variants | ICSS | Partial<Record<keyof Composition, StyleAtom<AnyRecord, Variants, boolean, boolean> | StyleAtom<AnyRecord, Variants, boolean, boolean>[]>>>;
10
10
  export type StyleProp<Composition = AnyRecord, Variants = string> = StyleAtom<Composition, Variants> | StyleAtom<Composition, Variants>[];
11
11
  export type VariantStyleSheet = Record<string, any>;
12
12
  export type StyledProp<T extends string> = StyleProp<Record<T, ICSS>>;
13
+ export type StyleAggregator<T extends string = any> = (theme: ITheme, style: Record<T, ICSS>) => Record<T, ICSS>;
13
14
  export {};
@@ -40,23 +40,28 @@ export type Theme = {
40
40
  };
41
41
  export type DefaultColorSchemeName = 'default';
42
42
  export type ColorScheme<T extends Theme = Theme> = DefaultColorSchemeName | keyof T['alternateColors'];
43
- export type AppTheme<T extends Theme> = {
43
+ type PredefinedThemeDerivedValues<T extends Theme> = {
44
44
  colors: T['colors'];
45
45
  breakpoints: T['breakpoints'];
46
- setColorScheme: (colorScheme: ColorScheme<T>) => void;
47
- currentColorScheme: ColorScheme<T>;
48
- spacing: SpacingMap;
49
46
  presets: DefaultVariants & T['presets'];
50
47
  borderRadius: T['borderRadius'];
51
- media: MediaQueries;
52
48
  effects: T['effects'];
53
- border: BorderCreator;
54
49
  typography: T['typography'];
55
50
  icons: T['icons'];
56
51
  values: T['values'];
52
+ };
53
+ type PredefinedAppTheme<T extends Theme> = PredefinedThemeDerivedValues<T> & {
54
+ setColorScheme: (colorScheme: ColorScheme<T>) => void;
55
+ currentColorScheme: ColorScheme<T>;
56
+ spacing: SpacingMap;
57
+ media: MediaQueries;
58
+ border: BorderCreator;
57
59
  inset: InsetMap;
58
60
  baseSpacing: number;
59
61
  value: (multiplier: number) => number;
60
62
  sized: (size: number | string) => ICSS;
61
63
  };
64
+ export type AppTheme<T extends Theme> = PredefinedAppTheme<T> & {
65
+ [P in Exclude<keyof T, keyof PredefinedThemeDerivedValues<T>>]: T[P];
66
+ };
62
67
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeleap/styles",
3
- "version": "4.2.1",
3
+ "version": "4.2.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "UNLICENSED",
@@ -10,7 +10,7 @@
10
10
  "directory": "packages/styles"
11
11
  },
12
12
  "devDependencies": {
13
- "@codeleap/config": "4.2.1",
13
+ "@codeleap/config": "4.2.3",
14
14
  "ts-node-dev": "1.1.8"
15
15
  },
16
16
  "scripts": {
package/package.json.bak CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeleap/styles",
3
- "version": "4.2.1",
3
+ "version": "4.2.3",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "UNLICENSED",
@@ -9,11 +9,15 @@ export class StyleCache {
9
9
  baseKey: string
10
10
 
11
11
  styles = new Cache('styles')
12
+
12
13
  compositions = new Cache('compositions')
14
+
13
15
  responsive = new Cache('responsive')
14
16
 
15
17
  variants: Cache
18
+
16
19
  common: Cache
20
+
17
21
  components: Cache
18
22
 
19
23
  constructor(storage: StateStorage) {
@@ -44,7 +48,15 @@ export class StyleCache {
44
48
  keyFor(type: CacheType, keyData: Array<any> | any) {
45
49
  const cache = this[type]
46
50
 
47
- const values = [this.baseKey, keyData]
51
+ const withFunctionsHash = Object.values(keyData).map((value) => {
52
+ if (typeof value === 'function') {
53
+ return value.toString()
54
+ }
55
+
56
+ return value
57
+ })
58
+
59
+ const values = [this.baseKey, ...withFunctionsHash]
48
60
 
49
61
  const cacheKey = hashKey(values)
50
62
  const cachedValue = minifier.decompress(cache.cache[cacheKey] ?? null)
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable dot-notation */
2
- import { AnyRecord, AnyStyledComponent, ICSS, ITheme, StyleProp, VariantStyleSheet } from '../types'
2
+ import { AnyRecord, AnyStyledComponent, ICSS, ITheme, StyleAggregator, StyleProp, VariantStyleSheet } from '../types'
3
3
  import { ThemeStore, themeStore } from './themeStore'
4
4
  import deepmerge from '@fastify/deepmerge'
5
5
  import { MultiplierFunction } from './spacing'
@@ -15,6 +15,8 @@ export class CodeleapStyleRegistry {
15
15
 
16
16
  commonVariants: Record<string, ICSS | MultiplierFunction> = {}
17
17
 
18
+ aggregators: Record<string, StyleAggregator> = {}
19
+
18
20
  components: Record<string, AnyStyledComponent> = {}
19
21
 
20
22
  private theme: ThemeStore
@@ -91,8 +93,9 @@ export class CodeleapStyleRegistry {
91
93
  const component = _component ?? rootElement
92
94
 
93
95
  const stylesheet = minifier.decompress(this.stylesheets[componentName])
96
+ const aggregator = this.aggregators[componentName]
94
97
 
95
- const cache = this.styleCache.keyFor('variants', { componentName, component, stylesheet, variants })
98
+ const cache = this.styleCache.keyFor('variants', { componentName, component, stylesheet, variants, aggregator })
96
99
 
97
100
  if (!!cache.value) {
98
101
  return cache.value
@@ -102,6 +105,7 @@ export class CodeleapStyleRegistry {
102
105
 
103
106
  const variantStyles = variants.map((variant) => {
104
107
  if (!!stylesheet[variant]) {
108
+
105
109
  return stylesheet[variant]
106
110
  }
107
111
 
@@ -122,7 +126,11 @@ export class CodeleapStyleRegistry {
122
126
  return this.computeCommonVariantStyle(componentName, variant, component)
123
127
  }).filter(variantStyle => !!variantStyle)
124
128
 
125
- const variantStyle = deepmerge({ all: true })(...variantStyles)
129
+ let variantStyle = deepmerge({ all: true })(...variantStyles)
130
+
131
+ if (!!aggregator) {
132
+ variantStyle = aggregator(theme, variantStyle)
133
+ }
126
134
 
127
135
  this.styleCache.cacheFor('variants', cache.key, variantStyle)
128
136
 
@@ -402,40 +410,50 @@ export class CodeleapStyleRegistry {
402
410
  if (isStyleArray) {
403
411
  const filteredStyle = (style as Array<any>)?.filter(s => !!s)
404
412
 
405
- const variants: string[] = []
413
+ let variants: string[] = []
406
414
  const styles: ICSS[] = [defaultStyle]
407
415
  let idx = 0
408
416
 
409
417
  for (const s of filteredStyle) {
418
+
410
419
  if (typeof s === 'string') {
420
+
411
421
  variants.push(s)
412
422
  }
413
423
 
414
- if (typeof s === 'object') {
415
- const { isComposition, composition } = this.isCompositionStyle(registeredComponent, s)
424
+ const isObj = typeof s === 'object'
416
425
 
417
- if (isComposition) {
418
- const compositionStyles = this.getCompositionStyle(componentName, composition, s)
426
+ if (variants.length > 0 && (idx === filteredStyle.length - 1 || isObj)) {
427
+ const computedVariantStyle = this.computeVariantStyle(componentName, variants)
419
428
 
420
- styles.push(...compositionStyles)
421
- }
429
+ styles.push(computedVariantStyle)
422
430
 
431
+ variants = []
432
+ }
433
+
434
+ if (isObj) {
435
+ const { isComposition, composition } = this.isCompositionStyle(registeredComponent, s)
423
436
  const { isResponsive, responsiveStyleKey } = this.isResponsiveStyle(s)
424
437
 
425
- if (isResponsive) {
438
+ if (Array.isArray(s)) {
439
+ const styleComposition = this.styleFor(componentName, s, false)
440
+
441
+ styles.push(styleComposition)
442
+ } else if (isComposition) {
443
+ const compositionStyles = this.getCompositionStyle(componentName, composition, s)
444
+
445
+ styles.push(...compositionStyles)
446
+ } else if (isResponsive) {
426
447
  const responsiveStyles = this.getResponsiveStyle(componentName, responsiveStyleKey, s)
427
448
 
428
449
  styles.push(responsiveStyles)
429
450
 
430
451
  delete s[responsiveStyleKey]
431
- }
452
+ } else {
432
453
 
433
- styles.push({ [rootElement]: s })
434
- }
454
+ styles.push({ [rootElement]: s })
455
+ }
435
456
 
436
- if (idx === filteredStyle.length - 1 && variants.length > 0) {
437
- const computedVariantStyle = this.computeVariantStyle(componentName, variants)
438
- styles.push(computedVariantStyle)
439
457
  }
440
458
 
441
459
  idx++
@@ -467,10 +485,11 @@ export class CodeleapStyleRegistry {
467
485
  this.commonVariants = commonVariants
468
486
  }
469
487
 
470
- registerVariants(componentName: string, variants: VariantStyleSheet) {
488
+ registerVariants<Composition extends string = any>(componentName: string, variants: VariantStyleSheet, aggregator?: StyleAggregator<Composition>) {
471
489
  if (this.stylesheets[componentName]) {
472
490
  throw new Error(`Variants for ${componentName} already registered`)
473
491
  }
492
+ this.aggregators[componentName] = aggregator
474
493
 
475
494
  this.stylesheets[componentName] = minifier.compress(variants)
476
495
  }
@@ -507,7 +526,7 @@ export class CodeleapStyleRegistry {
507
526
  if (Array.isArray(style)) {
508
527
  copiedStyle = [...style]
509
528
  } else if (typeof style == 'object') {
510
- copiedStyle = {...style}
529
+ copiedStyle = { ...style }
511
530
  } else {
512
531
  copiedStyle = style
513
532
  }
@@ -12,18 +12,31 @@ type ColorSchemaPersistor = {
12
12
  }
13
13
 
14
14
  export const createTheme = <T extends Theme>(theme: T, colorSchemaPersistor: ColorSchemaPersistor): AppTheme<T> => {
15
+ const {
16
+ colors,
17
+ breakpoints,
18
+ presets,
19
+ borderRadius,
20
+ effects,
21
+ typography,
22
+ icons,
23
+ values,
24
+ ...otherThemeValues
25
+ } = theme
26
+
15
27
  const themeObj:AppTheme<T> = {
28
+ ...otherThemeValues,
16
29
  get currentColorScheme(): string {
17
30
  return themeStore.getState().colorScheme
18
31
  },
19
32
 
20
- breakpoints: theme.breakpoints ?? {},
33
+ breakpoints: breakpoints ?? {},
21
34
 
22
35
  get colors() {
23
36
  const colorScheme = themeStore.getState().colorScheme
24
37
 
25
- if (colorScheme === 'default') return theme.colors
26
-
38
+ if (colorScheme === 'default') return colors
39
+
27
40
  return theme.alternateColors?.[colorScheme]
28
41
  },
29
42
 
@@ -48,31 +61,31 @@ export const createTheme = <T extends Theme>(theme: T, colorSchemaPersistor: Col
48
61
  top: multiplierProperty(theme.baseSpacing, 'top'),
49
62
  bottom: multiplierProperty(theme.baseSpacing, 'bottom'),
50
63
  left: multiplierProperty(theme.baseSpacing, 'left'),
51
- right: multiplierProperty(theme.baseSpacing, 'right')
64
+ right: multiplierProperty(theme.baseSpacing, 'right'),
52
65
  },
53
66
 
54
67
  presets: {
55
68
  ...defaultVariants,
56
- ...theme.presets,
69
+ ...presets,
57
70
  },
58
71
 
59
- borderRadius: theme.borderRadius ?? {},
72
+ borderRadius: borderRadius ?? {},
60
73
 
61
- effects: theme.effects ?? {},
74
+ effects: effects ?? {},
62
75
 
63
- media: createMediaQueries(theme.breakpoints),
76
+ media: createMediaQueries(breakpoints),
64
77
 
65
78
  border: borderCreator,
66
79
 
67
- typography: theme.typography ?? {},
80
+ typography: typography ?? {},
68
81
 
69
- icons: theme.icons,
82
+ icons: icons,
70
83
 
71
- values: theme.values ?? {},
84
+ values: values ?? {},
72
85
 
73
86
  sized: (size) => {
74
87
  const value = typeof size == 'number' ? size * theme.baseSpacing : size
75
-
88
+
76
89
  return {
77
90
  width: value,
78
91
  height: value,
@@ -80,9 +93,9 @@ export const createTheme = <T extends Theme>(theme: T, colorSchemaPersistor: Col
80
93
  },
81
94
  }
82
95
 
83
- themeStore.setState({
84
- current: themeObj,
85
- colorScheme: colorSchemaPersistor.get() ?? 'default'
96
+ themeStore.setState({
97
+ current: themeObj,
98
+ colorScheme: colorSchemaPersistor.get() ?? 'default',
86
99
  })
87
100
 
88
101
  return themeObj
@@ -1,7 +1,7 @@
1
1
  import { DynamicVariants } from '../lib/dynamicVariants'
2
2
  import { Queries } from '../lib/mediaQuery'
3
3
  import { DefaultVariants } from '../lib/defaultVariants'
4
- import { AnyRecord, IAppVariants, IBreakpoints, ICSS, IEffects } from './core'
4
+ import { AnyRecord, IAppVariants, IBreakpoints, ICSS, IEffects, ITheme } from './core'
5
5
  import { Multiplier, Spacing } from './spacing'
6
6
 
7
7
  type Inset =
@@ -25,11 +25,29 @@ type StyleAtom<Composition = AnyRecord, Variants = string, HasBreakpoints = stri
25
25
  boolean |
26
26
  null |
27
27
  '' |
28
- // (HasBreakpoints extends string ? `${keyof IBreakpoints}:${CommonVariants}` : null) |
28
+
29
29
  (HasBreakpoints extends string ? {
30
- 'breakpoints': Partial<Record<keyof IBreakpoints, StyleAtom<Composition, Variants, boolean, string> | StyleAtom<Composition, Variants, boolean, string>[]>>
30
+ 'breakpoints': Partial<
31
+ Record<keyof IBreakpoints,
32
+ StyleAtom<Composition, Variants, boolean, string> |
33
+ StyleAtom<Composition, Variants, boolean, string>[]>
34
+ >
31
35
  } : null) |
32
- (HasComposition extends string ? Partial<Record<keyof Composition, StyleAtom<AnyRecord, Variants, boolean, boolean> | StyleAtom<AnyRecord, Variants, boolean, boolean>[]>> : null)
36
+ (HasComposition extends string ?
37
+ Partial<Record<
38
+ keyof Composition,
39
+ StyleAtom<AnyRecord, Variants, boolean, boolean> |
40
+ StyleAtom<AnyRecord, Variants, boolean, boolean>[]
41
+ >>
42
+ : null
43
+ )
44
+ | Array<Variants | ICSS | Partial<
45
+ Record<
46
+ keyof Composition,
47
+ StyleAtom<AnyRecord, Variants, boolean, boolean> |
48
+ StyleAtom<AnyRecord, Variants, boolean, boolean>[]
49
+ >
50
+ >>
33
51
 
34
52
  export type StyleProp<
35
53
  Composition = AnyRecord,
@@ -39,3 +57,5 @@ export type StyleProp<
39
57
  export type VariantStyleSheet = Record<string, any>
40
58
 
41
59
  export type StyledProp<T extends string> = StyleProp<Record<T, ICSS>>
60
+
61
+ export type StyleAggregator<T extends string = any> = (theme: ITheme, style: Record< T, ICSS>) => Record< T, ICSS>
@@ -20,8 +20,8 @@ type EffectsMap = {
20
20
  [key: string]: IEffect
21
21
  }
22
22
 
23
- export type SpacingMap =
24
- Spacings<'margin'> &
23
+ export type SpacingMap =
24
+ Spacings<'margin'> &
25
25
  Spacings<'padding'> &
26
26
  Spacings<'m', string> &
27
27
  Spacings<'p', string> &
@@ -29,7 +29,7 @@ export type SpacingMap =
29
29
  gap: MultiplierFunction
30
30
  }
31
31
 
32
- export type InsetMap =
32
+ export type InsetMap =
33
33
  {
34
34
  bottom: MultiplierFunction
35
35
  top: MultiplierFunction
@@ -56,22 +56,29 @@ export type DefaultColorSchemeName = 'default'
56
56
 
57
57
  export type ColorScheme<T extends Theme = Theme> = DefaultColorSchemeName | keyof T['alternateColors']
58
58
 
59
- export type AppTheme<T extends Theme> = {
59
+ type PredefinedThemeDerivedValues<T extends Theme> = {
60
60
  colors: T['colors']
61
61
  breakpoints: T['breakpoints']
62
- setColorScheme: (colorScheme: ColorScheme<T>) => void
63
- currentColorScheme: ColorScheme<T>
64
- spacing: SpacingMap
65
62
  presets: DefaultVariants & T['presets']
66
63
  borderRadius: T['borderRadius']
67
- media: MediaQueries
68
64
  effects: T['effects']
69
- border: BorderCreator
70
65
  typography: T['typography']
71
66
  icons: T['icons']
72
67
  values: T['values']
68
+ }
69
+
70
+ type PredefinedAppTheme<T extends Theme> = PredefinedThemeDerivedValues<T> & {
71
+ setColorScheme: (colorScheme: ColorScheme<T>) => void
72
+ currentColorScheme: ColorScheme<T>
73
+ spacing: SpacingMap
74
+ media: MediaQueries
75
+ border: BorderCreator
73
76
  inset: InsetMap
74
77
  baseSpacing: number
75
78
  value: (multiplier: number) => number
76
79
  sized: (size: number | string) => ICSS
77
80
  }
81
+
82
+ export type AppTheme<T extends Theme> = PredefinedAppTheme<T> & {
83
+ [P in Exclude<keyof T, keyof PredefinedThemeDerivedValues<T>>]: T[P]
84
+ }