UNPKG

@wordpress/components

Version:
313 lines (312 loc) • 14 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 clsx = require("clsx"); clsx = __toESM(clsx); let colord = require("colord"); let colord_plugins_names = require("colord/plugins/names"); colord_plugins_names = __toESM(colord_plugins_names); let _wordpress_element = require("@wordpress/element"); let _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_ui = require("@wordpress/ui"); let ___context_index_ts = require("../context/index.cjs"); let ___h_stack_index_ts = require("../h-stack/index.cjs"); let ___flex_index_ts = require("../flex/index.cjs"); let ___utils_polymorphic_element_tsx = require("../utils/polymorphic-element.cjs"); let __color_copy_button_tsx = require("./color-copy-button.cjs"); let __color_input_tsx = require("./color-input.cjs"); let __picker_tsx = require("./picker.cjs"); let ___utils_hooks_index_ts = require("../utils/hooks/index.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/style-runtime/src/index.ts const STYLE_HASH_ATTRIBUTE = "data-wp-hash"; /** * Returns the shared style runtime registry. * * The registry is stored on `globalThis` so separately bundled copies of this * package can coordinate through the same document and style maps. * * @return The shared runtime registry. */ function getRuntime() { const globalScope = globalThis; if (globalScope.__wpStyleRuntime) return globalScope.__wpStyleRuntime; globalScope.__wpStyleRuntime = { documents: /* @__PURE__ */ new Map(), styles: /* @__PURE__ */ new Map(), injectedStyles: /* @__PURE__ */ new WeakMap() }; if (typeof document !== "undefined") registerDocument(document); return globalScope.__wpStyleRuntime; } /** * Checks whether a document already contains a style tag for a hash. * * @param targetDocument Document to inspect. * @param hash Stable hash for the transformed CSS. * * @return Whether the style hash already exists in the document. */ function documentContainsStyleHash(targetDocument, hash) { if (!targetDocument.head) return false; for (const style of targetDocument.head.querySelectorAll(`style[${STYLE_HASH_ATTRIBUTE}]`)) if (style.getAttribute(STYLE_HASH_ATTRIBUTE) === hash) return true; return false; } /** * Injects a registered style into a document, unless that document already * contains a style tag for the same hash. * * @param targetDocument Document to inject the style into. * @param hash Stable hash for the transformed CSS. * @param css CSS text to inject. */ function injectStyle(targetDocument, hash, css) { if (!targetDocument.head) return; const runtime = getRuntime(); let injectedStyles = runtime.injectedStyles.get(targetDocument); if (!injectedStyles) { injectedStyles = /* @__PURE__ */ new Set(); runtime.injectedStyles.set(targetDocument, injectedStyles); } if (injectedStyles.has(hash)) return; if (documentContainsStyleHash(targetDocument, hash)) { injectedStyles.add(hash); return; } const style = targetDocument.createElement("style"); style.setAttribute(STYLE_HASH_ATTRIBUTE, hash); style.appendChild(targetDocument.createTextNode(css)); targetDocument.head.appendChild(style); injectedStyles.add(hash); } /** * Registers a document as a style injection target. * * Existing registered styles are replayed into the document immediately. * Documents are reference-counted so multiple providers can safely register the * same document without one cleanup removing it while another registration is * still active. * * @param targetDocument Document to receive registered styles. * @return Cleanup function that unregisters this document registration. */ function registerDocument(targetDocument) { const runtime = getRuntime(); runtime.documents.set(targetDocument, (runtime.documents.get(targetDocument) ?? 0) + 1); for (const [hash, css] of runtime.styles) injectStyle(targetDocument, hash, css); return () => { const count = runtime.documents.get(targetDocument); if (count === void 0) return; if (count <= 1) { runtime.documents.delete(targetDocument); return; } runtime.documents.set(targetDocument, count - 1); }; } /** * Registers a style and injects it into all registered documents. * * The hash is used as the deduplication key, so calling this repeatedly with * the same hash will not add duplicate style tags to a document. * Registered styles are retained for the lifetime of the page so they can be * replayed into documents that are registered later. * * @param hash Stable hash for the transformed CSS. * @param css CSS text to inject. */ function registerStyle(hash, css) { const runtime = getRuntime(); runtime.styles.set(hash, css); for (const targetDocument of runtime.documents.keys()) injectStyle(targetDocument, hash, css); } // packages/components/src/color-picker/style.module.scss if (typeof process === "undefined" || process.env.NODE_ENV !== "test") registerStyle("7c9da4804d", ".fc84b54ab7eadaac__prefix{color:var(--wp-components-color-accent,var(--wp-admin-theme-color,#3858e9))}._0931450c28ea78af__number-control{width:96px}.d5340e8c9e22c868__select{margin-left:-8px}._96be3f8f4bf140d7__range{flex:1;margin-right:8px}.ba153a3ef3adbfdc__auxiliary{padding:8px 0 0}._8b76d21b029c2e21__header{padding-inline:16px}._10f48d0e75fe17ef__color-input{padding-block:16px 20px;padding-left:16px;padding-right:12px}.a2ba900a769046e5__colorful{box-sizing:border-box}.a2ba900a769046e5__colorful *,.a2ba900a769046e5__colorful :after,.a2ba900a769046e5__colorful :before{box-sizing:inherit}.a2ba900a769046e5__colorful{width:216px}.a2ba900a769046e5__colorful .react-colorful{align-items:center;display:flex;flex-direction:column;height:auto;width:216px}.a2ba900a769046e5__colorful .react-colorful__saturation{border-bottom:none;border-radius:0;height:216px;margin-bottom:16px;width:100%}.a2ba900a769046e5__colorful .react-colorful__alpha,.a2ba900a769046e5__colorful .react-colorful__hue{border-radius:9999px;height:16px;margin-bottom:8px;width:184px}.a2ba900a769046e5__colorful .react-colorful__pointer{border:none;box-shadow:0 0 2px 0 rgba(0,0,0,.25);height:16px;outline:2px solid transparent;width:16px}@media not (prefers-reduced-motion){.a2ba900a769046e5__colorful .react-colorful__pointer{transition:transform .16s ease-in-out}}.a2ba900a769046e5__colorful .react-colorful__interactive:focus .react-colorful__pointer{border:1.5px solid #000;box-shadow:0 0 0 1.5px #fff;transform:translate(-50%,-50%) scale(1.5)}.a2ba900a769046e5__colorful .react-colorful__pointer-fill{box-shadow:inset 0 0 0 1.5px #fff}.a2ba900a769046e5__colorful .react-colorful__interactive{margin-left:4px;width:calc(100% - 8px)}"); var style_module_default = { "prefix": "fc84b54ab7eadaac__prefix", "number-control": "_0931450c28ea78af__number-control", "select": "d5340e8c9e22c868__select", "range": "_96be3f8f4bf140d7__range", "auxiliary": "ba153a3ef3adbfdc__auxiliary", "header": "_8b76d21b029c2e21__header", "color-input": "_10f48d0e75fe17ef__color-input", "colorful": "a2ba900a769046e5__colorful" }; // packages/components/src/color-picker/component.tsx (0, colord.extend)([colord_plugins_names.default]); /** * Merges incoming HSLA with previous state, preserving hue for achromatic * colors and saturation only at lightness extremes (black/white) where * it has no visual effect. */ 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; } const options = [ { label: "RGB", value: "rgb" }, { label: "HSL", value: "hsl" }, { label: "Hex", value: "hex" } ]; const UnconnectedColorPicker = (props, forwardedRef) => { (0, _wordpress_ui.useEnableWpCompatOverlaySlot)(); const { enableAlpha = false, color: colorProp, onChange, defaultValue = "#fff", copyFormat, className, ...divProps } = (0, ___context_index_ts.useContextSystem)(props, "ColorPicker"); const [color, setColor] = (0, ___utils_hooks_index_ts.useControlledValue)({ onChange, value: colorProp, defaultValue }); const safeColordColor = (0, _wordpress_element.useMemo)(() => { return (0, colord.colord)(color || ""); }, [color]); const [internalHSLA, setInternalHSLA] = (0, _wordpress_element.useState)(() => ({ ...safeColordColor.toHsl() })); const lastProducedHexRef = (0, _wordpress_element.useRef)(safeColordColor.toHex()); const isPickerInteractingRef = (0, _wordpress_element.useRef)(false); (0, _wordpress_element.useEffect)(() => { if (isPickerInteractingRef.current) return; if (safeColordColor.isEqual(lastProducedHexRef.current)) return; lastProducedHexRef.current = safeColordColor.toHex(); const externalHSLA = safeColordColor.toHsl(); setInternalHSLA((prev) => mergeHSLA(externalHSLA, prev)); }, [safeColordColor]); const handleHSLAChange = (0, _wordpress_element.useCallback)((nextHSLA) => { const previousHex = lastProducedHexRef.current; const isAlphaOnlyChange = nextHSLA.h === internalHSLA.h && nextHSLA.s === internalHSLA.s && nextHSLA.l === internalHSLA.l; setInternalHSLA(nextHSLA); const nextHex = isAlphaOnlyChange ? (0, colord.colord)(previousHex).alpha(nextHSLA.a).toHex() : (0, colord.colord)(nextHSLA).toHex(); if (!(0, colord.colord)(nextHex).isEqual(previousHex)) { lastProducedHexRef.current = nextHex; setColor(nextHex); } }, [internalHSLA, setColor]); const handleChange = (0, _wordpress_element.useCallback)((nextValue) => { const nextHSLA = nextValue.toHsl(); setInternalHSLA((prev) => mergeHSLA(nextHSLA, prev)); const nextHex = nextValue.toHex(); lastProducedHexRef.current = nextHex; setColor(nextHex); }, [setColor]); const [colorType, setColorType] = (0, _wordpress_element.useState)(copyFormat || "hex"); const maybeHandlePaste = (0, _wordpress_element.useCallback)((event) => { const pastedText = event.clipboardData?.getData("text")?.trim(); if (!pastedText) return; const parsedColor = (0, colord.colord)(pastedText); if (!parsedColor.isValid()) return; handleChange(parsedColor); const newColorType = { hex: "hex", rgb: "rgb", hsl: "hsl" }[String((0, colord.getFormat)(pastedText))]; if (newColorType) setColorType(newColorType); event.stopPropagation(); event.preventDefault(); }, [handleChange, setColorType]); return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(___utils_polymorphic_element_tsx.PolymorphicElement, { ref: forwardedRef, ...divProps, className: (0, clsx.default)(style_module_default.colorful, className), onPasteCapture: maybeHandlePaste, children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(__picker_tsx.Picker, { onChange: handleHSLAChange, hsla: internalHSLA, enableAlpha, onInteractionStart: () => { isPickerInteractingRef.current = true; }, onInteractionEnd: () => { isPickerInteractingRef.current = false; } }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", { className: style_module_default.auxiliary, children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(___h_stack_index_ts.HStack, { className: style_module_default.header, justify: "space-between", children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_ui.Select.Root, { items: options, value: colorType, onValueChange: (nextColorType) => { if (nextColorType !== null) setColorType(nextColorType); }, children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Select.Trigger, { size: "compact", variant: "minimal", "aria-label": (0, _wordpress_i18n.__)("Color format"), className: style_module_default.select }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Select.Popup, { width: "content", children: options.map((option) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Select.Item, { value: option.value, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Select.ItemLabel, { children: option.label }) }, option.value)) })] }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__color_copy_button_tsx.ColorCopyButton, { color: safeColordColor, colorType: copyFormat || colorType })] }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___flex_index_ts.Flex, { className: style_module_default["color-input"], direction: "column", gap: 2, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__color_input_tsx.ColorInput, { colorType, color: safeColordColor, hsla: internalHSLA, onChange: handleChange, onHSLChange: handleHSLAChange, enableAlpha }) })] })] }); }; /** * `ColorPicker` lets users select a color from a visual color surface, or by * editing its hex, RGB, or HSL values. */ const ColorPicker = (0, ___context_index_ts.contextConnect)(UnconnectedColorPicker, "ColorPicker"); exports.ColorPicker = ColorPicker; exports.default = ColorPicker; //# sourceMappingURL=component.cjs.map