UNPKG

@wordpress/components

Version:
212 lines (210 loc) 8.81 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/border-control/border-control-dropdown/component.tsx var component_exports = {}; __export(component_exports, { default: () => component_default }); module.exports = __toCommonJS(component_exports); var import_i18n = require("@wordpress/i18n"); var import_border_control_style_picker = __toESM(require("../border-control-style-picker")); var import_button = __toESM(require("../../button")); var import_color_indicator = __toESM(require("../../color-indicator")); var import_color_palette = __toESM(require("../../color-palette")); var import_dropdown = __toESM(require("../../dropdown")); var import_v_stack = require("../../v-stack"); var import_context = require("../../context"); var import_hook = require("./hook"); var import_dropdown_content_wrapper = __toESM(require("../../dropdown/dropdown-content-wrapper")); var import_utils = require("../../color-palette/utils"); var import_jsx_runtime = require("react/jsx-runtime"); var getAriaLabelColorValue = (colorValue) => { return colorValue.replace(/^var\((.+)\)$/, "$1"); }; var getColorObject = (colorValue, colors) => { if (!colorValue || !colors) { return; } if ((0, import_utils.isMultiplePaletteArray)(colors)) { let matchedColor; colors.some((origin) => origin.colors.some((color) => { if (color.color === colorValue) { matchedColor = color; return true; } return false; })); return matchedColor; } return colors.find((color) => color.color === colorValue); }; var getToggleAriaLabel = (colorValue, colorObject, style, isStyleEnabled) => { if (isStyleEnabled) { if (colorObject) { const ariaLabelValue = getAriaLabelColorValue(colorObject.color); return style ? (0, import_i18n.sprintf)( // translators: 1: The name of the color e.g. "vivid red". 2: The color's hex code e.g.: "#f00:". 3: The current border style selection e.g. "solid". (0, import_i18n.__)('Border color and style picker. The currently selected color is called "%1$s" and has a value of "%2$s". The currently selected style is "%3$s".'), colorObject.name, ariaLabelValue, style ) : (0, import_i18n.sprintf)( // translators: 1: The name of the color e.g. "vivid red". 2: The color's hex code e.g.: "#f00:". (0, import_i18n.__)('Border color and style picker. The currently selected color is called "%1$s" and has a value of "%2$s".'), colorObject.name, ariaLabelValue ); } if (colorValue) { const ariaLabelValue = getAriaLabelColorValue(colorValue); return style ? (0, import_i18n.sprintf)( // translators: 1: The color's hex code e.g.: "#f00:". 2: The current border style selection e.g. "solid". (0, import_i18n.__)('Border color and style picker. The currently selected color has a value of "%1$s". The currently selected style is "%2$s".'), ariaLabelValue, style ) : (0, import_i18n.sprintf)( // translators: %s: The color's hex code e.g: "#f00". (0, import_i18n.__)('Border color and style picker. The currently selected color has a value of "%s".'), ariaLabelValue ); } return (0, import_i18n.__)("Border color and style picker."); } if (colorObject) { return (0, import_i18n.sprintf)( // translators: 1: The name of the color e.g. "vivid red". 2: The color's hex code e.g: "#f00". (0, import_i18n.__)('Border color picker. The currently selected color is called "%1$s" and has a value of "%2$s".'), colorObject.name, getAriaLabelColorValue(colorObject.color) ); } if (colorValue) { return (0, import_i18n.sprintf)( // translators: %s: The color's hex code e.g: "#f00". (0, import_i18n.__)('Border color picker. The currently selected color has a value of "%s".'), getAriaLabelColorValue(colorValue) ); } return (0, import_i18n.__)("Border color picker."); }; var BorderControlDropdown = (props, forwardedRef) => { const { __experimentalIsRenderedInSidebar, border, colors, disableCustomColors, enableAlpha, enableStyle, indicatorClassName, indicatorWrapperClassName, isStyleSettable, onReset, onColorChange, onStyleChange, popoverContentClassName, popoverControlsClassName, resetButtonWrapperClassName, size, __unstablePopoverProps, ...otherProps } = (0, import_hook.useBorderControlDropdown)(props); const { color, style } = border || {}; const colorObject = getColorObject(color, colors); const toggleAriaLabel = getToggleAriaLabel(color, colorObject, style, enableStyle); const enableResetButton = color || style && style !== "none"; const dropdownPosition = __experimentalIsRenderedInSidebar ? "bottom left" : void 0; const renderToggle = ({ onToggle }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_button.default, { onClick: onToggle, variant: "tertiary", "aria-label": toggleAriaLabel, tooltipPosition: dropdownPosition, label: (0, import_i18n.__)("Border color and style picker"), showTooltip: true, __next40pxDefaultSize: size === "__unstable-large", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: indicatorWrapperClassName, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_color_indicator.default, { className: indicatorClassName, colorValue: color }) }) }); const renderContent = () => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_dropdown_content_wrapper.default, { paddingSize: "medium", children: [/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_v_stack.VStack, { className: popoverControlsClassName, spacing: 6, children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_color_palette.default, { className: popoverContentClassName, value: color, onChange: onColorChange, colors, disableCustomColors, __experimentalIsRenderedInSidebar, clearable: false, enableAlpha }), enableStyle && isStyleSettable && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_border_control_style_picker.default, { label: (0, import_i18n.__)("Style"), value: style, onChange: onStyleChange })] }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: resetButtonWrapperClassName, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_button.default, { variant: "tertiary", onClick: () => { onReset(); }, disabled: !enableResetButton, accessibleWhenDisabled: true, __next40pxDefaultSize: true, children: (0, import_i18n.__)("Reset") }) })] }) }); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_dropdown.default, { renderToggle, renderContent, popoverProps: { ...__unstablePopoverProps }, ...otherProps, ref: forwardedRef }); }; var ConnectedBorderControlDropdown = (0, import_context.contextConnect)(BorderControlDropdown, "BorderControlDropdown"); var component_default = ConnectedBorderControlDropdown; //# sourceMappingURL=component.js.map