UNPKG

spacery

Version:

Space Modifier utilities for working with React

107 lines (101 loc) 3.66 kB
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( isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.js var src_exports = {}; __export(src_exports, { Spacery: () => Spacery, modsToStyle: () => modsToStyle, withSpacery: () => withSpacery }); module.exports = __toCommonJS(src_exports); // src/components/spacery.js var import_react = __toESM(require("react"), 1); // src/lib/mods-to-style.js var mgnPattern = /(margin)[TBRLXY]*-/; var padPattern = /(padding)[TBRLXY]*-/; var XPattern = /X$/; var YPattern = /Y$/; var toDims = (value, unit) => { if (isNaN(value)) return value; if (unit) return value + unit; return +value; }; var normalizeModifer = (prop) => { let x = prop; return x.endsWith("T") && (x += "op") || x.endsWith("L") && (x += "eft") || x.endsWith("B") && (x += "ottom") || x.endsWith("R") && (x += "ight") || prop; }; function modsToStyle(mods, dimUnit = "px") { const style = {}; const sanitizedProps = { ...mods }; for (const key of Object.keys(mods)) { if (!(mgnPattern.test(key) || padPattern.test(key))) continue; delete sanitizedProps[key]; const propSplits = key.split("-"); const modifier = normalizeModifer(propSplits[0]); propSplits[1] = propSplits[1] || 0; if (XPattern.test(modifier)) { const xProp = modifier.replace(XPattern, ""); style[xProp + "Left"] = toDims(propSplits[1], dimUnit); style[xProp + "Right"] = toDims(propSplits[1], dimUnit); } else if (YPattern.test(modifier)) { const yProp = modifier.replace(YPattern, ""); style[yProp + "Top"] = toDims(propSplits[1], dimUnit); style[yProp + "Bottom"] = toDims(propSplits[1], dimUnit); } else { style[modifier] = toDims(propSplits[1], dimUnit); } } return { style, sanitizedProps }; } // src/components/spacery.js function Spacery({ children, style, ...props }) { const { style: modifiersStyle, sanitizedProps } = modsToStyle(props); return /* @__PURE__ */ import_react.default.createElement("div", { style: { ...modifiersStyle, ...style }, ...sanitizedProps }, children); } // src/hoc/with-spacery.js var import_react2 = __toESM(require("react"), 1); function withSpacery(Component, dimensionUnit) { return ({ style, ...props }) => { const { style: modifiersStyle, sanitizedProps } = modsToStyle( props, dimensionUnit ); return /* @__PURE__ */ import_react2.default.createElement(Component, { style: { ...modifiersStyle, ...style }, ...sanitizedProps }); }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Spacery, modsToStyle, withSpacery });