@styleless-ui/react
Version:
Completely unstyled, headless and accessible React UI components.
1 lines • 2.05 kB
JavaScript
import{isContainingBlock as t,isHTMLElement as e,isShadowRoot as o,isWindow as n}from"./is";export var getWindow=function(t){if(!t)return window;if(!n(t)){var e=t.ownerDocument;return e&&e.defaultView||window}return t};export var getDocumentElement=function(t){var e;return(null!==(e=t instanceof getWindow(t).Node?t.ownerDocument:t.document)&&void 0!==e?e:window.document).documentElement};export var getNodeName=function(t){return n(t)?"":t?(t.nodeName||"").toLowerCase():""};export var getParentNode=function(t){return"html"===getNodeName(t)?t:t.assignedSlot||t.parentNode||(o(t)?t.host:null)||getDocumentElement(t)};export var getContainingBlock=function(n){var i=getParentNode(n);for(i&&o(i)&&(i=i.host);e(i)&&!["html","body"].includes(getNodeName(i));){if(t(i))return i;i=i.parentNode}return null};export var getOffsetParent=function(o){for(var n=getWindow(o),i=function(t){return e(t)&&"fixed"!==n.getComputedStyle(t).position?t.offsetParent:null},r=i(o);r&&["table","td","th"].includes(getNodeName(r))&&"static"===n.getComputedStyle(r).position;)r=i(r);return r&&("html"===getNodeName(r)||"body"===getNodeName(r)&&"static"===n.getComputedStyle(r).position&&!t(r))?n:r||getContainingBlock(o)||n};export var getBoundingClientRect=function(t,o){void 0===o&&(o=!1);var n=t.getBoundingClientRect(),i=1,r=1;return o&&e(t)&&(i=t.offsetWidth>0&&Math.round(n.width)/t.offsetWidth||1,r=t.offsetHeight>0&&Math.round(n.height)/t.offsetHeight||1),{width:n.width/i,height:n.height/r,top:n.top/r,right:n.right/i,bottom:n.bottom/r,left:n.left/i,x:n.left/i,y:n.top/r}};export var getViewportRect=function(t){var e=getWindow(t),o=getDocumentElement(t),n=e.visualViewport,i=o.clientWidth,r=o.clientHeight,d=0,a=0;return n&&(i=n.width,r=n.height,Math.abs(e.innerWidth/n.scale-n.width)<.01&&(d=n.offsetLeft,a=n.offsetTop)),{width:i,height:r,x:d,y:a}};export var contains=function(t,e){var n;if(t.contains(e))return!0;var i=null===(n=e.getRootNode)||void 0===n?void 0:n.call(e);if(i&&o(i)){var r=e;do{if(r&&t===r)return!0;r=r.parentNode||r.host}while(r)}return!1};