@wordpress/components
Version:
UI components for WordPress.
112 lines (111 loc) • 5.25 kB
JavaScript
import { __, sprintf } from "@wordpress/i18n";
import BorderControlStylePicker from "../border-control-style-picker/index.mjs";
import Button from "../../button/index.mjs";
import ColorIndicator from "../../color-indicator/index.mjs";
import ColorPalette from "../../color-palette/index.mjs";
import Dropdown from "../../dropdown/index.mjs";
import { VStack } from "../../v-stack/index.mjs";
import { contextConnect } from "../../context/index.mjs";
import { useBorderControlDropdown } from "./hook.mjs";
import DropdownContentWrapper from "../../dropdown/dropdown-content-wrapper.mjs";
import { isMultiplePaletteArray } from "../../color-palette/utils.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/components/src/border-control/border-control-dropdown/component.tsx
const getAriaLabelColorValue = (colorValue) => {
return colorValue.replace(/^var\((.+)\)$/, "$1");
};
const getColorObject = (colorValue, colors) => {
if (!colorValue || !colors) return;
if (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);
};
const getToggleAriaLabel = (colorValue, colorObject, style, isStyleEnabled) => {
if (isStyleEnabled) {
if (colorObject) {
const ariaLabelValue = getAriaLabelColorValue(colorObject.color);
return style ? sprintf(__("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) : sprintf(__("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 ? sprintf(__("Border color and style picker. The currently selected color has a value of \"%1$s\". The currently selected style is \"%2$s\"."), ariaLabelValue, style) : sprintf(__("Border color and style picker. The currently selected color has a value of \"%s\"."), ariaLabelValue);
}
return __("Border color and style picker.");
}
if (colorObject) return sprintf(__("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 sprintf(__("Border color picker. The currently selected color has a value of \"%s\"."), getAriaLabelColorValue(colorValue));
return __("Border color picker.");
};
const BorderControlDropdown = (props, forwardedRef) => {
const { __experimentalIsRenderedInSidebar, border, colors, disableCustomColors, enableAlpha, enableStyle, indicatorWrapperClassName, indicatorWrapperStyle, isStyleSettable, onReset, onColorChange, onStyleChange, popoverControlsClassName, resetButtonWrapperClassName, __unstablePopoverProps, onToggle: onToggleProp, ...otherProps } = 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__ */ jsx(Button, {
onClick: onToggle,
variant: "tertiary",
"aria-label": toggleAriaLabel,
tooltipPosition: dropdownPosition,
label: __("Border color and style picker"),
showTooltip: true,
__next40pxDefaultSize: true,
children: /* @__PURE__ */ jsx("span", {
className: indicatorWrapperClassName,
style: indicatorWrapperStyle,
children: /* @__PURE__ */ jsx(ColorIndicator, { colorValue: color })
})
});
const renderContent = () => /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsxs(DropdownContentWrapper, {
paddingSize: "medium",
children: [/* @__PURE__ */ jsxs(VStack, {
className: popoverControlsClassName,
spacing: 6,
children: [/* @__PURE__ */ jsx(ColorPalette, {
value: color,
onChange: onColorChange,
colors,
disableCustomColors,
__experimentalIsRenderedInSidebar,
clearable: false,
enableAlpha
}), enableStyle && isStyleSettable && /* @__PURE__ */ jsx(BorderControlStylePicker, {
label: __("Style"),
value: style,
onChange: onStyleChange
})]
}), /* @__PURE__ */ jsx("div", {
className: resetButtonWrapperClassName,
children: /* @__PURE__ */ jsx(Button, {
variant: "tertiary",
onClick: () => {
onReset();
},
disabled: !enableResetButton,
accessibleWhenDisabled: true,
__next40pxDefaultSize: true,
children: __("Reset")
})
})]
}) });
return /* @__PURE__ */ jsx(Dropdown, {
renderToggle,
renderContent,
popoverProps: { ...__unstablePopoverProps },
...otherProps,
ref: forwardedRef
});
};
const ConnectedBorderControlDropdown = contextConnect(BorderControlDropdown, "BorderControlDropdown");
export { ConnectedBorderControlDropdown as default };
//# sourceMappingURL=component.mjs.map