UNPKG

react-color

Version:

A Collection of Color Pickers from Sketch, Photoshop, Chrome & more

174 lines (145 loc) 7.14 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Saturation = undefined; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _reactcss = require('reactcss'); var _reactcss2 = _interopRequireDefault(_reactcss); var _throttle = require('lodash/throttle'); var _throttle2 = _interopRequireDefault(_throttle); var _saturation = require('../../helpers/saturation'); var saturation = _interopRequireWildcard(_saturation); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var Saturation = exports.Saturation = function (_ref) { _inherits(Saturation, _ref); function Saturation(props) { _classCallCheck(this, Saturation); var _this = _possibleConstructorReturn(this, (Saturation.__proto__ || Object.getPrototypeOf(Saturation)).call(this, props)); _this.handleChange = function (e) { typeof _this.props.onChange === 'function' && _this.throttle(_this.props.onChange, saturation.calculateChange(e, _this.props.hsl, _this.container), e); }; _this.handleMouseDown = function (e) { _this.handleChange(e); var renderWindow = _this.getContainerRenderWindow(); renderWindow.addEventListener('mousemove', _this.handleChange); renderWindow.addEventListener('mouseup', _this.handleMouseUp); }; _this.handleMouseUp = function () { _this.unbindEventListeners(); }; _this.throttle = (0, _throttle2.default)(function (fn, data, e) { fn(data, e); }, 50); return _this; } _createClass(Saturation, [{ key: 'componentWillUnmount', value: function componentWillUnmount() { this.throttle.cancel(); this.unbindEventListeners(); } }, { key: 'getContainerRenderWindow', value: function getContainerRenderWindow() { var container = this.container; var renderWindow = window; while (!renderWindow.document.contains(container) && renderWindow.parent !== renderWindow) { renderWindow = renderWindow.parent; } return renderWindow; } }, { key: 'unbindEventListeners', value: function unbindEventListeners() { var renderWindow = this.getContainerRenderWindow(); renderWindow.removeEventListener('mousemove', this.handleChange); renderWindow.removeEventListener('mouseup', this.handleMouseUp); } }, { key: 'render', value: function render() { var _this2 = this; var _ref2 = this.props.style || {}, color = _ref2.color, white = _ref2.white, black = _ref2.black, pointer = _ref2.pointer, circle = _ref2.circle; var styles = (0, _reactcss2.default)({ 'default': { color: { absolute: '0px 0px 0px 0px', background: 'hsl(' + this.props.hsl.h + ',100%, 50%)', borderRadius: this.props.radius }, white: { absolute: '0px 0px 0px 0px', borderRadius: this.props.radius }, black: { absolute: '0px 0px 0px 0px', boxShadow: this.props.shadow, borderRadius: this.props.radius }, pointer: { position: 'absolute', top: -(this.props.hsv.v * 100) + 100 + '%', left: this.props.hsv.s * 100 + '%', cursor: 'default' }, circle: { width: '4px', height: '4px', boxShadow: '0 0 0 1.5px #fff, inset 0 0 1px 1px rgba(0,0,0,.3),\n 0 0 1px 2px rgba(0,0,0,.4)', borderRadius: '50%', cursor: 'hand', transform: 'translate(-2px, -2px)' } }, 'custom': { color: color, white: white, black: black, pointer: pointer, circle: circle } }, { 'custom': !!this.props.style }); return _react2.default.createElement( 'div', { style: styles.color, ref: function ref(container) { return _this2.container = container; }, onMouseDown: this.handleMouseDown, onTouchMove: this.handleChange, onTouchStart: this.handleChange }, _react2.default.createElement( 'style', null, '\n .saturation-white {\n background: -webkit-linear-gradient(to right, #fff, rgba(255,255,255,0));\n background: linear-gradient(to right, #fff, rgba(255,255,255,0));\n }\n .saturation-black {\n background: -webkit-linear-gradient(to top, #000, rgba(0,0,0,0));\n background: linear-gradient(to top, #000, rgba(0,0,0,0));\n }\n ' ), _react2.default.createElement( 'div', { style: styles.white, className: 'saturation-white' }, _react2.default.createElement('div', { style: styles.black, className: 'saturation-black' }), _react2.default.createElement( 'div', { style: styles.pointer }, this.props.pointer ? _react2.default.createElement(this.props.pointer, this.props) : _react2.default.createElement('div', { style: styles.circle }) ) ) ); } }]); return Saturation; }(_react.PureComponent || _react.Component); exports.default = Saturation;