UNPKG

@twstyled/theme

Version:

Runtime theming support for twstyled -- the full-featured Tailwind CSS + CSS in JS Compiler

63 lines (62 loc) 2.22 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.merge = exports.get = exports.THEME_TW_DEFAULT_KEY = void 0; const deepmerge_1 = __importDefault(require("deepmerge")); /** * Allows for nested scales with shorthand values * @example * { * colors: { * primary: { __default: '#00f', light: '#33f' } * } * } * css({ color: 'primary' }); // { color: '#00f' } * css({ color: 'primary.light' }) // { color: '#33f' } */ exports.THEME_TW_DEFAULT_KEY = '__default'; const hasDefault = (x) => { return typeof x === 'object' && x !== null && exports.THEME_TW_DEFAULT_KEY in x; }; /** * Extracts value under path from a deeply nested object. * Used for Themes, variants and Theme UI style objects. * Given a path to object with `__default` key, returns the value under that key. * * @param obj a theme, variant or style object * @param path path separated with dots (`.`) * @param fallback default value returned if get(obj, path) is not found */ function get(obj, path, fallback, p, undef) { const pathArray = path && typeof path === 'string' ? path.split('.') : [path]; for (p = 0; p < pathArray.length; p++) { obj = obj ? obj[pathArray[p]] : undef; } if (obj === undef) return fallback; return hasDefault(obj) ? obj[exports.THEME_TW_DEFAULT_KEY] : obj; } exports.get = get; // // Merge Utilities // const canUseSymbol = typeof Symbol === 'function' && Symbol.for; const REACT_ELEMENT = canUseSymbol ? Symbol.for('react.element') : 0xeac7; const FORWARD_REF = canUseSymbol ? Symbol.for('react.forward_ref') : 0xeac7; const deepmergeOptions = { isMergeableObject: (n) => { return (!!n && typeof n === 'object' && n.$$typeof !== REACT_ELEMENT && n.$$typeof !== FORWARD_REF); }, arrayMerge: (target, rightArray) => rightArray }; const merge = (a, b) => deepmerge_1.default(a, b, deepmergeOptions); exports.merge = merge; function mergeAll(...args) { return deepmerge_1.default.all(args, deepmergeOptions); } merge.all = mergeAll;