@wordpress/block-editor
Version:
240 lines (238 loc) • 8.93 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/block-editor/src/components/border-radius-control/single-input-control.js
var single_input_control_exports = {};
__export(single_input_control_exports, {
default: () => SingleInputControl
});
module.exports = __toCommonJS(single_input_control_exports);
var import_components = require("@wordpress/components");
var import_i18n = require("@wordpress/i18n");
var import_element = require("@wordpress/element");
var import_icons = require("@wordpress/icons");
var import_utils = require("./utils");
var import_constants = require("./constants");
var import_jsx_runtime = require("react/jsx-runtime");
function SingleInputControl({
corner,
onChange,
selectedUnits,
setSelectedUnits,
values: valuesProp,
units,
presets
}) {
const changeCornerValue = (validatedValue) => {
if (corner === "all") {
onChange({
topLeft: validatedValue,
topRight: validatedValue,
bottomLeft: validatedValue,
bottomRight: validatedValue
});
} else {
onChange({
...values,
[corner]: validatedValue
});
}
};
const onChangeValue = (next) => {
if (!onChange) {
return;
}
const isNumeric = !isNaN(parseFloat(next));
const nextValue = isNumeric ? next : void 0;
changeCornerValue(nextValue);
};
const onChangeUnit = (next) => {
const newUnits = { ...selectedUnits };
if (corner === "all") {
newUnits.flat = next;
newUnits.topLeft = next;
newUnits.topRight = next;
newUnits.bottomLeft = next;
newUnits.bottomRight = next;
} else {
newUnits[corner] = next;
}
setSelectedUnits(newUnits);
};
const values = typeof valuesProp !== "string" ? valuesProp : {
topLeft: valuesProp,
topRight: valuesProp,
bottomLeft: valuesProp,
bottomRight: valuesProp
};
let value;
if (corner === "all") {
const convertedValues = (0, import_utils.convertPresetsToCustomValues)(values, presets);
const customValue = (0, import_utils.getAllValue)(convertedValues);
value = (0, import_utils.getPresetValueFromCustomValue)(customValue, presets);
} else {
value = (0, import_utils.getPresetValueFromCustomValue)(values[corner], presets);
}
const resolvedPresetValue = (0, import_utils.isValuePreset)(value) ? (0, import_utils.getCustomValueFromPreset)(value, presets) : value;
const [parsedQuantity, parsedUnit] = (0, import_components.__experimentalParseQuantityAndUnitFromRawValue)(resolvedPresetValue);
const computedUnit = value ? parsedUnit : selectedUnits[corner] || selectedUnits.flat || "px";
const unitConfig = units && units.find((item) => item.value === computedUnit);
const step = unitConfig?.step || 1;
const [showCustomValueControl, setShowCustomValueControl] = (0, import_element.useState)(
value !== void 0 && !(0, import_utils.isValuePreset)(value)
);
const showRangeControl = presets.length <= import_constants.RANGE_CONTROL_MAX_SIZE;
const presetIndex = (0, import_utils.getSliderValueFromPreset)(value, presets);
const rangeTooltip = (newValue) => value === void 0 ? void 0 : presets[newValue]?.name;
const marks = presets.slice(1, presets.length - 1).map((_newValue, index) => ({
value: index + 1,
label: void 0
}));
const hasPresets = marks.length > 0;
let options = [];
if (!showRangeControl) {
options = [
...presets,
{
name: (0, import_i18n.__)("Custom"),
slug: "custom",
size: resolvedPresetValue
}
].map((size, index) => ({
key: index,
name: size.name
}));
}
const icon = import_constants.ICONS[corner];
const handleSliderChange = (next) => {
const val = next !== void 0 ? `${next}${computedUnit}` : void 0;
changeCornerValue(val);
};
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.__experimentalHStack, { children: [
icon && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.Icon,
{
className: "components-border-radius-control__icon",
icon,
size: 24
}
),
(!hasPresets || showCustomValueControl) && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "components-border-radius-control__input-controls-wrapper", children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Tooltip, { text: import_constants.CORNERS[corner], placement: "top", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "components-border-radius-control__tooltip-wrapper", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.__experimentalUnitControl,
{
className: "components-border-radius-control__unit-control",
"aria-label": import_constants.CORNERS[corner],
value: [parsedQuantity, computedUnit].join(
""
),
onChange: onChangeValue,
onUnitChange: onChangeUnit,
size: "__unstable-large",
min: import_constants.MIN_BORDER_RADIUS_VALUE,
units
}
) }) }),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.RangeControl,
{
__next40pxDefaultSize: true,
label: (0, import_i18n.__)("Border radius"),
hideLabelFromVision: true,
className: "components-border-radius-control__range-control",
value: parsedQuantity ?? "",
min: import_constants.MIN_BORDER_RADIUS_VALUE,
max: import_constants.MAX_BORDER_RADIUS_VALUES[computedUnit],
initialPosition: 0,
withInputField: false,
onChange: handleSliderChange,
step,
__nextHasNoMarginBottom: true
}
)
] }),
hasPresets && showRangeControl && !showCustomValueControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.RangeControl,
{
__next40pxDefaultSize: true,
className: "components-border-radius-control__range-control",
value: presetIndex,
onChange: (newSize) => {
changeCornerValue(
(0, import_utils.getPresetValueFromControlValue)(
newSize,
"range",
presets
)
);
},
withInputField: false,
"aria-valuenow": presetIndex,
"aria-valuetext": presets[presetIndex]?.name,
renderTooltipContent: rangeTooltip,
min: 0,
max: presets.length - 1,
marks,
label: import_constants.CORNERS[corner],
hideLabelFromVision: true,
__nextHasNoMarginBottom: true
}
),
!showRangeControl && !showCustomValueControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.CustomSelectControl,
{
className: "components-border-radius-control__custom-select-control",
value: options.find(
(option) => option.key === presetIndex
) || options[options.length - 1],
onChange: (selection) => {
if (selection.selectedItem.key === options.length - 1) {
setShowCustomValueControl(true);
} else {
changeCornerValue(
(0, import_utils.getPresetValueFromControlValue)(
selection.selectedItem.key,
"selectList",
presets
)
);
}
},
options,
label: import_constants.CORNERS[corner],
hideLabelFromVision: true,
size: "__unstable-large"
}
),
hasPresets && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.Button,
{
label: showCustomValueControl ? (0, import_i18n.__)("Use border radius preset") : (0, import_i18n.__)("Set custom border radius"),
icon: import_icons.settings,
onClick: () => {
setShowCustomValueControl(!showCustomValueControl);
},
isPressed: showCustomValueControl,
size: "small",
className: "components-border-radius-control__custom-toggle",
iconSize: 24
}
)
] });
}
//# sourceMappingURL=single-input-control.js.map