UNPKG

wix-style-react

Version:
131 lines (104 loc) 5.03 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } import React from 'react'; import color from 'color'; import clamp from 'lodash/clamp'; import PropTypes from 'prop-types'; import { classes } from './ColorPickerHue.st.css'; var ColorPickerHue = /*#__PURE__*/function (_React$PureComponent) { _inherits(ColorPickerHue, _React$PureComponent); var _super = _createSuper(ColorPickerHue); function ColorPickerHue() { var _this; _classCallCheck(this, ColorPickerHue); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _defineProperty(_assertThisInitialized(_this), "onMarkerDragStart", function (e) { e.preventDefault(); window.addEventListener('mousemove', _this.setNewColorByMouseEvent); window.addEventListener('touchmove', _this.setNewColorByMouseEvent); window.addEventListener('mouseup', _this.onMarkerDragEnd); window.addEventListener('touchcancel', _this.onMarkerDragEnd); window.addEventListener('touchend', _this.onMarkerDragEnd); _this.sliderRect = _this.slider.getBoundingClientRect(); _this.setNewColorByMouseEvent(e); }); _defineProperty(_assertThisInitialized(_this), "onMarkerDragEnd", function () { window.removeEventListener('touchmove', _this.setNewColorByMouseEvent); window.removeEventListener('mousemove', _this.setNewColorByMouseEvent); window.removeEventListener('touchcancel', _this.onMarkerDragEnd); window.removeEventListener('touchend', _this.onMarkerDragEnd); window.removeEventListener('mouseup', _this.onMarkerDragEnd); }); _defineProperty(_assertThisInitialized(_this), "getHueByMouseEvent", function (e) { var eventX = 0; if (e.clientX) { eventX = e.clientX; } else { eventX = e.touches[0].clientX; } var x = eventX - _this.sliderRect.left; return clamp(360 * x / _this.sliderRect.width, 0, 359); }); _defineProperty(_assertThisInitialized(_this), "setNewColorByMouseEvent", function (e) { var _this$props = _this.props, onChange = _this$props.onChange, current = _this$props.current; var h = _this.getHueByMouseEvent(e); onChange(color({ h: h, s: current.saturationv(), v: current.value() })); }); return _this; } _createClass(ColorPickerHue, [{ key: "componentWillUnmount", value: function componentWillUnmount() { this.onMarkerDragEnd(); } }, { key: "render", value: function render() { var _this2 = this; var _this$props2 = this.props, dataHook = _this$props2.dataHook, current = _this$props2.current; // HUE is an integer value from 0 to 360. var percentage = current.hue() / 360 * 100; return /*#__PURE__*/React.createElement("div", { className: classes.root, "data-hook": dataHook, ref: function ref(e) { return _this2.slider = e; }, onMouseDown: this.onMarkerDragStart, onTouchStart: this.onMarkerDragStart }, /*#__PURE__*/React.createElement("div", { className: classes.handle, style: { left: "".concat(percentage, "%") } })); } }]); return ColorPickerHue; }(React.PureComponent); _defineProperty(ColorPickerHue, "propTypes", { /** Applied as data-hook HTML attribute that can be used to create driver in testing */ dataHook: PropTypes.string, /** The current Hue value */ current: PropTypes.object.isRequired, /** A callback function that will be triggered when the value is changed */ onChange: PropTypes.func.isRequired }); export { ColorPickerHue as default };