@wordpress/components
Version:
UI components for WordPress.
149 lines (147 loc) • 5.73 kB
JavaScript
;
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");
var import_styles = require("./styles");
var import_color_copy_button = require("./color-copy-button");
var import_color_input = require("./color-input");
var import_picker = require("./picker");
var import_hooks = require("../utils/hooks");
var import_jsx_runtime = require("react/jsx-runtime");
(0, import_colord.extend)([import_names.default]);
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 handleChange = (0, import_element.useCallback)((nextValue) => {
debouncedSetColor(nextValue.toHex());
}, [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: handleChange,
color: safeColordColor,
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, {
__nextHasNoMarginBottom: true,
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,
onChange: handleChange,
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.js.map