@wordpress/components
Version:
UI components for WordPress.
299 lines (297 loc) • 11.5 kB
JavaScript
;
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/range-control/index.tsx
var range_control_exports = {};
__export(range_control_exports, {
RangeControl: () => RangeControl,
default: () => range_control_default
});
module.exports = __toCommonJS(range_control_exports);
var import_clsx = __toESM(require("clsx"));
var import_i18n = require("@wordpress/i18n");
var import_element = require("@wordpress/element");
var import_compose = require("@wordpress/compose");
var import_base_control = __toESM(require("../base-control"));
var import_button = __toESM(require("../button"));
var import_icon = __toESM(require("../icon"));
var import_utils = require("../utils");
var import_utils2 = require("./utils");
var import_math = require("../utils/math");
var import_input_range = __toESM(require("./input-range"));
var import_rail = __toESM(require("./rail"));
var import_tooltip = __toESM(require("./tooltip"));
var import_range_control_styles = require("./styles/range-control-styles");
var import_space = require("../utils/space");
var import_deprecated_36px_size = require("../utils/deprecated-36px-size");
var import_jsx_runtime = require("react/jsx-runtime");
var noop = () => {
};
function computeResetValue({
resetFallbackValue,
initialPosition
}) {
if (resetFallbackValue !== void 0) {
return !Number.isNaN(resetFallbackValue) ? resetFallbackValue : null;
}
if (initialPosition !== void 0) {
return !Number.isNaN(initialPosition) ? initialPosition : null;
}
return null;
}
function UnforwardedRangeControl(props, forwardedRef) {
const {
__nextHasNoMarginBottom = false,
afterIcon,
allowReset = false,
beforeIcon,
className,
color: colorProp = import_utils.COLORS.theme.accent,
currentInput,
disabled = false,
help,
hideLabelFromVision = false,
initialPosition,
isShiftStepEnabled = true,
label,
marks = false,
max = 100,
min = 0,
onBlur = noop,
onChange = noop,
onFocus = noop,
onMouseLeave = noop,
onMouseMove = noop,
railColor,
renderTooltipContent = (v) => v,
resetFallbackValue,
__next40pxDefaultSize = false,
shiftStep = 10,
showTooltip: showTooltipProp,
step = 1,
trackColor,
value: valueProp,
withInputField = true,
__shouldNotWarnDeprecated36pxSize,
...otherProps
} = props;
const [value, setValue] = (0, import_utils2.useControlledRangeValue)({
min,
max,
value: valueProp !== null && valueProp !== void 0 ? valueProp : null,
initial: initialPosition
});
const isResetPendent = (0, import_element.useRef)(false);
let hasTooltip = showTooltipProp;
let hasInputField = withInputField;
if (step === "any") {
hasTooltip = false;
hasInputField = false;
}
const [showTooltip, setShowTooltip] = (0, import_element.useState)(hasTooltip);
const [isFocused, setIsFocused] = (0, import_element.useState)(false);
const inputRef = (0, import_element.useRef)();
const isCurrentlyFocused = inputRef.current?.matches(":focus");
const isThumbFocused = !disabled && isFocused;
const isValueReset = value === null;
const currentValue = value !== void 0 ? value : currentInput;
const inputSliderValue = isValueReset ? "" : currentValue;
const rangeFillValue = isValueReset ? (max - min) / 2 + min : value;
const fillValue = isValueReset ? 50 : (value - min) / (max - min) * 100;
const fillValueOffset = `${(0, import_math.clamp)(fillValue, 0, 100)}%`;
const classes = (0, import_clsx.default)("components-range-control", className);
const wrapperClasses = (0, import_clsx.default)("components-range-control__wrapper", !!marks && "is-marked");
const id = (0, import_compose.useInstanceId)(UnforwardedRangeControl, "inspector-range-control");
const describedBy = !!help ? `${id}__help` : void 0;
const enableTooltip = hasTooltip !== false && Number.isFinite(value);
const handleOnRangeChange = (event) => {
const nextValue = parseFloat(event.target.value);
setValue(nextValue);
onChange(nextValue);
};
const handleOnChange = (next) => {
let nextValue = parseFloat(next);
setValue(nextValue);
if (!isNaN(nextValue)) {
if (nextValue < min || nextValue > max) {
nextValue = (0, import_utils2.floatClamp)(nextValue, min, max);
}
onChange(nextValue);
isResetPendent.current = false;
} else if (allowReset) {
isResetPendent.current = true;
}
};
const handleOnInputNumberBlur = () => {
if (isResetPendent.current) {
handleOnReset();
isResetPendent.current = false;
}
};
const handleOnReset = () => {
const resetValue = Number.isNaN(resetFallbackValue) ? null : resetFallbackValue !== null && resetFallbackValue !== void 0 ? resetFallbackValue : null;
setValue(resetValue);
onChange(resetValue !== null && resetValue !== void 0 ? resetValue : void 0);
};
const handleShowTooltip = () => setShowTooltip(true);
const handleHideTooltip = () => setShowTooltip(false);
const handleOnBlur = (event) => {
onBlur(event);
setIsFocused(false);
handleHideTooltip();
};
const handleOnFocus = (event) => {
onFocus(event);
setIsFocused(true);
handleShowTooltip();
};
const offsetStyle = {
[(0, import_i18n.isRTL)() ? "right" : "left"]: fillValueOffset
};
(0, import_deprecated_36px_size.maybeWarnDeprecated36pxSize)({
componentName: "RangeControl",
__next40pxDefaultSize,
size: void 0,
__shouldNotWarnDeprecated36pxSize
});
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_base_control.default, {
__nextHasNoMarginBottom,
__associatedWPComponentName: "RangeControl",
className: classes,
label,
hideLabelFromVision,
id: `${id}`,
help,
children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_range_control_styles.Root, {
className: "components-range-control__root",
__next40pxDefaultSize,
children: [beforeIcon && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_range_control_styles.BeforeIconWrapper, {
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icon.default, {
icon: beforeIcon
})
}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_range_control_styles.Wrapper, {
__nextHasNoMarginBottom,
className: wrapperClasses,
color: colorProp,
marks: !!marks,
children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_input_range.default, {
...otherProps,
className: "components-range-control__slider",
describedBy,
disabled,
id: `${id}`,
label,
max,
min,
onBlur: handleOnBlur,
onChange: handleOnRangeChange,
onFocus: handleOnFocus,
onMouseMove,
onMouseLeave,
ref: (0, import_compose.useMergeRefs)([inputRef, forwardedRef]),
step,
value: inputSliderValue !== null && inputSliderValue !== void 0 ? inputSliderValue : void 0
}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_rail.default, {
"aria-hidden": true,
disabled,
marks,
max,
min,
railColor,
step,
value: rangeFillValue
}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_range_control_styles.Track, {
"aria-hidden": true,
className: "components-range-control__track",
disabled,
style: {
width: fillValueOffset
},
trackColor
}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_range_control_styles.ThumbWrapper, {
className: "components-range-control__thumb-wrapper",
style: offsetStyle,
disabled,
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_range_control_styles.Thumb, {
"aria-hidden": true,
isFocused: isThumbFocused,
disabled
})
}), enableTooltip && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_tooltip.default, {
className: "components-range-control__tooltip",
inputRef,
tooltipPlacement: "bottom",
renderTooltipContent,
show: isCurrentlyFocused || showTooltip,
style: offsetStyle,
value
})]
}), afterIcon && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_range_control_styles.AfterIconWrapper, {
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icon.default, {
icon: afterIcon
})
}), hasInputField && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_range_control_styles.InputNumber, {
"aria-label": label,
className: "components-range-control__number",
disabled,
inputMode: "decimal",
isShiftStepEnabled,
max,
min,
onBlur: handleOnInputNumberBlur,
onChange: handleOnChange,
shiftStep,
size: __next40pxDefaultSize ? "__unstable-large" : "default",
__unstableInputWidth: __next40pxDefaultSize ? (0, import_space.space)(20) : (0, import_space.space)(16),
step,
value: inputSliderValue,
__shouldNotWarnDeprecated36pxSize: true
}), allowReset && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_range_control_styles.ActionRightWrapper, {
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_button.default, {
className: "components-range-control__reset",
accessibleWhenDisabled: !disabled,
disabled: disabled || value === computeResetValue({
resetFallbackValue,
initialPosition
}),
variant: "secondary",
size: "small",
onClick: handleOnReset,
children: (0, import_i18n.__)("Reset")
})
})]
})
});
}
var RangeControl = (0, import_element.forwardRef)(UnforwardedRangeControl);
var range_control_default = RangeControl;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
RangeControl
});
//# sourceMappingURL=index.js.map