@codeleap/styles 6.3.0 → 7.0.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/dist/classes/Cacher.d.ts +87 -0
- package/dist/classes/Cacher.d.ts.map +1 -0
- package/dist/classes/StaleControl.d.ts +65 -0
- package/dist/classes/StaleControl.d.ts.map +1 -0
- package/dist/classes/StyleCache.d.ts +63 -0
- package/dist/classes/StyleCache.d.ts.map +1 -0
- package/dist/classes/StylePersistor.d.ts +52 -0
- package/dist/classes/StylePersistor.d.ts.map +1 -0
- package/dist/classes/StyleRegistry.d.ts +108 -0
- package/dist/classes/StyleRegistry.d.ts.map +1 -0
- package/dist/classes/index.d.ts +3 -0
- package/dist/classes/index.d.ts.map +1 -0
- package/dist/constants.d.ts +22 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/hooks/index.d.ts +5 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/useCompositionStyles.d.ts +12 -0
- package/dist/hooks/useCompositionStyles.d.ts.map +1 -0
- package/dist/hooks/useNestedStylesByKey.d.ts +11 -0
- package/dist/hooks/useNestedStylesByKey.d.ts.map +1 -0
- package/dist/hooks/useStyleObserver.d.ts +9 -0
- package/dist/hooks/useStyleObserver.d.ts.map +1 -0
- package/dist/hooks/useTheme.d.ts +19 -0
- package/dist/hooks/useTheme.d.ts.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/lib/calc.d.ts +27 -0
- package/dist/lib/calc.d.ts.map +1 -0
- package/dist/lib/createStyles.d.ts +30 -0
- package/dist/lib/createStyles.d.ts.map +1 -0
- package/dist/lib/createTheme.d.ts +28 -0
- package/dist/lib/createTheme.d.ts.map +1 -0
- package/dist/lib/cssVariables.d.ts +35 -0
- package/dist/lib/cssVariables.d.ts.map +1 -0
- package/dist/lib/index.d.ts +5 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/theme/generateColorScheme.d.ts +22 -0
- package/dist/theme/generateColorScheme.d.ts.map +1 -0
- package/dist/theme/index.d.ts +4 -0
- package/dist/theme/index.d.ts.map +1 -0
- package/dist/theme/themeStore.d.ts +106 -0
- package/dist/theme/themeStore.d.ts.map +1 -0
- package/dist/theme/validateTheme.d.ts +19 -0
- package/dist/theme/validateTheme.d.ts.map +1 -0
- package/dist/tools/colors.d.ts +70 -0
- package/dist/tools/colors.d.ts.map +1 -0
- package/dist/tools/deepClone.d.ts +7 -0
- package/dist/tools/deepClone.d.ts.map +1 -0
- package/dist/tools/deepmerge.d.ts +13 -0
- package/dist/tools/deepmerge.d.ts.map +1 -0
- package/dist/tools/hashKey.d.ts +8 -0
- package/dist/tools/hashKey.d.ts.map +1 -0
- package/dist/tools/index.d.ts +7 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/minifier.d.ts +24 -0
- package/dist/tools/minifier.d.ts.map +1 -0
- package/dist/tools/multiplierProperty.d.ts +4 -0
- package/dist/tools/multiplierProperty.d.ts.map +1 -0
- package/dist/types/cache.d.ts +12 -0
- package/dist/types/cache.d.ts.map +1 -0
- package/dist/types/component.d.ts +58 -0
- package/dist/types/component.d.ts.map +1 -0
- package/dist/types/core.d.ts +77 -0
- package/dist/types/core.d.ts.map +1 -0
- package/dist/types/icon.d.ts +15 -0
- package/dist/types/icon.d.ts.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/spacing.d.ts +28 -0
- package/dist/types/spacing.d.ts.map +1 -0
- package/dist/types/store.d.ts +12 -0
- package/dist/types/store.d.ts.map +1 -0
- package/dist/types/style.d.ts +42 -0
- package/dist/types/style.d.ts.map +1 -0
- package/dist/types/theme.d.ts +109 -0
- package/dist/types/theme.d.ts.map +1 -0
- package/dist/utils.d.ts +40 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/variants/borderCreator.d.ts +22 -0
- package/dist/variants/borderCreator.d.ts.map +1 -0
- package/dist/variants/createAppVariants.d.ts +18 -0
- package/dist/variants/createAppVariants.d.ts.map +1 -0
- package/dist/variants/defaultVariants.d.ts +140 -0
- package/dist/variants/defaultVariants.d.ts.map +1 -0
- package/dist/variants/dynamicVariants.d.ts +43 -0
- package/dist/variants/dynamicVariants.d.ts.map +1 -0
- package/dist/variants/index.d.ts +7 -0
- package/dist/variants/index.d.ts.map +1 -0
- package/dist/variants/mediaQuery.d.ts +30 -0
- package/dist/variants/mediaQuery.d.ts.map +1 -0
- package/dist/variants/spacing.d.ts +26 -0
- package/dist/variants/spacing.d.ts.map +1 -0
- package/package.json +19 -5
- package/src/classes/Cacher.ts +9 -9
- package/src/classes/StaleControl.ts +1 -1
- package/src/classes/StyleCache.ts +9 -3
- package/src/classes/StylePersistor.ts +11 -0
- package/src/classes/StyleRegistry.ts +124 -43
- package/src/classes/tests/StyleRegistry.spec.ts +169 -0
- package/src/constants.ts +14 -0
- package/src/hooks/useCompositionStyles.ts +9 -7
- package/src/hooks/useNestedStylesByKey.ts +8 -0
- package/src/hooks/useStyleObserver.ts +6 -5
- package/src/hooks/useTheme.ts +14 -0
- package/src/lib/calc.ts +13 -0
- package/src/lib/createStyles.ts +35 -4
- package/src/lib/createTheme.ts +74 -25
- package/src/lib/cssVariables.ts +74 -0
- package/src/lib/index.ts +2 -1
- package/src/lib/tests/createStyles.spec.ts +80 -23
- package/src/lib/tests/createStylesWithContext.spec.ts +108 -0
- package/src/tests/theme.ts +6 -2
- package/src/theme/generateColorScheme.ts +13 -10
- package/src/theme/tests/themeStore.spec.ts +38 -37
- package/src/theme/themeStore.ts +10 -7
- package/src/theme/validateTheme.ts +1 -1
- package/src/tools/colors.ts +24 -36
- package/src/tools/deepClone.ts +3 -5
- package/src/tools/deepmerge.ts +8 -6
- package/src/tools/hashKey.ts +4 -5
- package/src/tools/minifier.ts +11 -12
- package/src/tools/tests/deepClone.spec.ts +2 -2
- package/src/types/cache.ts +10 -0
- package/src/types/component.ts +41 -5
- package/src/types/core.ts +66 -6
- package/src/types/icon.ts +11 -0
- package/src/types/spacing.ts +21 -0
- package/src/types/store.ts +6 -0
- package/src/types/style.ts +37 -10
- package/src/types/theme.ts +37 -1
- package/src/utils.ts +34 -4
- package/src/variants/borderCreator.ts +14 -5
- package/src/variants/createAppVariants.ts +11 -0
- package/src/variants/defaultVariants.ts +28 -8
- package/src/variants/dynamicVariants.ts +76 -18
- package/src/variants/mediaQuery.ts +18 -0
- package/src/variants/spacing.ts +15 -1
- package/package.json.bak +0 -30
package/src/types/cache.ts
CHANGED
|
@@ -1,2 +1,12 @@
|
|
|
1
1
|
|
|
2
|
+
/**
|
|
3
|
+
* Discriminant union identifying the six cache buckets managed by `StyleCache`.
|
|
4
|
+
* Each bucket stores a different stage of the style-resolution pipeline:
|
|
5
|
+
* - `variants` — per-component variant merge results (persisted)
|
|
6
|
+
* - `common` — shared/common variant lookups (persisted)
|
|
7
|
+
* - `components` — final merged component output (persisted)
|
|
8
|
+
* - `styles` — `createStyles` call results (in-memory)
|
|
9
|
+
* - `compositions` — composition style resolution (in-memory)
|
|
10
|
+
* - `responsive` — responsive breakpoint style resolution (in-memory)
|
|
11
|
+
*/
|
|
2
12
|
export type CacheType = 'variants' | 'common' | 'styles' | 'compositions' | 'responsive' | 'components'
|
package/src/types/component.ts
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
|
-
import { AnyRecord, IJSX } from './core'
|
|
1
|
+
import { AnyRecord, ICSS, IJSX } from './core'
|
|
2
2
|
import { StyleProp, VariantStyleSheet } from './style'
|
|
3
3
|
import React, { JSX } from 'react'
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Injects a typed `style` prop onto a component based on the composition keys
|
|
7
|
+
* inferred from the variant stylesheet. Primarily used when a component's style
|
|
8
|
+
* prop shape must mirror its variant composition keys exactly.
|
|
9
|
+
*/
|
|
10
|
+
export type StyledProps<VariantStyles extends AnyRecord> = {
|
|
11
|
+
style?: StyleProp<
|
|
12
|
+
VariantStyles[keyof VariantStyles] extends Partial<Record<infer K, any>> ? K extends string ? K : never : never,
|
|
13
|
+
keyof VariantStyles
|
|
14
|
+
>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Merges external `Props` with variant-aware `style`. If `Props` already declares a
|
|
19
|
+
* `style` prop whose inner composition type can be inferred, that composition is
|
|
20
|
+
* preserved; otherwise the composition is derived from `VariantStyles`.
|
|
21
|
+
*/
|
|
5
22
|
export type PropsWithVariants<Props extends AnyRecord, VariantStyles extends AnyRecord> = Omit<Props, 'style'> & {
|
|
6
23
|
style?: StyleProp<
|
|
7
24
|
Extract<keyof Props, 'style'> extends never ? VariantStyles[keyof VariantStyles] : (
|
|
@@ -11,11 +28,18 @@ export type PropsWithVariants<Props extends AnyRecord, VariantStyles extends Any
|
|
|
11
28
|
>
|
|
12
29
|
}
|
|
13
30
|
|
|
31
|
+
/** Unconstrained function type used for variant callbacks and other callbacks. */
|
|
14
32
|
export type AnyFunction = (...args: any[]) => any
|
|
15
33
|
|
|
16
|
-
|
|
34
|
+
/** Resolved props type for a component that has been bound to a variant stylesheet. */
|
|
35
|
+
export type StyledComponentProps<Props extends AnyRecord, VariantStyles extends AnyRecord> = PropsWithVariants<Props, VariantStyles>
|
|
17
36
|
|
|
18
|
-
|
|
37
|
+
/**
|
|
38
|
+
* A component function that accepts variant-aware style props, plus the registry
|
|
39
|
+
* metadata fields (`styleRegistryName`, `elements`, `rootElement`) used by the
|
|
40
|
+
* `CodeleapStyleRegistry` to resolve composition and responsive styles.
|
|
41
|
+
*/
|
|
42
|
+
export type StyledComponent<VariantStyles extends AnyRecord, Props extends AnyRecord = AnyRecord> = (
|
|
19
43
|
(props: StyledComponentProps<Props, VariantStyles>) => IJSX
|
|
20
44
|
) & {
|
|
21
45
|
styleRegistryName?: string
|
|
@@ -23,19 +47,31 @@ export type StyledComponent<VariantStyles, Props extends AnyRecord = AnyRecord>
|
|
|
23
47
|
rootElement?: string
|
|
24
48
|
}
|
|
25
49
|
|
|
26
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Static attributes attached to every component created by the style system.
|
|
52
|
+
* `withVariantTypes` narrows the component's style prop to a specific variant stylesheet
|
|
53
|
+
* without changing the runtime behaviour — it is a TypeScript-only helper.
|
|
54
|
+
*/
|
|
55
|
+
export type GenericStyledComponentAttributes<Props extends AnyRecord> = {
|
|
27
56
|
styleRegistryName?: string
|
|
28
57
|
withVariantTypes?: <VariantStyles extends VariantStyleSheet>(variants: VariantStyles) => StyledComponent<VariantStyles, Props>
|
|
29
58
|
elements?: string[]
|
|
30
59
|
rootElement?: string
|
|
31
60
|
}
|
|
32
61
|
|
|
62
|
+
/**
|
|
63
|
+
* A component that hasn't been bound to a specific variant stylesheet yet but carries
|
|
64
|
+
* the registry metadata needed for the style system to work at runtime.
|
|
65
|
+
*/
|
|
33
66
|
export type GenericStyledComponent<
|
|
34
67
|
Props extends AnyRecord
|
|
35
68
|
> = ((props: Props) => IJSX) & GenericStyledComponentAttributes<Props>
|
|
36
69
|
|
|
70
|
+
/** Escape-hatch alias for `GenericStyledComponent<any>` used in registry maps. */
|
|
37
71
|
export type AnyStyledComponent = GenericStyledComponent<any>
|
|
38
72
|
|
|
73
|
+
/** Component function type that optionally accepts `defaultProps` — mirrors React's pattern. */
|
|
39
74
|
export type ComponentWithDefaultProps<P> = ((props: P) => JSX.Element) & { defaultProps?: Partial<P> }
|
|
40
75
|
|
|
41
|
-
|
|
76
|
+
/** Combines `ComponentWithDefaultProps` with the style-registry metadata attributes. */
|
|
77
|
+
export type StyledComponentWithProps<Props extends AnyRecord> = ComponentWithDefaultProps<Props> & GenericStyledComponentAttributes<Props>
|
package/src/types/core.ts
CHANGED
|
@@ -1,40 +1,100 @@
|
|
|
1
|
-
|
|
2
1
|
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Declaration-merging target for the app's CSS/style property bag.
|
|
5
|
+
* Platform packages (`@codeleap/web`, `@codeleap/mobile`) augment this interface
|
|
6
|
+
* with the actual CSS or React Native style properties they support.
|
|
7
|
+
*/
|
|
3
8
|
export interface ICSS {
|
|
4
9
|
|
|
5
10
|
}
|
|
6
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Declaration-merging target for the resolved `AppTheme` object available at runtime.
|
|
14
|
+
* Consumer apps augment this so that `useTheme()` returns a fully typed theme without
|
|
15
|
+
* explicit generics at every callsite.
|
|
16
|
+
*/
|
|
7
17
|
export interface ITheme {
|
|
8
18
|
|
|
9
19
|
}
|
|
10
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Declaration-merging target for the JSX element type used by the platform.
|
|
23
|
+
* Web augments this with `React.ReactElement`; mobile augments with React Native's
|
|
24
|
+
* equivalent. Allows cross-platform component return types to be typed correctly.
|
|
25
|
+
*/
|
|
11
26
|
export interface IJSX {
|
|
12
27
|
|
|
13
28
|
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Declaration-merging target for the app's named breakpoint map.
|
|
32
|
+
* Augment with `{ sm: unknown; md: unknown; lg: unknown }` (or your own breakpoints)
|
|
33
|
+
* so that `Breakpoint` and the responsive style API become fully typed.
|
|
34
|
+
*/
|
|
14
35
|
export interface IBreakpoints {
|
|
15
36
|
|
|
16
37
|
}
|
|
17
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Declaration-merging target for the app's color token map.
|
|
41
|
+
* Augment with the flattened color keys from your theme (e.g., `{ primary: string }`)
|
|
42
|
+
* so that `color:`, `bg:`, and `borderColor:` variants are narrowed to valid tokens.
|
|
43
|
+
*/
|
|
18
44
|
export interface IColors {
|
|
19
|
-
|
|
45
|
+
|
|
20
46
|
}
|
|
21
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Declaration-merging target for the app's border-radius token map.
|
|
50
|
+
* Augment with keys from `theme.radius` so that `borderRadius:` and `br:` variants
|
|
51
|
+
* accept only the defined radius tokens.
|
|
52
|
+
*/
|
|
22
53
|
export interface IBorderRadius {
|
|
23
|
-
|
|
54
|
+
|
|
24
55
|
}
|
|
25
56
|
|
|
57
|
+
/**
|
|
58
|
+
* Declaration-merging target for app-defined style variants registered via
|
|
59
|
+
* `createAppVariants`. Augment so that custom variant names are accepted by `StyleProp`.
|
|
60
|
+
*/
|
|
26
61
|
export interface IAppVariants {
|
|
27
|
-
|
|
62
|
+
|
|
28
63
|
}
|
|
29
64
|
|
|
65
|
+
/**
|
|
66
|
+
* Declaration-merging target for the app's named size tokens (from `theme.size`).
|
|
67
|
+
* Augment so that the `size:` dynamic variant is narrowed to valid size keys.
|
|
68
|
+
*/
|
|
69
|
+
export interface ISizes {
|
|
70
|
+
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Declaration-merging target for a single effect object (e.g., a shadow or filter
|
|
75
|
+
* definition). Shape is platform-specific; augmented by platform packages.
|
|
76
|
+
*/
|
|
30
77
|
export interface IEffect {
|
|
31
|
-
|
|
78
|
+
|
|
32
79
|
}
|
|
33
80
|
|
|
81
|
+
/**
|
|
82
|
+
* Declaration-merging target for the named effects map (`theme.effects`).
|
|
83
|
+
* Augment with `{ shadow: IEffect; ... }` so that `effect:` variants are narrowed.
|
|
84
|
+
*/
|
|
34
85
|
export interface IEffects {
|
|
35
|
-
|
|
86
|
+
|
|
36
87
|
}
|
|
37
88
|
|
|
89
|
+
/** Convenience alias for an unconstrained key-value record. */
|
|
38
90
|
export type AnyRecord = Record<string, any>
|
|
39
91
|
|
|
92
|
+
/** Union of all breakpoint names defined via `IBreakpoints` augmentation. */
|
|
40
93
|
export type Breakpoint = keyof IBreakpoints
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Runtime context passed to context-aware style factories created with
|
|
97
|
+
* `createStylesWithContext`. Keys are feature flags or numeric state values
|
|
98
|
+
* (e.g., `{ isDisabled: true, tabIndex: 2 }`).
|
|
99
|
+
*/
|
|
100
|
+
export type ComponentContext = Record<string, boolean|number>
|
package/src/types/icon.ts
CHANGED
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Declaration-merging target for the app's named icon set.
|
|
5
|
+
* Augment with `{ arrowLeft: unknown; close: unknown; ... }` to produce a typed
|
|
6
|
+
* `AppIcon` union. Shapes are platform-defined; only the keys matter for typing.
|
|
7
|
+
*/
|
|
3
8
|
export interface AppIcons {
|
|
4
9
|
|
|
5
10
|
}
|
|
11
|
+
|
|
12
|
+
/** Union of all named icon keys declared in `AppIcons`. Resolves to `never` if not augmented. */
|
|
6
13
|
export type AppIcon = keyof AppIcons
|
|
7
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Accepted value for any `icon` prop: either a named `AppIcon` key or an inline
|
|
17
|
+
* URL in the form `url:<href>` (e.g., `"url:https://example.com/icon.svg"`).
|
|
18
|
+
*/
|
|
8
19
|
export type IconProp = AppIcon | `url:${string}`
|
package/src/types/spacing.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Long-form spacing direction suffixes used with `margin` and `padding`.
|
|
3
|
+
* The empty string `''` represents the un-suffixed property (e.g., `padding`).
|
|
4
|
+
*/
|
|
1
5
|
export const spacingVariants = [
|
|
2
6
|
'Vertical',
|
|
3
7
|
'Horizontal',
|
|
@@ -8,6 +12,11 @@ export const spacingVariants = [
|
|
|
8
12
|
'',
|
|
9
13
|
] as const
|
|
10
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Short-form spacing direction suffixes used with `m` and `p`.
|
|
17
|
+
* Maps to the long-form via `shortPositionMap` inside `spacingFactory`.
|
|
18
|
+
* The empty string `''` represents the un-suffixed property (e.g., `p`).
|
|
19
|
+
*/
|
|
11
20
|
export const spacingShortVariants = [
|
|
12
21
|
'y',
|
|
13
22
|
'x',
|
|
@@ -18,15 +27,27 @@ export const spacingShortVariants = [
|
|
|
18
27
|
'',
|
|
19
28
|
] as const
|
|
20
29
|
|
|
30
|
+
/** Union of all long-form direction suffixes (e.g., `'Vertical'`, `''`). */
|
|
21
31
|
export type SpacingVariants = typeof spacingVariants[number]
|
|
22
32
|
|
|
33
|
+
/** Union of all short-form direction suffixes (e.g., `'x'`, `''`). */
|
|
23
34
|
export type SpacingShortVariants = typeof spacingShortVariants[number]
|
|
24
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Valid multiplier values for spacing and inset variant strings.
|
|
38
|
+
* `'auto'` maps to the CSS `auto` keyword; a number is scaled by `baseSpacing`;
|
|
39
|
+
* `''` is the empty-string terminator used in template literal unions.
|
|
40
|
+
*/
|
|
25
41
|
export type Multiplier =
|
|
26
42
|
| 'auto'
|
|
27
43
|
| number
|
|
28
44
|
| ''
|
|
29
45
|
|
|
46
|
+
/**
|
|
47
|
+
* All valid spacing variant strings accepted by `StyleProp`.
|
|
48
|
+
* Examples: `"paddingVertical:2"`, `"mt:1"`, `"gap:auto"`.
|
|
49
|
+
* The numeric part is multiplied by `baseSpacing` at resolution time.
|
|
50
|
+
*/
|
|
30
51
|
export type Spacing =
|
|
31
52
|
| `padding${SpacingVariants}:${Multiplier}`
|
|
32
53
|
| `margin${SpacingVariants}:${Multiplier}`
|
package/src/types/store.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal key-value storage contract used by `StylePersistor` and `Cache`.
|
|
3
|
+
* Designed to be implemented by any synchronous store (e.g., MMKV, AsyncStorage
|
|
4
|
+
* adapters, or `localStorage` wrappers). `getItem` must return `null` (not `undefined`)
|
|
5
|
+
* when the key is absent — callers use `?? null` guards based on that assumption.
|
|
6
|
+
*/
|
|
1
7
|
export interface StateStorage {
|
|
2
8
|
getItem: (name: string) => any | null
|
|
3
9
|
setItem: (name: string, value: any) => void
|
package/src/types/style.ts
CHANGED
|
@@ -10,6 +10,12 @@ type Inset =
|
|
|
10
10
|
| `left:${Multiplier}`
|
|
11
11
|
| `right:${Multiplier}`
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Union of all built-in variant string literals accepted anywhere a style variant
|
|
15
|
+
* name is valid: spacing shorthands, inset offsets, dynamic token variants (color,
|
|
16
|
+
* border, cursor, etc.), the static `defaultVariants` keys, app-defined variants,
|
|
17
|
+
* and named effect tokens.
|
|
18
|
+
*/
|
|
13
19
|
export type CommonVariants =
|
|
14
20
|
Spacing |
|
|
15
21
|
Inset |
|
|
@@ -18,12 +24,13 @@ export type CommonVariants =
|
|
|
18
24
|
keyof IAppVariants |
|
|
19
25
|
`effect:${keyof IEffects}`
|
|
20
26
|
|
|
21
|
-
type StyleAtom<Composition =
|
|
27
|
+
type StyleAtom<Composition extends string = string, Variants = string, HasBreakpoints = string, HasComposition = string> =
|
|
22
28
|
ICSS |
|
|
23
29
|
Variants |
|
|
24
30
|
CommonVariants |
|
|
25
31
|
boolean |
|
|
26
32
|
null |
|
|
33
|
+
undefined |
|
|
27
34
|
'' |
|
|
28
35
|
|
|
29
36
|
(HasBreakpoints extends string ? {
|
|
@@ -35,29 +42,49 @@ type StyleAtom<Composition = AnyRecord, Variants = string, HasBreakpoints = stri
|
|
|
35
42
|
} : null) |
|
|
36
43
|
(HasComposition extends string ?
|
|
37
44
|
Partial<Record<
|
|
38
|
-
|
|
39
|
-
StyleAtom<
|
|
40
|
-
StyleAtom<
|
|
45
|
+
Composition,
|
|
46
|
+
StyleAtom<Composition, Variants, boolean, boolean> |
|
|
47
|
+
StyleAtom<Composition, Variants, boolean, boolean>[]
|
|
41
48
|
>>
|
|
42
49
|
: null
|
|
43
50
|
)
|
|
44
51
|
| Array<Variants | ICSS | Partial<
|
|
45
52
|
Record<
|
|
46
|
-
|
|
47
|
-
StyleAtom<
|
|
48
|
-
StyleAtom<
|
|
53
|
+
Composition,
|
|
54
|
+
StyleAtom<Composition, Variants, boolean, boolean> |
|
|
55
|
+
StyleAtom<Composition, Variants, boolean, boolean>[]
|
|
49
56
|
>
|
|
50
57
|
>>
|
|
51
58
|
|
|
59
|
+
/**
|
|
60
|
+
* The value accepted by any component's `style` prop. Supports:
|
|
61
|
+
* - A raw `ICSS` object applied to the root element.
|
|
62
|
+
* - A variant string (or array of strings) resolved through the registry.
|
|
63
|
+
* - A composition object mapping sub-element names to their own `StyleAtom`.
|
|
64
|
+
* - A `{ breakpoints: { [key]: StyleAtom } }` object for responsive overrides.
|
|
65
|
+
* - An array mixing any of the above — processed left-to-right, last write wins.
|
|
66
|
+
*/
|
|
52
67
|
export type StyleProp<
|
|
53
|
-
Composition =
|
|
68
|
+
Composition extends string = string,
|
|
54
69
|
Variants = string
|
|
55
70
|
> = StyleAtom<Composition, Variants> | StyleAtom<Composition, Variants>[]
|
|
56
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Loosely typed variant stylesheet — a record mapping variant names to their style
|
|
74
|
+
* definitions. Used as the input to `createStyles` and `registerVariants`.
|
|
75
|
+
*/
|
|
57
76
|
export type VariantStyleSheet = Record<string, any>
|
|
58
77
|
|
|
59
|
-
|
|
78
|
+
/** Alias for `StyleProp<T>` scoped to a specific set of composition keys. */
|
|
79
|
+
export type StyledProp<T extends string> = StyleProp<T>
|
|
60
80
|
|
|
81
|
+
/**
|
|
82
|
+
* Post-processing function that receives the merged theme and fully resolved variant
|
|
83
|
+
* styles and returns a (potentially transformed) style record. Useful for injecting
|
|
84
|
+
* cross-variant logic such as conditional overrides that depend on multiple variant
|
|
85
|
+
* states simultaneously.
|
|
86
|
+
*/
|
|
61
87
|
export type StyleAggregator<T extends string = any> = (theme: ITheme, style: Record< T, ICSS>) => Record< T, ICSS>
|
|
62
88
|
|
|
63
|
-
|
|
89
|
+
/** Typed record mapping composition/element keys to their resolved `ICSS` values. */
|
|
90
|
+
export type StyleRecord<K extends string> = Record<K, ICSS>
|
package/src/types/theme.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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:
|
|
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
|
|