UNPKG

@wordpress/global-styles-ui

Version:
60 lines (59 loc) • 2.5 kB
// packages/global-styles-ui/src/shadow-utils.ts function getShadowParts(shadow) { return (shadow.match(/(?:[^,(]|\([^)]*\))+/g) || []).map((value) => value.trim()); } function shadowStringToObject(shadowValue) { const defaultShadow = { x: "0", y: "0", blur: "0", spread: "0", color: "#000", inset: false }; if (!shadowValue) return defaultShadow; if (shadowValue.includes("none")) return defaultShadow; const lengthsRegex = /((?:^|\s+)(-?\d*\.?\d+(?:px|%|in|cm|mm|em|rem|ex|pt|pc|vh|vw|vmin|vmax|ch|lh)?)(?=\s|$)(?![^(]*\))){1,4}/g; const matches = shadowValue.match(lengthsRegex) || []; if (matches.length !== 1) return defaultShadow; const lengths = matches[0].split(" ").map((value) => value.trim()).filter((value) => value); if (lengths.length < 2) return defaultShadow; const insets = shadowValue.match(/inset/gi) || []; if (insets.length > 1) return defaultShadow; const hasInset = insets.length === 1; let colorString = shadowValue.replace(lengthsRegex, "").trim(); if (hasInset) colorString = colorString.replace("inset", "").replace("INSET", "").trim(); let colorMatches = (colorString.match(/^#([0-9a-f]{3}){1,2}$|^#([0-9a-f]{4}){1,2}$|^(?:rgb|hsl)a?\(?[\d*\.?\d+%?,?\/?\s]*\)$/gi) || []).map((value) => value?.trim()).filter((value) => value); if (colorMatches.length > 1) return defaultShadow; else if (colorMatches.length === 0) { colorMatches = colorString.trim().split(" ").filter((value) => value); if (colorMatches.length > 1) return defaultShadow; } const [x, y, blur, spread] = lengths; return { x, y, blur: blur || defaultShadow.blur, spread: spread || defaultShadow.spread, inset: hasInset, color: colorString || defaultShadow.color }; } function shadowObjectToString(shadowObj) { const shadowString = `${shadowObj.x || "0px"} ${shadowObj.y || "0px"} ${shadowObj.blur || "0px"} ${shadowObj.spread || "0px"}`; return `${shadowObj.inset ? "inset" : ""} ${shadowString} ${shadowObj.color || ""}`.trim(); } function textShadowStringToObject(textShadowValue) { const { x, y, blur, color } = shadowStringToObject(textShadowValue); return { x, y, blur, color }; } function textShadowObjectToString(textShadowObj) { return `${`${textShadowObj.x || "0px"} ${textShadowObj.y || "0px"} ${textShadowObj.blur || "0px"}`} ${textShadowObj.color || ""}`.trim(); } export { getShadowParts, shadowObjectToString, shadowStringToObject, textShadowObjectToString, textShadowStringToObject }; //# sourceMappingURL=shadow-utils.mjs.map