UNPKG

@pandabox/define-recipe

Version:

End-to-end type-safe theming for PandaCSS

215 lines (211 loc) 8.48 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { defineRecipe: () => defineRecipe, defineSlotRecipe: () => defineSlotRecipe }); module.exports = __toCommonJS(src_exports); // src/define-recipe.ts var import_lodash = __toESM(require("lodash.merge")); function defineRecipe(config) { return Object.assign( { extend: function(variants) { return defineRecipe(Object.assign({}, config, (0, import_lodash.default)({ variants: config.variants }, { variants }))); }, merge: function(extension) { return defineRecipe((0, import_lodash.default)({}, config, extension)); }, pick: function(...keys) { return defineRecipe( Object.assign({}, config, { variants: keys.reduce((acc, key) => ({ ...acc, [key]: config.variants?.[key] }), {}), compoundVariants: config.compoundVariants?.filter((compound) => { return Object.keys(compound).some((variant) => keys.includes(variant)); }) }) ); }, omit: function(...keys) { return defineRecipe( Object.assign({}, config, { variants: Object.entries(config.variants ?? {}).reduce((acc, [key, value]) => { if (keys.includes(key)) return acc; return Object.assign({}, acc, { [key]: value }); }, {}), compoundVariants: config.compoundVariants?.filter((compound) => { return !Object.keys(compound).some((variant) => keys.includes(variant)); }) }) ); }, cast: function() { return config; } }, config ); } // src/define-slot-recipe.ts var import_lodash2 = __toESM(require("lodash.merge")); function defineSlotRecipe(config) { return Object.assign( { extend: function(variants) { return defineSlotRecipe(Object.assign({}, config, (0, import_lodash2.default)({ variants: config.variants }, { variants }))); }, merge: function(extension) { return defineSlotRecipe((0, import_lodash2.default)({}, config, extension)); }, pick: function(...keys) { return defineSlotRecipe( Object.assign({}, config, { variants: keys.reduce((acc, key) => ({ ...acc, [key]: config.variants?.[key] }), {}), compoundVariants: config.compoundVariants?.filter((compound) => { return Object.keys(compound).some((variant) => keys.includes(variant)); }) }) ); }, omit: function(...keys) { return defineSlotRecipe( Object.assign({}, config, { variants: Object.entries(config.variants ?? {}).reduce((acc, [key, value]) => { if (keys.includes(key)) return acc; return Object.assign({}, acc, { [key]: value }); }, {}), compoundVariants: config.compoundVariants?.filter((compound) => { return !Object.keys(compound).some((variant) => keys.includes(variant)); }) }) ); }, cast: function() { return config; }, slot: { add: function(...slots) { return defineSlotRecipe(Object.assign({}, config, { slots: [...config.slots ?? [], ...slots] })); }, pick: function(...keys) { const { slots = [], compoundVariants = [] } = config; const pickedSlots = slots.filter((slot) => keys.includes(slot)); const pickedVariants = {}; const variants = config.variants ?? {}; for (const [vName, variantRecord] of Object.entries(variants)) { pickedVariants[vName] ||= {}; for (const [vKey, bySlots] of Object.entries(variantRecord)) { pickedVariants[vName][vKey] ||= {}; for (const [vSlot, styles] of Object.entries(bySlots)) { if (keys.includes(vSlot)) { pickedVariants[vName][vKey][vSlot] = styles; } } } } const pickedCompoundVariants = compoundVariants.filter((compound) => { return !Object.keys(compound).some((variant) => keys.includes(variant)); }); return defineSlotRecipe({ ...config, slots: pickedSlots, variants: pickedVariants, compoundVariants: pickedCompoundVariants }); }, omit: function(...keys) { const { slots = [], compoundVariants = [] } = config; const pickedSlots = slots.filter((slot) => !keys.includes(slot)); const pickedVariants = {}; const variants = config.variants ?? {}; for (const [vName, variantRecord] of Object.entries(variants)) { if (!keys.includes(vName)) { pickedVariants[vName] ||= {}; for (const [vKey, bySlots] of Object.entries(variantRecord)) { pickedVariants[vName][vKey] ||= {}; for (const [vSlot, styles] of Object.entries(bySlots)) { if (!keys.includes(vSlot)) { pickedVariants[vName][vKey][vSlot] = styles; } } } } } const pickedCompoundVariants = compoundVariants.filter((compound) => { return !Object.keys(compound).some((variant) => keys.includes(variant)); }); return defineSlotRecipe({ ...config, slots: pickedSlots, variants: pickedVariants, compoundVariants: pickedCompoundVariants }); }, assignTo: function(slot, recipe) { const base = config.base ?? {}; const recipeVariants = recipe.variants ?? {}; const variants = config.variants ?? {}; const overridenVariants = cloneDeep(variants); for (const [vName, variantRecord] of Object.entries(variants)) { overridenVariants[vName] ||= {}; for (const [vKey, bySlots] of Object.entries(variantRecord)) { overridenVariants[vName][vKey] ||= {}; for (const [vSlot, styles] of Object.entries(bySlots)) { if (vSlot === slot) { const recipeStyles = recipeVariants[vName]?.[vKey]; overridenVariants[vName][vKey][vSlot] = Object.assign({}, styles, recipeStyles); } } } } const overridenBase = cloneDeep(base); for (const [vSlot, styles] of Object.entries(base)) { if (vSlot === slot) { overridenBase[vSlot] = Object.assign({}, styles, recipe.base); } } return defineSlotRecipe({ ...config, variants: overridenVariants, base: overridenBase }); } } }, config ); } function cloneDeep(variants) { return JSON.parse(JSON.stringify(variants)); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { defineRecipe, defineSlotRecipe });