UNPKG

@wordpress/components

Version:
212 lines (211 loc) • 5.84 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let ___utils_unit_values_ts = require("../utils/unit-values.cjs"); // packages/components/src/border-box-control/utils.ts const sides = [ "top", "right", "bottom", "left" ]; const borderProps = [ "color", "style", "width" ]; const isEmptyBorder = (border) => { if (!border) return true; return !borderProps.some((prop) => border[prop] !== void 0); }; const isDefinedBorder = (border) => { if (!border) return false; if (hasSplitBorders(border)) return !sides.every((side) => isEmptyBorder(border[side])); return !isEmptyBorder(border); }; const isCompleteBorder = (border) => { if (!border) return false; return borderProps.every((prop) => border[prop] !== void 0); }; const hasSplitBorders = (border = {}) => { return Object.keys(border).some((side) => sides.indexOf(side) !== -1); }; const hasMixedBorders = (borders) => { if (!hasSplitBorders(borders)) return false; const shorthandBorders = sides.map((side) => getShorthandBorderStyle(borders?.[side])); return !shorthandBorders.every((border) => border === shorthandBorders[0]); }; /** * Splits a CSS box shorthand string into parts, preserving values * with parentheses like `calc()` and `var()`. * * @param value The shorthand string to split. * * @return Array of split values. */ function splitBoxShorthand(value) { const parts = []; let current = ""; let depth = 0; for (const char of value.trim()) if (char === "(") { depth++; current += char; } else if (char === ")") { depth = Math.max(0, depth - 1); current += char; } else if (/\s/.test(char) && depth === 0) { if (current) { parts.push(current); current = ""; } } else current += char; if (current) parts.push(current); return parts; } /** * Unpacks a 1-to-4 value CSS box shorthand string into individual sides * (top, right, bottom, left) following the CSS Box Model specification. * * @param value The box shorthand value (e.g. `0 0 0 2px`). * * @return An object containing individual side values. */ function unpackBoxShorthand(value) { if (typeof value !== "string") return { top: value, right: value, bottom: value, left: value }; const parts = splitBoxShorthand(value); switch (parts.length) { case 2: return { top: parts[0], right: parts[1], bottom: parts[0], left: parts[1] }; case 3: return { top: parts[0], right: parts[1], bottom: parts[2], left: parts[1] }; case 4: return { top: parts[0], right: parts[1], bottom: parts[2], left: parts[3] }; default: return { top: value, right: value, bottom: value, left: value }; } } const getSplitBorders = (border) => { if (!border || isEmptyBorder(border)) return; if (border.width === void 0) return { top: border, right: border, bottom: border, left: border }; const unpackedWidths = unpackBoxShorthand(border.width); return { top: { ...border, width: unpackedWidths.top }, right: { ...border, width: unpackedWidths.right }, bottom: { ...border, width: unpackedWidths.bottom }, left: { ...border, width: unpackedWidths.left } }; }; const getBorderDiff = (original, updated) => { const diff = {}; if (original.color !== updated.color) diff.color = updated.color; if (original.style !== updated.style) diff.style = updated.style; if (original.width !== updated.width) diff.width = updated.width; return diff; }; const getCommonBorder = (borders) => { if (!borders) return; const colors = []; const styles = []; const widths = []; sides.forEach((side) => { colors.push(borders[side]?.color); styles.push(borders[side]?.style); widths.push(borders[side]?.width); }); const allColorsMatch = colors.every((value) => value === colors[0]); const allStylesMatch = styles.every((value) => value === styles[0]); const allWidthsMatch = widths.every((value) => value === widths[0]); return { color: allColorsMatch ? colors[0] : void 0, style: allStylesMatch ? styles[0] : void 0, width: allWidthsMatch ? widths[0] : getMostCommonUnit(widths) }; }; const getShorthandBorderStyle = (border, fallbackBorder) => { if (isEmptyBorder(border)) return fallbackBorder; const { color: fallbackColor, style: fallbackStyle, width: fallbackWidth } = fallbackBorder || {}; const { color = fallbackColor, style = fallbackStyle, width = fallbackWidth } = border; return [ width, !!width && width !== "0" || !!color ? style || "solid" : style, color ].filter(Boolean).join(" "); }; const getMostCommonUnit = (values) => { return mode(values.map((value) => value === void 0 ? void 0 : (0, ___utils_unit_values_ts.parseCSSUnitValue)(`${value}`)[1]).filter((value) => value !== void 0)); }; /** * Finds the mode value out of the array passed favouring the first value * as a tiebreaker. * * @param values Values to determine the mode from. * * @return The mode value. */ function mode(values) { if (values.length === 0) return; const map = {}; let maxCount = 0; let currentMode; values.forEach((value) => { map[value] = map[value] === void 0 ? 1 : map[value] + 1; if (map[value] > maxCount) { currentMode = value; maxCount = map[value]; } }); return currentMode; } exports.getBorderDiff = getBorderDiff; exports.getCommonBorder = getCommonBorder; exports.getMostCommonUnit = getMostCommonUnit; exports.getShorthandBorderStyle = getShorthandBorderStyle; exports.getSplitBorders = getSplitBorders; exports.hasMixedBorders = hasMixedBorders; exports.hasSplitBorders = hasSplitBorders; exports.isCompleteBorder = isCompleteBorder; exports.isDefinedBorder = isDefinedBorder; exports.isEmptyBorder = isEmptyBorder; exports.splitBoxShorthand = splitBoxShorthand; exports.unpackBoxShorthand = unpackBoxShorthand; //# sourceMappingURL=utils.cjs.map