UNPKG

tailwindest

Version:

typesafe, reusable tailwind

246 lines (232 loc) 6.95 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { createTools: () => createTools }); module.exports = __toCommonJS(src_exports); // src/core/deep.merge.ts var deepMerge = (...nestedObject) => nestedObject.reduce((mergedObject, currentObject) => { for (const [currKey, currValue] of Object.entries( currentObject )) { if (mergedObject[currKey] === void 0) { mergedObject[currKey] = currValue; } else { if (typeof currValue === "string") { mergedObject[currKey] = currValue; } else { if (currValue) { mergedObject[currKey] = deepMerge( mergedObject[currKey], currValue ); } } } } return mergedObject; }, {}); // src/core/cache.ts var cache = () => { let store = /* @__PURE__ */ new Map(); return { set: (key, value) => { store.set(key, value); }, has: (key) => store.has(key), get: (key, cacheFindFallback) => { if (store.has(key)) return store.get(key); const newCacheValue = cacheFindFallback(); store.set(key, newCacheValue); return newCacheValue; }, reset: () => { store = /* @__PURE__ */ new Map(); } }; }; // src/core/flatten.object.ts var flattenObject = (object) => Object.values(object ?? {}).map( (value) => typeof value !== "string" ? flattenObject(value) : value ).flat(); // src/core/get.styleclass.ts var DIVIDER = " "; var getStyleClass = (stylesArray) => stylesArray.join(DIVIDER); // src/core/get.tailwind.class.ts var getTailwindClass = (styleObject) => getStyleClass(flattenObject(styleObject)); // src/core/tools/create.style.ts var StyleSheet = class { s; c; constructor(style) { this.s = style; this.c = getTailwindClass(style); } get class() { return this.c; } get style() { return this.s; } compose(...styles) { this.s = deepMerge(this.s, ...styles); this.c = getTailwindClass(this.s); return this; } }; var createStyle = () => (style) => new StyleSheet(style); // src/constants/base.key.ts var BASE_KEY = Symbol(); // src/core/tools/create.rotary.ts var createRotary = () => ({ base, ...styles }) => { let isBaseUpdated = false; const rotaryCache = cache(); const getCachedBaseStyle = () => rotaryCache.get(BASE_KEY, () => [base ?? {}, ""])[0]; const getCachedValues = (variant) => { if (isBaseUpdated) { const updatedVariantStyle = deepMerge( getCachedBaseStyle(), styles[variant] ); const updated = [ updatedVariantStyle, getTailwindClass(updatedVariantStyle) ]; rotaryCache.set(variant, updated); isBaseUpdated = false; return updated; } const cachedVariantStyle = rotaryCache.get(variant, () => { const variantStyle = deepMerge( getCachedBaseStyle(), styles[variant] ); return [variantStyle, getTailwindClass(variantStyle)]; }); return cachedVariantStyle; }; return { style: (variant) => getCachedValues(variant)[0], class: (variant) => getCachedValues(variant)[1], compose: function(...styles2) { rotaryCache.reset(); const composedStyle = deepMerge(getCachedBaseStyle(), ...styles2); rotaryCache.set(BASE_KEY, [ composedStyle, getTailwindClass(composedStyle) ]); isBaseUpdated = true; return this; } }; }; // src/core/tools/create.toggle.ts var TOGGLE_TRUTHY_KEY = "truthy"; var TOGGLE_FALSY_KEY = "falsy"; var createToggle = () => { const rotary = createRotary(); return (toggleVariants) => { const toggleCache = rotary(toggleVariants); return { style: (toggleCondition) => toggleCache.style( toggleCondition ? TOGGLE_TRUTHY_KEY : TOGGLE_FALSY_KEY ), class: (toggleCondition) => toggleCache.class( toggleCondition ? TOGGLE_TRUTHY_KEY : TOGGLE_FALSY_KEY ), compose: function(...styles) { toggleCache.compose(...styles); return this; } }; }; }; // src/core/tools/create.variants.ts var createVariants = () => ({ base, variants }) => { let isBaseUpdated = false; const variantCache = cache(); const getCachedBaseStyle = () => variantCache.get(BASE_KEY, () => [base ?? {}, ""])[0]; const getUpdatedVariantCacheValue = (variantOption, baseStyle) => { const updatedVariantStyle = Object.keys(variantOption).reduce( (accStyle, key) => deepMerge( accStyle, variants[key][variantOption[key]] ), baseStyle ); return [updatedVariantStyle, getTailwindClass(updatedVariantStyle)]; }; const getCachedVariant = (variantOption) => { const cachedVariantKey = Object.values(variantOption).join(""); if (isBaseUpdated) { const updatedCacheValue = getUpdatedVariantCacheValue( variantOption, getCachedBaseStyle() ); variantCache.set(cachedVariantKey, updatedCacheValue); isBaseUpdated = false; return updatedCacheValue; } const cachedVariant = variantCache.get( cachedVariantKey, () => getUpdatedVariantCacheValue(variantOption, getCachedBaseStyle()) ); return cachedVariant; }; return { style: (variantOption) => getCachedVariant(variantOption)[0], class: (variantOption) => getCachedVariant(variantOption)[1], compose: function(...styles) { variantCache.reset(); const composedStyle = deepMerge(getCachedBaseStyle(), ...styles); variantCache.set(BASE_KEY, [composedStyle, ""]); isBaseUpdated = true; return this; } }; }; // src/core/tools/create.merge.props.ts var createMergeProps = () => (baseStyle, styleProps) => getTailwindClass(deepMerge(baseStyle, styleProps)); // src/create.tools.ts var createTools = () => { const style = createStyle(); const toggle = createToggle(); const rotary = createRotary(); const variants = createVariants(); const mergeProps = createMergeProps(); return { style, toggle, rotary, variants, mergeProps }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createTools });