@codecademy/variance 0.20.1-alpha.e38a27.0 → 0.20.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.
Files changed (38) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/LICENSE +1 -1
  3. package/dist/core.js +245 -183
  4. package/dist/createTheme/createTheme.js +132 -58
  5. package/dist/createTheme/createTheme.test.js +184 -159
  6. package/dist/createTheme/index.js +1 -2
  7. package/dist/createTheme/types.js +0 -2
  8. package/dist/index.js +1 -2
  9. package/dist/scales/createScale.js +3 -2
  10. package/dist/scales/createScaleLookup.js +23 -13
  11. package/dist/transforms/index.js +1 -2
  12. package/dist/transforms/transformSize.js +45 -24
  13. package/dist/types/config.js +0 -2
  14. package/dist/types/properties.js +0 -2
  15. package/dist/types/props.js +0 -2
  16. package/dist/types/theme.js +0 -2
  17. package/dist/types/utils.js +0 -2
  18. package/dist/utils/__fixtures__/testConfig.js +153 -94
  19. package/dist/utils/flattenScale.js +22 -16
  20. package/dist/utils/getStaticProperties.js +5 -2
  21. package/dist/utils/propNames.js +43 -57
  22. package/dist/utils/responsive.js +86 -57
  23. package/dist/utils/serializeTokens.js +39 -30
  24. package/package.json +3 -7
  25. package/project.json +22 -0
  26. package/.turbo/turbo-build.log +0 -6
  27. package/dist/src/core.d.ts +0 -10
  28. package/dist/src/core.js +0 -189
  29. package/dist/src/core.js.map +0 -1
  30. package/dist/src/createTheme/createTheme.test.d.ts +0 -1
  31. package/dist/src/createTheme/createTheme.test.js +0 -167
  32. package/dist/src/createTheme/createTheme.test.js.map +0 -1
  33. package/dist/src/index.d.ts +0 -5
  34. package/dist/src/index.js +0 -6
  35. package/dist/src/index.js.map +0 -1
  36. package/dist/src/utils/__fixtures__/testConfig.d.ts +0 -153
  37. package/dist/src/utils/__fixtures__/testConfig.js +0 -94
  38. package/dist/src/utils/__fixtures__/testConfig.js.map +0 -1
package/CHANGELOG.md CHANGED
@@ -3,7 +3,7 @@
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.20.1-alpha.e38a27.0](https://github.com/Codecademy/gamut/compare/@codecademy/variance@0.20.0...@codecademy/variance@0.20.1-alpha.e38a27.0) (2022-01-31)
6
+ ### [0.20.1](https://github.com/Codecademy/gamut/compare/@codecademy/variance@0.20.0...@codecademy/variance@0.20.1) (2022-07-29)
7
7
 
8
8
  **Note:** Version bump only for package @codecademy/variance
9
9
 
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2016 Ryzac, Inc.
3
+ Copyright (c) 2022 Codecademy LLC
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/core.js CHANGED
@@ -1,189 +1,251 @@
1
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
2
+
3
+ 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."); }
4
+
5
+ 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); }
6
+
7
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
8
+
9
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
10
+
11
+ 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; }
12
+
13
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
14
+
15
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
16
+
17
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
18
+
19
+ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
20
+
1
21
  import { get, identity, isArray, isObject, isUndefined, merge } from 'lodash';
2
22
  import { createScaleLookup } from './scales/createScaleLookup';
3
23
  import { getStaticCss } from './utils/getStaticProperties';
4
24
  import { orderPropNames } from './utils/propNames';
5
- import { arrayParser, isMediaArray, isMediaMap, objectParser, orderBreakpoints, parseBreakpoints, } from './utils/responsive';
6
- export const variance = {
7
- // Parser to handle any set of configured props
8
- createParser(config) {
9
- const propNames = orderPropNames(config);
10
- let breakpoints;
11
- const parser = (props) => {
12
- const styles = {};
13
- const { theme } = props;
14
- // Attempt to cache the breakpoints if we have not yet or if theme has become available.
15
- if (breakpoints === undefined ||
16
- (breakpoints === null && theme?.breakpoints)) {
17
- // Save the breakpoints if we can
18
- breakpoints = parseBreakpoints(theme?.breakpoints);
19
- }
20
- // Loops over all prop names on the configured config to check for configured styles
21
- propNames.forEach((prop) => {
22
- const property = config[prop];
23
- const value = get(props, prop);
24
- switch (typeof value) {
25
- case 'string':
26
- case 'number':
27
- case 'function':
28
- return Object.assign(styles, property.styleFn(value, prop, props));
29
- // handle any props configured with the responsive notation
30
- case 'object':
31
- if (!breakpoints) {
32
- return;
33
- }
34
- // If it is an array the order of values is smallest to largest: [_, xs, ...]
35
- if (isMediaArray(value)) {
36
- return merge(styles, arrayParser(value, props, property, breakpoints.array));
37
- }
38
- // Check to see if value is an object matching the responsive syntax and generate the styles.
39
- if (isMediaMap(value)) {
40
- return merge(styles, objectParser(value, props, property, breakpoints.map));
41
- }
42
- }
43
- });
44
- return breakpoints ? orderBreakpoints(styles, breakpoints.array) : styles;
45
- };
46
- // return the parser function with the resulting meta information for further composition
47
- return Object.assign(parser, { propNames, config });
48
- },
49
- // Given a single property configuration enrich the config with a transform function
50
- // that traverses the properties the function is responsible for.
51
- createTransform(prop, config) {
52
- const { transform = identity, property, properties = [property], scale, } = config;
53
- const getScaleValue = createScaleLookup(scale);
54
- const alwaysTransform = scale === undefined || isArray(scale);
55
- return {
56
- ...config,
57
- prop,
58
- styleFn: (value, prop, props) => {
59
- const styles = {};
60
- if (isUndefined(value)) {
61
- return styles;
62
- }
63
- let useTransform = false;
64
- let intermediateValue;
65
- let scaleValue;
66
- switch (typeof value) {
67
- case 'number':
68
- case 'string':
69
- scaleValue = getScaleValue(value, props);
70
- useTransform = scaleValue !== undefined || alwaysTransform;
71
- intermediateValue = scaleValue ?? value;
72
- break;
73
- case 'function':
74
- if (props.theme) {
75
- intermediateValue = value(props.theme);
76
- }
77
- break;
78
- default:
79
- return styles;
80
- }
81
- // for each property look up the scale value from theme if passed and apply any
82
- // final transforms to the value
83
- properties.forEach((property) => {
84
- let styleValue = intermediateValue;
85
- if (useTransform && !isUndefined(styleValue)) {
86
- styleValue = transform(styleValue, property, props);
87
- }
88
- switch (typeof styleValue) {
89
- case 'number':
90
- case 'string':
91
- return (styles[property] = styleValue);
92
- case 'object':
93
- return Object.assign(styles, styleValue);
94
- default:
95
- }
96
- });
97
- // return the resulting styles object
98
- return styles;
99
- },
100
- };
101
- },
102
- compose(...parsers) {
103
- return this.createParser(parsers.reduce((carry, parser) => ({ ...carry, ...parser.config }), {}));
104
- },
105
- createCss(config) {
106
- const parser = this.create(config);
107
- const filteredProps = parser.propNames;
108
- return (cssProps) => {
109
- let cache;
110
- const allKeys = Object.keys(cssProps);
111
- /** Any key of the CSSProps that is not a System Prop or a Static CSS Property is treated as a nested selector */
112
- const selectors = allKeys.filter((key) => !filteredProps.includes(key) && isObject(cssProps[key]));
113
- /** Static CSS Properties get extracted if they match neither syntax */
114
- const staticCss = getStaticCss(cssProps, [
115
- 'theme',
116
- ...selectors,
117
- ...filteredProps,
118
- ]);
119
- return ({ theme }) => {
120
- if (cache)
121
- return cache;
122
- const css = parser({ ...cssProps, theme });
123
- selectors.forEach((selector) => {
124
- const selectorConfig = cssProps[selector] ?? {};
125
- css[selector] = {
126
- ...getStaticCss(selectorConfig, filteredProps),
127
- ...parser({ ...selectorConfig, theme }),
128
- };
129
- });
130
- /** Merge the static and generated css and save it to the cache */
131
- cache = {
132
- ...staticCss,
133
- ...css,
134
- };
135
- return cache;
136
- };
137
- };
138
- },
139
- createVariant(config) {
140
- const css = this.createCss(config);
141
- return ({ prop = 'variant', defaultVariant, base = {}, variants }) => {
142
- const baseFn = css(base);
143
- const variantFns = {};
144
- Object.keys(variants).forEach((key) => {
145
- const variantKey = key;
146
- const cssProps = variants[variantKey];
147
- variantFns[variantKey] = css(cssProps);
148
- });
149
- return (props) => {
150
- const { [prop]: selected = defaultVariant } = props;
151
- const styles = {};
152
- if (!selected)
153
- return styles;
154
- return merge(styles, baseFn(props), variantFns?.[selected]?.(props));
155
- };
156
- };
157
- },
158
- createStates(config) {
159
- const css = this.createCss(config);
160
- return (states) => {
161
- const orderedStates = Object.keys(states);
162
- const stateFns = {};
163
- orderedStates.forEach((key) => {
164
- const stateKey = key;
165
- const cssProps = states[stateKey];
166
- stateFns[stateKey] = css(cssProps);
167
- });
168
- return (props) => {
169
- const styles = {};
170
- orderedStates.forEach((state) => {
171
- merge(styles, props[state] && stateFns[state](props));
172
- });
173
- return styles;
174
- };
175
- };
176
- },
177
- create(config) {
178
- const transforms = {};
179
- // Create a transform function for each of the props
180
- for (const prop in config) {
181
- if (typeof prop === 'string') {
182
- transforms[prop] = this.createTransform(prop, config[prop]);
25
+ import { arrayParser, isMediaArray, isMediaMap, objectParser, orderBreakpoints, parseBreakpoints } from './utils/responsive';
26
+ export var variance = {
27
+ // Parser to handle any set of configured props
28
+ createParser: function createParser(config) {
29
+ var propNames = orderPropNames(config);
30
+ var breakpoints;
31
+
32
+ var parser = function parser(props) {
33
+ var styles = {};
34
+ var theme = props.theme; // Attempt to cache the breakpoints if we have not yet or if theme has become available.
35
+
36
+ if (breakpoints === undefined || breakpoints === null && theme !== null && theme !== void 0 && theme.breakpoints) {
37
+ // Save the breakpoints if we can
38
+ breakpoints = parseBreakpoints(theme === null || theme === void 0 ? void 0 : theme.breakpoints);
39
+ } // Loops over all prop names on the configured config to check for configured styles
40
+
41
+
42
+ propNames.forEach(function (prop) {
43
+ var property = config[prop];
44
+ var value = get(props, prop);
45
+
46
+ switch (_typeof(value)) {
47
+ case 'string':
48
+ case 'number':
49
+ case 'function':
50
+ return Object.assign(styles, property.styleFn(value, prop, props));
51
+ // handle any props configured with the responsive notation
52
+
53
+ case 'object':
54
+ if (!breakpoints) {
55
+ return;
56
+ } // If it is an array the order of values is smallest to largest: [_, xs, ...]
57
+
58
+
59
+ if (isMediaArray(value)) {
60
+ return merge(styles, arrayParser(value, props, property, breakpoints.array));
61
+ } // Check to see if value is an object matching the responsive syntax and generate the styles.
62
+
63
+
64
+ if (isMediaMap(value)) {
65
+ return merge(styles, objectParser(value, props, property, breakpoints.map));
183
66
  }
67
+
68
+ }
69
+ });
70
+ return breakpoints ? orderBreakpoints(styles, breakpoints.array) : styles;
71
+ }; // return the parser function with the resulting meta information for further composition
72
+
73
+
74
+ return Object.assign(parser, {
75
+ propNames: propNames,
76
+ config: config
77
+ });
78
+ },
79
+ // Given a single property configuration enrich the config with a transform function
80
+ // that traverses the properties the function is responsible for.
81
+ createTransform: function createTransform(prop, config) {
82
+ var _config$transform = config.transform,
83
+ transform = _config$transform === void 0 ? identity : _config$transform,
84
+ property = config.property,
85
+ _config$properties = config.properties,
86
+ properties = _config$properties === void 0 ? [property] : _config$properties,
87
+ scale = config.scale;
88
+ var getScaleValue = createScaleLookup(scale);
89
+ var alwaysTransform = scale === undefined || isArray(scale);
90
+ return _objectSpread(_objectSpread({}, config), {}, {
91
+ prop: prop,
92
+ styleFn: function styleFn(value, prop, props) {
93
+ var _scaleValue;
94
+
95
+ var styles = {};
96
+
97
+ if (isUndefined(value)) {
98
+ return styles;
184
99
  }
185
- // Create a parser that handles all the props within the config
186
- return this.createParser(transforms);
187
- },
188
- };
189
- //# sourceMappingURL=core.js.map
100
+
101
+ var useTransform = false;
102
+ var intermediateValue;
103
+ var scaleValue;
104
+
105
+ switch (_typeof(value)) {
106
+ case 'number':
107
+ case 'string':
108
+ scaleValue = getScaleValue(value, props);
109
+ useTransform = scaleValue !== undefined || alwaysTransform;
110
+ intermediateValue = (_scaleValue = scaleValue) !== null && _scaleValue !== void 0 ? _scaleValue : value;
111
+ break;
112
+
113
+ case 'function':
114
+ if (props.theme) {
115
+ intermediateValue = value(props.theme);
116
+ }
117
+
118
+ break;
119
+
120
+ default:
121
+ return styles;
122
+ } // for each property look up the scale value from theme if passed and apply any
123
+ // final transforms to the value
124
+
125
+
126
+ properties.forEach(function (property) {
127
+ var styleValue = intermediateValue;
128
+
129
+ if (useTransform && !isUndefined(styleValue)) {
130
+ styleValue = transform(styleValue, property, props);
131
+ }
132
+
133
+ switch (_typeof(styleValue)) {
134
+ case 'number':
135
+ case 'string':
136
+ return styles[property] = styleValue;
137
+
138
+ case 'object':
139
+ return Object.assign(styles, styleValue);
140
+
141
+ default:
142
+ }
143
+ }); // return the resulting styles object
144
+
145
+ return styles;
146
+ }
147
+ });
148
+ },
149
+ compose: function compose() {
150
+ for (var _len = arguments.length, parsers = new Array(_len), _key = 0; _key < _len; _key++) {
151
+ parsers[_key] = arguments[_key];
152
+ }
153
+
154
+ return this.createParser(parsers.reduce(function (carry, parser) {
155
+ return _objectSpread(_objectSpread({}, carry), parser.config);
156
+ }, {}));
157
+ },
158
+ createCss: function createCss(config) {
159
+ var parser = this.create(config);
160
+ var filteredProps = parser.propNames;
161
+ return function (cssProps) {
162
+ var cache;
163
+ var allKeys = Object.keys(cssProps);
164
+ /** Any key of the CSSProps that is not a System Prop or a Static CSS Property is treated as a nested selector */
165
+
166
+ var selectors = allKeys.filter(function (key) {
167
+ return !filteredProps.includes(key) && isObject(cssProps[key]);
168
+ });
169
+ /** Static CSS Properties get extracted if they match neither syntax */
170
+
171
+ var staticCss = getStaticCss(cssProps, ['theme'].concat(_toConsumableArray(selectors), _toConsumableArray(filteredProps)));
172
+ return function (_ref) {
173
+ var theme = _ref.theme;
174
+ if (cache) return cache;
175
+ var css = parser(_objectSpread(_objectSpread({}, cssProps), {}, {
176
+ theme: theme
177
+ }));
178
+ selectors.forEach(function (selector) {
179
+ var _cssProps$selector;
180
+
181
+ var selectorConfig = (_cssProps$selector = cssProps[selector]) !== null && _cssProps$selector !== void 0 ? _cssProps$selector : {};
182
+ css[selector] = _objectSpread(_objectSpread({}, getStaticCss(selectorConfig, filteredProps)), parser(_objectSpread(_objectSpread({}, selectorConfig), {}, {
183
+ theme: theme
184
+ })));
185
+ });
186
+ /** Merge the static and generated css and save it to the cache */
187
+
188
+ cache = _objectSpread(_objectSpread({}, staticCss), css);
189
+ return cache;
190
+ };
191
+ };
192
+ },
193
+ createVariant: function createVariant(config) {
194
+ var css = this.createCss(config);
195
+ return function (_ref2) {
196
+ var _ref2$prop = _ref2.prop,
197
+ prop = _ref2$prop === void 0 ? 'variant' : _ref2$prop,
198
+ defaultVariant = _ref2.defaultVariant,
199
+ _ref2$base = _ref2.base,
200
+ base = _ref2$base === void 0 ? {} : _ref2$base,
201
+ variants = _ref2.variants;
202
+ var baseFn = css(base);
203
+ var variantFns = {};
204
+ Object.keys(variants).forEach(function (key) {
205
+ var variantKey = key;
206
+ var cssProps = variants[variantKey];
207
+ variantFns[variantKey] = css(cssProps);
208
+ });
209
+ return function (props) {
210
+ var _variantFns;
211
+
212
+ var _props$prop = props[prop],
213
+ selected = _props$prop === void 0 ? defaultVariant : _props$prop;
214
+ var styles = {};
215
+ if (!selected) return styles;
216
+ return merge(styles, baseFn(props), variantFns === null || variantFns === void 0 ? void 0 : (_variantFns = variantFns[selected]) === null || _variantFns === void 0 ? void 0 : _variantFns.call(variantFns, props));
217
+ };
218
+ };
219
+ },
220
+ createStates: function createStates(config) {
221
+ var css = this.createCss(config);
222
+ return function (states) {
223
+ var orderedStates = Object.keys(states);
224
+ var stateFns = {};
225
+ orderedStates.forEach(function (key) {
226
+ var stateKey = key;
227
+ var cssProps = states[stateKey];
228
+ stateFns[stateKey] = css(cssProps);
229
+ });
230
+ return function (props) {
231
+ var styles = {};
232
+ orderedStates.forEach(function (state) {
233
+ merge(styles, props[state] && stateFns[state](props));
234
+ });
235
+ return styles;
236
+ };
237
+ };
238
+ },
239
+ create: function create(config) {
240
+ var transforms = {}; // Create a transform function for each of the props
241
+
242
+ for (var prop in config) {
243
+ if (typeof prop === 'string') {
244
+ transforms[prop] = this.createTransform(prop, config[prop]);
245
+ }
246
+ } // Create a parser that handles all the props within the config
247
+
248
+
249
+ return this.createParser(transforms);
250
+ }
251
+ };