@codecademy/variance 0.21.1 → 0.21.2-alpha.6a1d33.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/dist/core.js +111 -110
- package/dist/createTheme/createTheme.js +102 -132
- package/dist/createTheme/createTheme.test.js +45 -61
- package/dist/scales/createScale.js +1 -3
- package/dist/scales/createScaleLookup.js +5 -13
- package/dist/transforms/transformSize.js +8 -18
- package/dist/utils/flattenScale.js +23 -11
- package/dist/utils/getStaticProperties.js +1 -5
- package/dist/utils/propNames.js +33 -31
- package/dist/utils/responsive.js +45 -48
- package/dist/utils/serializeTokens.js +30 -21
- package/package.json +20 -23
package/dist/core.js
CHANGED
|
@@ -1,38 +1,29 @@
|
|
|
1
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
2
|
-
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
3
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
4
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
5
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
6
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
7
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
8
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
9
|
-
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
10
|
-
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
11
|
-
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
12
|
-
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
13
1
|
import { get, identity, isArray, isObject, isUndefined, merge } from 'lodash';
|
|
14
2
|
import { createScaleLookup } from './scales/createScaleLookup';
|
|
15
3
|
import { getStaticCss } from './utils/getStaticProperties';
|
|
16
4
|
import { orderPropNames } from './utils/propNames';
|
|
17
5
|
import { arrayParser, isMediaArray, isMediaMap, objectParser, orderBreakpoints, parseBreakpoints } from './utils/responsive';
|
|
18
|
-
export
|
|
6
|
+
export const variance = {
|
|
19
7
|
// Parser to handle any set of configured props
|
|
20
|
-
createParser
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
8
|
+
createParser(config) {
|
|
9
|
+
const propNames = orderPropNames(config);
|
|
10
|
+
let breakpoints;
|
|
11
|
+
const parser = props => {
|
|
12
|
+
const styles = {};
|
|
13
|
+
const {
|
|
14
|
+
theme
|
|
15
|
+
} = props;
|
|
16
|
+
// Attempt to cache the breakpoints if we have not yet or if theme has become available.
|
|
17
|
+
if (breakpoints === undefined || breakpoints === null && theme?.breakpoints) {
|
|
27
18
|
// Save the breakpoints if we can
|
|
28
|
-
breakpoints = parseBreakpoints(theme
|
|
19
|
+
breakpoints = parseBreakpoints(theme?.breakpoints);
|
|
29
20
|
}
|
|
30
21
|
|
|
31
22
|
// Loops over all prop names on the configured config to check for configured styles
|
|
32
|
-
propNames.forEach(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
switch (
|
|
23
|
+
propNames.forEach(prop => {
|
|
24
|
+
const property = config[prop];
|
|
25
|
+
const value = get(props, prop);
|
|
26
|
+
switch (typeof value) {
|
|
36
27
|
case 'string':
|
|
37
28
|
case 'number':
|
|
38
29
|
case 'function':
|
|
@@ -56,38 +47,38 @@ export var variance = {
|
|
|
56
47
|
};
|
|
57
48
|
// return the parser function with the resulting meta information for further composition
|
|
58
49
|
return Object.assign(parser, {
|
|
59
|
-
propNames
|
|
60
|
-
config
|
|
50
|
+
propNames,
|
|
51
|
+
config
|
|
61
52
|
});
|
|
62
53
|
},
|
|
63
54
|
// Given a single property configuration enrich the config with a transform function
|
|
64
55
|
// that traverses the properties the function is responsible for.
|
|
65
|
-
createTransform
|
|
66
|
-
|
|
67
|
-
transform =
|
|
68
|
-
property
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
56
|
+
createTransform(prop, config) {
|
|
57
|
+
const {
|
|
58
|
+
transform = identity,
|
|
59
|
+
property,
|
|
60
|
+
properties = [property],
|
|
61
|
+
scale
|
|
62
|
+
} = config;
|
|
63
|
+
const getScaleValue = createScaleLookup(scale);
|
|
64
|
+
const alwaysTransform = scale === undefined || isArray(scale);
|
|
65
|
+
return {
|
|
66
|
+
...config,
|
|
67
|
+
prop,
|
|
68
|
+
styleFn: (value, prop, props) => {
|
|
69
|
+
const styles = {};
|
|
79
70
|
if (isUndefined(value)) {
|
|
80
71
|
return styles;
|
|
81
72
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
switch (
|
|
73
|
+
let useTransform = false;
|
|
74
|
+
let intermediateValue;
|
|
75
|
+
let scaleValue;
|
|
76
|
+
switch (typeof value) {
|
|
86
77
|
case 'number':
|
|
87
78
|
case 'string':
|
|
88
79
|
scaleValue = getScaleValue(value, props);
|
|
89
80
|
useTransform = scaleValue !== undefined || alwaysTransform;
|
|
90
|
-
intermediateValue =
|
|
81
|
+
intermediateValue = scaleValue ?? value;
|
|
91
82
|
break;
|
|
92
83
|
case 'function':
|
|
93
84
|
if (props.theme) {
|
|
@@ -100,12 +91,12 @@ export var variance = {
|
|
|
100
91
|
|
|
101
92
|
// for each property look up the scale value from theme if passed and apply any
|
|
102
93
|
// final transforms to the value
|
|
103
|
-
properties.forEach(
|
|
104
|
-
|
|
94
|
+
properties.forEach(property => {
|
|
95
|
+
let styleValue = intermediateValue;
|
|
105
96
|
if (useTransform && !isUndefined(styleValue)) {
|
|
106
97
|
styleValue = transform(styleValue, property, props);
|
|
107
98
|
}
|
|
108
|
-
switch (
|
|
99
|
+
switch (typeof styleValue) {
|
|
109
100
|
case 'number':
|
|
110
101
|
case 'string':
|
|
111
102
|
return styles[property] = styleValue;
|
|
@@ -117,100 +108,110 @@ export var variance = {
|
|
|
117
108
|
// return the resulting styles object
|
|
118
109
|
return styles;
|
|
119
110
|
}
|
|
120
|
-
}
|
|
111
|
+
};
|
|
121
112
|
},
|
|
122
|
-
compose
|
|
113
|
+
compose() {
|
|
123
114
|
for (var _len = arguments.length, parsers = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
124
115
|
parsers[_key] = arguments[_key];
|
|
125
116
|
}
|
|
126
|
-
return this.createParser(parsers.reduce(
|
|
127
|
-
|
|
128
|
-
|
|
117
|
+
return this.createParser(parsers.reduce((carry, parser) => ({
|
|
118
|
+
...carry,
|
|
119
|
+
...parser.config
|
|
120
|
+
}), {}));
|
|
129
121
|
},
|
|
130
|
-
createCss
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
return
|
|
134
|
-
|
|
135
|
-
|
|
122
|
+
createCss(config) {
|
|
123
|
+
const parser = this.create(config);
|
|
124
|
+
const filteredProps = parser.propNames;
|
|
125
|
+
return cssProps => {
|
|
126
|
+
let cache;
|
|
127
|
+
const allKeys = Object.keys(cssProps);
|
|
136
128
|
|
|
137
129
|
/** Any key of the CSSProps that is not a System Prop or a Static CSS Property is treated as a nested selector */
|
|
138
|
-
|
|
139
|
-
return !filteredProps.includes(key) && isObject(cssProps[key]);
|
|
140
|
-
});
|
|
130
|
+
const selectors = allKeys.filter(key => !filteredProps.includes(key) && isObject(cssProps[key]));
|
|
141
131
|
|
|
142
132
|
/** Static CSS Properties get extracted if they match neither syntax */
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
133
|
+
const staticCss = getStaticCss(cssProps, ['theme',
|
|
134
|
+
// Just in case this gets passed somehow
|
|
135
|
+
...selectors, ...filteredProps]);
|
|
136
|
+
return _ref => {
|
|
137
|
+
let {
|
|
138
|
+
theme
|
|
139
|
+
} = _ref;
|
|
146
140
|
if (cache) return cache;
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
css[selector] =
|
|
154
|
-
|
|
155
|
-
|
|
141
|
+
const css = parser({
|
|
142
|
+
...cssProps,
|
|
143
|
+
theme
|
|
144
|
+
});
|
|
145
|
+
selectors.forEach(selector => {
|
|
146
|
+
const selectorConfig = cssProps[selector] ?? {};
|
|
147
|
+
css[selector] = {
|
|
148
|
+
...getStaticCss(selectorConfig, filteredProps),
|
|
149
|
+
...parser({
|
|
150
|
+
...selectorConfig,
|
|
151
|
+
theme
|
|
152
|
+
})
|
|
153
|
+
};
|
|
156
154
|
});
|
|
157
155
|
|
|
158
156
|
/** Merge the static and generated css and save it to the cache */
|
|
159
|
-
cache =
|
|
157
|
+
cache = {
|
|
158
|
+
...staticCss,
|
|
159
|
+
...css
|
|
160
|
+
};
|
|
160
161
|
return cache;
|
|
161
162
|
};
|
|
162
163
|
};
|
|
163
164
|
},
|
|
164
|
-
createVariant
|
|
165
|
-
|
|
166
|
-
return
|
|
167
|
-
|
|
168
|
-
prop =
|
|
169
|
-
defaultVariant
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
Object.keys(variants).forEach(
|
|
176
|
-
|
|
177
|
-
|
|
165
|
+
createVariant(config) {
|
|
166
|
+
const css = this.createCss(config);
|
|
167
|
+
return _ref2 => {
|
|
168
|
+
let {
|
|
169
|
+
prop = 'variant',
|
|
170
|
+
defaultVariant,
|
|
171
|
+
base = {},
|
|
172
|
+
variants
|
|
173
|
+
} = _ref2;
|
|
174
|
+
const baseFn = css(base);
|
|
175
|
+
const variantFns = {};
|
|
176
|
+
Object.keys(variants).forEach(key => {
|
|
177
|
+
const variantKey = key;
|
|
178
|
+
const cssProps = variants[variantKey];
|
|
178
179
|
variantFns[variantKey] = css(cssProps);
|
|
179
180
|
});
|
|
180
|
-
return
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
181
|
+
return props => {
|
|
182
|
+
const {
|
|
183
|
+
[prop]: selected = defaultVariant
|
|
184
|
+
} = props;
|
|
185
|
+
const styles = {};
|
|
185
186
|
if (!selected) return styles;
|
|
186
|
-
return merge(styles, baseFn(props), variantFns
|
|
187
|
+
return merge(styles, baseFn(props), variantFns?.[selected]?.(props));
|
|
187
188
|
};
|
|
188
189
|
};
|
|
189
190
|
},
|
|
190
|
-
createStates
|
|
191
|
-
|
|
192
|
-
return
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
orderedStates.forEach(
|
|
196
|
-
|
|
197
|
-
|
|
191
|
+
createStates(config) {
|
|
192
|
+
const css = this.createCss(config);
|
|
193
|
+
return states => {
|
|
194
|
+
const orderedStates = Object.keys(states);
|
|
195
|
+
const stateFns = {};
|
|
196
|
+
orderedStates.forEach(key => {
|
|
197
|
+
const stateKey = key;
|
|
198
|
+
const cssProps = states[stateKey];
|
|
198
199
|
stateFns[stateKey] = css(cssProps);
|
|
199
200
|
});
|
|
200
|
-
return
|
|
201
|
-
|
|
202
|
-
orderedStates.forEach(
|
|
201
|
+
return props => {
|
|
202
|
+
const styles = {};
|
|
203
|
+
orderedStates.forEach(state => {
|
|
203
204
|
merge(styles, props[state] && stateFns[state](props));
|
|
204
205
|
});
|
|
205
206
|
return styles;
|
|
206
207
|
};
|
|
207
208
|
};
|
|
208
209
|
},
|
|
209
|
-
create
|
|
210
|
-
|
|
210
|
+
create(config) {
|
|
211
|
+
const transforms = {};
|
|
211
212
|
|
|
212
213
|
// Create a transform function for each of the props
|
|
213
|
-
for (
|
|
214
|
+
for (const prop in config) {
|
|
214
215
|
if (typeof prop === 'string') {
|
|
215
216
|
transforms[prop] = this.createTransform(prop, config[prop]);
|
|
216
217
|
}
|
|
@@ -1,150 +1,120 @@
|
|
|
1
|
-
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
2
|
-
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
3
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
4
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
5
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
6
|
-
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
7
|
-
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
8
|
-
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
|
|
9
|
-
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
|
|
10
|
-
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
|
|
11
|
-
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
|
|
12
|
-
function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } }
|
|
13
1
|
import { mapValues, merge } from 'lodash';
|
|
14
2
|
import { flattenScale } from '../utils/flattenScale';
|
|
15
3
|
import { serializeTokens } from '../utils/serializeTokens';
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
_theme.set(this, {
|
|
21
|
-
writable: true,
|
|
22
|
-
value: {}
|
|
23
|
-
});
|
|
24
|
-
_classPrivateFieldSet(this, _theme, baseTheme);
|
|
4
|
+
class ThemeBuilder {
|
|
5
|
+
#theme = {};
|
|
6
|
+
constructor(baseTheme) {
|
|
7
|
+
this.#theme = baseTheme;
|
|
25
8
|
}
|
|
26
9
|
/**
|
|
27
10
|
*
|
|
28
11
|
* @param key A key of the current theme to transform into CSS Variables and Variable References
|
|
29
12
|
* @example .createScaleVariables('fontSize')
|
|
30
13
|
*/
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
14
|
+
createScaleVariables(key) {
|
|
15
|
+
const {
|
|
16
|
+
variables,
|
|
17
|
+
tokens
|
|
18
|
+
} = serializeTokens(this.#theme[key], key, this.#theme);
|
|
19
|
+
this.#theme = merge({}, this.#theme, {
|
|
20
|
+
[key]: tokens,
|
|
21
|
+
_variables: {
|
|
39
22
|
root: variables
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
23
|
+
},
|
|
24
|
+
_tokens: {
|
|
25
|
+
[key]: this.#theme[key]
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
43
30
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
31
|
+
/**
|
|
32
|
+
*
|
|
33
|
+
* @param colors A map of color tokens to add to the theme. These tokens are immediately converted to CSS Variables `--color-${key}`.
|
|
34
|
+
* @example .addColors({ navy: 'navy', hyper: 'purple' })
|
|
35
|
+
*/
|
|
36
|
+
addColors(colors) {
|
|
37
|
+
const flatColors = flattenScale(colors);
|
|
38
|
+
const {
|
|
39
|
+
variables,
|
|
40
|
+
tokens
|
|
41
|
+
} = serializeTokens(flatColors, 'color', this.#theme);
|
|
42
|
+
this.#theme = merge({}, this.#theme, {
|
|
43
|
+
colors: tokens,
|
|
44
|
+
_variables: {
|
|
45
|
+
root: variables
|
|
46
|
+
},
|
|
47
|
+
_tokens: {
|
|
48
|
+
colors: flatColors
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
67
53
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
_getColorValue: getColorValue,
|
|
96
|
-
_variables: {
|
|
97
|
-
mode: variables
|
|
98
|
-
},
|
|
99
|
-
_tokens: {
|
|
100
|
-
modes: mapValues(modes, function (mode) {
|
|
101
|
-
return mapValues(mode, getColorValue);
|
|
102
|
-
})
|
|
103
|
-
}
|
|
104
|
-
}));
|
|
105
|
-
return this;
|
|
106
|
-
}
|
|
54
|
+
/**
|
|
55
|
+
*
|
|
56
|
+
* @param initialMode A key of the object passed for modes. This sets the default state for the theme and transforms the correct variables.
|
|
57
|
+
* @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`
|
|
58
|
+
* @example .addColorModes('light', { light: { primary: 'hyper' }, { dark: { primary: 'navy' } } })
|
|
59
|
+
*/
|
|
60
|
+
addColorModes(initialMode, modeConfig) {
|
|
61
|
+
const modes = mapValues(modeConfig, mode => flattenScale(mode));
|
|
62
|
+
const {
|
|
63
|
+
tokens: colors,
|
|
64
|
+
variables
|
|
65
|
+
} = serializeTokens(mapValues(merge({}, this.#theme.modes?.[initialMode], modes[initialMode]), color => this.#theme.colors[color]), 'color', this.#theme);
|
|
66
|
+
const getColorValue = color => this.#theme._tokens?.colors?.[color];
|
|
67
|
+
this.#theme = merge({}, this.#theme, {
|
|
68
|
+
colors,
|
|
69
|
+
modes,
|
|
70
|
+
mode: initialMode,
|
|
71
|
+
_getColorValue: getColorValue,
|
|
72
|
+
_variables: {
|
|
73
|
+
mode: variables
|
|
74
|
+
},
|
|
75
|
+
_tokens: {
|
|
76
|
+
modes: mapValues(modes, mode => mapValues(mode, getColorValue))
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
107
81
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
82
|
+
/**
|
|
83
|
+
*
|
|
84
|
+
* @param key A new key of theme
|
|
85
|
+
* @param createScale A function that accepts the current theme and returns a new object of scale values.
|
|
86
|
+
* @example .addScale('fonts', () => ({ basic: 'Gotham', cool: 'Wingdings' }))
|
|
87
|
+
*/
|
|
88
|
+
addScale(key, createScale) {
|
|
89
|
+
this.#theme = merge({}, this.#theme, {
|
|
90
|
+
[key]: flattenScale(createScale(this.#theme))
|
|
91
|
+
});
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
120
94
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
95
|
+
/**
|
|
96
|
+
*
|
|
97
|
+
* @param key A current key of theme to be updated with new or computed values
|
|
98
|
+
* @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.
|
|
99
|
+
* @example .updateScale('fonts', ({ basic }) => ({ basicFallback: `{basic}, Montserrat` }))
|
|
100
|
+
*/
|
|
101
|
+
updateScale(key, updateFn) {
|
|
102
|
+
this.#theme = merge({}, this.#theme, {
|
|
103
|
+
[key]: updateFn(this.#theme[key])
|
|
104
|
+
});
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
133
107
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
}]);
|
|
146
|
-
return ThemeBuilder;
|
|
147
|
-
}();
|
|
108
|
+
/**
|
|
109
|
+
* This finalizes the theme build and returns the final theme and variables to be provided.
|
|
110
|
+
*/
|
|
111
|
+
build() {
|
|
112
|
+
return merge({}, this.#theme, {
|
|
113
|
+
_variables: {},
|
|
114
|
+
_tokens: {}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
148
118
|
export function createTheme(base) {
|
|
149
119
|
return new ThemeBuilder(base);
|
|
150
120
|
}
|
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
2
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
3
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
4
|
-
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
5
|
-
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
6
|
-
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
7
1
|
import { mapValues } from 'lodash';
|
|
8
2
|
import { createTheme } from './createTheme';
|
|
9
|
-
describe('createTheme',
|
|
10
|
-
|
|
3
|
+
describe('createTheme', () => {
|
|
4
|
+
const base = {
|
|
11
5
|
breakpoints: {
|
|
12
6
|
xs: '1',
|
|
13
7
|
sm: '2',
|
|
@@ -16,53 +10,46 @@ describe('createTheme', function () {
|
|
|
16
10
|
xl: '5'
|
|
17
11
|
}
|
|
18
12
|
};
|
|
19
|
-
it('works',
|
|
20
|
-
expect(createTheme(base).build()).toEqual(
|
|
13
|
+
it('works', () => {
|
|
14
|
+
expect(createTheme(base).build()).toEqual({
|
|
15
|
+
...base,
|
|
21
16
|
_variables: {},
|
|
22
17
|
_tokens: {}
|
|
23
|
-
})
|
|
18
|
+
});
|
|
24
19
|
});
|
|
25
|
-
it('adds a scale',
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
};
|
|
31
|
-
}).build();
|
|
20
|
+
it('adds a scale', () => {
|
|
21
|
+
const theme = createTheme(base).addScale('test', () => ({
|
|
22
|
+
test: 1,
|
|
23
|
+
test2: 2
|
|
24
|
+
})).build();
|
|
32
25
|
expect(theme.test).toEqual({
|
|
33
26
|
test: 1,
|
|
34
27
|
test2: 2
|
|
35
28
|
});
|
|
36
29
|
});
|
|
37
|
-
it('updates a scale',
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
};
|
|
43
|
-
});
|
|
30
|
+
it('updates a scale', () => {
|
|
31
|
+
const builder = createTheme(base).addScale('test', () => ({
|
|
32
|
+
test: 1,
|
|
33
|
+
test2: 2
|
|
34
|
+
}));
|
|
44
35
|
expect(builder.build().test).toEqual({
|
|
45
36
|
test: 1,
|
|
46
37
|
test2: 2
|
|
47
38
|
});
|
|
48
|
-
builder.updateScale('test',
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
};
|
|
52
|
-
});
|
|
39
|
+
builder.updateScale('test', () => ({
|
|
40
|
+
test3: 3
|
|
41
|
+
}));
|
|
53
42
|
expect(builder.build().test).toEqual({
|
|
54
43
|
test: 1,
|
|
55
44
|
test2: 2,
|
|
56
45
|
test3: 3
|
|
57
46
|
});
|
|
58
47
|
});
|
|
59
|
-
it('serializes variables',
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
};
|
|
65
|
-
}).createScaleVariables('test').build();
|
|
48
|
+
it('serializes variables', () => {
|
|
49
|
+
const theme = createTheme(base).addScale('test', () => ({
|
|
50
|
+
test: 1,
|
|
51
|
+
test2: 2
|
|
52
|
+
})).createScaleVariables('test').build();
|
|
66
53
|
expect(theme.test).toEqual({
|
|
67
54
|
test: 'var(--test-test)',
|
|
68
55
|
test2: 'var(--test-test2)'
|
|
@@ -72,24 +59,22 @@ describe('createTheme', function () {
|
|
|
72
59
|
'--test-test2': 2
|
|
73
60
|
});
|
|
74
61
|
});
|
|
75
|
-
describe('colors',
|
|
76
|
-
|
|
62
|
+
describe('colors', () => {
|
|
63
|
+
const staticColors = {
|
|
77
64
|
white: 'white',
|
|
78
65
|
black: 'black',
|
|
79
66
|
blue: 'blue',
|
|
80
67
|
green: 'green',
|
|
81
68
|
red: 'red'
|
|
82
69
|
};
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
it('creates color variables', function () {
|
|
88
|
-
var theme = builder.addColors(staticColors).build();
|
|
70
|
+
const cssVariableReferences = mapValues(staticColors, val => `var(--color-${val})`);
|
|
71
|
+
const builder = createTheme(base);
|
|
72
|
+
it('creates color variables', () => {
|
|
73
|
+
const theme = builder.addColors(staticColors).build();
|
|
89
74
|
expect(theme.colors).toEqual(cssVariableReferences);
|
|
90
75
|
});
|
|
91
|
-
it('adds colorModes',
|
|
92
|
-
|
|
76
|
+
it('adds colorModes', () => {
|
|
77
|
+
const theme = builder.addColors(staticColors).addColorModes('light', {
|
|
93
78
|
light: {
|
|
94
79
|
primary: 'red'
|
|
95
80
|
},
|
|
@@ -97,17 +82,16 @@ describe('createTheme', function () {
|
|
|
97
82
|
primary: 'blue'
|
|
98
83
|
}
|
|
99
84
|
}).build();
|
|
100
|
-
expect(theme.colors).toEqual(
|
|
101
|
-
|
|
102
|
-
})), {}, {
|
|
85
|
+
expect(theme.colors).toEqual({
|
|
86
|
+
...mapValues(staticColors, val => `var(--color-${val})`),
|
|
103
87
|
primary: 'var(--color-primary)'
|
|
104
|
-
})
|
|
88
|
+
});
|
|
105
89
|
expect(theme._variables.mode).toEqual({
|
|
106
90
|
'--color-primary': 'var(--color-red)'
|
|
107
91
|
});
|
|
108
92
|
});
|
|
109
|
-
it('returns value checker for colors',
|
|
110
|
-
|
|
93
|
+
it('returns value checker for colors', () => {
|
|
94
|
+
const theme = builder.addColors({
|
|
111
95
|
black: '#000000',
|
|
112
96
|
white: '#FFFFFF'
|
|
113
97
|
}).addColorModes('light', {
|
|
@@ -121,8 +105,8 @@ describe('createTheme', function () {
|
|
|
121
105
|
expect(theme._getColorValue('white')).toEqual('#FFFFFF');
|
|
122
106
|
expect(theme._getColorValue(theme.modes.light.primary)).toEqual('#000000');
|
|
123
107
|
});
|
|
124
|
-
it('returns value checker for colors with deep values',
|
|
125
|
-
|
|
108
|
+
it('returns value checker for colors with deep values', () => {
|
|
109
|
+
const theme = builder.addColors({
|
|
126
110
|
black: '#000000',
|
|
127
111
|
white: '#FFFFFF',
|
|
128
112
|
gray: {
|
|
@@ -132,7 +116,7 @@ describe('createTheme', function () {
|
|
|
132
116
|
}).addColorModes('light', {
|
|
133
117
|
light: {
|
|
134
118
|
primary: {
|
|
135
|
-
|
|
119
|
+
default: 'gray-200',
|
|
136
120
|
cool: {
|
|
137
121
|
_: 'gray-300',
|
|
138
122
|
town: 'black'
|
|
@@ -143,8 +127,8 @@ describe('createTheme', function () {
|
|
|
143
127
|
expect(theme._getColorValue('gray-300')).toEqual('#666666');
|
|
144
128
|
expect(theme._getColorValue(theme.modes.light['primary-default'])).toEqual('#eeeeee');
|
|
145
129
|
});
|
|
146
|
-
it('merges color mode configurations when overriden',
|
|
147
|
-
|
|
130
|
+
it('merges color mode configurations when overriden', () => {
|
|
131
|
+
const theme = builder.addColors({
|
|
148
132
|
black: '#000000',
|
|
149
133
|
white: '#FFFFFF'
|
|
150
134
|
}).addColorModes('light', {
|
|
@@ -155,7 +139,7 @@ describe('createTheme', function () {
|
|
|
155
139
|
}
|
|
156
140
|
}
|
|
157
141
|
}).build();
|
|
158
|
-
|
|
142
|
+
const override = createTheme(theme).addColorModes('light', {
|
|
159
143
|
light: {
|
|
160
144
|
primary: {
|
|
161
145
|
_: 'white',
|
|
@@ -165,8 +149,8 @@ describe('createTheme', function () {
|
|
|
165
149
|
}).build();
|
|
166
150
|
expect(override.modes.light.primary).toEqual('white');
|
|
167
151
|
});
|
|
168
|
-
it('returns the raw values of color mode colors on the tokens object',
|
|
169
|
-
|
|
152
|
+
it('returns the raw values of color mode colors on the tokens object', () => {
|
|
153
|
+
const theme = createTheme(base).addColors({
|
|
170
154
|
black: '#000000',
|
|
171
155
|
gray: {
|
|
172
156
|
300: '#666666'
|
|
@@ -1,21 +1,13 @@
|
|
|
1
1
|
import { get, isArray, isObject, isString } from 'lodash';
|
|
2
|
-
export
|
|
2
|
+
export const createScaleLookup = scale => {
|
|
3
3
|
if (isString(scale)) {
|
|
4
|
-
return
|
|
5
|
-
return get(props, ['theme', scale, val]);
|
|
6
|
-
};
|
|
4
|
+
return (val, props) => get(props, ['theme', scale, val]);
|
|
7
5
|
}
|
|
8
6
|
if (isArray(scale)) {
|
|
9
|
-
return
|
|
10
|
-
return val;
|
|
11
|
-
};
|
|
7
|
+
return val => val;
|
|
12
8
|
}
|
|
13
9
|
if (isObject(scale)) {
|
|
14
|
-
return
|
|
15
|
-
return get(scale, val);
|
|
16
|
-
};
|
|
10
|
+
return val => get(scale, val);
|
|
17
11
|
}
|
|
18
|
-
return
|
|
19
|
-
return undefined;
|
|
20
|
-
};
|
|
12
|
+
return () => undefined;
|
|
21
13
|
};
|
|
@@ -1,35 +1,25 @@
|
|
|
1
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
2
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
3
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
4
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
5
|
-
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0) { ; } } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
|
|
6
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
7
1
|
import { isNumber } from 'lodash';
|
|
8
|
-
export
|
|
2
|
+
export const percentageOrAbsolute = coordinate => {
|
|
9
3
|
if (coordinate === 0) {
|
|
10
4
|
return coordinate;
|
|
11
5
|
}
|
|
12
6
|
if (coordinate <= 1 && coordinate >= -1) {
|
|
13
|
-
return
|
|
7
|
+
return `${coordinate * 100}%`;
|
|
14
8
|
}
|
|
15
|
-
return
|
|
9
|
+
return `${coordinate}px`;
|
|
16
10
|
};
|
|
17
|
-
|
|
18
|
-
export
|
|
11
|
+
const valueWithUnit = /(-?\d*\.?\d+)(%|\w*)/;
|
|
12
|
+
export const transformSize = value => {
|
|
19
13
|
if (isNumber(value)) {
|
|
20
14
|
return percentageOrAbsolute(value);
|
|
21
15
|
}
|
|
22
16
|
if (value.includes('calc')) {
|
|
23
17
|
return value;
|
|
24
18
|
}
|
|
25
|
-
|
|
26
|
-
_ref2 = _slicedToArray(_ref, 3),
|
|
27
|
-
match = _ref2[0],
|
|
28
|
-
number = _ref2[1],
|
|
29
|
-
unit = _ref2[2];
|
|
19
|
+
const [match, number, unit] = valueWithUnit.exec(value) || [];
|
|
30
20
|
if (match === undefined) {
|
|
31
21
|
return value;
|
|
32
22
|
}
|
|
33
|
-
|
|
34
|
-
return !unit ? percentageOrAbsolute(numericValue) :
|
|
23
|
+
const numericValue = parseFloat(number);
|
|
24
|
+
return !unit ? percentageOrAbsolute(numericValue) : `${numericValue}${unit}`;
|
|
35
25
|
};
|
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
2
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
3
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
4
|
-
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
5
|
-
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
6
|
-
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
7
1
|
import { isObject } from 'lodash';
|
|
8
2
|
|
|
9
3
|
/**
|
|
@@ -11,13 +5,31 @@ import { isObject } from 'lodash';
|
|
|
11
5
|
* Possibilities are returned as `k1.k2.k3`.
|
|
12
6
|
*/
|
|
13
7
|
|
|
8
|
+
/** Returns valid paths of object T */
|
|
9
|
+
|
|
10
|
+
/** Returns the value of a valid path P `k1.k2.k3` in object T */
|
|
11
|
+
|
|
12
|
+
/** Check if path has a primitive end value and return only the union of end paths */
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Reduce all paths to a single map of paths with primitive values removing all extra non stateful paths
|
|
16
|
+
* { path: { sub: 1 } } => { 'path-sub': 1 }
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
|
|
14
20
|
export function flattenScale(object, path) {
|
|
15
|
-
return Object.keys(object).reduce(
|
|
16
|
-
|
|
17
|
-
|
|
21
|
+
return Object.keys(object).reduce((carry, key) => {
|
|
22
|
+
const nextKey = path ? `${path}${key === '_' ? '' : `-${key}`}` : key;
|
|
23
|
+
const current = object[key];
|
|
18
24
|
if (isObject(current)) {
|
|
19
|
-
return
|
|
25
|
+
return {
|
|
26
|
+
...carry,
|
|
27
|
+
...flattenScale(current, nextKey)
|
|
28
|
+
};
|
|
20
29
|
}
|
|
21
|
-
return
|
|
30
|
+
return {
|
|
31
|
+
...carry,
|
|
32
|
+
[nextKey]: object[key]
|
|
33
|
+
};
|
|
22
34
|
}, {});
|
|
23
35
|
}
|
|
@@ -1,6 +1,2 @@
|
|
|
1
1
|
import { keys, pick } from 'lodash';
|
|
2
|
-
export
|
|
3
|
-
return pick(props, keys(props).filter(function (key) {
|
|
4
|
-
return !filteredKeys.includes(key);
|
|
5
|
-
}));
|
|
6
|
-
};
|
|
2
|
+
export const getStaticCss = (props, filteredKeys) => pick(props, keys(props).filter(key => !filteredKeys.includes(key)));
|
package/dist/utils/propNames.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const SHORTHAND_PROPERTIES = ['border', 'borderTop', 'borderBottom', 'borderLeft', 'borderRight', 'borderWidth', 'borderStyle', 'borderColor', 'background', 'flex', 'margin', 'padding', 'transition', 'gap', 'grid', 'gridArea', 'gridColumn', 'gridRow', 'gridTemplate', 'overflow', 'transition'];
|
|
2
|
+
const SORT = {
|
|
3
3
|
A_BEFORE_B: -1,
|
|
4
4
|
B_BEFORE_A: 1,
|
|
5
5
|
EQUAL: 1
|
|
6
6
|
};
|
|
7
|
-
|
|
7
|
+
const compare = (a, b) => {
|
|
8
8
|
if (a < b) return SORT.A_BEFORE_B;
|
|
9
9
|
if (b < a) return SORT.B_BEFORE_A;
|
|
10
10
|
return SORT.EQUAL;
|
|
@@ -14,32 +14,34 @@ var compare = function compare(a, b) {
|
|
|
14
14
|
* Orders all properties by the most dependent props
|
|
15
15
|
* @param config
|
|
16
16
|
*/
|
|
17
|
-
export
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (bNum === 0) return SORT.B_BEFORE_A;
|
|
38
|
-
}
|
|
39
|
-
return compare(bNum, aNum);
|
|
17
|
+
export const orderPropNames = config => Object.keys(config).sort((a, b) => {
|
|
18
|
+
const {
|
|
19
|
+
[a]: aConf,
|
|
20
|
+
[b]: bConf
|
|
21
|
+
} = config;
|
|
22
|
+
const {
|
|
23
|
+
property: aProp,
|
|
24
|
+
properties: aProperties = []
|
|
25
|
+
} = aConf;
|
|
26
|
+
const {
|
|
27
|
+
property: bProp,
|
|
28
|
+
properties: bProperties = []
|
|
29
|
+
} = bConf;
|
|
30
|
+
const aIsShorthand = SHORTHAND_PROPERTIES.includes(aProp);
|
|
31
|
+
const bIsShorthand = SHORTHAND_PROPERTIES.includes(bProp);
|
|
32
|
+
if (aIsShorthand && bIsShorthand) {
|
|
33
|
+
const aNum = aProperties.length;
|
|
34
|
+
const bNum = bProperties.length;
|
|
35
|
+
if (aProp !== bProp) {
|
|
36
|
+
return compare(SHORTHAND_PROPERTIES.indexOf(aProp), SHORTHAND_PROPERTIES.indexOf(bProp));
|
|
40
37
|
}
|
|
41
|
-
if (
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
38
|
+
if (aProp === bProp) {
|
|
39
|
+
if (aNum === 0) return SORT.A_BEFORE_B;
|
|
40
|
+
if (bNum === 0) return SORT.B_BEFORE_A;
|
|
41
|
+
}
|
|
42
|
+
return compare(bNum, aNum);
|
|
43
|
+
}
|
|
44
|
+
if (aIsShorthand) return SORT.A_BEFORE_B;
|
|
45
|
+
if (bIsShorthand) return SORT.B_BEFORE_A;
|
|
46
|
+
return SORT.EQUAL;
|
|
47
|
+
});
|
package/dist/utils/responsive.js
CHANGED
|
@@ -1,78 +1,75 @@
|
|
|
1
|
-
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
2
|
-
function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest(); }
|
|
3
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
4
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
5
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
6
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
7
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
8
|
-
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
9
|
-
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
10
|
-
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
11
|
-
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
12
|
-
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
13
1
|
import { intersection, omit } from 'lodash';
|
|
14
|
-
|
|
2
|
+
const BREAKPOINT_KEYS = ['_', 'xs', 'sm', 'md', 'lg', 'xl'];
|
|
15
3
|
|
|
16
4
|
/**
|
|
17
5
|
* Destructures the themes breakpoints into an ordered structure to traverse
|
|
18
6
|
*/
|
|
19
|
-
export
|
|
7
|
+
export const parseBreakpoints = breakpoints => {
|
|
20
8
|
if (breakpoints === undefined) return null;
|
|
21
|
-
|
|
22
|
-
xs
|
|
23
|
-
sm
|
|
24
|
-
md
|
|
25
|
-
lg
|
|
26
|
-
xl
|
|
9
|
+
const {
|
|
10
|
+
xs,
|
|
11
|
+
sm,
|
|
12
|
+
md,
|
|
13
|
+
lg,
|
|
14
|
+
xl
|
|
15
|
+
} = breakpoints ?? {};
|
|
16
|
+
|
|
17
|
+
// Ensure order for mapping
|
|
27
18
|
return {
|
|
28
19
|
map: breakpoints,
|
|
29
20
|
array: [xs, sm, md, lg, xl]
|
|
30
21
|
};
|
|
31
22
|
};
|
|
32
|
-
export
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
23
|
+
export const isMediaArray = val => Array.isArray(val);
|
|
24
|
+
export const isMediaMap = val => intersection(Object.keys(val), BREAKPOINT_KEYS).length > 0;
|
|
25
|
+
export const objectParser = (value, props, config, breakpoints) => {
|
|
26
|
+
const styles = {};
|
|
27
|
+
const {
|
|
28
|
+
styleFn,
|
|
29
|
+
prop
|
|
30
|
+
} = config;
|
|
31
|
+
const {
|
|
32
|
+
_,
|
|
33
|
+
...rest
|
|
34
|
+
} = value;
|
|
35
|
+
// the keyof 'base' is base styles
|
|
44
36
|
if (_) Object.assign(styles, styleFn(_, prop, props));
|
|
45
37
|
|
|
46
38
|
// Map over remaining keys and merge the corresponding breakpoint styles
|
|
47
39
|
// for that property.
|
|
48
|
-
Object.keys(breakpoints).forEach(
|
|
49
|
-
|
|
40
|
+
Object.keys(breakpoints).forEach(breakpointKey => {
|
|
41
|
+
const bpStyles = rest[breakpointKey];
|
|
50
42
|
if (typeof bpStyles === 'undefined') return;
|
|
51
|
-
Object.assign(styles,
|
|
43
|
+
Object.assign(styles, {
|
|
44
|
+
[breakpoints[breakpointKey]]: styleFn(bpStyles, prop, props)
|
|
45
|
+
});
|
|
52
46
|
});
|
|
53
47
|
return styles;
|
|
54
48
|
};
|
|
55
|
-
export
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
49
|
+
export const arrayParser = (value, props, config, breakpoints) => {
|
|
50
|
+
const styles = {};
|
|
51
|
+
const {
|
|
52
|
+
styleFn,
|
|
53
|
+
prop
|
|
54
|
+
} = config;
|
|
55
|
+
const [_, ...rest] = value;
|
|
56
|
+
// the first index is base styles
|
|
62
57
|
if (_) Object.assign(styles, styleFn(_, prop, props));
|
|
63
58
|
|
|
64
59
|
// Map over each value in the array and merge the corresponding breakpoint styles
|
|
65
60
|
// for that property.
|
|
66
|
-
rest.forEach(
|
|
67
|
-
|
|
61
|
+
rest.forEach((val, i) => {
|
|
62
|
+
const breakpointKey = breakpoints[i];
|
|
68
63
|
if (!breakpointKey || typeof val === 'undefined') return;
|
|
69
|
-
Object.assign(styles,
|
|
64
|
+
Object.assign(styles, {
|
|
65
|
+
[breakpointKey]: styleFn(val, prop, props)
|
|
66
|
+
});
|
|
70
67
|
});
|
|
71
68
|
return styles;
|
|
72
69
|
};
|
|
73
|
-
export
|
|
74
|
-
|
|
75
|
-
breakpoints.forEach(
|
|
70
|
+
export const orderBreakpoints = (styles, breakpoints) => {
|
|
71
|
+
const orderedStyles = omit(styles, breakpoints);
|
|
72
|
+
breakpoints.forEach(bp => {
|
|
76
73
|
if (styles[bp]) {
|
|
77
74
|
orderedStyles[bp] = styles[bp];
|
|
78
75
|
}
|
|
@@ -1,32 +1,41 @@
|
|
|
1
|
-
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
2
|
-
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
3
|
-
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
4
|
-
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
5
|
-
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
6
|
-
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
7
1
|
import { isObject, merge } from 'lodash';
|
|
8
|
-
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns an type of any object with { key: 'var(--key) }
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const templateBreakpoints = (value, alias, theme) => {
|
|
9
8
|
if (isObject(value)) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
const {
|
|
10
|
+
_,
|
|
11
|
+
base,
|
|
12
|
+
...rest
|
|
13
|
+
} = value;
|
|
14
|
+
const css = {
|
|
15
|
+
[alias]: _ ?? base
|
|
16
|
+
};
|
|
14
17
|
if (theme) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
const {
|
|
19
|
+
breakpoints
|
|
20
|
+
} = theme;
|
|
21
|
+
Object.keys(breakpoints).forEach(key => {
|
|
22
|
+
css[breakpoints[key]] = {
|
|
23
|
+
[alias]: rest[key]
|
|
24
|
+
};
|
|
18
25
|
});
|
|
19
26
|
}
|
|
20
27
|
return css;
|
|
21
28
|
}
|
|
22
|
-
return
|
|
29
|
+
return {
|
|
30
|
+
[alias]: value
|
|
31
|
+
};
|
|
23
32
|
};
|
|
24
|
-
export
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
Object.keys(tokens).forEach(
|
|
28
|
-
|
|
29
|
-
tokenReferences[key] =
|
|
33
|
+
export const serializeTokens = (tokens, prefix, theme) => {
|
|
34
|
+
const tokenReferences = {};
|
|
35
|
+
const tokenVariables = {};
|
|
36
|
+
Object.keys(tokens).forEach(key => {
|
|
37
|
+
const varName = `--${prefix}-${key}`;
|
|
38
|
+
tokenReferences[key] = `var(${varName})`;
|
|
30
39
|
merge(tokenVariables, templateBreakpoints(tokens[key], varName, theme));
|
|
31
40
|
});
|
|
32
41
|
return {
|
package/package.json
CHANGED
|
@@ -1,39 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codecademy/variance",
|
|
3
3
|
"description": "Constraint based CSS in JS for building scalable design systems",
|
|
4
|
-
"version": "0.21.
|
|
4
|
+
"version": "0.21.2-alpha.6a1d33.0",
|
|
5
|
+
"author": "codecaaron <aaron@codecademy.com>",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"csstype": "^3.0.7",
|
|
8
|
+
"lodash": "^4.17.5"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/Codecademy/gamut/variance",
|
|
5
14
|
"keywords": [
|
|
6
|
-
"emotion",
|
|
7
15
|
"css",
|
|
8
|
-
"
|
|
9
|
-
"
|
|
16
|
+
"css-in-js",
|
|
17
|
+
"emotion",
|
|
18
|
+
"styles"
|
|
10
19
|
],
|
|
11
|
-
"author": "codecaaron <aaron@codecademy.com>",
|
|
12
|
-
"homepage": "https://github.com/Codecademy/gamut/variance",
|
|
13
20
|
"license": "MIT",
|
|
14
21
|
"main": "dist/index.js",
|
|
15
22
|
"module": "dist/index.js",
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
"
|
|
19
|
-
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"@emotion/react": "*",
|
|
25
|
+
"typescript": ">=4.3.5"
|
|
26
|
+
},
|
|
20
27
|
"publishConfig": {
|
|
21
28
|
"access": "public"
|
|
22
29
|
},
|
|
23
|
-
"repository":
|
|
24
|
-
"type": "git",
|
|
25
|
-
"url": "git+https://github.com/Codecademy/gamut.git"
|
|
26
|
-
},
|
|
30
|
+
"repository": "Codecademy/gamut.git",
|
|
27
31
|
"scripts": {
|
|
28
32
|
"build": "nx build @codecademy/variance"
|
|
29
33
|
},
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
"typescript": ">=4.3.5"
|
|
33
|
-
},
|
|
34
|
-
"dependencies": {
|
|
35
|
-
"csstype": "^3.0.7",
|
|
36
|
-
"lodash": "^4.17.5"
|
|
37
|
-
},
|
|
38
|
-
"gitHead": "baa23988e9eb59834cafd89bbfd7923dfe1039d9"
|
|
34
|
+
"types": "dist/index.d.ts",
|
|
35
|
+
"gitHead": "18f61d5f7ddfbcac70bf7cd130922f31eef3593c"
|
|
39
36
|
}
|