@wordpress/block-editor
Version:
242 lines (241 loc) • 7.56 kB
JavaScript
// packages/block-editor/src/components/border-radius-control/single-input-control.js
import {
__experimentalParseQuantityAndUnitFromRawValue as parseQuantityAndUnitFromRawValue,
__experimentalUnitControl as UnitControl,
__experimentalHStack as HStack,
Icon,
Tooltip,
RangeControl,
Button,
CustomSelectControl
} from "@wordpress/components";
import { __ } from "@wordpress/i18n";
import { useState } from "@wordpress/element";
import { settings } from "@wordpress/icons";
import {
getAllValue,
getCustomValueFromPreset,
getPresetValueFromControlValue,
getPresetValueFromCustomValue,
getSliderValueFromPreset,
isValuePreset,
convertPresetsToCustomValues
} from "./utils";
import {
CORNERS,
ICONS,
MIN_BORDER_RADIUS_VALUE,
MAX_BORDER_RADIUS_VALUES,
RANGE_CONTROL_MAX_SIZE
} from "./constants";
import { jsx, jsxs } from "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 = convertPresetsToCustomValues(values, presets);
const customValue = getAllValue(convertedValues);
value = getPresetValueFromCustomValue(customValue, presets);
} else {
value = getPresetValueFromCustomValue(values[corner], presets);
}
const resolvedPresetValue = isValuePreset(value) ? getCustomValueFromPreset(value, presets) : value;
const [parsedQuantity, parsedUnit] = parseQuantityAndUnitFromRawValue(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] = useState(
value !== void 0 && !isValuePreset(value)
);
const showRangeControl = presets.length <= RANGE_CONTROL_MAX_SIZE;
const presetIndex = 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: __("Custom"),
slug: "custom",
size: resolvedPresetValue
}
].map((size, index) => ({
key: index,
name: size.name
}));
}
const icon = ICONS[corner];
const handleSliderChange = (next) => {
const val = next !== void 0 ? `${next}${computedUnit}` : void 0;
changeCornerValue(val);
};
return /* @__PURE__ */ jsxs(HStack, { children: [
icon && /* @__PURE__ */ jsx(
Icon,
{
className: "components-border-radius-control__icon",
icon,
size: 24
}
),
(!hasPresets || showCustomValueControl) && /* @__PURE__ */ jsxs("div", { className: "components-border-radius-control__input-controls-wrapper", children: [
/* @__PURE__ */ jsx(Tooltip, { text: CORNERS[corner], placement: "top", children: /* @__PURE__ */ jsx("div", { className: "components-border-radius-control__tooltip-wrapper", children: /* @__PURE__ */ jsx(
UnitControl,
{
className: "components-border-radius-control__unit-control",
"aria-label": CORNERS[corner],
value: [parsedQuantity, computedUnit].join(
""
),
onChange: onChangeValue,
onUnitChange: onChangeUnit,
size: "__unstable-large",
min: MIN_BORDER_RADIUS_VALUE,
units
}
) }) }),
/* @__PURE__ */ jsx(
RangeControl,
{
__next40pxDefaultSize: true,
label: __("Border radius"),
hideLabelFromVision: true,
className: "components-border-radius-control__range-control",
value: parsedQuantity ?? "",
min: MIN_BORDER_RADIUS_VALUE,
max: MAX_BORDER_RADIUS_VALUES[computedUnit],
initialPosition: 0,
withInputField: false,
onChange: handleSliderChange,
step,
__nextHasNoMarginBottom: true
}
)
] }),
hasPresets && showRangeControl && !showCustomValueControl && /* @__PURE__ */ jsx(
RangeControl,
{
__next40pxDefaultSize: true,
className: "components-border-radius-control__range-control",
value: presetIndex,
onChange: (newSize) => {
changeCornerValue(
getPresetValueFromControlValue(
newSize,
"range",
presets
)
);
},
withInputField: false,
"aria-valuenow": presetIndex,
"aria-valuetext": presets[presetIndex]?.name,
renderTooltipContent: rangeTooltip,
min: 0,
max: presets.length - 1,
marks,
label: CORNERS[corner],
hideLabelFromVision: true,
__nextHasNoMarginBottom: true
}
),
!showRangeControl && !showCustomValueControl && /* @__PURE__ */ jsx(
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(
getPresetValueFromControlValue(
selection.selectedItem.key,
"selectList",
presets
)
);
}
},
options,
label: CORNERS[corner],
hideLabelFromVision: true,
size: "__unstable-large"
}
),
hasPresets && /* @__PURE__ */ jsx(
Button,
{
label: showCustomValueControl ? __("Use border radius preset") : __("Set custom border radius"),
icon: settings,
onClick: () => {
setShowCustomValueControl(!showCustomValueControl);
},
isPressed: showCustomValueControl,
size: "small",
className: "components-border-radius-control__custom-toggle",
iconSize: 24
}
)
] });
}
export {
SingleInputControl as default
};
//# sourceMappingURL=single-input-control.js.map