@codeleap/styles 4.0.1

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 (94) hide show
  1. package/dist/index.d.ts +5 -0
  2. package/dist/index.js +26 -0
  3. package/dist/lib/Cacher.d.ts +37 -0
  4. package/dist/lib/Cacher.js +104 -0
  5. package/dist/lib/StaleControl.d.ts +21 -0
  6. package/dist/lib/StaleControl.js +78 -0
  7. package/dist/lib/StyleCache.d.ts +20 -0
  8. package/dist/lib/StyleCache.js +52 -0
  9. package/dist/lib/StylePersistor.d.ts +13 -0
  10. package/dist/lib/StylePersistor.js +22 -0
  11. package/dist/lib/StyleRegistry.d.ts +46 -0
  12. package/dist/lib/StyleRegistry.js +499 -0
  13. package/dist/lib/borderCreator.d.ts +11 -0
  14. package/dist/lib/borderCreator.js +44 -0
  15. package/dist/lib/constants.d.ts +5 -0
  16. package/dist/lib/constants.js +8 -0
  17. package/dist/lib/createAppVariants.d.ts +6 -0
  18. package/dist/lib/createAppVariants.js +9 -0
  19. package/dist/lib/createStyles.d.ts +4 -0
  20. package/dist/lib/createStyles.js +27 -0
  21. package/dist/lib/createTheme.d.ts +7 -0
  22. package/dist/lib/createTheme.js +76 -0
  23. package/dist/lib/defaultVariants.d.ts +178 -0
  24. package/dist/lib/defaultVariants.js +179 -0
  25. package/dist/lib/dynamicVariants.d.ts +12 -0
  26. package/dist/lib/dynamicVariants.js +88 -0
  27. package/dist/lib/hashKey.d.ts +1 -0
  28. package/dist/lib/hashKey.js +14 -0
  29. package/dist/lib/hooks.d.ts +8 -0
  30. package/dist/lib/hooks.js +76 -0
  31. package/dist/lib/index.d.ts +10 -0
  32. package/dist/lib/index.js +37 -0
  33. package/dist/lib/mediaQuery.d.ts +11 -0
  34. package/dist/lib/mediaQuery.js +65 -0
  35. package/dist/lib/minifier.d.ts +6 -0
  36. package/dist/lib/minifier.js +21 -0
  37. package/dist/lib/multiplierProperty.d.ts +3 -0
  38. package/dist/lib/multiplierProperty.js +13 -0
  39. package/dist/lib/spacing.d.ts +11 -0
  40. package/dist/lib/spacing.js +104 -0
  41. package/dist/lib/themeStore.d.ts +7 -0
  42. package/dist/lib/themeStore.js +9 -0
  43. package/dist/lib/utils.d.ts +8 -0
  44. package/dist/lib/utils.js +172 -0
  45. package/dist/lib/validateTheme.d.ts +2 -0
  46. package/dist/lib/validateTheme.js +68 -0
  47. package/dist/types/cache.d.ts +1 -0
  48. package/dist/types/cache.js +2 -0
  49. package/dist/types/component.d.ts +25 -0
  50. package/dist/types/component.js +2 -0
  51. package/dist/types/core.d.ts +20 -0
  52. package/dist/types/core.js +2 -0
  53. package/dist/types/icon.d.ts +4 -0
  54. package/dist/types/icon.js +3 -0
  55. package/dist/types/index.d.ts +5 -0
  56. package/dist/types/index.js +21 -0
  57. package/dist/types/spacing.d.ts +6 -0
  58. package/dist/types/spacing.js +21 -0
  59. package/dist/types/style.d.ts +13 -0
  60. package/dist/types/style.js +2 -0
  61. package/dist/types/theme.d.ts +62 -0
  62. package/dist/types/theme.js +2 -0
  63. package/package.json +34 -0
  64. package/src/index.ts +7 -0
  65. package/src/lib/Cacher.ts +131 -0
  66. package/src/lib/StaleControl.ts +73 -0
  67. package/src/lib/StyleCache.ts +69 -0
  68. package/src/lib/StylePersistor.ts +28 -0
  69. package/src/lib/StyleRegistry.ts +549 -0
  70. package/src/lib/borderCreator.ts +42 -0
  71. package/src/lib/constants.ts +6 -0
  72. package/src/lib/createAppVariants.ts +12 -0
  73. package/src/lib/createStyles.ts +32 -0
  74. package/src/lib/createTheme.ts +89 -0
  75. package/src/lib/defaultVariants.ts +180 -0
  76. package/src/lib/dynamicVariants.ts +83 -0
  77. package/src/lib/hashKey.ts +12 -0
  78. package/src/lib/hooks.ts +52 -0
  79. package/src/lib/index.ts +11 -0
  80. package/src/lib/mediaQuery.ts +70 -0
  81. package/src/lib/minifier.ts +20 -0
  82. package/src/lib/multiplierProperty.ts +13 -0
  83. package/src/lib/spacing.ts +83 -0
  84. package/src/lib/themeStore.ts +14 -0
  85. package/src/lib/utils.ts +74 -0
  86. package/src/lib/validateTheme.ts +22 -0
  87. package/src/types/cache.ts +2 -0
  88. package/src/types/component.ts +40 -0
  89. package/src/types/core.ts +40 -0
  90. package/src/types/icon.ts +8 -0
  91. package/src/types/index.ts +5 -0
  92. package/src/types/spacing.ts +35 -0
  93. package/src/types/style.ts +41 -0
  94. package/src/types/theme.ts +77 -0
@@ -0,0 +1,13 @@
1
+ import { DynamicVariants } from '../lib/dynamicVariants';
2
+ import { DefaultVariants } from '../lib/defaultVariants';
3
+ import { AnyRecord, IAppVariants, IBreakpoints, ICSS, IEffects } from './core';
4
+ import { Multiplier, Spacing } from './spacing';
5
+ type Inset = `top:${Multiplier}` | `bottom:${Multiplier}` | `left:${Multiplier}` | `right:${Multiplier}`;
6
+ export type CommonVariants = Spacing | Inset | DynamicVariants | keyof DefaultVariants | keyof IAppVariants | `effect:${keyof IEffects}`;
7
+ type StyleAtom<Composition = AnyRecord, Variants = string, HasBreakpoints = string, HasComposition = string> = ICSS | Variants | CommonVariants | boolean | null | '' | (HasBreakpoints extends string ? {
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);
10
+ export type StyleProp<Composition = AnyRecord, Variants = string> = StyleAtom<Composition, Variants> | StyleAtom<Composition, Variants>[];
11
+ export type VariantStyleSheet = Record<string, any>;
12
+ export type StyledProp<T extends string> = StyleProp<Record<T, ICSS>>;
13
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,62 @@
1
+ import { BorderCreator } from '../lib/borderCreator';
2
+ import { MediaQueries } from '../lib/mediaQuery';
3
+ import type { DefaultVariants } from '../lib/defaultVariants';
4
+ import { MultiplierFunction, Spacings } from '../lib/spacing';
5
+ import { ICSS, IEffect } from './core';
6
+ type AnyMap = {
7
+ [key: string]: any;
8
+ };
9
+ type ColorMap = {
10
+ [key: string]: string;
11
+ };
12
+ type BreakpointMap = {
13
+ [key: string]: number;
14
+ };
15
+ type EffectsMap = {
16
+ [key: string]: IEffect;
17
+ };
18
+ export type SpacingMap = Spacings<'margin'> & Spacings<'padding'> & Spacings<'m', string> & Spacings<'p', string> & {
19
+ gap: MultiplierFunction;
20
+ };
21
+ export type InsetMap = {
22
+ bottom: MultiplierFunction;
23
+ top: MultiplierFunction;
24
+ left: MultiplierFunction;
25
+ right: MultiplierFunction;
26
+ };
27
+ export type Theme = {
28
+ colors: ColorMap;
29
+ alternateColors?: {
30
+ [key: string]: ColorMap;
31
+ };
32
+ breakpoints?: BreakpointMap;
33
+ baseSpacing?: number;
34
+ presets?: AnyMap;
35
+ borderRadius?: AnyMap;
36
+ effects?: EffectsMap;
37
+ typography: AnyMap;
38
+ icons: AnyMap;
39
+ values?: AnyMap;
40
+ };
41
+ export type DefaultColorSchemeName = 'default';
42
+ export type ColorScheme<T extends Theme = Theme> = DefaultColorSchemeName | keyof T['alternateColors'];
43
+ export type AppTheme<T extends Theme> = {
44
+ colors: T['colors'];
45
+ breakpoints: T['breakpoints'];
46
+ setColorScheme: (colorScheme: ColorScheme<T>) => void;
47
+ currentColorScheme: ColorScheme<T>;
48
+ spacing: SpacingMap;
49
+ presets: DefaultVariants & T['presets'];
50
+ borderRadius: T['borderRadius'];
51
+ media: MediaQueries;
52
+ effects: T['effects'];
53
+ border: BorderCreator;
54
+ typography: T['typography'];
55
+ icons: T['icons'];
56
+ values: T['values'];
57
+ inset: InsetMap;
58
+ baseSpacing: number;
59
+ value: (multiplier: number) => number;
60
+ sized: (size: number | string) => ICSS;
61
+ };
62
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@codeleap/styles",
3
+ "version": "4.0.1",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "license": "UNLICENSED",
7
+ "repository": {
8
+ "url": "https://github.com/codeleap-uk/internal-libs-monorepo.git",
9
+ "type": "git",
10
+ "directory": "packages/styles"
11
+ },
12
+ "devDependencies": {
13
+ "@codeleap/config": "*",
14
+ "ts-node-dev": "1.1.8"
15
+ },
16
+ "scripts": {
17
+ "build": "tsc --build",
18
+ "lint": "eslint -c .eslintrc.js --fix \"./src/**/*.{ts,tsx,js,jsx}\"",
19
+ "test": "./node_modules/.bin/mocha --require ts-node/register src/tests/**/*.spec.ts",
20
+ "run-sc": "tsnd --transpile-only",
21
+ "test:watch": "./node_modules/.bin/mocha --require ts-node/register ./src/tests/**/*.spec.ts -w --watch-files ./src/tests/**/*.spec.ts",
22
+ "createStyleSheets": "yarn run-sc src/scripts/createStyleSheets.ts"
23
+ },
24
+ "peerDependencies": {
25
+ "typescript": "5.0.4",
26
+ "react": "18.1.0"
27
+ },
28
+ "dependencies": {
29
+ "@fastify/deepmerge": "1.3.0",
30
+ "js-sha256": "0.11.0",
31
+ "lz-string": "^1.5.0",
32
+ "zustand": "4.5.0"
33
+ }
34
+ }
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ import './lib/constants'
2
+ import './lib/utils'
3
+
4
+ export * from './lib'
5
+ export * from './types'
6
+
7
+ export { default as deepmerge } from '@fastify/deepmerge'
@@ -0,0 +1,131 @@
1
+ import { create } from 'zustand'
2
+ import { createJSONStorage, persist, StateStorage } from 'zustand/middleware'
3
+ import { AnyRecord } from '../types'
4
+ import { CacheType } from '../types/cache'
5
+ import { StyleConstants } from './constants'
6
+ import { hashKey } from './hashKey'
7
+
8
+ function getStaleTime() {
9
+ const time = 7
10
+
11
+ let currentTime = new Date()
12
+
13
+ currentTime.setDate(currentTime.getDate() + time)
14
+
15
+ return currentTime
16
+ }
17
+
18
+ type StorePersistor = {
19
+ cached: AnyRecord
20
+ cacheFor: (key: string, value: any) => void
21
+ staleTime: Date
22
+ reset: () => void
23
+ }
24
+
25
+ export class Cache<T extends any = any> {
26
+ cache: Record<string, T> = {}
27
+
28
+ persistor = null
29
+
30
+ store: StorePersistor = {
31
+ cached: {},
32
+ cacheFor: () => null,
33
+ reset: () => null,
34
+ staleTime: null
35
+ }
36
+
37
+ constructor(
38
+ public registryName: CacheType,
39
+ private storage: StateStorage = null,
40
+ public persistCache: boolean = !!storage,
41
+ ) {
42
+ if (!persistCache) return
43
+
44
+ this.createPersistor(registryName)
45
+
46
+ if (!StyleConstants.STORE_CACHE_ENABLED || !this.persistor) return
47
+
48
+ const currentTime = new Date()
49
+ const staleTime = new Date(this.store.staleTime)
50
+
51
+ const isStaled = currentTime > staleTime
52
+
53
+ if (isStaled) {
54
+ this.store.reset()
55
+ return
56
+ }
57
+
58
+ this.setCache(this.store.cached)
59
+ }
60
+
61
+ keyFor(cacheBaseKey: string, data: Array<any> | any) {
62
+ const values = [cacheBaseKey, data]
63
+
64
+ const cacheKey = hashKey(values)
65
+ const cachedValue = this.cache[cacheKey] ?? null
66
+
67
+ return {
68
+ key: cacheKey,
69
+ value: cachedValue,
70
+ }
71
+ }
72
+
73
+ cacheFor(key: string, cache: T) {
74
+ this.cache[key] = cache
75
+ if (this.persistCache) this.store.cacheFor(key, cache)
76
+ return cache
77
+ }
78
+
79
+ setCache(cache: Record<string, T>) {
80
+ this.cache = cache
81
+ }
82
+
83
+ clear() {
84
+ this.cache = {}
85
+ if (this.persistCache) this.persistor.persist.clearStorage()
86
+ }
87
+
88
+ createPersistor(registryName: string) {
89
+ if (!this.persistCache) return null
90
+
91
+ const persistor = create(persist<StorePersistor>(
92
+ (set, get) => ({
93
+ staleTime: getStaleTime(),
94
+ cached: {},
95
+ cacheFor: (key, value) => set(store => {
96
+ const cached = store.cached
97
+
98
+ cached[key] = value
99
+
100
+ return {
101
+ cached
102
+ }
103
+ }),
104
+ reset: () => set({
105
+ cached: {},
106
+ staleTime: getStaleTime()
107
+ })
108
+ }),
109
+ {
110
+ name: `@styles.caches.${registryName}`,
111
+ version: StyleConstants.STORES_PERSIST_VERSION,
112
+ migrate: (persistedState: StorePersistor, version) => {
113
+ if (version != StyleConstants.STORES_PERSIST_VERSION) {
114
+ persistedState.staleTime = getStaleTime()
115
+ persistedState.cached = {}
116
+
117
+ return persistedState
118
+ }
119
+
120
+ return persistedState as StorePersistor
121
+ },
122
+ storage: createJSONStorage(() => this.storage),
123
+ }
124
+ ))
125
+
126
+ this.persistor = persistor
127
+ this.store = persistor.getState()
128
+
129
+ return persistor
130
+ }
131
+ }
@@ -0,0 +1,73 @@
1
+
2
+ export class StaleControl {
3
+ private wiperId: NodeJS.Timer = null
4
+
5
+ constructor(
6
+ private staleTime: number = 60, // minutes
7
+ private staleTimeIdentifier: string = '//:',
8
+ private wiperInterval: number = 30 * 60 * 1000, // 30 minutes
9
+ ) {}
10
+
11
+ isStaled(value: string) {
12
+ const { staleTime } = this.extractStaleTime(value)
13
+
14
+ const currentTime = new Date()
15
+
16
+ const isStaled = currentTime > staleTime
17
+
18
+ return isStaled
19
+ }
20
+
21
+ insertStaleTime(value: string) {
22
+ let currentTime = new Date()
23
+
24
+ currentTime.setMinutes(currentTime.getMinutes() + this.staleTime)
25
+
26
+ const staleTime = currentTime.toISOString()
27
+
28
+ const valueWithStaleTime = `${value}${this.staleTimeIdentifier}${staleTime}`
29
+
30
+ return valueWithStaleTime
31
+ }
32
+
33
+ refreshStaleTime(value: string) {
34
+ const { value: extractedValue } = this.extractStaleTime(value)
35
+
36
+ const refreshedValue = this.insertStaleTime(extractedValue)
37
+
38
+ return refreshedValue
39
+ }
40
+
41
+ extractStaleTime(value: string) {
42
+ const [extractedValue, _staleTime] = value?.split(this.staleTimeIdentifier)
43
+
44
+ const staleTime = new Date(_staleTime)
45
+
46
+ return {
47
+ staleTime,
48
+ value: extractedValue,
49
+ }
50
+ }
51
+
52
+ /**
53
+ * wipe staled cache; verify isStaled values and remove
54
+ */
55
+ cacheWiper() {
56
+ throw new Error('Cache Wiper not implement')
57
+ }
58
+
59
+ registerCacheWiper() {
60
+ if (this.wiperId !== null) {
61
+ this.unregisterCacheWiper()
62
+ }
63
+
64
+ this.wiperId = setInterval(() => {
65
+ this.cacheWiper()
66
+ }, this.wiperInterval)
67
+ }
68
+
69
+ unregisterCacheWiper() {
70
+ clearInterval(this.wiperId)
71
+ this.wiperId = null
72
+ }
73
+ }
@@ -0,0 +1,69 @@
1
+ import { Cache } from './Cacher'
2
+ import { hashKey } from './hashKey'
3
+ import { StyleConstants } from './constants'
4
+ import { CacheType } from '../types/cache'
5
+ import { minifier } from './minifier'
6
+ import { StateStorage } from 'zustand/middleware'
7
+
8
+ export class StyleCache {
9
+ baseKey: string
10
+
11
+ styles = new Cache('styles')
12
+ compositions = new Cache('compositions')
13
+ responsive = new Cache('responsive')
14
+
15
+ variants: Cache
16
+ common: Cache
17
+ components: Cache
18
+
19
+ constructor(storage: StateStorage) {
20
+ this.variants = new Cache('variants', storage)
21
+ this.common = new Cache('common', storage)
22
+ this.components = new Cache('components', storage)
23
+ }
24
+
25
+ registerBaseKey(keys: Array<any>) {
26
+ keys.push(StyleConstants.STORES_PERSIST_VERSION)
27
+
28
+ const baseKey = hashKey(keys)
29
+
30
+ this.baseKey = baseKey
31
+
32
+ return baseKey
33
+ }
34
+
35
+ wipeCache() {
36
+ this.components.clear()
37
+ this.responsive.clear()
38
+ this.compositions.clear()
39
+ this.variants.clear()
40
+ this.common.clear()
41
+ this.styles.clear()
42
+ }
43
+
44
+ keyFor(type: CacheType, keyData: Array<any> | any) {
45
+ const cache = this[type]
46
+
47
+ const values = [this.baseKey, keyData]
48
+
49
+ const cacheKey = hashKey(values)
50
+ const cachedValue = minifier.decompress(cache.cache[cacheKey] ?? null)
51
+
52
+ return {
53
+ key: cacheKey,
54
+ value: cachedValue,
55
+ }
56
+ }
57
+
58
+ cacheFor(type: CacheType, key: string, value: any) {
59
+ if (!StyleConstants.CACHE_ENABLED) {
60
+ return value
61
+ }
62
+
63
+ const cache = this[type]
64
+
65
+ const minifiedValue = minifier.compress(value)
66
+
67
+ return cache.cacheFor(key, minifiedValue)
68
+ }
69
+ }
@@ -0,0 +1,28 @@
1
+ import { StateStorage } from 'zustand/middleware'
2
+ import { minifier } from './minifier'
3
+
4
+ export type StoragePersistor = {
5
+ set: (key: string, value: any) => void
6
+ get: (key: string) => any
7
+ del: (key: string) => void
8
+ }
9
+
10
+ export class StylePersistor implements StateStorage {
11
+ constructor(
12
+ private storage: StoragePersistor
13
+ ) {}
14
+
15
+ setItem(name: string, value: string): void {
16
+ return this.storage.set(name, value)
17
+ }
18
+
19
+ getItem(name: string): string | null {
20
+ const persistedValue = this.storage?.get(name)
21
+
22
+ return minifier.decompress(persistedValue ?? null)
23
+ }
24
+
25
+ removeItem(name: string): void {
26
+ return this.storage.del(name)
27
+ }
28
+ }