UNPKG

wix-style-react

Version:
115 lines (96 loc) 4.63 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, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import { withFocusable } from "wix-ui-core/dist/es/src/hocs/Focusable/FocusableHOC"; import AddSmall from 'wix-ui-icons-common/AddSmall'; import Color from 'color'; import { st, classes } from './ColorPickerConverter.st.css'; import Tooltip from '../Tooltip'; import { DataHooks } from './constants'; function getContrastColor(bg) { var light = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '#ffffff'; var dark = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '#162d3d'; try { var color = Color(bg); var luminosity = color.luminosity(); if (luminosity > 0.5) { return dark; } else { return light; } } catch (err) {} } var ColorPickerConverterViewer = /*#__PURE__*/function (_PureComponent) { _inherits(ColorPickerConverterViewer, _PureComponent); var _super = _createSuper(ColorPickerConverterViewer); function ColorPickerConverterViewer() { var _this; _classCallCheck(this, ColorPickerConverterViewer); 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), "onAddClick", function () { var _this$props = _this.props, color = _this$props.color, onAdd = _this$props.onAdd; var noColorSelected = color.alpha() === 0; !!onAdd && !noColorSelected && onAdd(color.hex()); }); _defineProperty(_assertThisInitialized(_this), "addTooltip", function (element) { var addTooltipContent = _this.props.addTooltipContent; return /*#__PURE__*/React.createElement(Tooltip, { disabled: !addTooltipContent, content: addTooltipContent, size: "small" }, element); }); return _this; } _createClass(ColorPickerConverterViewer, [{ key: "render", value: function render() { var _this$props2 = this.props, color = _this$props2.color, onAdd = _this$props2.onAdd, focusableOnFocus = _this$props2.focusableOnFocus, focusableOnBlur = _this$props2.focusableOnBlur, className = _this$props2.className; var noColorSelected = color.alpha() === 0; var clickable = !!onAdd && !noColorSelected; var viewer = /*#__PURE__*/React.createElement(clickable ? 'button' : 'div', { style: { backgroundColor: noColorSelected ? undefined : color.hex() }, 'data-hook': DataHooks.addColor, onFocus: focusableOnFocus, onBlur: focusableOnBlur, className: st(classes.preview, { clickable: clickable, noColorSelected: noColorSelected }, className), onClick: this.onAddClick }, clickable && /*#__PURE__*/React.createElement(AddSmall, { style: { color: getContrastColor(color.hex()) } })); return clickable ? this.addTooltip(viewer) : viewer; } }]); return ColorPickerConverterViewer; }(PureComponent); _defineProperty(ColorPickerConverterViewer, "propTypes", { color: PropTypes.object, onAdd: PropTypes.func, addTooltipContent: PropTypes.node }); export default withFocusable(ColorPickerConverterViewer);