UNPKG

primereact

Version:

PrimeReact is an open source UI library for React featuring a rich set of 80+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with Prime

2 lines (1 loc) 8.68 kB
import*as e from"react";import t from"primereact/api";import{useResizeListener as r,useOverlayScrollListener as n,useMountEffect as o,useUpdateEffect as u,useUnmountEffect as i}from"primereact/hooks";import{Portal as a}from"primereact/portal";import{DomHandler as c,ZIndexUtils as l,ObjectUtils as s,classNames as f}from"primereact/utils";function p(){return p=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},p.apply(this,arguments)}function d(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function h(e){if(Array.isArray(e))return d(e)}function m(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function v(e,t){if(e){if("string"==typeof e)return d(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?d(e,t):void 0}}function y(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function g(e){if(Array.isArray(e))return e}function E(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,u=[],i=!0,a=!1;try{for(r=r.call(e);!(i=(n=r.next()).done)&&(u.push(n.value),!t||u.length!==t);i=!0);}catch(e){a=!0,o=e}finally{try{i||null==r.return||r.return()}finally{if(a)throw o}}return u}}function b(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function w(e,t){return g(e)||E(e,t)||v(e,t)||b()}var T=e.memo(e.forwardRef((function(d,g){var E=w(e.useState(!1),2),b=E[0],x=E[1],S=w(e.useState(d.position),2),O=S[0],k=S[1],A=w(e.useState(""),2),I=A[0],C=A[1],D=e.useRef(null),H=e.useRef(null),L=e.useRef(null),j=e.useRef(null),N=e.useRef(!0),R=e.useRef({}),W=e.useRef(null),P=w(r({listener:function(e){!c.isTouchDevice()&&ee(e)}}),2),B=P[0],Z=P[1],F=w(n({target:L.current,listener:function(e){ee(e)},when:b}),2),M=F[0],Y=F[1],_=function(e){return X(e,"mousetrack")||d.mouseTrack},z=function(e){return"true"===X(e,"disabled")||$(e,"disabled")||d.disabled},K=function(e){return X(e,"showondisabled")||d.showOnDisabled},U=function(){return X(L.current,"autohide")||d.autoHide},X=function(e,t){return $(e,"data-pr-".concat(t))?e.getAttribute("data-pr-".concat(t)):null},$=function(e,t){return e&&e.hasAttribute(t)},q=function(e){var t=[X(e,"showevent")||d.showEvent],r=[X(e,"hideevent")||d.hideEvent];if(_(e))t=["mousemove"],r=["mouseleave"];else{var n=X(e,"event")||d.event;"focus"===n&&(t=["focus"],r=["blur"]),"both"===n&&(t=["focus","mouseenter"],r=["blur","mouseleave"])}return{showEvents:t,hideEvents:r}},G=function(e){return{top:X(e,"mousetracktop")||d.mouseTrackTop,left:X(e,"mousetrackleft")||d.mouseTrackLeft}},J=function(e,t){if(H.current){var r=X(e,"tooltip")||d.content;r?(H.current.innerHTML="",H.current.appendChild(document.createTextNode(r)),t()):d.children&&t()}},Q=function(e){J(L.current,(function(){var r=W.current,n=r.pageX,o=r.pageY;d.autoZIndex&&!l.get(D.current)&&l.set("tooltip",D.current,t.autoZIndex,d.baseZIndex||t.zIndex.tooltip),D.current.style.left="",D.current.style.top="",U()&&(D.current.style.pointerEvents="none"),_(L.current)&&!j.current&&(j.current={width:c.getOuterWidth(D.current),height:c.getOuterHeight(D.current)}),te(L.current,{x:n,y:o},e)}))},V=function(e){L.current=e.currentTarget;var t,r=z(L.current);(t=K(L.current)&&r?L.current.firstChild:L.current,!(d.content||X(t,"tooltip")||d.children))||r||(W.current=e,b?ae("updateDelay",Q):ce(d.onBeforeShow,{originalEvent:e,target:L.current})&&ae("showDelay",(function(){x(!0),ce(d.onShow,{originalEvent:e,target:L.current})})))},ee=function(e){(le(),b)&&(ce(d.onBeforeHide,{originalEvent:e,target:L.current})&&ae("hideDelay",(function(){(U()||!1!==N.current)&&(l.clear(D.current),c.removeClass(D.current,"p-tooltip-active"),x(!1),ce(d.onHide,{originalEvent:e,target:L.current}))})))},te=function(e,t,r){var n=0,o=0,u=r||O;if(_(e)&&t){var i={width:c.getOuterWidth(D.current),height:c.getOuterHeight(D.current)};n=t.x,o=t.y;var a=G(e),l=a.top,s=a.left;switch(u){case"left":n-=i.width+s,o-=i.height/2-l;break;case"right":n+=s,o-=i.height/2-l;break;case"top":n-=i.width/2-s,o-=i.height+l;break;case"bottom":n-=i.width/2-s,o+=l}n<=0||j.current.width>i.width?(D.current.style.left="0px",D.current.style.right=window.innerWidth-i.width-n+"px"):(D.current.style.right="",D.current.style.left=n+"px"),D.current.style.top=o+"px",c.addClass(D.current,"p-tooltip-active")}else{var f=c.findCollisionPosition(u),p=X(e,"my")||d.my||f.my,h=X(e,"at")||d.at||f.at;D.current.style.padding="0px",c.flipfitCollision(D.current,e,p,h,(function(e){var t=e.at,r=t.x,n=d.at?"center"!==r&&r!==e.my.x?r:t.y:e.at["".concat(f.axis)];D.current.style.padding="",k(n),re(n),c.addClass(D.current,"p-tooltip-active")}))}},re=function(e){if(D.current){var t=getComputedStyle(D.current);"left"===e?D.current.style.left=parseFloat(t.left)-2*parseFloat(t.paddingLeft)+"px":"top"===e&&(D.current.style.top=parseFloat(t.top)-2*parseFloat(t.paddingTop)+"px")}},ne=function(){U()||(N.current=!1)},oe=function(e){U()||(N.current=!0,ee(e))},ue=function(e){if(e){var t=q(e),r=t.showEvents,n=t.hideEvents,o=se(e);r.forEach((function(e){return o.addEventListener(e,V)})),n.forEach((function(e){return o.addEventListener(e,ee)}))}},ie=function(e){if(e){var t=q(e),r=t.showEvents,n=t.hideEvents,o=se(e);r.forEach((function(e){return o.removeEventListener(e,V)})),n.forEach((function(e){return o.removeEventListener(e,ee)}))}},ae=function(e,t){le();var r=X(L.current,e.toLowerCase())||d[e];r?R.current["".concat(e)]=setTimeout((function(){return t()}),r):t()},ce=function(e){if(e){for(var t=arguments.length,r=new Array(t>1?t-1:0),n=1;n<t;n++)r[n-1]=arguments[n];var o=e.apply(void 0,r);return void 0===o&&(o=!0),o}return!0},le=function(){Object.values(R.current).forEach((function(e){return clearTimeout(e)}))},se=function(e){if(e){if(K(e)){if(e.hasWrapper)return e.parentElement;var t=document.createElement("span");return e.parentNode.insertBefore(t,e),t.appendChild(e),e.hasWrapper=!0,t}var r;return e.hasWrapper&&((r=e.parentElement).replaceWith.apply(r,h(n=e.parentElement.childNodes)||m(n)||v(n)||y()),delete e.hasWrapper),e}var n;return null},fe=function(e){de(e),pe(e)},pe=function(e){he(e||d.target,ue)},de=function(e){he(e||d.target,ie)},he=function(e,t){if(e=s.getRefElement(e))if(c.isElement(e))t(e);else{var r=function(e){c.find(document,e).forEach((function(e){t(e)}))};e instanceof Array?e.forEach((function(e){r(e)})):r(e)}};e.useEffect((function(){b&&L.current&&z(L.current)&&ee()})),o((function(){pe()})),u((function(){return pe(),function(){de()}}),[V,ee,d.target]),u((function(){if(b){var e=X(L.current,"position")||O,t=X(L.current,"classname");k(e),C(t),Q(e),B(),M()}else k(d.position),C(""),L.current=null,j.current=null,N.current=!0;return function(){Z(),Y()}}),[b]),u((function(){b&&ae("updateDelay",(function(){J(L.current,(function(){te(L.current)}))}))}),[d.content]),i((function(){le(),de(),l.clear(D.current)})),e.useImperativeHandle(g,(function(){return{props:d,updateTargetEvents:fe,loadTargetEvents:pe,unloadTargetEvents:de,show:V,hide:ee,getElement:function(){return D.current},getTarget:function(){return L.current}}}));var me,ve,ye,ge,Ee,be,we;if(b){var Te=(Ee=s.findDiffKeys(d,T.defaultProps),be=f("p-tooltip p-component",(me={},ve="p-tooltip-".concat(O),ye=!0,ve in me?Object.defineProperty(me,ve,{value:ye,enumerable:!0,configurable:!0,writable:!0}):me[ve]=ye,me),d.className,I),ge=L.current,we=!(d.content||X(ge,"tooltip")),e.createElement("div",p({id:d.id,ref:D,className:be,style:d.style,role:"tooltip","aria-hidden":b},Ee,{onMouseEnter:ne,onMouseLeave:oe}),e.createElement("div",{className:"p-tooltip-arrow"}),e.createElement("div",{ref:H,className:"p-tooltip-text"},we&&d.children)));return e.createElement(a,{element:Te,appendTo:d.appendTo,visible:!0})}return null})));T.displayName="Tooltip",T.defaultProps={__TYPE:"Tooltip",id:null,target:null,content:null,disabled:!1,className:null,style:null,appendTo:null,position:"right",my:null,at:null,event:null,showEvent:"mouseenter",hideEvent:"mouseleave",autoZIndex:!0,baseZIndex:0,mouseTrack:!1,mouseTrackTop:5,mouseTrackLeft:5,showDelay:0,updateDelay:0,hideDelay:0,autoHide:!0,showOnDisabled:!1,onBeforeShow:null,onBeforeHide:null,onShow:null,onHide:null};export{T as Tooltip};