@animus-ui/theming 0.1.1-e0310963.100 → 0.1.1-e4cdacd2.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/CHANGELOG.md +96 -0
- package/dist/index.js +147 -0
- package/dist/utils/flattenScale.d.ts +1 -1
- package/package.json +8 -7
- package/dist/index.cjs.js +0 -1
- package/dist/index.esm.js +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,102 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [0.1.1-beta.12](https://github.com/codecaaron/animus/compare/@animus-ui/theming@0.1.1-beta.11...@animus-ui/theming@0.1.1-beta.12) (2022-01-24)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @animus-ui/theming
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## [0.1.1-beta.11](https://github.com/codecaaron/animus/compare/@animus-ui/theming@0.1.1-beta.10...@animus-ui/theming@0.1.1-beta.11) (2022-01-24)
|
|
15
|
+
|
|
16
|
+
**Note:** Version bump only for package @animus-ui/theming
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## [0.1.1-beta.10](https://github.com/codecaaron/animus/compare/@animus-ui/theming@0.1.1-beta.9...@animus-ui/theming@0.1.1-beta.10) (2022-01-23)
|
|
23
|
+
|
|
24
|
+
**Note:** Version bump only for package @animus-ui/theming
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
## [0.1.1-beta.9](https://github.com/codecaaron/animus/compare/@animus-ui/theming@0.1.1-beta.8...@animus-ui/theming@0.1.1-beta.9) (2022-01-18)
|
|
31
|
+
|
|
32
|
+
**Note:** Version bump only for package @animus-ui/theming
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
## [0.1.1-beta.8](https://github.com/codecaaron/animus/compare/@animus-ui/theming@0.1.1-beta.7...@animus-ui/theming@0.1.1-beta.8) (2022-01-16)
|
|
39
|
+
|
|
40
|
+
**Note:** Version bump only for package @animus-ui/theming
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
## [0.1.1-beta.7](https://github.com/codecaaron/animus/compare/@animus-ui/theming@0.1.1-beta.6...@animus-ui/theming@0.1.1-beta.7) (2022-01-16)
|
|
47
|
+
|
|
48
|
+
**Note:** Version bump only for package @animus-ui/theming
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
## [0.1.1-beta.6](https://github.com/codecaaron/animus/compare/@animus-ui/theming@0.1.1-beta.5...@animus-ui/theming@0.1.1-beta.6) (2022-01-11)
|
|
55
|
+
|
|
56
|
+
**Note:** Version bump only for package @animus-ui/theming
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
## [0.1.1-beta.5](https://github.com/codecaaron/animus/compare/@animus-ui/theming@0.1.1-beta.4...@animus-ui/theming@0.1.1-beta.5) (2022-01-09)
|
|
63
|
+
|
|
64
|
+
**Note:** Version bump only for package @animus-ui/theming
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
## [0.1.1-beta.4](https://github.com/codecaaron/animus/compare/@animus-ui/theming@0.1.1-beta.3...@animus-ui/theming@0.1.1-beta.4) (2022-01-09)
|
|
71
|
+
|
|
72
|
+
**Note:** Version bump only for package @animus-ui/theming
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
## [0.1.1-beta.3](https://github.com/codecaaron/animus/compare/@animus-ui/theming@0.1.1-beta.2...@animus-ui/theming@0.1.1-beta.3) (2022-01-09)
|
|
79
|
+
|
|
80
|
+
**Note:** Version bump only for package @animus-ui/theming
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
## 0.1.1-beta.2 (2022-01-09)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
## 0.1.1-beta.1 (2022-01-09)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
## 0.1.1-beta.0 (2022-01-09)
|
|
95
|
+
|
|
96
|
+
**Note:** Version bump only for package @animus-ui/theming
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
6
102
|
## [0.1.1-beta.1](https://github.com/codecaaron/animus/compare/v0.1.1-beta.0...v0.1.1-beta.1) (2022-01-09)
|
|
7
103
|
|
|
8
104
|
**Note:** Version bump only for package @animus-ui/theming
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { isObject, merge, mapValues } from 'lodash';
|
|
2
|
+
|
|
3
|
+
function flattenScale(object, path) {
|
|
4
|
+
return Object.keys(object).reduce((carry, key) => {
|
|
5
|
+
const nextKey = path ? `${path}${key === '_' ? '' : `-${key}`}` : key;
|
|
6
|
+
const current = object[key];
|
|
7
|
+
if (isObject(current)) {
|
|
8
|
+
return {
|
|
9
|
+
...carry,
|
|
10
|
+
...flattenScale(current, nextKey),
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
...carry,
|
|
15
|
+
[nextKey]: object[key],
|
|
16
|
+
};
|
|
17
|
+
}, {});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const templateBreakpoints = (value, alias, theme) => {
|
|
21
|
+
if (isObject(value)) {
|
|
22
|
+
const { _, base, ...rest } = value;
|
|
23
|
+
const css = {
|
|
24
|
+
[alias]: _ ?? base,
|
|
25
|
+
};
|
|
26
|
+
if (theme) {
|
|
27
|
+
const { breakpoints } = theme;
|
|
28
|
+
Object.keys(breakpoints).forEach((key) => {
|
|
29
|
+
css[breakpoints[key]] = {
|
|
30
|
+
[alias]: rest[key],
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return css;
|
|
35
|
+
}
|
|
36
|
+
return { [alias]: value };
|
|
37
|
+
};
|
|
38
|
+
const serializeTokens = (tokens, prefix, theme) => {
|
|
39
|
+
const tokenReferences = {};
|
|
40
|
+
const tokenVariables = {};
|
|
41
|
+
Object.keys(tokens).forEach((key) => {
|
|
42
|
+
const varName = `--${prefix}-${key}`;
|
|
43
|
+
tokenReferences[key] = `var(${varName})`;
|
|
44
|
+
merge(tokenVariables, templateBreakpoints(tokens[key], varName, theme));
|
|
45
|
+
});
|
|
46
|
+
return {
|
|
47
|
+
tokens: tokenReferences,
|
|
48
|
+
variables: tokenVariables,
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
class ThemeBuilder {
|
|
53
|
+
#theme = {};
|
|
54
|
+
constructor(baseTheme) {
|
|
55
|
+
this.#theme = baseTheme;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
*
|
|
59
|
+
* @param key A key of the current theme to transform into CSS Variables and Variable References
|
|
60
|
+
* @example .createScaleVariables('fontSize')
|
|
61
|
+
*/
|
|
62
|
+
createScaleVariables(key) {
|
|
63
|
+
const { variables, tokens } = serializeTokens(this.#theme[key], key, this.#theme);
|
|
64
|
+
this.#theme = merge({}, this.#theme, {
|
|
65
|
+
[key]: tokens,
|
|
66
|
+
_variables: { root: variables },
|
|
67
|
+
_tokens: {
|
|
68
|
+
[key]: this.#theme[key],
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
*
|
|
75
|
+
* @param colors A map of color tokens to add to the theme. These tokens are immediately converted to CSS Variables `--color-${key}`.
|
|
76
|
+
* @example .addColors({ navy: 'navy', hyper: 'purple' })
|
|
77
|
+
*/
|
|
78
|
+
addColors(colors) {
|
|
79
|
+
const flatColors = flattenScale(colors);
|
|
80
|
+
const { variables, tokens } = serializeTokens(flatColors, 'color', this.#theme);
|
|
81
|
+
this.#theme = merge({}, this.#theme, {
|
|
82
|
+
colors: tokens,
|
|
83
|
+
_variables: { root: variables },
|
|
84
|
+
_tokens: { colors: flatColors },
|
|
85
|
+
});
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
*
|
|
90
|
+
* @param initialMode A key of the object passed for modes. This sets the default state for the theme and transforms the correct variables.
|
|
91
|
+
* @param modes A map of color modes with keys of each possible mode with a value of alias to color keys. This must be called after `addColors`
|
|
92
|
+
* @example .addColorModes('light', { light: { primary: 'hyper' }, { dark: { primary: 'navy' } } })
|
|
93
|
+
*/
|
|
94
|
+
addColorModes(initialMode, modeConfig) {
|
|
95
|
+
const modes = mapValues(modeConfig, (mode) => flattenScale(mode));
|
|
96
|
+
const { tokens: colors, variables } = serializeTokens(mapValues(merge({}, this.#theme.modes?.[initialMode], modes[initialMode]), (color) => this.#theme.colors[color]), 'color', this.#theme);
|
|
97
|
+
const getColorValue = (color) => this.#theme._tokens?.colors?.[color];
|
|
98
|
+
this.#theme = merge({}, this.#theme, {
|
|
99
|
+
colors,
|
|
100
|
+
modes,
|
|
101
|
+
mode: initialMode,
|
|
102
|
+
_getColorValue: getColorValue,
|
|
103
|
+
_variables: { mode: variables },
|
|
104
|
+
_tokens: {
|
|
105
|
+
modes: mapValues(modes, (mode) => mapValues(mode, getColorValue)),
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
*
|
|
112
|
+
* @param key A new key of theme
|
|
113
|
+
* @param createScale A function that accepts the current theme and returns a new object of scale values.
|
|
114
|
+
* @example .addScale('fonts', () => ({ basic: 'Gotham', cool: 'Wingdings' }))
|
|
115
|
+
*/
|
|
116
|
+
addScale(key, createScale) {
|
|
117
|
+
this.#theme = merge({}, this.#theme, {
|
|
118
|
+
[key]: flattenScale(createScale(this.#theme)),
|
|
119
|
+
});
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
*
|
|
124
|
+
* @param key A current key of theme to be updated with new or computed values
|
|
125
|
+
* @param updateFn A function that accepts an argument of the current values at the specified keys an returns a map of new values to merge.
|
|
126
|
+
* @example .updateScale('fonts', ({ basic }) => ({ basicFallback: `{basic}, Montserrat` }))
|
|
127
|
+
*/
|
|
128
|
+
updateScale(key, updateFn) {
|
|
129
|
+
this.#theme = merge({}, this.#theme, { [key]: updateFn(this.#theme[key]) });
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* This finalizes the theme build and returns the final theme and variables to be provided.
|
|
134
|
+
*/
|
|
135
|
+
build() {
|
|
136
|
+
const { variables } = serializeTokens(mapValues(this.#theme.breakpoints, (val) => `${val}px`), 'breakpoint', this.#theme);
|
|
137
|
+
return merge({}, this.#theme, {
|
|
138
|
+
_variables: { breakpoints: variables },
|
|
139
|
+
_tokens: {},
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function createTheme(base) {
|
|
144
|
+
return new ThemeBuilder(base);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export { ThemeBuilder, createTheme, flattenScale, serializeTokens };
|
|
@@ -17,4 +17,4 @@ export declare type PathToLiteral<T, K extends Path<T, D>, D extends string = '.
|
|
|
17
17
|
export declare type LiteralPaths<T extends Record<string | number, any>, D extends string = '.', Base extends string = ''> = {
|
|
18
18
|
[K in Path<T, D> as PathToLiteral<T, K, D, Base>]: PathValue<T, PathToLiteral<T, K, D>, D>;
|
|
19
19
|
};
|
|
20
|
-
export declare function flattenScale<T extends Record<string | number,
|
|
20
|
+
export declare function flattenScale<T extends Record<string | number, unknown>, P extends string>(object: T, path?: P): LiteralPaths<T, '-', '_'>;
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@animus-ui/theming",
|
|
3
|
-
"version": "0.1.1-
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.1-e4cdacd2.0+e4cdacd",
|
|
4
|
+
"description": "Theming Utilities",
|
|
5
5
|
"author": "Aaron Robb <airrobb@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/codecaaron/animus#readme",
|
|
7
7
|
"license": "MIT",
|
|
8
|
-
"
|
|
9
|
-
"
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
10
|
"types": "dist/index.d.ts",
|
|
11
11
|
"publishConfig": {
|
|
12
12
|
"access": "public"
|
|
@@ -18,13 +18,14 @@
|
|
|
18
18
|
"scripts": {
|
|
19
19
|
"build:clean": "rm -rf ./dist",
|
|
20
20
|
"build": "yarn build:clean && rollup -c",
|
|
21
|
-
"lernaBuildTask": "yarn build"
|
|
21
|
+
"lernaBuildTask": "yarn build",
|
|
22
|
+
"compile": "tsc --noEmit"
|
|
22
23
|
},
|
|
23
24
|
"bugs": {
|
|
24
25
|
"url": "https://github.com/codecaaron/animus/issues"
|
|
25
26
|
},
|
|
26
27
|
"dependencies": {
|
|
27
|
-
"@animus-ui/core": "^0.1.1-
|
|
28
|
+
"@animus-ui/core": "^0.1.1-e4cdacd2.0+e4cdacd"
|
|
28
29
|
},
|
|
29
30
|
"peerDependencies": {
|
|
30
31
|
"@emotion/react": ">=11.0.0",
|
|
@@ -32,5 +33,5 @@
|
|
|
32
33
|
"lodash": "*",
|
|
33
34
|
"typescript": ">=4.3.5"
|
|
34
35
|
},
|
|
35
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "e4cdacd229ba2d35ac932de490ad4fab18fa7f11"
|
|
36
37
|
}
|
package/dist/index.cjs.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("lodash");function t(s,r){return Object.keys(s).reduce(((o,h)=>{const i=r?`${r}${"_"===h?"":`-${h}`}`:h,a=s[h];return e.isObject(a)?{...o,...t(a,i)}:{...o,[i]:s[h]}}),{})}const s=(t,s,r)=>{const o={},h={};return Object.keys(t).forEach((i=>{const a=`--${s}-${i}`;o[i]=`var(${a})`,e.merge(h,((t,s,r)=>{if(e.isObject(t)){const{_:e,base:o,...h}=t,i={[s]:e??o};if(r){const{breakpoints:e}=r;Object.keys(e).forEach((t=>{i[e[t]]={[s]:h[t]}}))}return i}return{[s]:t}})(t[i],a,r))})),{tokens:o,variables:h}};class r{#e={};constructor(e){this.#e=e}createScaleVariables(t){const{variables:r,tokens:o}=s(this.#e[t],t,this.#e);return this.#e=e.merge({},this.#e,{[t]:o,_variables:{root:r},_tokens:{[t]:this.#e[t]}}),this}addColors(r){const o=t(r),{variables:h,tokens:i}=s(o,"color",this.#e);return this.#e=e.merge({},this.#e,{colors:i,_variables:{root:h},_tokens:{colors:o}}),this}addColorModes(r,o){const h=e.mapValues(o,(e=>t(e))),{tokens:i,variables:a}=s(e.mapValues(e.merge({},this.#e.modes?.[r],h[r]),(e=>this.#e.colors[e])),"color",this.#e),m=e=>this.#e._tokens?.colors?.[e];return this.#e=e.merge({},this.#e,{colors:i,modes:h,mode:r,_getColorValue:m,_variables:{mode:a},_tokens:{modes:e.mapValues(h,(t=>e.mapValues(t,m)))}}),this}addScale(s,r){return this.#e=e.merge({},this.#e,{[s]:t(r(this.#e))}),this}updateScale(t,s){return this.#e=e.merge({},this.#e,{[t]:s(this.#e[t])}),this}build(){return e.merge({},this.#e,{_variables:{},_tokens:{}})}}exports.ThemeBuilder=r,exports.createTheme=function(e){return new r(e)},exports.flattenScale=t,exports.serializeTokens=s;
|
package/dist/index.esm.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{isObject as e,merge as t,mapValues as s}from"lodash";function o(t,s){return Object.keys(t).reduce(((r,h)=>{const i=s?`${s}${"_"===h?"":`-${h}`}`:h,n=t[h];return e(n)?{...r,...o(n,i)}:{...r,[i]:t[h]}}),{})}const r=(s,o,r)=>{const h={},i={};return Object.keys(s).forEach((n=>{const a=`--${o}-${n}`;h[n]=`var(${a})`,t(i,((t,s,o)=>{if(e(t)){const{_:e,base:r,...h}=t,i={[s]:e??r};if(o){const{breakpoints:e}=o;Object.keys(e).forEach((t=>{i[e[t]]={[s]:h[t]}}))}return i}return{[s]:t}})(s[n],a,r))})),{tokens:h,variables:i}};class h{#e={};constructor(e){this.#e=e}createScaleVariables(e){const{variables:s,tokens:o}=r(this.#e[e],e,this.#e);return this.#e=t({},this.#e,{[e]:o,_variables:{root:s},_tokens:{[e]:this.#e[e]}}),this}addColors(e){const s=o(e),{variables:h,tokens:i}=r(s,"color",this.#e);return this.#e=t({},this.#e,{colors:i,_variables:{root:h},_tokens:{colors:s}}),this}addColorModes(e,h){const i=s(h,(e=>o(e))),{tokens:n,variables:a}=r(s(t({},this.#e.modes?.[e],i[e]),(e=>this.#e.colors[e])),"color",this.#e),c=e=>this.#e._tokens?.colors?.[e];return this.#e=t({},this.#e,{colors:n,modes:i,mode:e,_getColorValue:c,_variables:{mode:a},_tokens:{modes:s(i,(e=>s(e,c)))}}),this}addScale(e,s){return this.#e=t({},this.#e,{[e]:o(s(this.#e))}),this}updateScale(e,s){return this.#e=t({},this.#e,{[e]:s(this.#e[e])}),this}build(){return t({},this.#e,{_variables:{},_tokens:{}})}}function i(e){return new h(e)}export{h as ThemeBuilder,i as createTheme,o as flattenScale,r as serializeTokens};
|