UNPKG

@progress/kendo-react-tooltip

Version:

React Tooltips library represents popups with information that is related to a UI element. KendoReact Tooltips package

16 lines (15 loc) 13.3 kB
/** * @license *------------------------------------------------------------------------------------------- * Copyright © 2025 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the package root for more information *------------------------------------------------------------------------------------------- */ /** * @license *------------------------------------------------------------------------------------------- * Copyright © 2025 Progress Software Corporation. All rights reserved. * Licensed under commercial license. See LICENSE.md in the package root for more information *------------------------------------------------------------------------------------------- */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("react-dom"),require("prop-types"),require("@progress/kendo-react-common"),require("@progress/kendo-react-popup")):"function"==typeof define&&define.amd?define(["exports","react","react-dom","prop-types","@progress/kendo-react-common","@progress/kendo-react-popup"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).KendoReactTooltip={},t.React,t.ReactDOM,t.PropTypes,t.KendoReactCommon,t.KendoReactPopup)}(this,(function(t,e,o,n,i,l){"use strict";function s(t){var e=Object.create(null);return t&&Object.keys(t).forEach((function(o){if("default"!==o){var n=Object.getOwnPropertyDescriptor(t,o);Object.defineProperty(e,o,n.get?n:{enumerable:!0,get:function(){return t[o]}})}})),e.default=t,Object.freeze(e)}var r=s(e),a=s(o);const p=["k-tooltip-content","k-animation-container","k-tooltip","k-tooltip-title","k-tooltip k-tooltip-closable","k-icon k-i-close"],c=t=>{if(null===t)return!1;let e=t;for(;e;){if(null!==e&&e.classList&&e.classList.contains("k-tooltip"))return!0;e=null!==e.parentNode&&e.parentNode}return p.filter((e=>e===t.className)).length>0},h=class t extends r.PureComponent{constructor(){super(...arguments),this.context=0,this.state={targetElement:null,open:!1,title:""},this.top=0,this.left=0,this.willOpen=!1,this.handleMouseOut=t=>{const{targetElement:e}=this.state,o=e?e.ownerDocument:document,n=o&&o.elementFromPoint(t.clientX,t.clientY);c(n)||this.props.open||(clearInterval(this.updateIntervalId),this.willOpen=!1,this.onClose(t))},this.compareTargets=t=>this.state.targetElement===t.nativeEvent.target,this.handleEscape=t=>{const e=this.compareTargets(t);this.state.open&&e&&this.onClose(t)},this.handleTargetItemClick=t=>{const e=this.compareTargets(t);this.state.open&&e?this.onClose(t):this.showToolTip(t)},this.handleKeyDown=(t,e)=>{const o=t.key;"Escape"===t.key?this.handleEscape(t):e.includes(o)&&this.handleMouseOver(t)},this.handleMouseOver=t=>{const e=t.target;!this.isVisible(e)||c(e)||e===this.state.targetElement||this.showToolTip(t)},this.handleBodyMousemove=t=>{this.top=t.clientY,this.left=t.clientX},this.onClose=t=>{i.dispatchEvent(this.props.onClose,t,this,void 0),this.setState({targetElement:null,open:!1,title:""})},this.setUpdateInterval=()=>{this.props.updateInterval&&(this.updateIntervalId=setInterval(this.onIntervalUpdate,this.props.updateInterval))},this.onIntervalUpdate=()=>{const t=this.state.targetElement;t&&(null===t.parentElement?this.onClose({target:t}):this.showToolTip({target:t}))},this.position=t=>{const e=null!==(i=this.state.targetElement)?i.getBoundingClientRect():document.body.getBoundingClientRect(),o=((t,e,o,n,i)=>{switch(i){case"bottom":case"top":return"pointer"===n?t-e/2:o.left-e/2+o.width/2;case"left":return"pointer"===n?t-e-10:o.left-e-10;case"right":return"pointer"===n?t+10:o.right+10;default:return"pointer"===n?t<window.screen.availWidth/2?t-10:t-e+10:t<window.screen.availWidth/2?o.left:o.right-e}})(this.left,t.offsetWidth,e,this.props.anchorElement,this.props.position),n=((t,e,o,n,i)=>{switch(i){case"bottom":return"pointer"===n?t+10:e.bottom+10;case"left":case"right":return"pointer"===n?t-o/2:e.top-o/2+e.height/2;case"top":return"pointer"===n?t-o-10:e.top-o-10;default:return"pointer"===n?t<window.innerHeight/2?t+10:t-o-10:t<window.innerHeight/2?e.bottom+10:e.top-o-10}})(this.top,e,t.offsetHeight,this.props.anchorElement,this.props.position);var i;return{left:o,top:n}},this.getTitle=t=>{for(;t;){if(null!==t.getAttribute("title")||t.titleExpando)return{title:t.getAttribute("title")||t.titleExpando,element:t};t=this.props.parentTitle&&t.parentElement||null}return{title:"",element:t}},this.calloutClassName=()=>{switch(this.props.position){case"bottom":return"k-callout k-callout-n";case"left":return"k-callout k-callout-e";case"right":return"k-callout k-callout-w";case"top":return"k-callout k-callout-s";default:return this.top<window.innerHeight/2?"k-callout k-callout-n":"k-callout k-callout-s"}},this.getCurrentZIndex=()=>this.context?this.context:100}componentDidMount(){document&&document.body.addEventListener("mousemove",this.handleBodyMousemove)}componentWillUnmount(){clearTimeout(this.openTimeoutId),clearInterval(this.updateIntervalId),document&&document.body.removeEventListener("mousemove",this.handleBodyMousemove)}componentDidUpdate(){this.props.open&&this.props.targetElement&&this.showToolTip({target:this.props.targetElement})}render(){if(this.props.children){const{children:e,showOption:o,...n}=this.props;let i=null;const l=void 0===(null==o?void 0:o.hover)||(null==o?void 0:o.hover),s=void 0!==(null==o?void 0:o.click)&&(null==o?void 0:o.click),a=void 0!==(null==o?void 0:o.focus)&&(null==o?void 0:o.focus),p=void 0!==(null==o?void 0:o.none)&&(null==o?void 0:o.none),c=(void 0!==(null==o?void 0:o.keys)?null==o?void 0:o.keys:[]).map((t=>"Space"===t?" ":t));return r.createElement("div",{onMouseOver:t=>{i&&!p&&l&&i.handleMouseOver(t)},onMouseOut:t=>{i&&!p&&l&&i.handleMouseOut(t)},onClick:t=>{i&&!p&&s&&i.handleTargetItemClick(t)},onFocus:t=>{i&&!p&&a&&i.handleMouseOver(t)},onKeyDown:t=>{(i&&!p&&c.length>0||i&&!p&&"Escape"===t.key)&&i.handleKeyDown(t,c)}},r.createElement(t,{ref:t=>{i=t},...n}),e)}if(!1===this.props.open)return null;const{targetElement:e}=this.state,o=this.getCurrentZIndex();return e&&this.state.title&&e.ownerDocument&&a.createPortal(r.createElement("div",{ref:t=>{if(!t)return;let e;if(this.props.onPosition){const o={element:t,targetElement:this.state.targetElement,mouseTop:this.top,mouseLeft:this.left,anchorElement:this.props.anchorElement,position:this.props.position,target:this,syntheticEvent:null,nativeEvent:null};e=this.props.onPosition.call(void 0,o)}else e=this.position(t);t.style.left=e.left+"px",t.style.top=e.top+"px"},className:i.classNames("k-animation-container","k-animation-container-fixed","k-animation-container-shown",this.props.className),style:{zIndex:o,...this.props.style},tabIndex:0},r.createElement("div",{className:"k-child-animation-container"},r.createElement("div",{id:this.props.id,role:"tooltip",className:i.classNames("k-tooltip",this.props.tooltipClassName),style:{position:"relative",...this.props.tooltipStyle}},r.createElement("div",{className:"k-tooltip-content"},this.props.content&&r.createElement(this.props.content,{title:this.state.title,target:this.state.targetElement})||this.state.title),this.props.showCallout&&r.createElement("div",{ref:t=>{t&&(t.className=this.calloutClassName(),"auto"===this.props.position&&(this.left<window.screen.availWidth/2?t.style.left=this.props.setCalloutOnPositionAuto||"25%":t.style.left=this.props.setCalloutOnPositionAuto||"75%"))}})))),this.props.appendTo?this.props.appendTo:e.ownerDocument.body)}showToolTip(t){clearTimeout(this.openTimeoutId),clearInterval(this.updateIntervalId),t.target.hasChildNodes()&&t.target.childNodes.forEach((e=>{"title"===e.nodeName&&(t.target.titleExpando=e.innerHTML,e.remove())}));const e=this.props.targetElement||t.target,o=this.getTitle(e);o.title?(o.element&&(o.element.titleExpando=o.title,o.element.title=""),this.willOpen=!0,this.props.openDelay?this.openTimeoutId=window.setTimeout((()=>{this.willOpen&&this.setState({targetElement:e,open:!0,title:o.title},this.setUpdateInterval)}),this.props.openDelay):this.setState({targetElement:e,open:!0,title:o.title},this.setUpdateInterval),this.state.title!==o.title&&i.dispatchEvent(this.props.onOpen,t,this,void 0)):this.state.open&&this.onClose(t)}isVisible(t){return!this.props.filter||this.props.filter(t)}};h.propTypes={anchorElement:n.oneOf(["pointer","target"]),content:n.func,filter:n.func,openDelay:n.number,position:n.oneOf(["right","left","top","bottom","auto"]),updateInterval:n.number,showOption:n.object},h.defaultProps={anchorElement:"pointer",openDelay:400,position:"auto",showCallout:!0,parentTitle:!1},h.contextType=i.ZIndexContext;let u=h;const d=r.forwardRef(((t,e)=>{const o=r.useRef(null),n=r.useRef(null),{alignment:l=m.alignment,orientation:s=m.orientation}=t;return r.useImperativeHandle(n,(()=>({props:t,element:o.current}))),r.useImperativeHandle(e,(()=>n.current)),r.createElement("div",{ref:o,className:i.classNames("k-popover-actions","k-actions",{[`k-actions-${l}`]:l,[`k-actions-${s}`]:s})},t.children)})),m={alignment:"stretched",orientation:"horizontal"};d.propTypes={alignment:n.oneOf(["start","center","end","stretched"]),orientation:n.oneOf(["horizontal","vertical"]),children:n.oneOfType([n.element,n.node])},d.displayName="KendoReactPopoverActionsBar";const f=t=>{if(t){const e=t.querySelectorAll(i.FOCUSABLE_ELEMENTS.join(", "));if(e.length)return e[0]}},v=t=>{const e=f(document.querySelector(".k-popover-inner")),o=(t=>{if(t){const e=t.querySelectorAll(i.FOCUSABLE_ELEMENTS.join(", "));if(e.length)return e[e.length-1]}})(document.querySelector(".k-popover-inner"));t.shiftKey&&t.target===e?(o&&o.focus(),t.preventDefault()):!t.shiftKey&&t.target===o&&(e&&e.focus(),t.preventDefault())},g={top:"k-callout-s",left:"k-callout-e",bottom:"k-callout-n",right:"k-callout-w"},y={top:"k-callout-n",left:"k-callout-w",bottom:"k-callout-s",right:"k-callout-e"},E={top:{vertical:"bottom",horizontal:"center"},left:{vertical:"center",horizontal:"right"},bottom:{vertical:"top",horizontal:"center"},right:{vertical:"center",horizontal:"left"}},k={top:{vertical:"top",horizontal:"center"},left:{vertical:"center",horizontal:"left"},bottom:{vertical:"bottom",horizontal:"center"},right:{vertical:"center",horizontal:"right"}},b=t=>r.Children.toArray(t).filter((t=>t&&t.type===d)),T=t=>r.Children.toArray(t).filter((t=>t&&t.type!==d)),w=r.forwardRef(((t,e)=>{const{callout:o=O.callout,position:n=O.position,collision:s=O.collision,title:a,children:p,show:c,anchor:h,appendTo:u,offset:d,animate:m,positionMode:w,scale:C,popoverClass:I,className:N,id:x,style:P,contentStyle:D,onPosition:M,onClose:A,onOpen:R,onKeyDown:S}=t,[j,z]=r.useState(!1),K="top"===n||"bottom"===n,q=t.margin||{vertical:o&&K?15:0,horizontal:o&&!K?15:0},L=E[n],B=k[n],H=r.useContext(i.ZIndexContext),U=H?H+2e3:12e3,F=r.useRef(null);r.useImperativeHandle(F,(()=>({props:t}))),r.useImperativeHandle(e,(()=>F.current));const W=r.useCallback((t=>{if(setTimeout((()=>z(t.flipped))),M){const e={target:F.current,flipped:t.flipped,fitted:t.fitted};M.call(void 0,e)}}),[M,z]),Z=r.useCallback((()=>{const t=f(document.querySelector(".k-popover-inner"));if(t&&t.focus(),R){const t={target:F.current};R.call(void 0,t)}}),[R]),V=r.useCallback((t=>{if(S&&"Escape"===t.key){const t={target:F.current,show:!1};S.call(void 0,t)}"Tab"===t.key&&v(t)}),[S]),X=r.useCallback((t=>{if(A){const t={target:F.current};A.call(void 0,t)}t&&t.target&&t.target.props&&t.target.props.anchor&&t.target.props.anchor.focus()}),[A]),Y={"k-popover":!0};Array.isArray(I)?I.forEach((t=>Y[t]=!0)):"object"==typeof I?Object.keys(I).forEach((t=>{Y[t]=I[t]})):I&&(Y[I]=!0);const $=j?y[n]:g[n],_=r.useMemo((()=>r.createElement(r.Fragment,null,a&&r.createElement("div",{className:"k-popover-header"},a),r.createElement("div",{className:"k-popover-body",style:D},T(p)),b(p))),[p,D,a]);return r.createElement(i.ZIndexContext.Provider,{value:U},r.createElement(l.Popup,{id:x,role:"tooltip",animate:m,collision:s,anchor:h,offset:d,margin:q,popupAlign:L,anchorAlign:B,appendTo:u,show:c,scale:C,positionMode:w,style:P,className:N,popupClass:Y,onOpen:Z,onClose:X,onPosition:W,onKeyDown:V},o&&n?r.createElement(r.Fragment,null,r.createElement("div",{className:i.classNames("k-popover-callout",$)}),r.createElement("div",{className:"k-popover-inner"},_)):_))})),O={callout:!0,position:"top",collision:{horizontal:"none",vertical:"none"}};w.propTypes={callout:n.bool,title:n.node,position:n.oneOf(["top","left","bottom","right"]),show:n.bool,animate:n.oneOfType([n.bool,n.shape({openDuration:n.number,closeDuration:n.number})]),anchor:function(t){const e=t.anchor;return e&&"number"!=typeof e.nodeType?new Error("Invalid prop `anchor` supplied to `Kendo React Popover`. Validation failed."):null},appendTo:function(t){const e=t.appendTo;return e&&"number"!=typeof e.nodeType?new Error("Invalid prop `appendTo` supplied to `Kendo React Popover`. Validation failed."):null},positionMode:n.oneOf(["fixed","absolute"]),scale:n.number,offset:n.shape({left:n.number,top:n.number}),children:n.oneOfType([n.element,n.node]),className:n.oneOfType([n.string,n.arrayOf(n.string),n.object]),id:n.string,popoverClass:n.oneOfType([n.string,n.arrayOf(n.string),n.object]),style:n.object,onClose:n.func,onPosition:n.func,onOpen:n.func},w.displayName="KendoReactPopover",t.Popover=w,t.PopoverActionsBar=d,t.Tooltip=u}));