UNPKG

@progress/kendo-react-inputs

Version:

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

372 lines (371 loc) 13.8 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 L, canUseDOM as F, Navigation as f, getActiveElement as b, disableNavigatableContainer as C, keepFocusInContainer as G, TABBABLE_ELEMENTS as A, focusFirstFocusableChild as M, enableNavigatableContainer as N, getTabIndex as k, classNames as I, kendoThemeMaps as _, Draggable as T, WatermarkOverlay as O, createPropsContext as z, withIdHOC as B, withPropsContext as P } from "@progress/kendo-react-common"; import { packageMetadata as V } from "../package-metadata.mjs"; import { Slider as R } from "../slider/Slider.mjs"; import { isPresent as S, fitIntoBounds as y } from "./utils/misc.mjs"; import { getHSV as u, getColorFromHue as p, getRGBA as c, getColorFromRGBA as K, parseColor as d, getColorFromHSV as U } from "./utils/color-parser.mjs"; import { cacheHex as W, cacheRgba as $, removeCachedColor as j, cacheHsva as X, getCachedHsva as Y, getCachedRgba as q, getCachedHex as J } from "./utils/color-cache.mjs"; import Q from "./ColorInput.mjs"; import { ColorContrastLabels as Z } from "./ColorContrastLabels.mjs"; import { ColorContrastSvg as ee } from "./ColorContrastSvg.mjs"; import { provideLocalizationService as te } from "@progress/kendo-react-intl"; import { colorGradientHueSliderLabel as E, messages as w, colorGradientAlphaSliderLabel as x } from "../messages/index.mjs"; const se = "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 = u(s); this.setState({ hsva: n, backgroundColor: p(n.h), rgba: c(s), hex: e }); } else W(this.state.guid, s, e); this.dispatchChangeEvent(s, i, i.nativeEvent); }, this.onRgbaChange = (e, s) => { const i = K(e); if (this.isUncontrolled) { const n = u(i), l = d(i, "hex"); this.setState({ hsva: n, backgroundColor: p(n.h), rgba: e, hex: l }); } else $(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 = !L(V, { component: "ColorGradient" }); const a = t.value || t.defaultValue || d(H, se); 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.gradientWrapper && (this.resizeObserver = F && window.ResizeObserver && new window.ResizeObserver(() => { this.forceUpdate(); }), this.resizeObserver && this.resizeObserver.observe(this.gradientWrapper)), 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) => { b(document) === t ? C(t) : G(e, t, A); }, Enter: (t, a, e) => { b(document) === t && (M(t), N(t)); }, Escape: (t, a, e) => { b(document) !== t && (t.focus(), C(t)); } } } })); } /** * @hidden */ componentWillUnmount() { j(this.state.guid), this.resizeObserver && this.resizeObserver.disconnect(); } /** * @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), D = Object.assign({}, this.state.hsva, { s: l / s, v: 1 - n / i }); this.handleHsvaChange(D, {}, {}); } /** * @hidden */ handleHsvaChange(t, a, e) { const s = U(t), i = d(s, "hex"); this.isUncontrolled || X(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) { S(d(t, "hex")) || (t = H); const e = Y(a, t) || u(t), s = q(a, t) || c(t), i = J(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 = te(this); return /* @__PURE__ */ r.createElement( "div", { id: this.props.id, role: this.props.role, className: I( "k-colorgradient", { [`k-colorgradient-${_.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( T, { 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( ee, { 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( R, { 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( R, { value: S(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( Q, { 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(Z, { bgColor: c(this.props.backgroundColor), rgba: this.state.rgba }), this.showLicenseWatermark && /* @__PURE__ */ r.createElement(O, 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 ae = z(), ie = B( P( ae, m ) ); ie.displayName = "KendoReactColorGradient"; export { ie as ColorGradient, ae as ColorGradientPropsContext, m as ColorGradientWithoutContext };