@instructure/ui-themeable
Version:
A UI component library made by Instructure Inc.
66 lines (56 loc) • 3.17 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { warnDeprecated as _warnDeprecated } from "@instructure/console";
/**
* ---
* category: utilities/themes
* ---
* Create an adapter function for mapping old theme variable names to updated ones.
*
* @param {Object} args
* @param {Object} args.map an object mapping of old theme variable names to new theme variable names
* @param {String} args.version a specified version in which the old theme variable will be removed
* @param {Boolean} args.shouldIncludeOldValues should the adapter include the old values in the new theme object (false by default)
* @returns {Function} an adapter function that takes an object with `theme` and `displayName` properties as an argument
*/
export var createThemeAdapter = function createThemeAdapter() {
var _ref = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {},
_ref$map = _ref.map,
map = _ref$map === void 0 ? {} : _ref$map,
version = _ref.version,
_ref$shouldIncludeOld = _ref.shouldIncludeOldValues,
shouldIncludeOldValues = _ref$shouldIncludeOld === void 0 ? false : _ref$shouldIncludeOld;
return function () {
var _ref2 = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {},
_ref2$theme = _ref2.theme,
theme = _ref2$theme === void 0 ? {} : _ref2$theme,
displayName = _ref2.displayName;
return Object.entries(theme).reduce(function (result, _ref3) {
var _ref4 = _slicedToArray(_ref3, 2),
key = _ref4[0],
value = _ref4[1];
if (map[key]) {
if (Array.isArray(map[key])) {
/*#__PURE__*/
/*#__PURE__*/
_warnDeprecated(false, "[".concat(displayName, "] The theme variable `").concat(key, "` has been split into the following values `").concat(map[key].join(', '), "`.").concat(version ? " In version ".concat(version, ", `").concat(key, "` will no longer work as an override. Override each value individually instead.") : ''));
} else {
/*#__PURE__*/
/*#__PURE__*/
_warnDeprecated(false, "[".concat(displayName, "] The theme variable `").concat(key, "` has been changed to `").concat(map[key], "`.").concat(version ? " In version ".concat(version, ", `").concat(key, "` will no longer work as an override. Use `").concat(map[key], "` instead.") : ''));
}
var updatedThemeVars = {};
if (Array.isArray(map[key])) {
updatedThemeVars = map[key].reduce(function (result, updatedVar) {
return _objectSpread({}, result, _defineProperty({}, updatedVar, value));
}, {});
} else {
updatedThemeVars = _defineProperty({}, map[key], value);
}
return shouldIncludeOldValues ? _objectSpread({}, result, {}, updatedThemeVars, _defineProperty({}, key, value)) : _objectSpread({}, result, {}, updatedThemeVars);
}
return _objectSpread({}, result, _defineProperty({}, key, value));
}, {});
};
};