UNPKG

@wordpress/components

Version:
192 lines (190 loc) 7.16 kB
"use strict"; 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( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/components/src/color-picker/component.tsx var component_exports = {}; __export(component_exports, { ColorPicker: () => ColorPicker, default: () => component_default }); module.exports = __toCommonJS(component_exports); var import_colord = require("colord"); var import_names = __toESM(require("colord/plugins/names")); var import_element = require("@wordpress/element"); var import_compose = require("@wordpress/compose"); var import_i18n = require("@wordpress/i18n"); var import_context = require("../context/index.cjs"); var import_styles = require("./styles.cjs"); var import_color_copy_button = require("./color-copy-button.cjs"); var import_color_input = require("./color-input.cjs"); var import_picker = require("./picker.cjs"); var import_hooks = require("../utils/hooks/index.cjs"); var import_jsx_runtime = require("react/jsx-runtime"); (0, import_colord.extend)([import_names.default]); function mergeHSLA(nextHSLA, prevHSLA) { if (nextHSLA.s === 0) { if (nextHSLA.l === 0 || nextHSLA.l === 100) { return { ...nextHSLA, h: prevHSLA.h, s: prevHSLA.s }; } return { ...nextHSLA, h: prevHSLA.h }; } return nextHSLA; } var options = [{ label: "RGB", value: "rgb" }, { label: "HSL", value: "hsl" }, { label: "Hex", value: "hex" }]; var UnconnectedColorPicker = (props, forwardedRef) => { const { enableAlpha = false, color: colorProp, onChange, defaultValue = "#fff", copyFormat, ...divProps } = (0, import_context.useContextSystem)(props, "ColorPicker"); const [color, setColor] = (0, import_hooks.useControlledValue)({ onChange, value: colorProp, defaultValue }); const safeColordColor = (0, import_element.useMemo)(() => { return (0, import_colord.colord)(color || ""); }, [color]); const debouncedSetColor = (0, import_compose.useDebounce)(setColor); const [internalHSLA, setInternalHSLA] = (0, import_element.useState)(() => ({ ...safeColordColor.toHsl() })); const lastProducedHexRef = (0, import_element.useRef)(safeColordColor.toHex()); (0, import_element.useEffect)(() => { const incomingHex = safeColordColor.toHex(); if (incomingHex === lastProducedHexRef.current) { return; } lastProducedHexRef.current = incomingHex; const externalHSLA = safeColordColor.toHsl(); setInternalHSLA((prev) => mergeHSLA(externalHSLA, prev)); }, [safeColordColor]); const handleHSLAChange = (0, import_element.useCallback)((nextHSLA) => { setInternalHSLA(nextHSLA); const previousHex = lastProducedHexRef.current; const nextHex = (0, import_colord.colord)(nextHSLA).toHex(); if (nextHex !== previousHex) { lastProducedHexRef.current = nextHex; setColor(nextHex); } }, [setColor]); const handleChange = (0, import_element.useCallback)((nextValue) => { const nextHSLA = nextValue.toHsl(); setInternalHSLA((prev) => mergeHSLA(nextHSLA, prev)); const nextHex = nextValue.toHex(); lastProducedHexRef.current = nextHex; debouncedSetColor(nextHex); }, [debouncedSetColor]); const [colorType, setColorType] = (0, import_element.useState)(copyFormat || "hex"); const maybeHandlePaste = (0, import_element.useCallback)((event) => { const pastedText = event.clipboardData?.getData("text")?.trim(); if (!pastedText) { return; } const parsedColor = (0, import_colord.colord)(pastedText); if (!parsedColor.isValid()) { return; } handleChange(parsedColor); const supportedFormats = { hex: "hex", rgb: "rgb", hsl: "hsl" }; const detectedFormat = String((0, import_colord.getFormat)(pastedText)); const newColorType = supportedFormats[detectedFormat]; if (newColorType) { setColorType(newColorType); } event.stopPropagation(); event.preventDefault(); }, [handleChange, setColorType]); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_styles.ColorfulWrapper, { ref: forwardedRef, ...divProps, onPasteCapture: maybeHandlePaste, children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_picker.Picker, { onChange: handleHSLAChange, hsla: internalHSLA, enableAlpha }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_styles.AuxiliaryColorArtefactWrapper, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_styles.AuxiliaryColorArtefactHStackHeader, { justify: "space-between", children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_styles.SelectControl, { size: "compact", options, value: colorType, onChange: (nextColorType) => setColorType(nextColorType), label: (0, import_i18n.__)("Color format"), hideLabelFromVision: true, variant: "minimal" }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_color_copy_button.ColorCopyButton, { color: safeColordColor, colorType: copyFormat || colorType })] }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_styles.ColorInputWrapper, { direction: "column", gap: 2, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_color_input.ColorInput, { colorType, color: safeColordColor, hsla: internalHSLA, onChange: handleChange, onHSLChange: handleHSLAChange, enableAlpha }) })] })] }); }; var ColorPicker = (0, import_context.contextConnect)(UnconnectedColorPicker, "ColorPicker"); var component_default = ColorPicker; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ColorPicker }); //# sourceMappingURL=component.cjs.map