UNPKG

office-ui-fabric-react

Version:

Reusable React components for building experiences for Office 365.

86 lines 4.01 kB
import * as tslib_1 from "tslib"; import * as React from 'react'; import { BaseComponent, assign, autobind, css } from '../../Utilities'; import { MAX_COLOR_SATURATION, MAX_COLOR_VALUE, getFullColorString, hsv2hex } from '../../utilities/color/colors'; import * as stylesImport from './ColorPicker.scss'; var styles = stylesImport; var ColorRectangle = /** @class */ (function (_super) { tslib_1.__extends(ColorRectangle, _super); function ColorRectangle(props) { var _this = _super.call(this, props) || this; var color = _this.props.color; _this.state = { isAdjusting: false, origin: undefined, color: color, fullColorString: getFullColorString(color) }; return _this; } ColorRectangle.prototype.componentWillUnmount = function () { this._events.dispose(); }; ColorRectangle.prototype.componentWillReceiveProps = function (newProps) { var color = newProps.color; this.setState({ color: color, fullColorString: getFullColorString(color) }); }; ColorRectangle.prototype.render = function () { var minSize = this.props.minSize; var _a = this.state, color = _a.color, fullColorString = _a.fullColorString; return (React.createElement("div", { ref: this._resolveRef('_root'), className: css('ms-ColorPicker-colorRect', styles.colorRect), style: { minWidth: minSize, minHeight: minSize, backgroundColor: fullColorString }, onMouseDown: this._onMouseDown }, React.createElement("div", { className: css('ms-ColorPicker-light', styles.light) }), React.createElement("div", { className: css('ms-ColorPicker-dark', styles.dark) }), React.createElement("div", { className: css('ms-ColorPicker-thumb', styles.thumb), style: { left: color.s + '%', top: (MAX_COLOR_VALUE - color.v) + '%', backgroundColor: color.str } }))); }; ColorRectangle.prototype._onMouseDown = function (ev) { this._events.on(window, 'mousemove', this._onMouseMove, true); this._events.on(window, 'mouseup', this._onMouseUp, true); this._onMouseMove(ev); }; ColorRectangle.prototype._onMouseMove = function (ev) { var _a = this.props, color = _a.color, onSVChanged = _a.onSVChanged; var rectSize = this._root.getBoundingClientRect(); var sPercentage = (ev.clientX - rectSize.left) / rectSize.width; var vPercentage = (ev.clientY - rectSize.top) / rectSize.height; var newColor = assign({}, color, { s: Math.min(MAX_COLOR_SATURATION, Math.max(0, sPercentage * MAX_COLOR_SATURATION)), v: Math.min(MAX_COLOR_VALUE, Math.max(0, MAX_COLOR_VALUE - (vPercentage * MAX_COLOR_VALUE))), }); newColor.hex = hsv2hex(newColor.h, newColor.s, newColor.v); newColor.str = newColor.a === 100 ? '#' + newColor.hex : "rgba(" + newColor.r + ", " + newColor.g + ", " + newColor.b + ", " + newColor.a / 100 + ")"; this.setState({ isAdjusting: true, color: newColor }); if (onSVChanged) { onSVChanged(newColor.s, newColor.v); } ev.preventDefault(); ev.stopPropagation(); }; ColorRectangle.prototype._onMouseUp = function (ev) { this._events.off(); this.setState({ isAdjusting: false, origin: undefined }); }; ColorRectangle.defaultProps = { minSize: 220 }; tslib_1.__decorate([ autobind ], ColorRectangle.prototype, "_onMouseDown", null); tslib_1.__decorate([ autobind ], ColorRectangle.prototype, "_onMouseMove", null); tslib_1.__decorate([ autobind ], ColorRectangle.prototype, "_onMouseUp", null); return ColorRectangle; }(BaseComponent)); export { ColorRectangle }; //# sourceMappingURL=ColorRectangle.js.map