UNPKG

@wordpress/components

Version:
202 lines (173 loc) 5.83 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.Hue = void 0; var _element = require("@wordpress/element"); var _lodash = require("lodash"); var _compose = require("@wordpress/compose"); var _i18n = require("@wordpress/i18n"); var _keycodes = require("@wordpress/keycodes"); var _utils = require("./utils"); var _keyboardShortcuts = _interopRequireDefault(require("../keyboard-shortcuts")); var _visuallyHidden = _interopRequireDefault(require("../visually-hidden")); /** * Parts of this source were derived and modified from react-color, * released under the MIT license. * * https://github.com/casesandberg/react-color/ * * Copyright (c) 2015 Case Sandberg * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ class Hue extends _element.Component { constructor() { super(...arguments); this.container = (0, _element.createRef)(); this.increase = this.increase.bind(this); this.decrease = this.decrease.bind(this); this.handleChange = this.handleChange.bind(this); this.handleMouseDown = this.handleMouseDown.bind(this); this.handleMouseUp = this.handleMouseUp.bind(this); } componentWillUnmount() { this.unbindEventListeners(); } increase(amount = 1) { const { hsl, onChange = _lodash.noop } = this.props; const change = { h: hsl.h + amount >= 359 ? 359 : hsl.h + amount, s: hsl.s, l: hsl.l, a: hsl.a, source: 'rgb' }; onChange(change); } decrease(amount = 1) { const { hsl, onChange = _lodash.noop } = this.props; const change = { h: hsl.h <= amount ? 0 : hsl.h - amount, s: hsl.s, l: hsl.l, a: hsl.a, source: 'rgb' }; onChange(change); } handleChange(e) { const { onChange = _lodash.noop } = this.props; const change = (0, _utils.calculateHueChange)(e, this.props, this.container.current); if (change) { onChange(change, e); } } handleMouseDown(e) { this.handleChange(e); window.addEventListener('mousemove', this.handleChange); window.addEventListener('mouseup', this.handleMouseUp); } handleMouseUp() { this.unbindEventListeners(); } preventKeyEvents(event) { if (event.keyCode === _keycodes.TAB) { return; } event.preventDefault(); } unbindEventListeners() { window.removeEventListener('mousemove', this.handleChange); window.removeEventListener('mouseup', this.handleMouseUp); } render() { const { hsl = {}, instanceId } = this.props; const pointerLocation = { left: `${hsl.h * 100 / 360}%` }; const shortcuts = { up: () => this.increase(), right: () => this.increase(), 'shift+up': () => this.increase(10), 'shift+right': () => this.increase(10), pageup: () => this.increase(10), end: () => this.increase(359), down: () => this.decrease(), left: () => this.decrease(), 'shift+down': () => this.decrease(10), 'shift+left': () => this.decrease(10), pagedown: () => this.decrease(10), home: () => this.decrease(359) }; return (0, _element.createElement)(_keyboardShortcuts.default, { shortcuts: shortcuts }, (0, _element.createElement)("div", { className: "components-color-picker__hue" }, (0, _element.createElement)("div", { className: "components-color-picker__hue-gradient" }), (0, _element.createElement)("div", { className: "components-color-picker__hue-bar", ref: this.container, onMouseDown: this.handleMouseDown, onTouchMove: this.handleChange, onTouchStart: this.handleChange }, (0, _element.createElement)("div", { tabIndex: "0", role: "slider", "aria-valuemax": "1", "aria-valuemin": "359", "aria-valuenow": hsl.h, "aria-orientation": "horizontal", "aria-label": (0, _i18n.__)('Hue value in degrees, from 0 to 359.'), "aria-describedby": `components-color-picker__hue-description-${instanceId}`, className: "components-color-picker__hue-pointer", style: pointerLocation, onKeyDown: this.preventKeyEvents }), (0, _element.createElement)(_visuallyHidden.default, { as: "p", id: `components-color-picker__hue-description-${instanceId}` }, (0, _i18n.__)('Move the arrow left or right to change hue.'))))); } } exports.Hue = Hue; var _default = (0, _compose.compose)(_compose.pure, _compose.withInstanceId)(Hue); exports.default = _default; //# sourceMappingURL=hue.js.map