UNPKG

primereact

Version:

PrimeReact is an open source UI library for React featuring a rich set of 90+ 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) 9.6 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{ObjectUtils as l,DomHandler as c,ZIndexUtils 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){return d="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},d(e)}function m(e,t){if("object"!==d(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!==d(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}function h(e){var t=m(e,"string");return"symbol"===d(t)?t:String(t)}function v(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 y(e){if(Array.isArray(e))return v(e)}function g(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function b(e,t){if(e){if("string"==typeof e)return v(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)?v(e,t):void 0}}function E(){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 w(e){if(Array.isArray(e))return e}function T(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,u,i,a=[],l=!0,c=!1;try{if(u=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;l=!1}else for(;!(l=(n=u.call(r)).done)&&(a.push(n.value),a.length!==t);l=!0);}catch(e){c=!0,o=e}finally{try{if(!l&&null!=r.return&&(i=r.return(),Object(i)!==i))return}finally{if(c)throw o}}return a}}function S(){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 x(e,t){return w(e)||T(e,t)||b(e,t)||S()}var O={defaultProps:{__TYPE:"Tooltip",appendTo:null,at:null,autoHide:!0,autoZIndex:!0,baseZIndex:0,className:null,content:null,disabled:!1,event:null,hideDelay:0,hideEvent:"mouseleave",id:null,mouseTrack:!1,mouseTrackLeft:5,mouseTrackTop:5,my:null,onBeforeHide:null,onBeforeShow:null,onHide:null,onShow:null,position:"right",showDelay:0,showEvent:"mouseenter",showOnDisabled:!1,style:null,target:null,updateDelay:0,children:void 0},getProps:function(e){return l.getMergedProps(e,O.defaultProps)},getOtherProps:function(e){return l.getDiffProps(e,O.defaultProps)}},P=e.memo(e.forwardRef((function(d,m){var v=O.getProps(d),w=x(e.useState(!1),2),T=w[0],S=w[1],P=x(e.useState(v.position),2),j=P[0],k=P[1],A=x(e.useState(""),2),C=A[0],I=A[1],D=e.useRef(null),H=e.useRef(null),L=e.useRef(null),N=e.useRef(null),R=e.useRef(!0),W=e.useRef({}),B=e.useRef(null),M=x(r({listener:function(e){!c.isTouchDevice()&&re(e)}}),2),Z=M[0],F=M[1],Y=x(n({target:L.current,listener:function(e){re(e)},when:T}),2),_=Y[0],z=Y[1],U=function(e){return G(e,"mousetrack")||v.mouseTrack},X=function(e){return"true"===G(e,"disabled")||J(e,"disabled")||v.disabled},$=function(e){return G(e,"showondisabled")||v.showOnDisabled},q=function(){return G(L.current,"autohide")||v.autoHide},G=function(e,t){return J(e,"data-pr-".concat(t))?e.getAttribute("data-pr-".concat(t)):null},J=function(e,t){return e&&e.hasAttribute(t)},K=function(e){var t=[G(e,"showevent")||v.showEvent],r=[G(e,"hideevent")||v.hideEvent];if(U(e))t=["mousemove"],r=["mouseleave"];else{var n=G(e,"event")||v.event;"focus"===n&&(t=["focus"],r=["blur"]),"both"===n&&(t=["focus","mouseenter"],r=["blur","mouseleave"])}return{showEvents:t,hideEvents:r}},Q=function(e){return{top:G(e,"mousetracktop")||v.mouseTrackTop,left:G(e,"mousetrackleft")||v.mouseTrackLeft}},V=function(e,t){if(H.current){var r=G(e,"tooltip")||v.content;r?(H.current.innerHTML="",H.current.appendChild(document.createTextNode(r)),t()):v.children&&t()}},ee=function(e){V(L.current,(function(){var r=B.current,n=r.pageX,o=r.pageY;v.autoZIndex&&!s.get(D.current)&&s.set("tooltip",D.current,t.autoZIndex,v.baseZIndex||t.zIndex.tooltip),D.current.style.left="",D.current.style.top="",q()&&(D.current.style.pointerEvents="none"),!U(L.current)&&"mouse"!=e||N.current||(N.current={width:c.getOuterWidth(D.current),height:c.getOuterHeight(D.current)}),ne(L.current,{x:n,y:o},e)}))},te=function(e){L.current=e.currentTarget;var t,r=X(L.current);(t=$(L.current)&&r?L.current.firstChild:L.current,!(v.content||G(t,"tooltip")||v.children))||r||(B.current=e,T?ce("updateDelay",ee):se(v.onBeforeShow,{originalEvent:e,target:L.current})&&ce("showDelay",(function(){S(!0),se(v.onShow,{originalEvent:e,target:L.current})})))},re=function(e){(fe(),T)&&(se(v.onBeforeHide,{originalEvent:e,target:L.current})&&ce("hideDelay",(function(){(q()||!1!==R.current)&&(s.clear(D.current),c.removeClass(D.current,"p-tooltip-active"),S(!1),se(v.onHide,{originalEvent:e,target:L.current}))})))},ne=function(e,t,r){var n=0,o=0,u=r||j;if((U(e)||"mouse"==u)&&t){var i={width:c.getOuterWidth(D.current),height:c.getOuterHeight(D.current)};n=t.x,o=t.y;var a=Q(e),l=a.top,s=a.left;switch(u){case"left":n-=i.width+s,o-=i.height/2-l;break;case"right":case"mouse":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||N.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=G(e,"my")||v.my||f.my,d=G(e,"at")||v.at||f.at;D.current.style.padding="0px",c.flipfitCollision(D.current,e,p,d,(function(e){var t=e.at,r=t.x,n=v.at?"center"!==r&&r!==e.my.x?r:t.y:e.at["".concat(f.axis)];D.current.style.padding="",k(n),oe(n),c.addClass(D.current,"p-tooltip-active")}))}},oe=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")}},ue=function(){q()||(R.current=!1)},ie=function(e){q()||(R.current=!0,re(e))},ae=function(e){if(e){var t=K(e),r=t.showEvents,n=t.hideEvents,o=pe(e);r.forEach((function(e){return null==o?void 0:o.addEventListener(e,te)})),n.forEach((function(e){return null==o?void 0:o.addEventListener(e,re)}))}},le=function(e){if(e){var t=K(e),r=t.showEvents,n=t.hideEvents,o=pe(e);r.forEach((function(e){return null==o?void 0:o.removeEventListener(e,te)})),n.forEach((function(e){return null==o?void 0:o.removeEventListener(e,re)}))}},ce=function(e,t){fe();var r=G(L.current,e.toLowerCase())||v[e];r?W.current["".concat(e)]=setTimeout((function(){return t()}),r):t()},se=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},fe=function(){Object.values(W.current).forEach((function(e){return clearTimeout(e)}))},pe=function(e){if(e){if($(e)){if(e.hasWrapper)return e.parentElement;var t=document.createElement("span");return c.addClass(t,"p-tooltip-target-wrapper"),e.parentNode.insertBefore(t,e),t.appendChild(e),e.hasWrapper=!0,t}var r;return e.hasWrapper&&((r=e.parentElement).replaceWith.apply(r,y(n=e.parentElement.childNodes)||g(n)||b(n)||E()),delete e.hasWrapper),e}var n;return null},de=function(e){he(e),me(e)},me=function(e){ve(e||v.target,ae)},he=function(e){ve(e||v.target,le)},ve=function(e,t){if(e=l.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)}};o((function(){me(),T&&L.current&&X(L.current)&&re()})),u((function(){return me(),function(){he()}}),[te,re,v.target]),u((function(){if(T){var e=G(L.current,"position")||j,t=G(L.current,"classname");k(e),I(t),ee(e),Z(),_()}else k(v.position),I(""),L.current=null,N.current=null,R.current=!0;return function(){F(),z()}}),[T]),u((function(){T&&ce("updateDelay",(function(){V(L.current,(function(){ne(L.current)}))}))}),[v.content]),i((function(){fe(),he(),s.clear(D.current)})),e.useImperativeHandle(m,(function(){return{props:v,updateTargetEvents:de,loadTargetEvents:me,unloadTargetEvents:he,show:te,hide:re,getElement:function(){return D.current},getTarget:function(){return L.current}}}));var ye,ge,be,Ee,we,Te,Se;if(T){var xe=(we=O.getOtherProps(v),Te=f("p-tooltip p-component",(ye={},ge="p-tooltip-".concat(j),be=!0,(ge=h(ge))in ye?Object.defineProperty(ye,ge,{value:be,enumerable:!0,configurable:!0,writable:!0}):ye[ge]=be,ye),v.className,C),Ee=L.current,Se=!(v.content||G(Ee,"tooltip")),e.createElement("div",p({id:v.id,ref:D,className:Te,style:v.style,role:"tooltip","aria-hidden":T},we,{onMouseEnter:ue,onMouseLeave:ie}),e.createElement("div",{className:"p-tooltip-arrow"}),e.createElement("div",{ref:H,className:"p-tooltip-text"},Se&&v.children)));return e.createElement(a,{element:xe,appendTo:v.appendTo,visible:!0})}return null})));P.displayName="Tooltip";export{P as Tooltip};