@wordpress/components
Version:
UI components for WordPress.
133 lines (131 loc) • 3.89 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/rail.tsx
var rail_exports = {};
__export(rail_exports, {
default: () => RangeRail
});
module.exports = __toCommonJS(rail_exports);
var import_i18n = require("@wordpress/i18n");
var import_mark = __toESM(require("./mark"));
var import_range_control_styles = require("./styles/range-control-styles");
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
function RangeRail(props) {
const {
disabled = false,
marks = false,
min = 0,
max = 100,
step = 1,
value = 0,
...restProps
} = props;
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, {
children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_range_control_styles.Rail, {
disabled,
...restProps
}), marks && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Marks, {
disabled,
marks,
min,
max,
step,
value
})]
});
}
function Marks(props) {
const {
disabled = false,
marks = false,
min = 0,
max = 100,
step: stepProp = 1,
value = 0
} = props;
const step = stepProp === "any" ? 1 : stepProp;
const marksData = useMarks({
marks,
min,
max,
step,
value
});
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_range_control_styles.MarksWrapper, {
"aria-hidden": "true",
className: "components-range-control__marks",
children: marksData.map((mark) => /* @__PURE__ */ (0, import_react.createElement)(import_mark.default, {
...mark,
key: mark.key,
"aria-hidden": "true",
disabled
}))
});
}
function useMarks({
marks,
min = 0,
max = 100,
step = 1,
value = 0
}) {
if (!marks) {
return [];
}
const range = max - min;
if (!Array.isArray(marks)) {
marks = [];
const count = 1 + Math.round(range / step);
while (count > marks.push({
value: step * marks.length + min
})) {
}
}
const placedMarks = [];
marks.forEach((mark, index) => {
if (mark.value < min || mark.value > max) {
return;
}
const key = `mark-${index}`;
const isFilled = mark.value <= value;
const offset = `${(mark.value - min) / range * 100}%`;
const offsetStyle = {
[(0, import_i18n.isRTL)() ? "right" : "left"]: offset
};
placedMarks.push({
...mark,
isFilled,
key,
style: offsetStyle
});
});
return placedMarks;
}
//# sourceMappingURL=rail.js.map