@codeleap/styles 6.2.3 → 6.8.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.
Files changed (138) hide show
  1. package/dist/classes/Cacher.d.ts +87 -0
  2. package/dist/classes/Cacher.d.ts.map +1 -0
  3. package/dist/classes/StaleControl.d.ts +65 -0
  4. package/dist/classes/StaleControl.d.ts.map +1 -0
  5. package/dist/classes/StyleCache.d.ts +63 -0
  6. package/dist/classes/StyleCache.d.ts.map +1 -0
  7. package/dist/classes/StylePersistor.d.ts +52 -0
  8. package/dist/classes/StylePersistor.d.ts.map +1 -0
  9. package/dist/classes/StyleRegistry.d.ts +108 -0
  10. package/dist/classes/StyleRegistry.d.ts.map +1 -0
  11. package/dist/classes/index.d.ts +3 -0
  12. package/dist/classes/index.d.ts.map +1 -0
  13. package/dist/constants.d.ts +22 -0
  14. package/dist/constants.d.ts.map +1 -0
  15. package/dist/hooks/index.d.ts +5 -0
  16. package/dist/hooks/index.d.ts.map +1 -0
  17. package/dist/hooks/useCompositionStyles.d.ts +12 -0
  18. package/dist/hooks/useCompositionStyles.d.ts.map +1 -0
  19. package/dist/hooks/useNestedStylesByKey.d.ts +11 -0
  20. package/dist/hooks/useNestedStylesByKey.d.ts.map +1 -0
  21. package/dist/hooks/useStyleObserver.d.ts +9 -0
  22. package/dist/hooks/useStyleObserver.d.ts.map +1 -0
  23. package/dist/hooks/useTheme.d.ts +19 -0
  24. package/dist/hooks/useTheme.d.ts.map +1 -0
  25. package/dist/index.d.ts +12 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/lib/calc.d.ts +27 -0
  28. package/dist/lib/calc.d.ts.map +1 -0
  29. package/dist/lib/createStyles.d.ts +30 -0
  30. package/dist/lib/createStyles.d.ts.map +1 -0
  31. package/dist/lib/createTheme.d.ts +28 -0
  32. package/dist/lib/createTheme.d.ts.map +1 -0
  33. package/dist/lib/cssVariables.d.ts +35 -0
  34. package/dist/lib/cssVariables.d.ts.map +1 -0
  35. package/dist/lib/index.d.ts +5 -0
  36. package/dist/lib/index.d.ts.map +1 -0
  37. package/dist/theme/generateColorScheme.d.ts +22 -0
  38. package/dist/theme/generateColorScheme.d.ts.map +1 -0
  39. package/dist/theme/index.d.ts +4 -0
  40. package/dist/theme/index.d.ts.map +1 -0
  41. package/dist/theme/themeStore.d.ts +106 -0
  42. package/dist/theme/themeStore.d.ts.map +1 -0
  43. package/dist/theme/validateTheme.d.ts +19 -0
  44. package/dist/theme/validateTheme.d.ts.map +1 -0
  45. package/dist/tools/colors.d.ts +70 -0
  46. package/dist/tools/colors.d.ts.map +1 -0
  47. package/dist/tools/deepClone.d.ts +7 -0
  48. package/dist/tools/deepClone.d.ts.map +1 -0
  49. package/dist/tools/deepmerge.d.ts +13 -0
  50. package/dist/tools/deepmerge.d.ts.map +1 -0
  51. package/dist/tools/hashKey.d.ts +8 -0
  52. package/dist/tools/hashKey.d.ts.map +1 -0
  53. package/dist/tools/index.d.ts +7 -0
  54. package/dist/tools/index.d.ts.map +1 -0
  55. package/dist/tools/minifier.d.ts +24 -0
  56. package/dist/tools/minifier.d.ts.map +1 -0
  57. package/dist/tools/multiplierProperty.d.ts +4 -0
  58. package/dist/tools/multiplierProperty.d.ts.map +1 -0
  59. package/dist/types/cache.d.ts +12 -0
  60. package/dist/types/cache.d.ts.map +1 -0
  61. package/dist/types/component.d.ts +58 -0
  62. package/dist/types/component.d.ts.map +1 -0
  63. package/dist/types/core.d.ts +77 -0
  64. package/dist/types/core.d.ts.map +1 -0
  65. package/dist/types/icon.d.ts +15 -0
  66. package/dist/types/icon.d.ts.map +1 -0
  67. package/dist/types/index.d.ts +6 -0
  68. package/dist/types/index.d.ts.map +1 -0
  69. package/dist/types/spacing.d.ts +28 -0
  70. package/dist/types/spacing.d.ts.map +1 -0
  71. package/dist/types/store.d.ts +12 -0
  72. package/dist/types/store.d.ts.map +1 -0
  73. package/dist/types/style.d.ts +42 -0
  74. package/dist/types/style.d.ts.map +1 -0
  75. package/dist/types/theme.d.ts +109 -0
  76. package/dist/types/theme.d.ts.map +1 -0
  77. package/dist/utils.d.ts +40 -0
  78. package/dist/utils.d.ts.map +1 -0
  79. package/dist/variants/borderCreator.d.ts +22 -0
  80. package/dist/variants/borderCreator.d.ts.map +1 -0
  81. package/dist/variants/createAppVariants.d.ts +18 -0
  82. package/dist/variants/createAppVariants.d.ts.map +1 -0
  83. package/dist/variants/defaultVariants.d.ts +140 -0
  84. package/dist/variants/defaultVariants.d.ts.map +1 -0
  85. package/dist/variants/dynamicVariants.d.ts +43 -0
  86. package/dist/variants/dynamicVariants.d.ts.map +1 -0
  87. package/dist/variants/index.d.ts +7 -0
  88. package/dist/variants/index.d.ts.map +1 -0
  89. package/dist/variants/mediaQuery.d.ts +30 -0
  90. package/dist/variants/mediaQuery.d.ts.map +1 -0
  91. package/dist/variants/spacing.d.ts +26 -0
  92. package/dist/variants/spacing.d.ts.map +1 -0
  93. package/package.json +19 -5
  94. package/src/classes/Cacher.ts +9 -9
  95. package/src/classes/StaleControl.ts +1 -1
  96. package/src/classes/StyleCache.ts +9 -3
  97. package/src/classes/StylePersistor.ts +11 -0
  98. package/src/classes/StyleRegistry.ts +124 -43
  99. package/src/classes/tests/StyleRegistry.spec.ts +169 -0
  100. package/src/constants.ts +14 -0
  101. package/src/hooks/useCompositionStyles.ts +9 -7
  102. package/src/hooks/useNestedStylesByKey.ts +8 -0
  103. package/src/hooks/useStyleObserver.ts +6 -5
  104. package/src/hooks/useTheme.ts +14 -0
  105. package/src/lib/calc.ts +13 -0
  106. package/src/lib/createStyles.ts +35 -4
  107. package/src/lib/createTheme.ts +74 -25
  108. package/src/lib/cssVariables.ts +74 -0
  109. package/src/lib/index.ts +2 -1
  110. package/src/lib/tests/createStyles.spec.ts +80 -23
  111. package/src/lib/tests/createStylesWithContext.spec.ts +108 -0
  112. package/src/tests/theme.ts +6 -2
  113. package/src/theme/generateColorScheme.ts +13 -10
  114. package/src/theme/tests/themeStore.spec.ts +72 -71
  115. package/src/theme/themeStore.ts +10 -7
  116. package/src/theme/validateTheme.ts +1 -1
  117. package/src/tools/colors.ts +24 -36
  118. package/src/tools/deepClone.ts +3 -5
  119. package/src/tools/deepmerge.ts +8 -6
  120. package/src/tools/hashKey.ts +4 -5
  121. package/src/tools/minifier.ts +11 -12
  122. package/src/tools/tests/deepClone.spec.ts +2 -2
  123. package/src/types/cache.ts +10 -0
  124. package/src/types/component.ts +41 -5
  125. package/src/types/core.ts +66 -6
  126. package/src/types/icon.ts +11 -0
  127. package/src/types/spacing.ts +21 -0
  128. package/src/types/store.ts +6 -0
  129. package/src/types/style.ts +37 -10
  130. package/src/types/theme.ts +37 -1
  131. package/src/utils.ts +34 -4
  132. package/src/variants/borderCreator.ts +14 -5
  133. package/src/variants/createAppVariants.ts +11 -0
  134. package/src/variants/defaultVariants.ts +28 -8
  135. package/src/variants/dynamicVariants.ts +76 -18
  136. package/src/variants/mediaQuery.ts +18 -0
  137. package/src/variants/spacing.ts +15 -1
  138. package/package.json.bak +0 -30
@@ -8,18 +8,34 @@ type AnyMap = {
8
8
  [key: string]: any
9
9
  }
10
10
 
11
+ /**
12
+ * Flat or nested color token map passed to `createTheme`.
13
+ * This type is intentionally empty so that `validateTheme` and the CSS-var
14
+ * machinery can accept any shape; strong typing comes via `IColors` augmentation.
15
+ */
11
16
  export type ColorMap = {
12
- [key: string]: string
17
+
13
18
  }
14
19
 
20
+ /**
21
+ * Maps breakpoint names to pixel widths. Used by `createMediaQueries` to generate
22
+ * `@media screen and (min/max-width: <N>px)` strings. Breakpoints not in this map
23
+ * fall back to `Infinity` (treated as unbounded).
24
+ */
15
25
  type BreakpointMap = {
16
26
  [key: string]: number
17
27
  }
18
28
 
29
+ /** Map of named effect tokens (shadows, filters, etc.) to their effect shapes. */
19
30
  type EffectsMap = {
20
31
  [key: string]: IEffect
21
32
  }
22
33
 
34
+ /**
35
+ * The full spacing API attached to every `AppTheme`. Combines the long-form
36
+ * (`margin`, `padding` with variant suffixes) and short-form (`m`, `p`) factories,
37
+ * plus `gap`. Each property is a `MultiplierFunction` that scales by `baseSpacing`.
38
+ */
23
39
  export type SpacingMap =
24
40
  Spacings<'margin'> &
25
41
  Spacings<'padding'> &
@@ -29,6 +45,10 @@ export type SpacingMap =
29
45
  gap: MultiplierFunction
30
46
  }
31
47
 
48
+ /**
49
+ * Position-offset functions (`top`, `bottom`, `left`, `right`) attached to `AppTheme`.
50
+ * Each function multiplies its argument by `baseSpacing` and returns an `ICSS` object.
51
+ */
32
52
  export type InsetMap =
33
53
  {
34
54
  bottom: MultiplierFunction
@@ -37,6 +57,16 @@ export type InsetMap =
37
57
  right: MultiplierFunction
38
58
  }
39
59
 
60
+ /**
61
+ * Raw theme definition passed to `createTheme`. After processing, the resolved object
62
+ * is an `AppTheme<T>` with additional runtime methods (`setColorScheme`, `spacing`, etc.).
63
+ *
64
+ * - `baseColors` — primitive palette tokens merged into both `colors` and every alternate scheme.
65
+ * - `colors` — semantic/alias tokens for the default color scheme.
66
+ * - `alternateColors` — named overrides; each must supply all keys present in `colors`.
67
+ * - `baseSpacing` — the multiplier used by spacing/inset factories (default: 1).
68
+ * - `isBrowser` — when `true`, enables CSS custom-property mode for colors and browser-only styles.
69
+ */
40
70
  export type Theme = {
41
71
  baseColors: ColorMap
42
72
  colors: ColorMap
@@ -44,6 +74,7 @@ export type Theme = {
44
74
  [key: string]: ColorMap
45
75
  }
46
76
  breakpoints?: BreakpointMap
77
+
47
78
  baseSpacing?: number
48
79
  presets?: AnyMap
49
80
  radius?: AnyMap
@@ -56,14 +87,17 @@ export type Theme = {
56
87
  isBrowser?: boolean
57
88
  }
58
89
 
90
+ /** The reserved name for the primary (non-alternate) color scheme. */
59
91
  export type DefaultColorSchemeName = 'default'
60
92
 
93
+ /** Union of `'default'` and all keys defined in `T['alternateColors']`. */
61
94
  export type ColorScheme<T extends Theme = Theme> = DefaultColorSchemeName | keyof T['alternateColors']
62
95
 
63
96
  type PredefinedThemeDerivedValues<T extends Theme> = {
64
97
  baseColors: T['baseColors']
65
98
  colors: T['colors']
66
99
  breakpoints: T['breakpoints']
100
+
67
101
  presets: DefaultVariants & T['presets']
68
102
  radius: T['radius']
69
103
  stroke: T['stroke']
@@ -79,6 +113,8 @@ type PredefinedAppTheme<T extends Theme> = PredefinedThemeDerivedValues<T> & {
79
113
  currentColorScheme: () => ColorScheme<T>
80
114
  injectColorScheme: (name: string, colorMap: ColorMap) => void
81
115
  ejectColorScheme: (name: string) => void
116
+ getCssVariables: (schemeName?: string) => Record<string, string>
117
+ currentSchemeColors: T['colors']
82
118
  spacing: SpacingMap
83
119
  media: MediaQueries
84
120
  border: BorderCreator
package/src/utils.ts CHANGED
@@ -2,12 +2,21 @@ import { ICSS, StyledProp } from './types'
2
2
  import { spacingShortVariants, spacingVariants } from './types/spacing'
3
3
  import { deepmerge } from './tools'
4
4
 
5
+ /**
6
+ * Uppercases (or lowercases when `reverse = true`) the first character of a string.
7
+ * Returns the input unchanged when it is falsy or empty.
8
+ */
5
9
  export function capitalize(str: string, reverse = false) {
6
- if (!str.length) return str
10
+ if (!str?.length) return str
7
11
  const firstChar = reverse ? str[0].toLowerCase() : str[0].toUpperCase()
8
12
  return firstChar + str.substring(1)
9
13
  }
10
14
 
15
+ /**
16
+ * Complete list of CSS/RN spacing-related property names that the style registry treats
17
+ * specially (resolved via the spacing factory rather than passed through as raw ICSS).
18
+ * Built eagerly at module load time by iterating `spacingVariants` and `spacingShortVariants`.
19
+ */
11
20
  export const spacingKeys = [
12
21
  'gap',
13
22
  'top',
@@ -28,14 +37,21 @@ for (const shortProperty of ['p', 'm']) {
28
37
  }
29
38
  }
30
39
 
40
+ /** Returns `true` if `key` is one of the pre-built spacing property names. */
31
41
  export function isSpacingKey(key: string) {
32
42
  if (!key) return false
33
43
 
34
44
  return spacingKeys?.includes(key)
35
45
  }
36
46
 
47
+ /**
48
+ * Extracts all entries whose key starts with `match`, stripping the prefix and
49
+ * lowercasing the first character of the remainder. Used by composition hooks to
50
+ * slice a flat `{ wrapperText: ..., wrapperIcon: ... }` record into a nested
51
+ * `{ text: ..., icon: ... }` map for a single composition element.
52
+ */
37
53
  export function getNestedStylesByKey<T extends string>(match: string, styles: Partial<Record<T, ICSS>>) {
38
- const stylesByKey = {}
54
+ const stylesByKey: Record<string, any> = {}
39
55
 
40
56
  for (const [key, value] of Object.entries(styles)) {
41
57
  if (key.startsWith(match)) {
@@ -47,13 +63,22 @@ export function getNestedStylesByKey<T extends string>(match: string, styles: Pa
47
63
  return stylesByKey
48
64
  }
49
65
 
66
+ /**
67
+ * Merges an array of style objects into one using `@fastify/deepmerge` with `all: true`.
68
+ * Falsy entries are filtered out before merging. Later entries overwrite earlier ones.
69
+ */
50
70
  export const mergeStyles = (styles: Array<any>) => {
51
71
  const style = styles?.filter(s => !!s)
52
72
 
53
73
  return deepmerge({ all: true })(...style)
54
74
  }
55
75
 
56
- // @note these words are reserved by css
76
+ /**
77
+ * Style property names that collide with CSS keywords and must not be treated as
78
+ * composition-element prefixes by the registry. Without this list, a style object
79
+ * like `{ textAlign: 'center' }` would be misidentified as a composition key starting
80
+ * with `text`.
81
+ */
57
82
  export const ignoredStyleKeys = [
58
83
  'textAlign',
59
84
  'textDecoration',
@@ -64,9 +89,14 @@ export const ignoredStyleKeys = [
64
89
  'bottom',
65
90
  ]
66
91
 
92
+ /**
93
+ * Flattens nested arrays of `StyledProp` into a single flat array. Useful when
94
+ * assembling the final `style` prop from multiple sources (e.g., variant defaults
95
+ * plus overrides), without losing the order-dependent merge semantics of `StyleProp`.
96
+ */
67
97
  export const concatStyles = <T extends string>(styles: Array<StyledProp<T>>): StyledProp<T> => {
68
98
  const results = []
69
-
99
+
70
100
  for (const style of styles) {
71
101
  if (Array.isArray(style)) {
72
102
  results.push(...style)
@@ -3,16 +3,25 @@ import { borderDirection } from './dynamicVariants'
3
3
  import { themeStore } from '../theme'
4
4
  import { capitalize } from '../utils'
5
5
 
6
+ /** Arguments for `borderCreator`. */
6
7
  type BorderCreatorArgs = {
7
8
  color: keyof IColors | (string & {})
8
9
  width?: number | string
9
10
  directions?: typeof borderDirection[number][]
10
- // @ts-expect-error borderStyle not exists
11
- style?: ICSS['borderStyle']
11
+ style?: "dashed" | "dotted" | "double" | "groove" | "hidden" | "inset" | "none" | "outset" | "ridge" | "solid";
12
12
  }
13
13
 
14
+ /** Function signature of the `border` helper attached to `AppTheme`. */
14
15
  export type BorderCreator = (args: BorderCreatorArgs) => ICSS
15
16
 
17
+ /**
18
+ * Resolves `color` from `theme.colors` (falls back to the raw string if not found),
19
+ * then emits `border{Direction}Color`, `border{Direction}Width`, and — on web only —
20
+ * `border{Direction}Style` for each requested direction.
21
+ * Defaults: all four sides, width 1, style `'solid'`.
22
+ * `borderStyle` is omitted on React Native because RN requires it on individual sides
23
+ * and `isBrowser` guards that path.
24
+ */
16
25
  export const borderCreator: BorderCreator = (args) => {
17
26
  const {
18
27
  color: colorKey,
@@ -23,9 +32,9 @@ export const borderCreator: BorderCreator = (args) => {
23
32
 
24
33
  const theme = themeStore.themeTyped
25
34
 
26
- const color = theme?.colors?.[colorKey] ?? colorKey
35
+ const color = (theme?.colors as Record<string, any>)?.[colorKey] ?? colorKey
27
36
 
28
- let borderStyles: ICSS = {}
37
+ let borderStyles: Record<string, any> = {}
29
38
 
30
39
  for (const direction of directions) {
31
40
  const property = `border${capitalize(direction)}`
@@ -35,5 +44,5 @@ export const borderCreator: BorderCreator = (args) => {
35
44
  if (theme?.isBrowser) borderStyles[`${property}Style`] = style
36
45
  }
37
46
 
38
- return borderStyles
47
+ return borderStyles as ICSS
39
48
  }
@@ -5,6 +5,17 @@ type AppVariantsMap = {
5
5
  [x: string]: ICSS | ((theme: ITheme) => ICSS)
6
6
  }
7
7
 
8
+ /**
9
+ * Registers app-specific style variants with the global `themeStore`.
10
+ * Call this once during app initialisation (before any component mounts) with the
11
+ * variants object that extends the base variant set. The variants are merged into
12
+ * `commonVariants` when `CodeleapStyleRegistry` initialises.
13
+ *
14
+ * Each entry can be either a static `ICSS` object (applied unconditionally) or a
15
+ * function `(theme: ITheme) => ICSS` (resolved lazily using the current theme).
16
+ *
17
+ * @returns The same `variants` object — useful for typing augmentation of `IAppVariants`.
18
+ */
8
19
  export function createAppVariants<T extends AppVariantsMap>(variants: T) {
9
20
  themeStore.setVariants(variants)
10
21
 
@@ -1,11 +1,22 @@
1
1
  import { ICSS } from '../types'
2
2
 
3
+ /**
4
+ * Static built-in style variants available to every component without any registration.
5
+ * These are merged as the lowest-priority layer in `commonVariants`, so app-defined
6
+ * variants and dynamic variants can override them.
7
+ *
8
+ * Notable entries:
9
+ * - `grow` — sets `flex: 1` (not `display: flex`; use `row`/`column` for flex containers).
10
+ * - `inset` — shorthand for `{ top:0, bottom:0, left:0, right:0 }` (useful with `absolute`).
11
+ * - `insetX` / `insetY` — pin to horizontal or vertical edges only.
12
+ * - `centerChildren` — centers children via `alignItems`/`justifyContent`; requires a flex container.
13
+ * - `'visibility:hidden'` / `'display:none'` — use quoted keys in style arrays.
14
+ */
3
15
  export const defaultVariants = {
4
16
  block: {
5
17
  display: 'block',
6
18
  },
7
- flex: {
8
- display: 'flex',
19
+ grow: {
9
20
  flex: 1,
10
21
  },
11
22
  absolute: {
@@ -28,9 +39,7 @@ export const defaultVariants = {
28
39
  top: 0,
29
40
  bottom: 0,
30
41
  },
31
- hidden: {
32
- display: 'none',
33
- },
42
+
34
43
  full: {
35
44
  width: '100%',
36
45
  height: '100%',
@@ -44,7 +53,7 @@ export const defaultVariants = {
44
53
  fullHeight: {
45
54
  height: '100%',
46
55
  },
47
- whole: {
56
+ inset: {
48
57
  top: 0,
49
58
  bottom: 0,
50
59
  left: 0,
@@ -60,9 +69,10 @@ export const defaultVariants = {
60
69
  flexDirection: 'row',
61
70
  },
62
71
  column: {
72
+ display: 'flex',
63
73
  flexDirection: 'column',
64
74
  },
65
- center: {
75
+ centerChildren: {
66
76
  alignItems: 'center',
67
77
  justifyContent: 'center',
68
78
  },
@@ -114,9 +124,19 @@ export const defaultVariants = {
114
124
  textCenter: {
115
125
  textAlign: 'center',
116
126
  },
117
- wrap: {
127
+ flexWrap: {
118
128
  flexWrap: 'wrap',
119
129
  },
130
+ 'visibility:hidden': {
131
+ visibility: 'hidden'
132
+ },
133
+ 'visibility:visible': {
134
+ visibility: 'visible'
135
+ },
136
+ 'display:none': {
137
+ display:'none'
138
+ }
120
139
  } as const
121
140
 
141
+ /** Typed record mapping every built-in variant name to an `ICSS` value. */
122
142
  export type DefaultVariants = Record<keyof typeof defaultVariants, ICSS>
@@ -1,15 +1,22 @@
1
- import { IBorderRadius, IColors, IEffects } from '../types'
1
+ import { IBorderRadius, IColors, IEffects, ISizes, ITheme } from '../types'
2
2
  import { capitalize } from '../utils'
3
3
 
4
+ /** Callback shape for a variant that receives a raw value and returns a style fragment. */
4
5
  export type VariantFunction = (value: any) => any
5
6
 
7
+ /** CSS properties that accept a color token: `backgroundColor` and `color`. */
6
8
  export const colorVariants = ['backgroundColor', 'color'] as const
7
9
 
10
+ /** Horizontal border directions used when generating per-corner radius variants. */
8
11
  export const borderXDirection = ['left', 'right'] as const
12
+ /** Vertical border directions used when generating per-corner radius variants. */
9
13
  export const borderYDirection = ['bottom', 'top'] as const
14
+ /** All four border directions plus the un-suffixed (all-sides) form. */
10
15
  export const borderDirection = [...borderYDirection, ...borderXDirection, ''] as const
16
+ /** Border sub-properties generated for each direction. */
11
17
  export const borderProperties = ['color', 'radius', 'width'] as const
12
18
 
19
+ /** CSS cursor values surfaced as `cursor:<value>` style variants. */
13
20
  export const cursorTypes = [
14
21
  'not-allowed',
15
22
  'help',
@@ -18,35 +25,58 @@ export const cursorTypes = [
18
25
  '',
19
26
  ] as const
20
27
 
28
+ /** Numeric value or the empty-string terminator used in `scale:` variant templates. */
21
29
  export type Value =
22
30
  | number
23
31
  | ''
24
32
 
25
- // @note
26
- // Typing has been removed because figma's new color organization is extremely heavy,
27
- // perhaps in a future typescript update
28
- type Color = string // keyof IColors
33
+ type MatchKeysByValue<
34
+ T extends Record<string, any>,
35
+ Values
36
+ > = { [K in keyof T]: T[K] extends Values ? K : never }[keyof T]
29
37
 
38
+ // @note
39
+ // Adding the baseColors here make it extremely heavy,
40
+ // perhaps in a future typescript update it will be better. We exclude any non-string keys because those are namespaces (usually components)
41
+ type Color = MatchKeysByValue<IColors, string>// keyof IColors
42
+
43
+ type Sizes = keyof ISizes
44
+
45
+ /**
46
+ * Union of all parametric variant strings resolved at runtime by looking up the
47
+ * provided value in the theme. Examples: `"color:primary"`, `"bg:neutral"`,
48
+ * `"borderRadius:sm"`, `"cursor:pointer"`, `"scale:0.95"`, `"size:icon"`.
49
+ * The part before `:` is the variant name; the part after is the token key or raw value.
50
+ */
30
51
  export type DynamicVariants =
31
52
  `color:${Color}` |
32
53
  `border${Capitalize<typeof borderDirection[number]>}Width:${keyof IBorderRadius}` |
33
54
  `border${Capitalize<typeof borderDirection[number]>}Color:${Color}` |
34
55
  `borderRadius:${keyof IBorderRadius}` |
35
56
  `border${Capitalize<typeof borderYDirection[number]>}${Capitalize<typeof borderXDirection[number]>}Radius:${keyof IBorderRadius}` |
57
+ `border${Capitalize<typeof borderYDirection[number]>}Radius:${keyof IBorderRadius}` |
58
+ `border${Capitalize<typeof borderXDirection[number]>}Radius:${keyof IBorderRadius}` |
36
59
  `cursor:${typeof cursorTypes[number]}` |
37
60
  `bg:${Color}` |
38
61
  `br:${keyof IBorderRadius}` |
39
- `scale:${Value}`
40
-
62
+ `scale:${Value}` |
63
+ `size:${Sizes}`
64
+
65
+ /**
66
+ * Builds the dynamic variant registry object at module initialisation time.
67
+ * Each entry is a function `(theme, value) => ICSS` keyed by the variant name
68
+ * (the part before `:` in the variant string). The registry is consumed by
69
+ * `CodeleapStyleRegistry.computeCommonVariantStyle`.
70
+ */
41
71
  export const createDynamicVariants = () => {
42
- const dynamicVariants = {}
72
+ const dynamicVariants: Record<string, any> = {}
43
73
 
44
74
  function createVariant(variantName: string, variantReturn: any) {
45
75
  dynamicVariants[variantName] = variantReturn
46
76
  }
47
77
 
48
78
  colorVariants.forEach(variant => {
49
- createVariant(variant, (theme, color: Color) => ({
79
+ createVariant(variant, (theme: ITheme & Record<string, any>, color: Color) => ({
50
80
  [variant]: theme.colors[color],
51
81
  }))
52
82
  })
@@ -56,42 +86,70 @@ export const createDynamicVariants = () => {
56
86
  borderXDirection.forEach(y => {
57
87
  const variant = `border${capitalize(direction)}${capitalize(y)}Radius`
58
88
 
59
- createVariant(variant, (theme, value: keyof IBorderRadius) => ({
89
+ createVariant(variant, (theme: ITheme & Record<string, any>, value: keyof IBorderRadius) => ({
60
90
  [variant]: theme.radius[value],
61
91
  }))
62
92
  })
63
93
  }
64
94
 
95
+
96
+
65
97
  borderProperties.forEach(property => {
66
98
  const variant = `border${capitalize(direction)}${capitalize(property)}`
67
99
 
68
- createVariant(variant, (theme, value: string) => ({
69
- [variant]: property == 'color' ? theme.colors[value] : theme.radius[value],
100
+ createVariant(variant, (theme: ITheme & Record<string, any>, value: string) => ({
101
+ [variant]: property == 'color' ? (theme.colors as Record<string, any>)[value] : (theme.radius as Record<string, any>)[value],
70
102
  }))
71
103
  })
72
104
  })
73
105
 
74
- createVariant('cursor', (theme, cursor: typeof cursorTypes[number]) => ({ cursor }))
106
+ borderXDirection.forEach(x => {
107
+ const variant = `border${capitalize(x)}Radius`
108
+
109
+ createVariant(variant, (theme: ITheme & Record<string, any>, value: keyof IBorderRadius) => ({
110
+ [variant]: theme.radius[value],
111
+ }))
112
+ })
113
+
114
+ borderYDirection.forEach(y => {
115
+ const variant = `border${capitalize(y)}Radius`
116
+
117
+ createVariant(variant, (theme: ITheme & Record<string, any>, value: keyof IBorderRadius) => ({
118
+ [variant]: theme.radius[value],
119
+ }))
120
+ })
121
+
122
+
123
+ createVariant('cursor', (theme: ITheme & Record<string, any>, cursor: typeof cursorTypes[number]) => ({ cursor }))
75
124
 
76
- createVariant('bg', (theme, color: Color) => ({
125
+ createVariant('bg', (theme: ITheme & Record<string, any>, color: Color) => ({
77
126
  backgroundColor: theme.colors[color],
78
127
  }))
79
128
 
80
- createVariant('effect', (theme, effect: keyof IEffects) => theme.effects[effect])
129
+ createVariant('effect', (theme: ITheme & Record<string, any>, effect: keyof IEffects) => theme.effects[effect])
81
130
 
82
- createVariant('scale', (theme, value: any) => ({
131
+ createVariant('scale', (theme: ITheme & Record<string, any>, value: any) => ({
83
132
  transform: theme.isBrowser ? `scale(${value})` : [{ 'scale': Number(value) }],
84
133
  }))
85
134
 
86
- createVariant('br', (theme, value: keyof IBorderRadius) => ({
135
+ createVariant('br', (theme: ITheme & Record<string, any>, value: keyof IBorderRadius) => ({
87
136
  borderRadius: theme.radius[value],
88
137
  }))
89
138
 
90
- createVariant('borderRadius', (theme, value: keyof IBorderRadius) => ({
139
+ createVariant('borderRadius', (theme: ITheme & Record<string, any>, value: keyof IBorderRadius) => ({
91
140
  borderRadius: theme.radius[value],
92
141
  }))
93
142
 
143
+ createVariant('size', (theme: ITheme & Record<string, any>, value: keyof ISizes) => ({
144
+ height: theme.size[value],
145
+ width: theme.size[value],
146
+ }))
147
+
94
148
  return dynamicVariants
95
149
  }
96
150
 
151
+ /**
152
+ * Pre-built dynamic variant registry used by the `CodeleapStyleRegistry`.
153
+ * Created once at module load; re-creation would lose the closed-over variant map.
154
+ */
97
155
  export const dynamicVariants = createDynamicVariants()
@@ -1,5 +1,13 @@
1
1
  import { Breakpoint } from '../types'
2
2
 
3
+ /**
4
+ * The four directional media-query helpers. Each returns a complete `@media` string
5
+ * that can be used as a nested style key.
6
+ * - `up` — `min-width` (at and above the breakpoint).
7
+ * - `down` — `max-width` (at and below the breakpoint).
8
+ * - `is` — exact match (`min-width` AND `max-width` at the same value).
9
+ * - `not` — negated exact match.
10
+ */
3
11
  export type Queries = {
4
12
  up: (breakpoint: string) => string
5
13
  down: (breakpoint: Breakpoint) => string
@@ -7,6 +15,11 @@ export type Queries = {
7
15
  not: (breakpoint: string) => string
8
16
  }
9
17
 
18
+ /**
19
+ * Extends `Queries` with `renderToPlatformQuery`, which inverts the query direction
20
+ * for React Native's platform-query rendering: `up` becomes `down`, `is` becomes `not`,
21
+ * etc. This is how responsive styles declared for web are translated to RN media conditions.
22
+ */
10
23
  export type MediaQueries = Queries & {
11
24
  renderToPlatformQuery: (props: Record<keyof Queries, any>) => string
12
25
  }
@@ -19,6 +32,11 @@ function getBreakpointValue(breakpoint: any, breakpoints: any) {
19
32
  return Infinity
20
33
  }
21
34
 
35
+ /**
36
+ * Produces a `MediaQueries` object bound to the provided `breakpoints` map.
37
+ * Unknown breakpoints (not in the map) fall back to `Infinity`, making `up(unknown)`
38
+ * match all widths and `down(unknown)` match none.
39
+ */
22
40
  export function createMediaQueries<T extends any>(breakpoints: T): MediaQueries {
23
41
  function getBreakpoint(breakpoint: any) {
24
42
  return getBreakpointValue(breakpoint, breakpoints)
@@ -1,8 +1,14 @@
1
1
  import { ICSS } from '../types'
2
2
  import { spacingVariants, spacingShortVariants, SpacingVariants, SpacingShortVariants } from '../types/spacing'
3
3
 
4
+ /** Function returned by a spacing factory entry; accepts a multiplier and returns a style fragment. */
4
5
  export type MultiplierFunction = (multiplier: number | string) => ICSS
5
6
 
7
+ /**
8
+ * Typed shape of the spacing object produced by `spacingFactory` for a given property.
9
+ * When `S` is `boolean`, the direction variants are long-form (`SpacingVariants`);
10
+ * when `S` is `string`, they are short-form (`SpacingShortVariants`).
11
+ */
6
12
  export type Spacings<T extends string, S = boolean> = {
7
13
  [Property in (S extends boolean ? SpacingVariants : SpacingShortVariants) as `${T}${string & Property}`]: MultiplierFunction;
8
14
  } & {
@@ -31,6 +37,14 @@ const shortPositionMap = {
31
37
  'b': 'Bottom'
32
38
  }
33
39
 
40
+ /**
41
+ * Builds a spacing variant map for a CSS property (`'padding'`, `'margin'`, `'p'`, `'m'`).
42
+ * Each key in the returned object is a callable that multiplies its argument by `base`.
43
+ * Passing `isShort = true` uses single-letter direction suffixes and maps them to their
44
+ * long-form equivalents before producing the CSS property name.
45
+ * The `'Horizontal'` and `'Vertical'` (or `'x'`/`'y'`) directions expand to two
46
+ * properties each; the empty-string direction produces all four sides at once.
47
+ */
34
48
  export function spacingFactory<T extends string>(
35
49
  base: number,
36
50
  spacingProperty: T,
@@ -39,7 +53,7 @@ export function spacingFactory<T extends string>(
39
53
  const baseProperty = isShort ? shortMapValues[spacingProperty as keyof typeof shortMapValues] : spacingProperty
40
54
  const positions = isShort ? spacingShortVariants : spacingVariants
41
55
 
42
- const spacings = {
56
+ const spacings: Record<string, (n: number | string) => Record<string, any>> = {
43
57
  [`${spacingProperty}`]: (n: number | string) => {
44
58
  if (n === 'auto') {
45
59
  return { [baseProperty]: 'auto' }
package/package.json.bak DELETED
@@ -1,30 +0,0 @@
1
- {
2
- "name": "@codeleap/styles",
3
- "version": "6.2.3",
4
- "main": "src/index.ts",
5
- "license": "UNLICENSED",
6
- "repository": {
7
- "url": "https://github.com/codeleap-uk/internal-libs-monorepo.git",
8
- "type": "git",
9
- "directory": "packages/styles"
10
- },
11
- "devDependencies": {
12
- "@codeleap/config": "workspace:*",
13
- "ts-node-dev": "^1.1.8"
14
- },
15
- "scripts": {
16
- "build": "echo 'No build needed'"
17
- },
18
- "peerDependencies": {
19
- "react": "19.1.0",
20
- "typescript": "5.5.2"
21
- },
22
- "dependencies": {
23
- "@fastify/deepmerge": "3.1.0",
24
- "@nanostores/react": "1.0.0",
25
- "js-sha256": "0.11.1",
26
- "lz-string": "1.5.0",
27
- "nanostores": "1.0.1",
28
- "rfdc": "1.4.1"
29
- }
30
- }