UNPKG

@progress/kendo-react-inputs

Version:

React Inputs offer a customizable interface for users to enter and pick different information. KendoReact Input package

366 lines (365 loc) 13.5 kB
/** * @license *------------------------------------------------------------------------------------------- * Copyright © 2025 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the package root for more information *------------------------------------------------------------------------------------------- */ import * as r from "react"; import o from "prop-types"; import { validatePackage as D, Navigation as f, getActiveElement as u, disableNavigatableContainer as C, keepFocusInContainer as F, TABBABLE_ELEMENTS as G, focusFirstFocusableChild as A, enableNavigatableContainer as N, getTabIndex as k, classNames as I, kendoThemeMaps as M, Draggable as _, WatermarkOverlay as T, createPropsContext as B, withIdHOC as P, withPropsContext as V } from "@progress/kendo-react-common"; import { packageMetadata as K } from "../package-metadata.mjs"; import { Slider as S } from "../slider/Slider.mjs"; import { isPresent as R, fitIntoBounds as y } from "./utils/misc.mjs"; import { getHSV as b, getColorFromHue as p, getRGBA as c, getColorFromRGBA as O, parseColor as d, getColorFromHSV as U } from "./utils/color-parser.mjs"; import { cacheHex as z, cacheRgba as W, removeCachedColor as $, cacheHsva as j, getCachedHsva as X, getCachedRgba as Y, getCachedHex as q } from "./utils/color-cache.mjs"; import J from "./ColorInput.mjs"; import { ColorContrastLabels as Q } from "./ColorContrastLabels.mjs"; import { ColorContrastSvg as Z } from "./ColorContrastSvg.mjs"; import { provideLocalizationService as ee } from "@progress/kendo-react-intl"; import { colorGradientHueSliderLabel as E, messages as w, colorGradientAlphaSliderLabel as x } from "../messages/index.mjs"; const te = "rgba", H = "rgba(255, 255, 255, 1)", g = 2, v = 5, h = class h extends r.Component { constructor(t) { super(t), this.wrapperRef = r.createRef(), this.hsvGradientRef = r.createRef(), this.showLicenseWatermark = !1, this.focus = () => { this.wrapperRef && this.wrapperRef.current && this.wrapperRef.current.focus(); }, this.onHexChange = (e, s, i) => { if (this.isUncontrolled) { const n = b(s); this.setState({ hsva: n, backgroundColor: p(n.h), rgba: c(s), hex: e }); } else z(this.state.guid, s, e); this.dispatchChangeEvent(s, i, i.nativeEvent); }, this.onRgbaChange = (e, s) => { const i = O(e); if (this.isUncontrolled) { const n = b(i), l = d(i, "hex"); this.setState({ hsva: n, backgroundColor: p(n.h), rgba: e, hex: l }); } else W(this.state.guid, i, e); this.dispatchChangeEvent(i, s.syntheticEvent, s.nativeEvent); }, this.onAlphaSliderChange = (e) => { this.handleHsvaChange( Object.assign({}, this.state.hsva, { a: e.value / 100 }), e.syntheticEvent, e.nativeEvent ); }, this.onHueSliderChange = (e) => { this.handleHsvaChange( Object.assign({}, this.state.hsva, { h: e.value }), e.syntheticEvent, e.nativeEvent ); }, this.onDrag = (e) => { this.gradientWrapper.classList.add("k-dragging"), this.changePosition(e.event); }, this.onRelease = () => { this.gradientWrapper.classList.remove("k-dragging"); }, this.onGradientWrapperClick = (e) => { this.changePosition(e); }, this.onHsvGradientKeyDown = (e) => { var s; (s = this.hsvNavigation) == null || s.triggerKeyboardEvent(e); }, this.onHsvGradientValueChange = (e, s, i) => { const n = parseInt(e.style.top, 10) + i, l = parseInt(e.style.left, 10) + s; e.style.top = `${n}px`, e.style.left = `${l}px`, this.moveDragHandle(l, n); }, this.changePosition = (e) => { const s = this.getGradientRectMetrics(), i = e.clientX - s.left, n = e.clientY - s.top; this.moveDragHandle(i, n); }, this.onFocus = (e) => { this.props.onFocus && this.props.onFocus.call(void 0, { nativeEvent: e, target: this }); }, this.onKeyDown = (e) => { var s; (s = this.navigation) == null || s.triggerKeyboardEvent(e); }, this.showLicenseWatermark = !D(K, { component: "ColorGradient" }); const a = t.value || t.defaultValue || d(H, te); this.state = Object.assign({}, h.getStateFromValue(a), { isFirstRender: !0, guid: this.props.id }); } /** * @hidden */ static getDerivedStateFromProps(t, a) { return t.value && !a.isFirstRender ? h.getStateFromValue(t.value, a.guid) : null; } /** * @hidden */ componentDidMount() { if (this.setAlphaSliderBackground(this.state.backgroundColor), this.state.isFirstRender && this.setState({ isFirstRender: !1 }), this.wrapperRef && this.hsvGradientRef) { let t = 0, a = 0; this.hsvNavigation = new f({ tabIndex: 0, root: this.hsvGradientRef, selectors: [".k-hsv-rectangle .k-hsv-draghandle"], keyboardEvents: { keydown: { ArrowDown: (e, s, i) => { a = i.shiftKey ? this.props.gradientSliderSmallStep || g : this.props.gradientSliderStep || v, this.onHsvGradientValueChange(e, 0, a); }, ArrowUp: (e, s, i) => { a = i.shiftKey ? -(this.props.gradientSliderSmallStep || g) : -(this.props.gradientSliderStep || v), this.onHsvGradientValueChange(e, 0, a); }, ArrowLeft: (e, s, i) => { t = i.shiftKey ? -(this.props.gradientSliderSmallStep || g) : -(this.props.gradientSliderStep || v), this.onHsvGradientValueChange(e, t, 0); }, ArrowRight: (e, s, i) => { t = i.shiftKey ? this.props.gradientSliderSmallStep || g : this.props.gradientSliderStep || v, this.onHsvGradientValueChange(e, t, 0); } } } }); } this.wrapperRef && this.wrapperRef.current && (this.props.isInsidePopup ? this.hsvGradientRef.current && this.hsvGradientRef.current.focus() : this.navigation = new f({ tabIndex: 0, root: this.wrapperRef, selectors: [".k-colorgradient"], keyboardEvents: { keydown: { Tab: (t, a, e) => { u(document) === t ? C(t) : F(e, t, G); }, Enter: (t, a, e) => { u(document) === t && (A(t), N(t)); }, Escape: (t, a, e) => { u(document) !== t && (t.focus(), C(t)); } } } })); } /** * @hidden */ componentWillUnmount() { $(this.state.guid); } /** * @hidden */ componentDidUpdate(t, a) { a.backgroundColor !== this.state.backgroundColor && this.setAlphaSliderBackground(this.state.backgroundColor); } /** * @hidden */ renderRectangleDragHandle() { const t = {}; if (!this.state.isFirstRender) { const a = this.getGradientRectMetrics(), e = (1 - this.state.hsva.v) * a.height, s = this.state.hsva.s * a.width; t.top = `${e}px`, t.left = `${s}px`; } return /* @__PURE__ */ r.createElement( "div", { ref: this.hsvGradientRef, role: "slider", tabIndex: k(this.props.tabIndex, this.props.disabled), "aria-valuetext": this.props.ariaValueText, "aria-valuenow": parseInt(this.state.hex.substring(1), 16), "aria-label": this.props.ariaLabelHSV, "aria-orientation": void 0, "aria-disabled": this.props.disabled ? "true" : void 0, className: "k-hsv-draghandle k-draghandle", style: t, onKeyDown: this.onHsvGradientKeyDown } ); } /** * @hidden */ moveDragHandle(t, a) { const e = this.getGradientRectMetrics(), s = e.width, i = e.height, n = y(a, 0, i), l = y(t, 0, s), L = Object.assign({}, this.state.hsva, { s: l / s, v: 1 - n / i }); this.handleHsvaChange(L, {}, {}); } /** * @hidden */ handleHsvaChange(t, a, e) { const s = U(t), i = d(s, "hex"); this.isUncontrolled || j(this.state.guid, s, t), this.setState({ hsva: t, backgroundColor: p(t.h), rgba: c(s), hex: i }), this.dispatchChangeEvent(s, a, e); } /** * @hidden */ dispatchChangeEvent(t, a, e) { this.props.onChange && this.props.onChange.call(void 0, { syntheticEvent: a, nativeEvent: e, target: this, value: t }); } /** * @hidden */ static getStateFromValue(t, a) { R(d(t, "hex")) || (t = H); const e = X(a, t) || b(t), s = Y(a, t) || c(t), i = q(a, t) || d(t, "hex"), n = p(e.h); return { hsva: e, backgroundColor: n, rgba: s, hex: i }; } /** * @hidden */ setAlphaSliderBackground(t) { this.props.opacity && this.alphaSlider && this.alphaSlider.sliderTrack && (this.alphaSlider.sliderTrack.style.background = `linear-gradient(to ${this.props._adaptive ? "right" : "top"}, transparent, ${t})`); } /** * @hidden */ get isUncontrolled() { return this.props.value === void 0; } /** * @hidden */ getGradientRectMetrics() { return this.gradientWrapper.getBoundingClientRect(); } /** * @hidden */ render() { const t = ee(this); return /* @__PURE__ */ r.createElement( "div", { id: this.props.id, role: this.props.role, className: I( "k-colorgradient", { [`k-colorgradient-${M.sizeMap[this.props.size] || this.props.size}`]: this.props.size, "k-disabled": this.props.disabled }, this.props.className ), "aria-disabled": this.props.disabled ? "true" : void 0, style: { position: "relative", ...this.props.style }, ref: this.wrapperRef, tabIndex: k(this.props.tabIndex, this.props.disabled), "aria-label": this.props.ariaLabel, "aria-labelledby": this.props.ariaLabelledBy, "aria-describedby": this.props.ariaDescribedBy, onFocus: this.onFocus, onKeyDown: this.onKeyDown }, /* @__PURE__ */ r.createElement("div", { className: `k-colorgradient-canvas ${this.props._adaptive ? "k-vstack" : "k-hstack"}` }, /* @__PURE__ */ r.createElement("div", { className: "k-hsv-rectangle", style: { background: this.state.backgroundColor } }, /* @__PURE__ */ r.createElement( _, { onDrag: this.onDrag, onRelease: this.onRelease, ref: (a) => this.gradientWrapper = a ? a.element : void 0 }, /* @__PURE__ */ r.createElement( "div", { className: "k-hsv-gradient", style: { touchAction: "none" }, onClick: this.onGradientWrapperClick }, this.renderRectangleDragHandle() ) ), this.props.backgroundColor && !this.state.isFirstRender && /* @__PURE__ */ r.createElement( Z, { metrics: this.gradientWrapper ? this.getGradientRectMetrics() : void 0, hsva: this.state.hsva, backgroundColor: this.props.backgroundColor } )), /* @__PURE__ */ r.createElement("div", { className: `k-hsv-controls ${this.props._adaptive ? "k-vstack" : "k-hstack"}` }, /* @__PURE__ */ r.createElement( S, { value: this.state.hsva.h, buttons: !1, vertical: !this.props._adaptive, min: 0, max: 360, step: 5, onChange: this.onHueSliderChange, className: "k-hue-slider k-colorgradient-slider", disabled: this.props.disabled, ariaLabel: t.toLanguageString( E, w[E] ) } ), this.props.opacity && /* @__PURE__ */ r.createElement( S, { value: R(this.state.hsva.a) ? this.state.hsva.a * 100 : 100, buttons: !1, vertical: !this.props._adaptive, min: 0, max: 100, step: 1, ariaLabel: t.toLanguageString( x, w[x] ), onChange: this.onAlphaSliderChange, className: "k-alpha-slider k-colorgradient-slider", disabled: this.props.disabled, ref: (a) => this.alphaSlider = a } ))), /* @__PURE__ */ r.createElement( J, { rgba: this.state.rgba, onRgbaChange: this.onRgbaChange, hex: this.state.hex, onHexChange: this.onHexChange, opacity: this.props.opacity, disabled: this.props.disabled, defaultInputMode: this.props.format, size: this.props.size, fillMode: this.props.fillMode } ), this.props.backgroundColor && /* @__PURE__ */ r.createElement(Q, { bgColor: c(this.props.backgroundColor), rgba: this.state.rgba }), this.showLicenseWatermark && /* @__PURE__ */ r.createElement(T, null) ); } }; h.displayName = "ColorGradient", h.propTypes = { defaultValue: o.string, value: o.string, onChange: o.func, onFocus: o.func, opacity: o.bool, backgroundColor: o.string, format: o.any, disabled: o.bool, style: o.any, id: o.string, role: o.string, ariaLabel: o.string, ariaLabelledBy: o.string, ariaDescribedBy: o.string, className: o.string, size: o.oneOf(["small", "medium", "large", null]) }, h.defaultProps = { opacity: !0, role: "textbox", format: "rgb", size: "medium" }; let m = h; const se = B(), ae = P( V( se, m ) ); ae.displayName = "KendoReactColorGradient"; export { ae as ColorGradient, se as ColorGradientPropsContext, m as ColorGradientWithoutContext };