react-color
Version:
A Collection of Color Pickers from Sketch, Photoshop, Chrome & more
152 lines (134 loc) • 6.34 kB
JavaScript
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; }; }();
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; }
import React, { Component, PureComponent } from 'react';
import reactCSS from 'reactcss';
import throttle from 'lodash-es/throttle';
import * as saturation from '../../helpers/saturation';
export var 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 = throttle(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 = reactCSS({
'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 React.createElement(
'div',
{
style: styles.color,
ref: function ref(container) {
return _this2.container = container;
},
onMouseDown: this.handleMouseDown,
onTouchMove: this.handleChange,
onTouchStart: this.handleChange
},
React.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 '
),
React.createElement(
'div',
{ style: styles.white, className: 'saturation-white' },
React.createElement('div', { style: styles.black, className: 'saturation-black' }),
React.createElement(
'div',
{ style: styles.pointer },
this.props.pointer ? React.createElement(this.props.pointer, this.props) : React.createElement('div', { style: styles.circle })
)
)
);
}
}]);
return Saturation;
}(PureComponent || Component);
export default Saturation;