tdesign-react
Version:
TDesign Component for React
88 lines (84 loc) • 3.13 kB
JavaScript
/**
* tdesign v1.16.2
* (c) 2025 tdesign
* @license MIT
*/
import React, { useRef, useEffect } from 'react';
import classNames from 'classnames';
import { e as SLIDER_DEFAULT_WIDTH } from '../../../_chunks/dep-8def4f8a.js';
import useMouseEvent from '../../../hooks/useMouseEvent.js';
import useStyles from '../../hooks/useStyles.js';
import '../../../_chunks/dep-10d5731f.js';
import '../../../_chunks/dep-74a10cfb.js';
var ColorSlider = function ColorSlider(props) {
var color = props.color,
_props$className = props.className,
className = _props$className === void 0 ? "" : _props$className,
_props$value = props.value,
value = _props$value === void 0 ? 0 : _props$value,
_props$railStyle = props.railStyle,
railStyle = _props$railStyle === void 0 ? {} : _props$railStyle,
_props$maxValue = props.maxValue,
maxValue = _props$maxValue === void 0 ? 360 : _props$maxValue,
baseClassName = props.baseClassName,
disabled = props.disabled,
onChange = props.onChange,
type = props.type;
var panelRef = useRef(null);
var panelRectRef = useRef({
width: SLIDER_DEFAULT_WIDTH
});
var _useStyles = useStyles({
color: color,
value: value,
maxValue: maxValue,
type: type
}, panelRectRef),
styles = _useStyles.styles;
var handleDrag = function handleDrag(coordinate) {
if (disabled) return;
var width = panelRectRef.current.width;
var x = coordinate.x;
var value2 = Math.round(x / width * Number(maxValue) * 100) / 100;
onChange(value2);
};
useMouseEvent(panelRef, {
onDown: function onDown(_, ctx) {
if (disabled) return;
panelRectRef.current.width = panelRef.current.offsetWidth;
handleDrag(ctx.coordinate);
},
onMove: function onMove(_, ctx) {
handleDrag(ctx.coordinate);
},
onUp: function onUp(_, ctx) {
handleDrag(ctx.coordinate);
}
});
useEffect(function () {
panelRectRef.current.width = panelRef.current.offsetWidth || SLIDER_DEFAULT_WIDTH;
}, []);
var paddingStyle = {
background: "linear-gradient(90deg, rgba(0,0,0,.0) 0%, rgba(0,0,0,.0) 93%, ".concat(props.color.rgb, " 93%, ").concat(props.color.rgb, " 100%)")
};
return /* @__PURE__ */React.createElement("div", {
className: classNames("".concat(baseClassName, "__slider-wrapper"), "".concat(baseClassName, "__slider-wrapper--").concat(type, "-type"))
}, type === "alpha" && /* @__PURE__ */React.createElement("div", {
className: "".concat(baseClassName, "__slider-padding"),
style: paddingStyle
}), /* @__PURE__ */React.createElement("div", {
className: classNames("".concat(baseClassName, "__slider"), className),
ref: panelRef
}, /* @__PURE__ */React.createElement("div", {
className: "".concat(baseClassName, "__rail"),
style: railStyle
}), /* @__PURE__ */React.createElement("span", {
className: "".concat(baseClassName, "__thumb"),
role: "slider",
tabIndex: 0,
style: styles
})));
};
var ColorSlider$1 = /*#__PURE__*/React.memo(ColorSlider);
export { ColorSlider$1 as default };
//# sourceMappingURL=slider.js.map