@animus-ui/theming 0.1.1-beta.9 → 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 CHANGED
@@ -3,6 +3,30 @@
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
+
6
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)
7
31
 
8
32
  **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 };
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@animus-ui/theming",
3
- "version": "0.1.1-beta.9",
3
+ "version": "0.1.1-e4cdacd2.0+e4cdacd",
4
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
- "main": "dist/index.cjs.js",
9
- "module": "dist/index.esm.js",
8
+ "module": "./dist/index.js",
9
+ "main": "./dist/index.js",
10
10
  "types": "dist/index.d.ts",
11
11
  "publishConfig": {
12
12
  "access": "public"
@@ -25,7 +25,7 @@
25
25
  "url": "https://github.com/codecaaron/animus/issues"
26
26
  },
27
27
  "dependencies": {
28
- "@animus-ui/core": "^0.1.1-beta.9"
28
+ "@animus-ui/core": "^0.1.1-e4cdacd2.0+e4cdacd"
29
29
  },
30
30
  "peerDependencies": {
31
31
  "@emotion/react": ">=11.0.0",
@@ -33,5 +33,5 @@
33
33
  "lodash": "*",
34
34
  "typescript": ">=4.3.5"
35
35
  },
36
- "gitHead": "4eb343b48a448f4e19eb2f2b96c66c9a64d60019"
36
+ "gitHead": "e4cdacd229ba2d35ac932de490ad4fab18fa7f11"
37
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(){const{variables:t}=s(e.mapValues(this.#e.breakpoints,(e=>`${e}px`)),"breakpoint",this.#e);return e.merge({},this.#e,{_variables:{breakpoints:t},_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(){const{variables:e}=r(s(this.#e.breakpoints,(e=>`${e}px`)),"breakpoint",this.#e);return t({},this.#e,{_variables:{breakpoints:e},_tokens:{}})}}function i(e){return new h(e)}export{h as ThemeBuilder,i as createTheme,o as flattenScale,r as serializeTokens};