UNPKG

@primeuix/utils

Version:

Utility functions and helpers for PrimeUI Libraries

14 lines 19.4 kB
function k(t,e){return t?t.classList?t.classList.contains(e):new RegExp("(^| )"+e+"( |$)","gi").test(t.className):!1}function W(t,e){if(t&&e){let o=n=>{k(t,n)||(t.classList?t.classList.add(n):t.className+=" "+n)};[e].flat().filter(Boolean).forEach(n=>n.split(" ").forEach(o))}}function N(){return window.innerWidth-document.documentElement.offsetWidth}function lt(t){typeof t=="string"?W(document.body,t||"p-overflow-hidden"):(t!=null&&t.variableName&&document.body.style.setProperty(t.variableName,N()+"px"),W(document.body,(t==null?void 0:t.className)||"p-overflow-hidden"))}function F(t){if(t){let e=document.createElement("a");if(e.download!==void 0){let{name:o,src:n}=t;return e.setAttribute("href",n),e.setAttribute("download",o),e.style.display="none",document.body.appendChild(e),e.click(),document.body.removeChild(e),!0}}return!1}function at(t,e){let o=new Blob([t],{type:"application/csv;charset=utf-8;"});window.navigator.msSaveOrOpenBlob?navigator.msSaveOrOpenBlob(o,e+".csv"):F({name:e+".csv",src:URL.createObjectURL(o)})||(t="data:text/csv;charset=utf-8,"+t,window.open(encodeURI(t)))}function P(t,e){if(t&&e){let o=n=>{t.classList?t.classList.remove(n):t.className=t.className.replace(new RegExp("(^|\\b)"+n.split(" ").join("|")+"(\\b|$)","gi")," ")};[e].flat().filter(Boolean).forEach(n=>n.split(" ").forEach(o))}}function dt(t){typeof t=="string"?P(document.body,t||"p-overflow-hidden"):(t!=null&&t.variableName&&document.body.style.removeProperty(t.variableName),P(document.body,(t==null?void 0:t.className)||"p-overflow-hidden"))}function w(t){for(let e of document==null?void 0:document.styleSheets)try{for(let o of e==null?void 0:e.cssRules)for(let n of o==null?void 0:o.style)if(t.test(n))return{name:n,value:o.style.getPropertyValue(n).trim()}}catch(o){}return null}function S(t){let e={width:0,height:0};if(t){let[o,n]=[t.style.visibility,t.style.display],r=t.getBoundingClientRect();t.style.visibility="hidden",t.style.display="block",e.width=r.width||t.offsetWidth,e.height=r.height||t.offsetHeight,t.style.display=n,t.style.visibility=o}return e}function h(){let t=window,e=document,o=e.documentElement,n=e.getElementsByTagName("body")[0],r=t.innerWidth||o.clientWidth||n.clientWidth,i=t.innerHeight||o.clientHeight||n.clientHeight;return{width:r,height:i}}function E(t){return t?Math.abs(t.scrollLeft):0}function B(){let t=document.documentElement;return(window.pageXOffset||E(t))-(t.clientLeft||0)}function $(){let t=document.documentElement;return(window.pageYOffset||t.scrollTop)-(t.clientTop||0)}function D(t){return t?getComputedStyle(t).direction==="rtl":!1}function I(t,e,o=!0){var n,r,i,s;if(t){let d=t.offsetParent?{width:t.offsetWidth,height:t.offsetHeight}:S(t),l=d.height,a=d.width,u=e.offsetHeight,c=e.offsetWidth,f=e.getBoundingClientRect(),g=$(),it=B(),st=h(),L,R,ot="top";f.top+u+l>st.height?(L=f.top+g-l,ot="bottom",L<0&&(L=g)):L=u+f.top+g,f.left+a>st.width?R=Math.max(0,f.left+it+c-a):R=f.left+it,D(t)?t.style.insetInlineEnd=R+"px":t.style.insetInlineStart=R+"px",t.style.top=L+"px",t.style.transformOrigin=ot,o&&(t.style.marginTop=ot==="bottom"?`calc(${(r=(n=w(/-anchor-gutter$/))==null?void 0:n.value)!=null?r:"2px"} * -1)`:(s=(i=w(/-anchor-gutter$/))==null?void 0:i.value)!=null?s:"")}}function v(t,e){t&&(typeof e=="string"?t.style.cssText=e:Object.entries(e||{}).forEach(([o,n])=>t.style[o]=n))}function T(t,e){if(t instanceof HTMLElement){let o=t.offsetWidth;if(e){let n=getComputedStyle(t);o+=parseFloat(n.marginLeft)+parseFloat(n.marginRight)}return o}return 0}function V(t,e,o=!0,n=void 0){var r;if(t){let i=t.offsetParent?{width:t.offsetWidth,height:t.offsetHeight}:S(t),s=e.offsetHeight,d=e.getBoundingClientRect(),l=h(),a,u,c=n!=null?n:"top";if(!n&&d.top+s+i.height>l.height?(a=-1*i.height,c="bottom",d.top+a<0&&(a=-1*d.top)):a=s,i.width>l.width?u=d.left*-1:d.left+i.width>l.width?u=(d.left+i.width-l.width)*-1:u=0,t.style.top=a+"px",t.style.insetInlineStart=u+"px",t.style.transformOrigin=c,o){let f=(r=w(/-anchor-gutter$/))==null?void 0:r.value;t.style.marginTop=c==="bottom"?`calc(${f!=null?f:"2px"} * -1)`:f!=null?f:""}}}function ft(t,e,o,n=!0){t&&e&&(o==="self"?V(t,e):(n&&(t.style.minWidth=T(e)+"px"),I(t,e)))}function y(t){if(t){let e=t.parentNode;return e&&e instanceof ShadowRoot&&e.host&&(e=e.host),e}return null}function C(t){return!!(t!==null&&typeof t!="undefined"&&t.nodeName&&y(t))}import{resolve as oe}from"@primeuix/utils/object";function p(t){return typeof Element!="undefined"?t instanceof Element:t!==null&&typeof t=="object"&&t.nodeType===1&&typeof t.nodeName=="string"}function b(t){var o;if(p(t))return t;if(!t||typeof t!="object")return;let e=t;if("current"in t)e=t.current,e=(o=b(e==null?void 0:e.elementRef))!=null?o:e;else if("value"in t)e=t.value;else if("nativeElement"in t)e=t.nativeElement;else if("el"in t){let n=t.el;n&&typeof n=="object"&&"nativeElement"in n?e=n.nativeElement:e=n}else if("elementRef"in t)return b(t.elementRef);return e=oe(e),p(e)?e:void 0}function U(t,e){var o,n,r;if(t)switch(t){case"document":return document;case"window":return window;case"body":return document.body;case"@next":return e==null?void 0:e.nextElementSibling;case"@prev":return e==null?void 0:e.previousElementSibling;case"@first":return e==null?void 0:e.firstElementChild;case"@last":return e==null?void 0:e.lastElementChild;case"@child":return(o=e==null?void 0:e.children)==null?void 0:o[0];case"@parent":return e==null?void 0:e.parentElement;case"@grandparent":return(n=e==null?void 0:e.parentElement)==null?void 0:n.parentElement;default:{if(typeof t=="string"){let l=t.match(/^@child\[(\d+)]/);return l?((r=e==null?void 0:e.children)==null?void 0:r[parseInt(l[1],10)])||null:document.querySelector(t)||null}let s=(l=>typeof l=="function"&&"call"in l&&"apply"in l)(t)?t():t,d=b(s);return C(d)?d:(s==null?void 0:s.nodeType)===9?s:void 0}}}function ut(t,e){let o=U(t,e);if(o)o.appendChild(e);else throw new Error("Cannot append "+e+" to "+t)}var nt;function pt(t){if(t){let e=getComputedStyle(t);return t.offsetHeight-t.clientHeight-parseFloat(e.borderTopWidth)-parseFloat(e.borderBottomWidth)}else{if(nt!=null)return nt;let e=document.createElement("div");v(e,{width:"100px",height:"100px",overflow:"scroll",position:"absolute",top:"-9999px"}),document.body.appendChild(e);let o=e.offsetHeight-e.clientHeight;return document.body.removeChild(e),nt=o,o}}var rt;function M(t){if(t){let e=getComputedStyle(t);return t.offsetWidth-t.clientWidth-parseFloat(e.borderLeftWidth)-parseFloat(e.borderRightWidth)}else{if(rt!=null)return rt;let e=document.createElement("div");v(e,{width:"100px",height:"100px",overflow:"scroll",position:"absolute",top:"-9999px"}),document.body.appendChild(e);let o=e.offsetWidth-e.clientWidth;return document.body.removeChild(e),rt=o,o}}function ct(){if(window.getSelection){let t=window.getSelection()||{};t.empty?t.empty():t.removeAllRanges&&t.rangeCount>0&&t.getRangeAt(0).getClientRects().length>0&&t.removeAllRanges()}}function A(t,e={}){if(p(t)){let o=(n,r)=>{var s,d;let i=(s=t==null?void 0:t.$attrs)!=null&&s[n]?[(d=t==null?void 0:t.$attrs)==null?void 0:d[n]]:[];return[r].flat().reduce((l,a)=>{if(a!=null){let u=typeof a;if(u==="string"||u==="number")l.push(a);else if(u==="object"){let c=Array.isArray(a)?o(n,a):Object.entries(a).map(([f,g])=>n==="style"&&(g||g===0)?`${f.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()}:${g}`:g?f:void 0);l=c.length?l.concat(c.filter(f=>!!f)):l}}return l},i)};Object.entries(e).forEach(([n,r])=>{if(r!=null){let i=n.match(/^on(.+)/);i?t.addEventListener(i[1].toLowerCase(),r):n==="p-bind"||n==="pBind"?A(t,r):(r=n==="class"?[...new Set(o("class",r))].join(" ").trim():n==="style"?o("style",r).join(";").trim():r,(t.$attrs=t.$attrs||{})&&(t.$attrs[n]=r),t.setAttribute(n,r))}})}}function j(t,e={},...o){if(t){let n=document.createElement(t);return A(n,e),n.append(...o),n}}function q(t,e={}){return t?`<style${Object.entries(e).reduce((o,[n,r])=>o+` ${n}="${r}"`,"")}>${t}</style>`:""}function mt(t,e={}){return q(t,e)}function X(t,e={},o){let n=j("style",e,t);return o==null||o.appendChild(n),n}function gt(t={},e){return X("",t,e||document.head)}function ht(t,e){if(t){t.style.opacity="0";let o=+new Date,n="0",r=function(){n=`${+t.style.opacity+(new Date().getTime()-o)/e}`,t.style.opacity=n,o=+new Date,+n<1&&("requestAnimationFrame"in window?requestAnimationFrame(r):setTimeout(r,16))};r()}}function yt(t,e){if(t){let o=1,n=50,r=n/e,i=setInterval(()=>{o-=r,o<=0&&(o=0,clearInterval(i)),t.style.opacity=o.toString()},n)}}function Y(t,e){return p(t)?Array.from(t.querySelectorAll(e)):[]}function z(t,e){return p(t)?t.matches(e)?t:t.querySelector(e):null}function bt(t,e){t&&document.activeElement!==t&&t.focus(e)}function Q(t,e){if(p(t)){let o=t.getAttribute(e);return isNaN(o)?o==="true"||o==="false"?o==="true":o:+o}}function Z(){let t=navigator.userAgent.toLowerCase(),e=/(chrome)[ ]([\w.]+)/.exec(t)||/(webkit)[ ]([\w.]+)/.exec(t)||/(opera)(?:.*version|)[ ]([\w.]+)/.exec(t)||/(msie) ([\w.]+)/.exec(t)||t.indexOf("compatible")<0&&/(mozilla)(?:.*? rv:([\w.]+)|)/.exec(t)||[];return{browser:e[1]||"",version:e[2]||"0"}}var m=null;function xt(){if(!m){m={};let t=Z();t.browser&&(m[t.browser]=!0,m.version=t.version),m.chrome?m.webkit=!0:m.webkit&&(m.safari=!0)}return m}function wt(){return navigator.languages&&navigator.languages.length&&navigator.languages[0]||navigator.language||"en"}function Et(t,e,o){var n;return t&&e?o?(n=t==null?void 0:t.style)==null?void 0:n.getPropertyValue(e):getComputedStyle(t).getPropertyValue(e):null}function St(t,e,o,n){if(t){let r=getComputedStyle(t),i=document.createElement("div");i.style.position="absolute",i.style.top="0px",i.style.left="0px",i.style.visibility="hidden",i.style.pointerEvents="none",i.style.overflow=r.overflow,i.style.width=r.width,i.style.height=r.height,i.style.padding=r.padding,i.style.border=r.border,i.style.overflowWrap=r.overflowWrap,i.style.whiteSpace=r.whiteSpace,i.style.lineHeight=r.lineHeight,i.innerHTML=e.replace(/\r\n|\r|\n/g,"<br />");let s=document.createElement("span");s.textContent=n,i.appendChild(s);let d=document.createTextNode(o);i.appendChild(d),document.body.appendChild(i);let{offsetLeft:l,offsetTop:a,clientHeight:u}=s;return document.body.removeChild(i),{left:Math.abs(l-t.scrollLeft),top:Math.abs(a-t.scrollTop)+u}}return{top:"auto",left:"auto"}}function x(t,e=""){let o=Y(t,`button:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${e}, [href]:not([tabindex = "-1"]):not([style*="display:none"]):not([hidden])${e}, input:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${e}, select:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${e}, textarea:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${e}, [tabIndex]:not([tabIndex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${e}, [contenteditable]:not([tabIndex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${e}`),n=[];for(let r of o)getComputedStyle(r).display!="none"&&getComputedStyle(r).visibility!="hidden"&&n.push(r);return n}function vt(t,e){let o=x(t,e);return o.length>0?o[0]:null}function Tt(t){if(t){let e=t.offsetHeight,o=getComputedStyle(t);return e-=parseFloat(o.paddingTop)+parseFloat(o.paddingBottom)+parseFloat(o.borderTopWidth)+parseFloat(o.borderBottomWidth),e}return 0}function G(t){if(t){let[e,o]=[t.style.visibility,t.style.display];t.style.visibility="hidden",t.style.display="block";let n=t.offsetHeight;return t.style.display=o,t.style.visibility=e,n}return 0}function J(t){if(t){let[e,o]=[t.style.visibility,t.style.display];t.style.visibility="hidden",t.style.display="block";let n=t.offsetWidth;return t.style.display=o,t.style.visibility=e,n}return 0}function Ct(t){var e;if(t){let o=(e=y(t))==null?void 0:e.childNodes,n=0;if(o)for(let r=0;r<o.length;r++){if(o[r]===t)return n;o[r].nodeType===1&&n++}}return-1}function Ht(t){if(t){let e=t.offsetWidth,o=getComputedStyle(t);return e-=parseFloat(o.borderLeft)+parseFloat(o.borderRight),e}return 0}function Lt(t,e){let o=x(t,e);return o.length>0?o[o.length-1]:null}function Wt(t,e){let o=t.nextElementSibling;for(;o;){if(o.matches(e))return o;o=o.nextElementSibling}return null}function Pt(t,e,o){let n=x(t,o),r=n.length>0?n.findIndex(s=>s===e):-1,i=r>-1&&n.length>=r+1?r+1:-1;return i>-1?n[i]:null}function K(t){if(t){let e=t.getBoundingClientRect();return{top:e.top+(window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0),left:e.left+(window.pageXOffset||E(document.documentElement)||E(document.body)||0)}}return{top:"auto",left:"auto"}}function H(t,e){if(t){let o=t.offsetHeight;if(e){let n=getComputedStyle(t);o+=parseFloat(n.marginTop)+parseFloat(n.marginBottom)}return o}return 0}function O(t,e=[]){let o=y(t);return o===null?e:O(o,e.concat([o]))}function Mt(t,e){let o=t.previousElementSibling;for(;o;){if(o.matches(e))return o;o=o.previousElementSibling}return null}function At(t){let e=[];if(t){let o=O(t),n=/(auto|scroll)/,r=i=>{try{let s=window.getComputedStyle(i,null);return n.test(s.getPropertyValue("overflow"))||n.test(s.getPropertyValue("overflowX"))||n.test(s.getPropertyValue("overflowY"))}catch(s){return!1}};for(let i of o){let s=i.nodeType===1&&i.dataset.scrollselectors;if(s){let d=s.split(",");for(let l of d){let a=z(i,l);a&&r(a)&&e.push(a)}}i.nodeType!==9&&r(i)&&e.push(i)}}return e}function Ot(){if(window.getSelection)return window.getSelection().toString();if(document.getSelection)return document.getSelection().toString()}function Rt(){return navigator.userAgent}function kt(t){if(t){let e=t.offsetWidth,o=getComputedStyle(t);return e-=parseFloat(o.paddingLeft)+parseFloat(o.paddingRight)+parseFloat(o.borderLeftWidth)+parseFloat(o.borderRightWidth),e}return 0}function Nt(t){if(t){let e=getComputedStyle(t);return parseFloat(e.getPropertyValue("animation-duration")||"0")>0}return!1}function Ft(t){if(t){let e=getComputedStyle(t);return parseFloat(e.getPropertyValue("transition-duration")||"0")>0}return!1}function Bt(t,e,o){let n=t[e];typeof n=="function"&&n.apply(t,o!=null?o:[])}function $t(){return/(android)/i.test(navigator.userAgent)}function _(t,e,o){return p(t)?Q(t,e)===o:!1}function Dt(t,e,o){return!_(t,e,o)}function It(t){if(t){let e=t.nodeName,o=t.parentElement&&t.parentElement.nodeName;return e==="INPUT"||e==="TEXTAREA"||e==="BUTTON"||e==="A"||o==="INPUT"||o==="TEXTAREA"||o==="BUTTON"||o==="A"||!!t.closest(".p-button, .p-checkbox, .p-radiobutton")}return!1}function tt(){return!!(typeof window!="undefined"&&window.document&&window.document.createElement)}function Vt(t,e){if(typeof CSS=="undefined"||!CSS.supports)return!1;if(e!==void 0){let o=t.toString().replace(/[A-Z]/g,n=>"-"+n.toLowerCase());return CSS.supports(o,e)}return CSS.supports(t.toString())}function Ut(t,e=""){return p(t)?t.matches(`button:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${e}, [href][clientHeight][clientWidth]:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${e}, input:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${e}, select:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${e}, textarea:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${e}, [tabIndex]:not([tabIndex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${e}, [contenteditable]:not([tabIndex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${e}`):!1}function et(t){return!!(t&&t.offsetParent!=null)}function jt(t){return!et(t)}function qt(){return/iPad|iPhone|iPod/.test(navigator.userAgent)&&!("MSStream"in window)}function Xt(){return typeof window=="undefined"||!window.matchMedia?!1:window.matchMedia("(prefers-reduced-motion: reduce)").matches}function Yt(){return!tt()}function zt(){return"ontouchstart"in window||navigator.maxTouchPoints>0||navigator.msMaxTouchPoints>0}function Qt(t,e){var o,n;if(t){let r=t.parentElement,i=K(r),s=h(),d=t.offsetParent?t.offsetWidth:J(t),l=t.offsetParent?t.offsetHeight:G(t),a=T((o=r==null?void 0:r.children)==null?void 0:o[0]),u=H((n=r==null?void 0:r.children)==null?void 0:n[0]),c="",f="";i.left+a+d>s.width-M()?i.left<d?e%2===1?c=i.left?"-"+i.left+"px":"100%":e%2===0&&(c=s.width-d-M()+"px"):c="-100%":c="100%",t.getBoundingClientRect().top+u+l>s.height?f=`-${l-u}px`:f="0px",t.style.top=f,t.style.insetInlineStart=c}}function Zt(){return new Promise(t=>{requestAnimationFrame(()=>{requestAnimationFrame(t)})})}function Gt(t){var e;t&&("remove"in Element.prototype?t.remove():(e=t.parentNode)==null||e.removeChild(t))}function Jt(t,e){let o=b(t);if(o)o.removeChild(e);else throw new Error("Cannot remove "+e+" from "+t)}function Kt(t){var e;if(C(t)){try{(e=t.parentNode)==null||e.removeChild(t)}catch(o){}return null}return t}function _t(t,e){let o=getComputedStyle(t).getPropertyValue("borderTopWidth"),n=o?parseFloat(o):0,r=getComputedStyle(t).getPropertyValue("paddingTop"),i=r?parseFloat(r):0,s=t.getBoundingClientRect(),l=e.getBoundingClientRect().top+document.body.scrollTop-(s.top+document.body.scrollTop)-n-i,a=t.scrollTop,u=t.clientHeight,c=H(e);l<0?t.scrollTop=a+l:l+c>u&&(t.scrollTop=a+l-u+c)}function te(t,e="",o){p(t)&&o!==null&&o!==void 0&&t.setAttribute(e,o)}function ee(t,e,o=null,n){var r;e&&((r=t==null?void 0:t.style)==null||r.setProperty(e,o,n))}export{I as absolutePosition,W as addClass,v as addStyle,ft as alignOverlay,ut as appendChild,lt as blockBodyScroll,N as calculateBodyScrollbarWidth,pt as calculateScrollbarHeight,M as calculateScrollbarWidth,ct as clearSelection,j as createElement,mt as createStyleAsString,X as createStyleElement,q as createStyleMarkup,gt as createStyleTag,at as exportCSV,ht as fadeIn,yt as fadeOut,Y as find,z as findSingle,bt as focus,Q as getAttribute,xt as getBrowser,wt as getBrowserLanguage,Et as getCSSProperty,w as getCSSVariableByRegex,St as getCursorOffset,vt as getFirstFocusableElement,x as getFocusableElements,Tt as getHeight,S as getHiddenElementDimensions,G as getHiddenElementOuterHeight,J as getHiddenElementOuterWidth,Ct as getIndex,Ht as getInnerWidth,Lt as getLastFocusableElement,Wt as getNextElementSibling,Pt as getNextFocusableElement,K as getOffset,H as getOuterHeight,T as getOuterWidth,y as getParentNode,O as getParents,Mt as getPreviousElementSibling,E as getScrollLeft,At as getScrollableParents,Ot as getSelection,U as getTargetElement,Rt as getUserAgent,h as getViewport,kt as getWidth,B as getWindowScrollLeft,$ as getWindowScrollTop,Nt as hasCSSAnimation,Ft as hasCSSTransition,k as hasClass,Bt as invokeElementMethod,$t as isAndroid,_ as isAttributeEquals,Dt as isAttributeNotEquals,It as isClickable,tt as isClient,Vt as isCssSupported,p as isElement,C as isExist,Ut as isFocusableElement,jt as isHidden,qt as isIOS,Xt as isPrefersReducedMotion,D as isRTL,Yt as isServer,zt as isTouchDevice,et as isVisible,Qt as nestedPosition,Zt as nextFrame,V as relativePosition,Gt as remove,Jt as removeChild,P as removeClass,Kt as removeStyleTag,Z as resolveUserAgent,F as saveAs,_t as scrollInView,te as setAttribute,A as setAttributes,ee as setCSSProperty,b as toElement,dt as unblockBodyScroll}; //# sourceMappingURL=index.mjs.map