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) 13 kB
import*as t from"react";import e,{PrimeReactContext as r}from"primereact/api";import{ComponentBase as n,useHandleStyle as o}from"primereact/componentbase";import{useMergeProps as i,useGlobalOnEscapeKey as a,ESC_KEY_HANDLING_PRIORITIES as u,useResizeListener as l,useOverlayScrollListener as c,useMountEffect as p,useUpdateEffect as s,useUnmountEffect as f}from"primereact/hooks";import{Portal as d}from"primereact/portal";import{classNames as m,DomHandler as h,ZIndexUtils as v,ObjectUtils as y}from"primereact/utils";function b(){return b=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},b.apply(this,arguments)}function g(t){return g="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},g(t)}function w(t,e){if("object"!==g(t)||null===t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,e||"default");if("object"!==g(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}function E(t){var e=w(t,"string");return"symbol"===g(e)?e:String(e)}function O(t,e,r){return(e=E(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function x(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function S(t){if(Array.isArray(t))return x(t)}function T(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}function j(t,e){if(t){if("string"==typeof t)return x(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?x(t,e):void 0}}function P(){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 k(t){if(Array.isArray(t))return t}function I(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i,a,u=[],l=!0,c=!1;try{if(i=(r=r.call(t)).next,0===e){if(Object(r)!==r)return;l=!1}else for(;!(l=(n=i.call(r)).done)&&(u.push(n.value),u.length!==e);l=!0);}catch(t){c=!0,o=t}finally{try{if(!l&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(c)throw o}}return u}}function D(){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 N(t,e){return k(t)||I(t,e)||j(t,e)||D()}var C=n.extend({defaultProps:{__TYPE:"Tooltip",appendTo:null,at:null,autoHide:!0,autoZIndex:!0,baseZIndex:0,className:null,closeOnEscape:!1,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},css:{classes:{root:function(t){var e=t.classNameState;return m("p-tooltip p-component",O({},"p-tooltip-".concat(t.positionState),!0),e)},arrow:"p-tooltip-arrow",text:"p-tooltip-text"},styles:"\n@layer primereact {\n .p-tooltip {\n position: absolute;\n padding: .25em .5rem;\n /* #3687: Tooltip prevent scrollbar flickering */\n top: -9999px;\n left: -9999px;\n }\n \n .p-tooltip.p-tooltip-right,\n .p-tooltip.p-tooltip-left {\n padding: 0 .25rem;\n }\n \n .p-tooltip.p-tooltip-top,\n .p-tooltip.p-tooltip-bottom {\n padding:.25em 0;\n }\n \n .p-tooltip .p-tooltip-text {\n white-space: pre-line;\n word-break: break-word;\n }\n \n .p-tooltip-arrow {\n position: absolute;\n width: 0;\n height: 0;\n border-color: transparent;\n border-style: solid;\n }\n \n .p-tooltip-right .p-tooltip-arrow {\n top: 50%;\n left: 0;\n margin-top: -.25rem;\n border-width: .25em .25em .25em 0;\n }\n \n .p-tooltip-left .p-tooltip-arrow {\n top: 50%;\n right: 0;\n margin-top: -.25rem;\n border-width: .25em 0 .25em .25rem;\n }\n \n .p-tooltip.p-tooltip-top {\n padding: .25em 0;\n }\n \n .p-tooltip-top .p-tooltip-arrow {\n bottom: 0;\n left: 50%;\n margin-left: -.25rem;\n border-width: .25em .25em 0;\n }\n \n .p-tooltip-bottom .p-tooltip-arrow {\n top: 0;\n left: 50%;\n margin-left: -.25rem;\n border-width: 0 .25em .25rem;\n }\n\n .p-tooltip-target-wrapper {\n display: inline-flex;\n }\n}\n",inlineStyles:{arrow:function(t){var e=t.context;return{top:e.bottom?"0":e.right||e.left||!e.right&&!e.left&&!e.top&&!e.bottom?"50%":null,bottom:e.top?"0":null,left:!e.right&&(e.right||e.left||e.top||e.bottom)?e.top||e.bottom?"50%":null:"0",right:e.left?"0":null}}}}});function A(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function L(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?A(Object(r),!0).forEach((function(e){O(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):A(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}var H=t.memo(t.forwardRef((function(n,g){var w=i(),E=t.useContext(r),O=C.getProps(n,E),x=N(t.useState(!1),2),k=x[0],I=x[1],D=N(t.useState(O.position||"right"),2),A=D[0],H=D[1],R=N(t.useState(""),2),W=R[0],M=R[1],Z=N(t.useState(!1),2),B=Z[0],F=Z[1],U={props:O,state:{visible:k,position:A,className:W},context:{right:"right"===A,left:"left"===A,top:"top"===A,bottom:"bottom"===A}},z=C.setMetaData(U),Y=z.ptm,_=z.cx,X=z.sx;o(C.css.styles,z.isUnstyled,{name:"tooltip"}),a({callback:function(){bt()},when:O.closeOnEscape,priority:[u.TOOLTIP,0]});var $=t.useRef(null),q=t.useRef(null),G=t.useRef(null),J=t.useRef(null),K=t.useRef(!0),Q=t.useRef({}),V=t.useRef(null),tt=N(l({listener:function(t){!h.isTouchDevice()&&bt(t)}}),2),et=tt[0],rt=tt[1],nt=N(c({target:G.current,listener:function(t){bt(t)},when:k}),2),ot=nt[0],it=nt[1],at=function(t){return pt(t,"mousetrack")||O.mouseTrack},ut=function(t){return"true"===pt(t,"disabled")||st(t,"disabled")||O.disabled},lt=function(t){return pt(t,"showondisabled")||O.showOnDisabled},ct=function(){return pt(G.current,"autohide")||O.autoHide},pt=function(t,e){return st(t,"data-pr-".concat(e))?t.getAttribute("data-pr-".concat(e)):null},st=function(t,e){return t&&t.hasAttribute(e)},ft=function(t){var e=[pt(t,"showevent")||O.showEvent],r=[pt(t,"hideevent")||O.hideEvent];if(at(t))e=["mousemove"],r=["mouseleave"];else{var n=pt(t,"event")||O.event;"focus"===n&&(e=["focus"],r=["blur"]),"both"===n&&(e=["focus","mouseenter"],r=B?["blur"]:["mouseleave","blur"])}return{showEvents:e,hideEvents:r}},dt=function(t){return pt(t,"position")||A},mt=function(t){return{top:pt(t,"mousetracktop")||O.mouseTrackTop,left:pt(t,"mousetrackleft")||O.mouseTrackLeft}},ht=function(t,e){if(q.current){var r=pt(t,"tooltip")||O.content;r?(q.current.innerHTML="",q.current.appendChild(document.createTextNode(r)),e()):O.children&&e()}},vt=function(t){ht(G.current,(function(){var r=V.current,n=r.pageX,o=r.pageY;O.autoZIndex&&!v.get($.current)&&v.set("tooltip",$.current,E&&E.autoZIndex||e.autoZIndex,O.baseZIndex||E&&E.zIndex.tooltip||e.zIndex.tooltip),$.current.style.left="",$.current.style.top="",ct()&&($.current.style.pointerEvents="none");var i=at(G.current)||"mouse"===t;(i&&!J.current||i)&&(J.current={width:h.getOuterWidth($.current),height:h.getOuterHeight($.current)}),gt(G.current,{x:n,y:o},t)}))},yt=function(t){t.type&&"focus"===t.type&&F(!0),G.current=t.currentTarget;var e,r=ut(G.current);(e=lt(G.current)&&r?G.current.firstChild:G.current,!(O.content||pt(e,"tooltip")||O.children))||r||(V.current=t,k?St("updateDelay",vt):Tt(O.onBeforeShow,{originalEvent:t,target:G.current})&&St("showDelay",(function(){I(!0),Tt(O.onShow,{originalEvent:t,target:G.current})})))},bt=function(t){(t&&"blur"===t.type&&F(!1),jt(),k)&&(Tt(O.onBeforeHide,{originalEvent:t,target:G.current})&&St("hideDelay",(function(){(ct()||!1!==K.current)&&(v.clear($.current),h.removeClass($.current,"p-tooltip-active"),I(!1),Tt(O.onHide,{originalEvent:t,target:G.current}))})))},gt=function(t,e,r){var n=0,o=0,i=r||A;if((at(t)||"mouse"==i)&&e){var a={width:h.getOuterWidth($.current),height:h.getOuterHeight($.current)};n=e.x,o=e.y;var u=mt(t),l=u.top,c=u.left;switch(i){case"left":n-=a.width+c,o-=a.height/2-l;break;case"right":case"mouse":n+=c,o-=a.height/2-l;break;case"top":n-=a.width/2-c,o-=a.height+l;break;case"bottom":n-=a.width/2-c,o+=l}n<=0||J.current.width>a.width?($.current.style.left="0px",$.current.style.right=window.innerWidth-a.width-n+"px"):($.current.style.right="",$.current.style.left=n+"px"),$.current.style.top=o+"px",h.addClass($.current,"p-tooltip-active")}else{var p=h.findCollisionPosition(i),s=pt(t,"my")||O.my||p.my,f=pt(t,"at")||O.at||p.at;$.current.style.padding="0px",h.flipfitCollision($.current,t,s,f,(function(t){var e=t.at,r=e.x,n=O.at?"center"!==r&&r!==t.my.x?r:e.y:t.at["".concat(p.axis)];$.current.style.padding="",H(n),wt(n),h.addClass($.current,"p-tooltip-active")}))}},wt=function(t){if($.current){var e=getComputedStyle($.current);"left"===t?$.current.style.left=parseFloat(e.left)-2*parseFloat(e.paddingLeft)+"px":"top"===t&&($.current.style.top=parseFloat(e.top)-2*parseFloat(e.paddingTop)+"px")}},Et=function(t){ct()||(K.current=!0,bt(t))},Ot=function(t){if(t){var e=ft(t),r=e.showEvents,n=e.hideEvents,o=Pt(t);r.forEach((function(t){return null==o?void 0:o.addEventListener(t,yt)})),n.forEach((function(t){return null==o?void 0:o.addEventListener(t,bt)}))}},xt=function(t){if(t){var e=ft(t),r=e.showEvents,n=e.hideEvents,o=Pt(t);r.forEach((function(t){return null==o?void 0:o.removeEventListener(t,yt)})),n.forEach((function(t){return null==o?void 0:o.removeEventListener(t,bt)}))}},St=function(t,e){jt();var r=pt(G.current,t.toLowerCase())||O[t];r?Q.current["".concat(t)]=setTimeout((function(){return e()}),r):e()},Tt=function(t){if(t){for(var e=arguments.length,r=new Array(e>1?e-1:0),n=1;n<e;n++)r[n-1]=arguments[n];var o=t.apply(void 0,r);return void 0===o&&(o=!0),o}return!0},jt=function(){Object.values(Q.current).forEach((function(t){return clearTimeout(t)}))},Pt=function(t){if(t){if(lt(t)){if(!t.hasWrapper){var e=document.createElement("div");return"INPUT"===t.nodeName?h.addMultipleClasses(e,"p-tooltip-target-wrapper p-inputwrapper"):h.addClass(e,"p-tooltip-target-wrapper"),t.parentNode.insertBefore(e,t),e.appendChild(t),t.hasWrapper=!0,e}return t.parentElement}var r;return t.hasWrapper&&((r=t.parentElement).replaceWith.apply(r,S(n=t.parentElement.childNodes)||T(n)||j(n)||P()),delete t.hasWrapper),t}var n;return null},kt=function(t){Dt(t),It(t)},It=function(t){Nt(t||O.target,Ot)},Dt=function(t){Nt(t||O.target,xt)},Nt=function(t,e){if(t=y.getRefElement(t))if(h.isElement(t))e(t);else{var r=function(t){h.find(document,t).forEach((function(t){e(t)}))};t instanceof Array?t.forEach((function(t){r(t)})):r(t)}};p((function(){k&&G.current&&ut(G.current)&&bt()})),s((function(){return It(),function(){Dt()}}),[yt,bt,O.target]),s((function(){if(k){var t=dt(G.current),e=pt(G.current,"classname");H(t),M(e),vt(t),et(),ot()}else H(O.position||"right"),M(""),G.current=null,J.current=null,K.current=!0;return function(){rt(),it()}}),[k]),s((function(){var t=dt(G.current);k&&"mouse"!==t&&St("updateDelay",(function(){ht(G.current,(function(){gt(G.current)}))}))}),[O.content]),f((function(){bt(),v.clear($.current)})),t.useImperativeHandle(g,(function(){return{props:O,updateTargetEvents:kt,loadTargetEvents:It,unloadTargetEvents:Dt,show:yt,hide:bt,getElement:function(){return $.current},getTarget:function(){return G.current}}}));var Ct,At,Lt,Ht,Rt;if(k){var Wt=(Ct=G.current,At=!(O.content||pt(Ct,"tooltip")),Lt=w({id:O.id,className:m(O.className,_("root",{positionState:A,classNameState:W})),style:O.style,role:"tooltip","aria-hidden":k,onMouseEnter:function(t){ct()||(K.current=!1)},onMouseLeave:function(t){return Et(t)}},C.getOtherProps(O),Y("root")),Ht=w({className:_("arrow"),style:X("arrow",L({},U))},Y("arrow")),Rt=w({className:_("text")},Y("text")),t.createElement("div",b({ref:$},Lt),t.createElement("div",Ht),t.createElement("div",b({ref:q},Rt),At&&O.children)));return t.createElement(d,{element:Wt,appendTo:O.appendTo,visible:!0})}return null})));H.displayName="Tooltip";export{H as Tooltip};