@codeleap/styles 5.5.1 → 5.5.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.
- package/package.json +2 -2
- package/package.json.bak +1 -1
- package/src/lib/createTheme.ts +3 -7
- package/src/lib/themeStore.ts +12 -18
- package/src/tools/colors.ts +45 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codeleap/styles",
|
|
3
|
-
"version": "5.5.
|
|
3
|
+
"version": "5.5.3",
|
|
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.5.
|
|
12
|
+
"@codeleap/config": "5.5.3",
|
|
13
13
|
"ts-node-dev": "^1.1.8"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
package/package.json.bak
CHANGED
package/src/lib/createTheme.ts
CHANGED
|
@@ -34,20 +34,16 @@ export const createTheme = <T extends Theme>(theme: T, themePersistor: ThemePers
|
|
|
34
34
|
|
|
35
35
|
const persistedAlternateColors = themePersistor.get(alternateColorsKey)
|
|
36
36
|
|
|
37
|
-
const
|
|
37
|
+
const currentAlternateColors = {
|
|
38
38
|
...(persistedAlternateColors ?? {}),
|
|
39
39
|
...otherThemeValues?.alternateColors,
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
themeStore.setAlternateColorsScheme(
|
|
42
|
+
themeStore.setAlternateColorsScheme(currentAlternateColors)
|
|
43
43
|
|
|
44
44
|
const themeObj: AppTheme<T> = {
|
|
45
45
|
...otherThemeValues,
|
|
46
46
|
|
|
47
|
-
get alternateColors() {
|
|
48
|
-
return themeStore.alternateColorsScheme
|
|
49
|
-
},
|
|
50
|
-
|
|
51
47
|
baseColors,
|
|
52
48
|
|
|
53
49
|
currentColorScheme() {
|
|
@@ -71,7 +67,7 @@ export const createTheme = <T extends Theme>(theme: T, themePersistor: ThemePers
|
|
|
71
67
|
},
|
|
72
68
|
|
|
73
69
|
setColorScheme(colorScheme: string) {
|
|
74
|
-
const hasScheme = !!themeStore.alternateColorsScheme?.[colorScheme]
|
|
70
|
+
const hasScheme = colorScheme === 'default' ? true : !!themeStore.alternateColorsScheme?.[colorScheme]
|
|
75
71
|
|
|
76
72
|
if (!hasScheme) {
|
|
77
73
|
console.warn(`Color scheme ${colorScheme} not found in theme`)
|
package/src/lib/themeStore.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
import { AppTheme, ColorMap, IAppVariants, ITheme, Theme } from '../types'
|
|
2
|
-
import { map,
|
|
2
|
+
import { map, computed } from 'nanostores'
|
|
3
3
|
|
|
4
4
|
export type ThemeState = {
|
|
5
5
|
theme: AppTheme<Theme> | null
|
|
6
|
-
colorScheme: string | null
|
|
7
|
-
variants: IAppVariants
|
|
8
6
|
}
|
|
9
7
|
|
|
10
8
|
class ThemeStore {
|
|
11
|
-
private
|
|
9
|
+
private alternateColorsSchemeStore: { [key: string]: ColorMap } = {}
|
|
12
10
|
|
|
13
|
-
public
|
|
11
|
+
public colorSchemeStore: string = null
|
|
14
12
|
|
|
15
|
-
public
|
|
13
|
+
public themeStore = map<ITheme | null>(null)
|
|
16
14
|
|
|
17
|
-
public
|
|
15
|
+
public variantsStore: IAppVariants = {} as IAppVariants
|
|
18
16
|
|
|
19
17
|
get theme() {
|
|
20
18
|
return this.themeStore.get()
|
|
@@ -25,23 +23,23 @@ class ThemeStore {
|
|
|
25
23
|
}
|
|
26
24
|
|
|
27
25
|
get colorScheme() {
|
|
28
|
-
return this.colorSchemeStore
|
|
26
|
+
return this.colorSchemeStore
|
|
29
27
|
}
|
|
30
28
|
|
|
31
29
|
get variants() {
|
|
32
|
-
return this.variantsStore
|
|
30
|
+
return this.variantsStore
|
|
33
31
|
}
|
|
34
32
|
|
|
35
33
|
get alternateColorsScheme() {
|
|
36
|
-
return this.alternateColorsSchemeStore
|
|
34
|
+
return this.alternateColorsSchemeStore ?? {}
|
|
37
35
|
}
|
|
38
36
|
|
|
39
37
|
setVariants<T>(variants: T) {
|
|
40
|
-
this.variantsStore
|
|
38
|
+
this.variantsStore = variants as unknown as IAppVariants
|
|
41
39
|
}
|
|
42
40
|
|
|
43
41
|
setColorScheme(colorScheme: string) {
|
|
44
|
-
this.colorSchemeStore
|
|
42
|
+
this.colorSchemeStore = colorScheme
|
|
45
43
|
}
|
|
46
44
|
|
|
47
45
|
setTheme(theme: ITheme) {
|
|
@@ -49,7 +47,7 @@ class ThemeStore {
|
|
|
49
47
|
}
|
|
50
48
|
|
|
51
49
|
setAlternateColorsScheme(colors: { [key: string]: ColorMap }) {
|
|
52
|
-
this.alternateColorsSchemeStore
|
|
50
|
+
this.alternateColorsSchemeStore = colors
|
|
53
51
|
}
|
|
54
52
|
|
|
55
53
|
// utils
|
|
@@ -88,10 +86,6 @@ export const themeStore = new ThemeStore()
|
|
|
88
86
|
|
|
89
87
|
export const themeStoreComputed = computed([
|
|
90
88
|
themeStore['themeStore'],
|
|
91
|
-
|
|
92
|
-
themeStore['variantsStore']
|
|
93
|
-
], (theme, colorScheme, variants) => ({
|
|
89
|
+
], (theme) => ({
|
|
94
90
|
theme: theme as unknown as AppTheme<Theme>,
|
|
95
|
-
colorScheme,
|
|
96
|
-
variants
|
|
97
91
|
}))
|
package/src/tools/colors.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ColorMap } from '../types'
|
|
2
|
+
|
|
1
3
|
export function hexToHSL(hex: string) {
|
|
2
4
|
const r = parseInt(hex.slice(1, 3), 16) / 255
|
|
3
5
|
const g = parseInt(hex.slice(3, 5), 16) / 255
|
|
@@ -36,28 +38,54 @@ export function hslToHex(h: number, s: number, l: number): string {
|
|
|
36
38
|
return `#${[f(0), f(8), f(4)].map(x => x.toString(16).padStart(2, '0')).join('')}`
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
export function
|
|
40
|
-
const
|
|
41
|
+
export function hexToRGB(hex: string) {
|
|
42
|
+
const r = parseInt(hex.slice(1, 3), 16)
|
|
43
|
+
const g = parseInt(hex.slice(3, 5), 16)
|
|
44
|
+
const b = parseInt(hex.slice(5, 7), 16)
|
|
45
|
+
return { r, g, b }
|
|
46
|
+
}
|
|
41
47
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
48
|
+
export function hslToRGB(h: number, s: number, l: number) {
|
|
49
|
+
s /= 100
|
|
50
|
+
l /= 100
|
|
45
51
|
|
|
46
|
-
const
|
|
52
|
+
const k = (n: number) => (n + h / 30) % 12
|
|
53
|
+
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)))))
|
|
47
56
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
57
|
+
return {
|
|
58
|
+
r: f(0),
|
|
59
|
+
g: f(8),
|
|
60
|
+
b: f(4)
|
|
51
61
|
}
|
|
52
|
-
|
|
53
|
-
return colors
|
|
54
62
|
}
|
|
55
63
|
|
|
56
|
-
export function
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
64
|
+
export function generateColorScheme(
|
|
65
|
+
anchorHex: string,
|
|
66
|
+
prefix: string = 'primary'
|
|
67
|
+
): ColorMap {
|
|
68
|
+
const { h, s } = hexToHSL(anchorHex)
|
|
69
|
+
const baseRGB = hexToRGB(anchorHex)
|
|
70
|
+
|
|
71
|
+
const scheme: ColorMap = {}
|
|
72
|
+
|
|
73
|
+
const lightnesses = [95, 85, 75, 60, 45, 30, 27, 21, 16, 10]
|
|
74
|
+
|
|
75
|
+
lightnesses.forEach((lightness, index) => {
|
|
76
|
+
const step = (index + 1) * 100
|
|
77
|
+
const rgb = hslToRGB(h, s, lightness)
|
|
78
|
+
scheme[`${prefix}Solid${step}`] = `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, 1.00)`
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
const alphas = [0.05, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90]
|
|
82
|
+
|
|
83
|
+
alphas.forEach((alpha, index) => {
|
|
84
|
+
const step = (index + 1) * 100
|
|
85
|
+
scheme[`${prefix}Transparent${step}`] = `rgba(${baseRGB.r}, ${baseRGB.g}, ${baseRGB.b}, ${alpha.toFixed(2)})`
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
return scheme
|
|
61
89
|
}
|
|
62
90
|
|
|
63
91
|
export function getLuminance({ r, g, b }: { r: number; g: number; b: number }): number {
|
|
@@ -75,4 +103,4 @@ export function getTextColor(backgroundHex: string, darkColor = 'black', lightCo
|
|
|
75
103
|
const rgb = hexToRGB(backgroundHex)
|
|
76
104
|
const luminance = getLuminance(rgb)
|
|
77
105
|
return luminance > 0.5 ? darkColor : lightColor
|
|
78
|
-
}
|
|
106
|
+
}
|