bootstrap-styled
Version:
Bootstrap Styled is a front-end ecosystem for React made with Bootstrap 4 philosophy, using the power of css-in-js thanks to styled-components.
114 lines (97 loc) • 4.08 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = createMakeTheme;
exports.makeScopedTheme = makeScopedTheme;
exports.toMakeTheme = void 0;
var _lodash = _interopRequireDefault(require("lodash.merge"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
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; }
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; }
/**
* @public
* @description Convert a theme or a scopedTheme into a makeTheme.
* @param {object} theme
* @returns {function} makeTheme
*
* @example
* const darkTheme = originalMakeTheme({
* '$body-bg': 'dark',
* '$body-color': 'white',
* '$btn-primary-bg': 'red',
* });
*
* const darkMakeTheme = toMakeTheme(darkTheme);
*
* Now you can create a new theme starting from the personalized theme as default theme rather than the default original theme.
* This allows others to build from your theme as a starting point and makes it a lot easier to share and make new themes.
*
* const anotherDarkTheme = darkMakeTheme({
* '$body-bg': '#121212'
* });
*
* The method also takes care of scopedTheme so that you don't have to worry about a thing.
*
* const loginFormTheme = makeScopedTheme('loginForm', {
* '$body-bg': 'dark',
* '$body-color': 'white',
* '$btn-primary-bg': 'red',
* })
*
* const loginFormMakeTheme = toMakeTheme(loginFormTheme);
*
* const anotherLoginFormTheme = loginFormMakeTheme({
* loginForm: {
* '$body-bg': '#121212'
* }
* });
*/
var toMakeTheme = function toMakeTheme(theme) {
return function (userTheme) {
// eslint-disable-line arrow-body-style
return (0, _lodash.default)({}, theme, userTheme);
};
};
exports.toMakeTheme = toMakeTheme;
function makeThemeFromList(list, theme) {
var all = [].concat(list);
var t = theme;
var mt;
while (mt = all.pop()) {
// eslint-disable-line
t = mt(t);
}
return t;
}
/**
* @public
* @description Create a makeTheme using a list of makeThemes.
* @param {Array} themes
* @returns {function(*=): *}
*/
function createMakeTheme(list) {
return function (theme) {
return makeThemeFromList(list, theme);
};
}
/**
* @public
* @description
* Creates a scoped makeTheme. Used for creating components or modules with their own variables.
* @param scopeName string
* @param userTheme
*
*/
function makeScopedTheme(scopeName) {
var userTheme = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _defineProperty({}, scopeName, {});
if (scopeName === undefined || typeof scopeName !== 'string') return console.warn('You may have forgotten to set the scope name in the makeScopedTheme function.'); // eslint-disable-line no-console
var newTheme = _defineProperty({}, scopeName, {});
var v = newTheme[scopeName];
var u = userTheme[scopeName] || {};
Object.keys(userTheme).forEach(function (variable) {
v[variable] = u[variable] || userTheme[variable];
});
return _defineProperty({}, scopeName, _objectSpread({}, userTheme, {}, v));
}
;