react-color
Version:
A Collection of Color Pickers from Sketch, Photoshop, Chrome & more
127 lines (110 loc) • 5.8 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 * as hue from '../../helpers/hue';
export var Hue = function (_ref) {
_inherits(Hue, _ref);
function Hue() {
var _ref2;
var _temp, _this, _ret;
_classCallCheck(this, Hue);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = Hue.__proto__ || Object.getPrototypeOf(Hue)).call.apply(_ref2, [this].concat(args))), _this), _this.handleChange = function (e) {
var change = hue.calculateChange(e, _this.props.direction, _this.props.hsl, _this.container);
change && typeof _this.props.onChange === 'function' && _this.props.onChange(change, e);
}, _this.handleMouseDown = function (e) {
_this.handleChange(e);
window.addEventListener('mousemove', _this.handleChange);
window.addEventListener('mouseup', _this.handleMouseUp);
}, _this.handleMouseUp = function () {
_this.unbindEventListeners();
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(Hue, [{
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this.unbindEventListeners();
}
}, {
key: 'unbindEventListeners',
value: function unbindEventListeners() {
window.removeEventListener('mousemove', this.handleChange);
window.removeEventListener('mouseup', this.handleMouseUp);
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var _props$direction = this.props.direction,
direction = _props$direction === undefined ? 'horizontal' : _props$direction;
var styles = reactCSS({
'default': {
hue: {
absolute: '0px 0px 0px 0px',
borderRadius: this.props.radius,
boxShadow: this.props.shadow
},
container: {
padding: '0 2px',
position: 'relative',
height: '100%',
borderRadius: this.props.radius
},
pointer: {
position: 'absolute',
left: this.props.hsl.h * 100 / 360 + '%'
},
slider: {
marginTop: '1px',
width: '4px',
borderRadius: '1px',
height: '8px',
boxShadow: '0 0 2px rgba(0, 0, 0, .6)',
background: '#fff',
transform: 'translateX(-2px)'
}
},
'vertical': {
pointer: {
left: '0px',
top: -(this.props.hsl.h * 100 / 360) + 100 + '%'
}
}
}, { vertical: direction === 'vertical' });
return React.createElement(
'div',
{ style: styles.hue },
React.createElement(
'div',
{
className: 'hue-' + direction,
style: styles.container,
ref: function ref(container) {
return _this2.container = container;
},
onMouseDown: this.handleMouseDown,
onTouchMove: this.handleChange,
onTouchStart: this.handleChange
},
React.createElement(
'style',
null,
'\n .hue-horizontal {\n background: linear-gradient(to right, #f00 0%, #ff0 17%, #0f0\n 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00 100%);\n background: -webkit-linear-gradient(to right, #f00 0%, #ff0\n 17%, #0f0 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00 100%);\n }\n\n .hue-vertical {\n background: linear-gradient(to top, #f00 0%, #ff0 17%, #0f0 33%,\n #0ff 50%, #00f 67%, #f0f 83%, #f00 100%);\n background: -webkit-linear-gradient(to top, #f00 0%, #ff0 17%,\n #0f0 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00 100%);\n }\n '
),
React.createElement(
'div',
{ style: styles.pointer },
this.props.pointer ? React.createElement(this.props.pointer, this.props) : React.createElement('div', { style: styles.slider })
)
)
);
}
}]);
return Hue;
}(PureComponent || Component);
export default Hue;