UNPKG

@wordpress/components

Version:
83 lines (82 loc) • 3.89 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); // \0rolldown/runtime.js 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 __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, 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)); let colord = require("colord"); let colord_plugins_names = require("colord/plugins/names"); colord_plugins_names = __toESM(colord_plugins_names); let colord_plugins_a11y = require("colord/plugins/a11y"); colord_plugins_a11y = __toESM(colord_plugins_a11y); let _wordpress_i18n = require("@wordpress/i18n"); // packages/components/src/color-palette/utils.ts (0, colord.extend)([colord_plugins_names.default, colord_plugins_a11y.default]); /** * Checks if a color value is a simple CSS color. * * @param value The color value to check. * @return A boolean indicating whether the color value is a simple CSS color. */ const isSimpleCSSColor = (value) => { const valueIsCssVariable = /var\(/.test(value ?? ""); const valueIsColorMix = /color-mix\(/.test(value ?? ""); return !valueIsCssVariable && !valueIsColorMix; }; const extractColorNameFromCurrentValue = (currentValue, colors = [], showMultiplePalettes = false, selectedSlug) => { if (!currentValue) return ""; const currentValueIsSimpleColor = currentValue ? isSimpleCSSColor(currentValue) : false; const normalizedCurrentValue = currentValueIsSimpleColor ? (0, colord.colord)(currentValue).toHex() : currentValue; const colorPalettes = showMultiplePalettes ? colors : [{ colors }]; for (const { colors: paletteColors } of colorPalettes) for (const { name: colorName, color: colorValue, slug } of paletteColors) { if (selectedSlug) { if (slug === selectedSlug) return colorName; continue; } if (normalizedCurrentValue === (currentValueIsSimpleColor ? (0, colord.colord)(colorValue).toHex() : colorValue)) return colorName; } return (0, _wordpress_i18n.__)("Custom"); }; const isMultiplePaletteObject = (obj) => Array.isArray(obj.colors) && !("color" in obj); const isMultiplePaletteArray = (arr) => { return arr.length > 0 && arr.every((colorObj) => isMultiplePaletteObject(colorObj)); }; /** * Transform a CSS variable used as background color into the color value itself. * * @param value The color value that may be a CSS variable. * @param element The element for which to get the computed style. * @return The background color value computed from a element. */ const normalizeColorValue = (value, element) => { if (!value || !element || isSimpleCSSColor(value)) return value; const { ownerDocument } = element; const { defaultView } = ownerDocument; const computedBackgroundColor = defaultView?.getComputedStyle(element).backgroundColor; return computedBackgroundColor ? (0, colord.colord)(computedBackgroundColor).toHex() : value; }; exports.extractColorNameFromCurrentValue = extractColorNameFromCurrentValue; exports.isMultiplePaletteArray = isMultiplePaletteArray; exports.isMultiplePaletteObject = isMultiplePaletteObject; exports.normalizeColorValue = normalizeColorValue; //# sourceMappingURL=utils.cjs.map