@codeleap/styles 5.6.3 → 5.7.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.
- package/package.json +2 -2
- package/package.json.bak +1 -1
- package/src/lib/createTheme.ts +11 -0
- package/src/lib/themeStore.ts +18 -6
- package/src/tools/colors.ts +33 -21
- package/src/types/theme.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codeleap/styles",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.7.1",
|
|
4
4
|
"main": "src/index.ts",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"directory": "packages/styles"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"@codeleap/config": "5.
|
|
12
|
+
"@codeleap/config": "5.7.1",
|
|
13
13
|
"ts-node-dev": "^1.1.8"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
package/package.json.bak
CHANGED
package/src/lib/createTheme.ts
CHANGED
|
@@ -95,6 +95,17 @@ export const createTheme = <T extends Theme>(theme: T, themePersistor: ThemePers
|
|
|
95
95
|
|
|
96
96
|
themePersistor.set(alternateColorsKey, unpersistedAlternateColors)
|
|
97
97
|
},
|
|
98
|
+
ejectColorScheme(name) {
|
|
99
|
+
themeStore.ejectColorScheme(name)
|
|
100
|
+
|
|
101
|
+
const persistedAlternateColors = themePersistor.get(alternateColorsKey)
|
|
102
|
+
|
|
103
|
+
if (name in persistedAlternateColors) {
|
|
104
|
+
delete persistedAlternateColors[name]
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
themePersistor.set(alternateColorsKey, persistedAlternateColors)
|
|
108
|
+
},
|
|
98
109
|
|
|
99
110
|
baseSpacing: theme.baseSpacing,
|
|
100
111
|
|
package/src/lib/themeStore.ts
CHANGED
|
@@ -56,11 +56,11 @@ class ThemeStore {
|
|
|
56
56
|
private getBaseColorScheme(): ColorMap {
|
|
57
57
|
const alternateColors = this.alternateColorsScheme ?? {}
|
|
58
58
|
const colorSchemeKeys = Object.keys(alternateColors)
|
|
59
|
-
|
|
59
|
+
|
|
60
60
|
if (colorSchemeKeys.length === 0) {
|
|
61
61
|
return {}
|
|
62
62
|
}
|
|
63
|
-
|
|
63
|
+
|
|
64
64
|
return alternateColors[colorSchemeKeys[0]] ?? {}
|
|
65
65
|
}
|
|
66
66
|
|
|
@@ -74,20 +74,32 @@ class ThemeStore {
|
|
|
74
74
|
[name]: {
|
|
75
75
|
...baseColors,
|
|
76
76
|
...colors,
|
|
77
|
-
}
|
|
77
|
+
},
|
|
78
78
|
}
|
|
79
|
-
|
|
79
|
+
|
|
80
80
|
this.setAlternateColorsScheme(alternateColors)
|
|
81
81
|
|
|
82
82
|
return alternateColors
|
|
83
83
|
}
|
|
84
|
+
|
|
85
|
+
ejectColorScheme(name:string) {
|
|
86
|
+
const currentAlternateColors = this.alternateColorsScheme ?? {}
|
|
87
|
+
|
|
88
|
+
if (name in currentAlternateColors) {
|
|
89
|
+
delete currentAlternateColors[name]
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
this.setAlternateColorsScheme(currentAlternateColors)
|
|
93
|
+
|
|
94
|
+
return currentAlternateColors
|
|
95
|
+
}
|
|
84
96
|
}
|
|
85
97
|
|
|
86
98
|
export const themeStore = new ThemeStore()
|
|
87
99
|
|
|
88
100
|
export const themeStoreComputed = computed([
|
|
89
|
-
themeStore
|
|
90
|
-
themeStore
|
|
101
|
+
themeStore.themeStore,
|
|
102
|
+
themeStore.colorSchemeStore,
|
|
91
103
|
], (theme, colorScheme) => ({
|
|
92
104
|
theme: theme as unknown as AppTheme<Theme>,
|
|
93
105
|
colorScheme,
|
package/src/tools/colors.ts
CHANGED
|
@@ -22,7 +22,7 @@ export function hexToHSL(hex: string) {
|
|
|
22
22
|
return {
|
|
23
23
|
h: Math.round(h * 360),
|
|
24
24
|
s: Math.round(s * 100),
|
|
25
|
-
l: Math.round(l * 100)
|
|
25
|
+
l: Math.round(l * 100),
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
|
|
@@ -32,8 +32,7 @@ export function hslToHex(h: number, s: number, l: number): string {
|
|
|
32
32
|
|
|
33
33
|
const k = (n: number) => (n + h / 30) % 12
|
|
34
34
|
const a = s * Math.min(l, 1 - l)
|
|
35
|
-
const f = (n: number) =>
|
|
36
|
-
Math.round(255 * (l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)))))
|
|
35
|
+
const f = (n: number) => Math.round(255 * (l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)))))
|
|
37
36
|
|
|
38
37
|
return `#${[f(0), f(8), f(4)].map(x => x.toString(16).padStart(2, '0')).join('')}`
|
|
39
38
|
}
|
|
@@ -51,40 +50,53 @@ export function hslToRGB(h: number, s: number, l: number) {
|
|
|
51
50
|
|
|
52
51
|
const k = (n: number) => (n + h / 30) % 12
|
|
53
52
|
const a = s * Math.min(l, 1 - l)
|
|
54
|
-
const f = (n: number) =>
|
|
55
|
-
Math.round(255 * (l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)))))
|
|
53
|
+
const f = (n: number) => Math.round(255 * (l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)))))
|
|
56
54
|
|
|
57
55
|
return {
|
|
58
56
|
r: f(0),
|
|
59
57
|
g: f(8),
|
|
60
|
-
b: f(4)
|
|
58
|
+
b: f(4),
|
|
61
59
|
}
|
|
62
60
|
}
|
|
63
61
|
|
|
62
|
+
const lightnesses = [95, 85, 75, 60, 45, 30, 27, 21, 16, 10]
|
|
63
|
+
|
|
64
|
+
const defaultLightnessMap = Object.fromEntries(
|
|
65
|
+
lightnesses.map((l, i) => {
|
|
66
|
+
const step = ((i + 1) * 100).toString()
|
|
67
|
+
return [step, l]
|
|
68
|
+
}),
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
const alphas = [0.05, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90]
|
|
72
|
+
|
|
73
|
+
const defaultAlphasMap = Object.fromEntries(
|
|
74
|
+
alphas.map((a, i) => {
|
|
75
|
+
const step = ((i + 1) * 100).toString()
|
|
76
|
+
return [step, a]
|
|
77
|
+
}),
|
|
78
|
+
)
|
|
79
|
+
|
|
64
80
|
export function generateColorScheme(
|
|
65
|
-
anchorHex: string,
|
|
66
|
-
prefix
|
|
81
|
+
anchorHex: string,
|
|
82
|
+
prefix = 'primary',
|
|
83
|
+
lightnesses:typeof defaultLightnessMap = defaultLightnessMap,
|
|
84
|
+
alphas: typeof defaultAlphasMap = defaultAlphasMap,
|
|
67
85
|
): ColorMap {
|
|
68
86
|
const { h, s } = hexToHSL(anchorHex)
|
|
69
87
|
const baseRGB = hexToRGB(anchorHex)
|
|
70
|
-
|
|
88
|
+
|
|
71
89
|
const scheme: ColorMap = {}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
lightnesses.forEach((lightness, index) => {
|
|
76
|
-
const step = (index + 1) * 100
|
|
90
|
+
|
|
91
|
+
Object.entries(lightnesses).forEach(([step, lightness]) => {
|
|
77
92
|
const rgb = hslToRGB(h, s, lightness)
|
|
78
93
|
scheme[`${prefix}Solid${step}`] = `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, 1.00)`
|
|
79
94
|
})
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
alphas.forEach((alpha, index) => {
|
|
84
|
-
const step = (index + 1) * 100
|
|
95
|
+
|
|
96
|
+
Object.entries(alphas).forEach(([step, alpha]) => {
|
|
85
97
|
scheme[`${prefix}Transparent${step}`] = `rgba(${baseRGB.r}, ${baseRGB.g}, ${baseRGB.b}, ${alpha.toFixed(2)})`
|
|
86
98
|
})
|
|
87
|
-
|
|
99
|
+
|
|
88
100
|
return scheme
|
|
89
101
|
}
|
|
90
102
|
|
|
@@ -103,4 +115,4 @@ export function getTextColor(backgroundHex: string, darkColor = 'black', lightCo
|
|
|
103
115
|
const rgb = hexToRGB(backgroundHex)
|
|
104
116
|
const luminance = getLuminance(rgb)
|
|
105
117
|
return luminance > 0.5 ? darkColor : lightColor
|
|
106
|
-
}
|
|
118
|
+
}
|
package/src/types/theme.ts
CHANGED
|
@@ -78,6 +78,7 @@ type PredefinedAppTheme<T extends Theme> = PredefinedThemeDerivedValues<T> & {
|
|
|
78
78
|
setColorScheme: (colorScheme: string) => void
|
|
79
79
|
currentColorScheme: () => ColorScheme<T>
|
|
80
80
|
injectColorScheme: (name: string, colorMap: ColorMap) => void
|
|
81
|
+
ejectColorScheme: (name: string) => void
|
|
81
82
|
spacing: SpacingMap
|
|
82
83
|
media: MediaQueries
|
|
83
84
|
border: BorderCreator
|