UNPKG

wix-style-react

Version:
152 lines (126 loc) • 5.74 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 './ColorPickerHsb.st.css'; var ColorPickerHsb = /*#__PURE__*/function (_React$PureComponent) { _inherits(ColorPickerHsb, _React$PureComponent); var _super = _createSuper(ColorPickerHsb); function ColorPickerHsb() { var _this; _classCallCheck(this, ColorPickerHsb); 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('touchend', _this.onMarkerDragEnd); window.addEventListener('touchcancel', _this.onMarkerDragEnd); _this.gradientRect = _this.gradient.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), "getSVByMouseEvent", function (e) { var eventX = 0; var eventY = 0; if (e.clientX) { eventX = e.clientX; eventY = e.clientY; } else { eventX = e.touches[0].clientX; eventY = e.touches[0].clientY; } var x = eventX - _this.gradientRect.left; var y = eventY - _this.gradientRect.top; var s = clamp(100 * x / _this.gradientRect.width, 0, 100); var v = clamp(100 - 100 * y / _this.gradientRect.height, 0, 100); return { s: s, v: v }; }); _defineProperty(_assertThisInitialized(_this), "setNewColorByMouseEvent", function (e) { var _this$props = _this.props, onChange = _this$props.onChange, current = _this$props.current; var _this$getSVByMouseEve = _this.getSVByMouseEvent(e), s = _this$getSVByMouseEve.s, v = _this$getSVByMouseEve.v; onChange(color({ h: current.hue(), s: s, v: v })); }); return _this; } _createClass(ColorPickerHsb, [{ 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; var hue = current.saturationv(100).lightness(50); var style = { left: "".concat(current.saturationv(), "%"), top: "".concat(100 - current.value(), "%") }; return /*#__PURE__*/React.createElement("div", { className: classes.root, "data-hook": dataHook, ref: function ref(e) { return _this2.gradient = e; }, onMouseDown: this.onMarkerDragStart, onTouchStart: this.onMarkerDragStart }, /*#__PURE__*/React.createElement("div", { className: classes.hue, style: { background: hue.hex() } }), /*#__PURE__*/React.createElement("div", { className: classes.saturation }), /*#__PURE__*/React.createElement("div", { className: classes.brightness }), /*#__PURE__*/React.createElement("div", { className: classes.handle, style: style })); } }]); return ColorPickerHsb; }(React.PureComponent); _defineProperty(ColorPickerHsb, "propTypes", { /** Applied as data-hook HTML attribute that can be used to create driver in testing */ dataHook: PropTypes.string, /** The current Hsb value */ current: PropTypes.object.isRequired, /** A callback function that will be triggered when the value is changed */ onChange: PropTypes.func.isRequired }); export default ColorPickerHsb;