UNPKG

@wordpress/components

Version:
352 lines (351 loc) • 15.2 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 clsx = require("clsx"); clsx = __toESM(clsx); let _wordpress_compose = require("@wordpress/compose"); let _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_element = require("@wordpress/element"); let ___dropdown_index_tsx = require("../dropdown/index.cjs"); ___dropdown_index_tsx = __toESM(___dropdown_index_tsx); let ___color_picker_index_ts = require("../color-picker/index.cjs"); let ___circular_option_picker_index_tsx = require("../circular-option-picker/index.cjs"); ___circular_option_picker_index_tsx = __toESM(___circular_option_picker_index_tsx); let ___v_stack_index_ts = require("../v-stack/index.cjs"); let ___truncate_index_ts = require("../truncate/index.cjs"); let ___heading_index_ts = require("../heading/index.cjs"); let ___dropdown_dropdown_content_wrapper_tsx = require("../dropdown/dropdown-content-wrapper.cjs"); ___dropdown_dropdown_content_wrapper_tsx = __toESM(___dropdown_dropdown_content_wrapper_tsx); let __utils_ts = require("./utils.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-palette/style.module.scss if (typeof process === "undefined" || process.env.NODE_ENV !== "test") registerStyle("ba988b4dc4", ".d557a658e7010f86__color-heading{text-transform:uppercase}.d557a658e7010f86__color-heading.d557a658e7010f86__color-heading.d557a658e7010f86__color-heading{font-size:11px;line-height:24px;margin-bottom:0}"); var style_module_default = { "color-heading": "d557a658e7010f86__color-heading" }; // packages/components/src/color-palette/index.tsx (0, colord.extend)([colord_plugins_names.default, colord_plugins_a11y.default]); function SinglePalette({ className, clearColor, colors, onChange, value, selectedSlug, presentation, ...additionalProps }) { const colorOptions = (0, _wordpress_element.useMemo)(() => { return colors.map(({ color, name, slug }, index) => { const colordColor = (0, colord.colord)(color); const isSelected = presentation !== "command-buttons" && (selectedSlug ? slug === selectedSlug : value === color); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___circular_option_picker_index_tsx.default.Option, { isSelected, selectedIconProps: isSelected ? { color: colordColor.contrast() > colordColor.contrast("#000") ? "#fff" : "#000" } : {}, tooltipText: name || (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Color code: %s"), color), style: { backgroundColor: color, color }, onClick: isSelected ? clearColor : () => onChange(color, index, slug) }, slug ?? `${color}-${index}`); }); }, [ colors, value, selectedSlug, onChange, clearColor, presentation ]); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___circular_option_picker_index_tsx.default.OptionGroup, { className, options: colorOptions, ...additionalProps }); } function MultiplePalettes({ className, clearColor, colors, onChange, value, selectedSlug, headingLevel, presentation }) { const instanceId = (0, _wordpress_compose.useInstanceId)(MultiplePalettes, "color-palette"); if (colors.length === 0) return null; return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___v_stack_index_ts.VStack, { spacing: 3, className, children: colors.map(({ name, colors: colorPalette }, index) => { const id = `${instanceId}-${index}`; return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(___v_stack_index_ts.VStack, { spacing: 2, children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(___heading_index_ts.Heading, { className: style_module_default["color-heading"], id, level: headingLevel, children: name }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SinglePalette, { clearColor, colors: colorPalette, onChange: (newColor, _colorIndex, slug) => onChange(newColor, index, slug), value, selectedSlug, presentation, "aria-labelledby": id })] }, index); }) }); } function CustomColorPickerDropdown({ isRenderedInSidebar, popoverProps: receivedPopoverProps, ...props }) { const popoverProps = (0, _wordpress_element.useMemo)(() => ({ shift: true, resize: false, ...isRenderedInSidebar ? { placement: "left-start", offset: 34 } : { placement: "bottom", offset: 8 }, ...receivedPopoverProps }), [isRenderedInSidebar, receivedPopoverProps]); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___dropdown_index_tsx.default, { contentClassName: "components-color-palette__custom-color-dropdown-content", popoverProps, ...props }); } function UnforwardedColorPalette(props, forwardedRef) { const { asButtons, presentation, loop, clearable = true, colors = [], disableCustomColors = false, enableAlpha = false, onChange, value, selectedSlug, __experimentalIsRenderedInSidebar = false, headingLevel = 2, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledby, ...additionalProps } = props; (0, ___circular_option_picker_index_tsx.warnIfCircularOptionPickerAsButtonsIsSet)("ColorPalette", asButtons); const [normalizedColorValue, setNormalizedColorValue] = (0, _wordpress_element.useState)(value); const clearColor = (0, _wordpress_element.useCallback)(() => onChange(void 0), [onChange]); const customColorPaletteCallbackRef = (0, _wordpress_element.useCallback)((node) => { setNormalizedColorValue((0, __utils_ts.normalizeColorValue)(value, node)); }, [value]); const hasMultipleColorOrigins = (0, __utils_ts.isMultiplePaletteArray)(colors); const buttonLabelName = (0, _wordpress_element.useMemo)(() => (0, __utils_ts.extractColorNameFromCurrentValue)(value, colors, hasMultipleColorOrigins, selectedSlug), [ value, colors, hasMultipleColorOrigins, selectedSlug ]); const renderCustomColorPicker = () => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___dropdown_dropdown_content_wrapper_tsx.default, { paddingSize: "none", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___color_picker_index_ts.ColorPicker, { color: normalizedColorValue, onChange: (color) => onChange(color), enableAlpha }) }); const isHex = value?.startsWith("#"); const displayValue = value?.replace(/^var\((.+)\)$/, "$1"); const customColorAccessibleLabel = !!displayValue ? (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Custom color picker. The currently selected color is called \"%1$s\" and has a value of \"%2$s\"."), buttonLabelName, displayValue) : (0, _wordpress_i18n.__)("Custom color picker"); const { metaProps, labelProps, resolvedPresentation } = (0, ___circular_option_picker_index_tsx.getComputeCircularOptionPickerCommonProps)(asButtons, loop, ariaLabel, ariaLabelledby, presentation); const paletteCommonProps = { clearColor, onChange, value, selectedSlug, presentation: resolvedPresentation }; const actions = !!clearable && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___circular_option_picker_index_tsx.default.ButtonAction, { onClick: clearColor, accessibleWhenDisabled: true, disabled: !value, children: (0, _wordpress_i18n.__)("Clear") }); if (disableCustomColors && colors.length === 0 && !actions) return null; return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(___v_stack_index_ts.VStack, { spacing: 3, ref: forwardedRef, ...additionalProps, children: [!disableCustomColors && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(CustomColorPickerDropdown, { isRenderedInSidebar: __experimentalIsRenderedInSidebar, renderContent: renderCustomColorPicker, renderToggle: ({ isOpen, onToggle }) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(___v_stack_index_ts.VStack, { className: "components-color-palette__custom-color-wrapper", spacing: 0, children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", { ref: customColorPaletteCallbackRef, className: "components-color-palette__custom-color-button", "aria-expanded": isOpen, "aria-haspopup": "true", onClick: onToggle, "aria-label": customColorAccessibleLabel, style: { background: value }, type: "button" }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(___v_stack_index_ts.VStack, { className: "components-color-palette__custom-color-text-wrapper", spacing: .5, children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(___truncate_index_ts.Truncate, { className: "components-color-palette__custom-color-name", children: value ? buttonLabelName : (0, _wordpress_i18n.__)("No color selected") }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___truncate_index_ts.Truncate, { className: (0, clsx.default)("components-color-palette__custom-color-value", { "components-color-palette__custom-color-value--is-hex": isHex }), children: displayValue })] })] }) }), (colors.length > 0 || actions) && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___circular_option_picker_index_tsx.default, { ...metaProps, ...labelProps, actions, options: hasMultipleColorOrigins ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MultiplePalettes, { ...paletteCommonProps, headingLevel, colors, value }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SinglePalette, { ...paletteCommonProps, colors, value }) })] }); } /** * Allows the user to pick a color from a list of pre-defined color entries. * * ```jsx * import { ColorPalette } from '@wordpress/components'; * import { useState } from '@wordpress/element'; * * const MyColorPalette = () => { * const [ color, setColor ] = useState ( '#f00' ) * const colors = [ * { name: 'red', color: '#f00' }, * { name: 'white', color: '#fff' }, * { name: 'blue', color: '#00f' }, * ]; * return ( * <ColorPalette * colors={ colors } * value={ color } * onChange={ ( color ) => setColor( color ) } * /> * ); * } ); * ``` */ const ColorPalette = (0, _wordpress_element.forwardRef)(UnforwardedColorPalette); ColorPalette.displayName = "ColorPalette"; exports.ColorPalette = ColorPalette; exports.default = ColorPalette; exports.CustomColorPickerDropdown = CustomColorPickerDropdown; //# sourceMappingURL=index.cjs.map