ariakit-utils
Version:
Ariakit utils
2 lines (1 loc) • 9.75 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).AriakitUtils={})}(this,(function(t){"use strict";const e="undefined"!=typeof window&&!!window.document?.createElement;function n(t){return t?t.ownerDocument||t:document}function o(t,e){void 0===e&&(e=!1);const{activeElement:r}=n(t);if(!r?.nodeName)return null;if(i(r)&&r.contentDocument)return o(r.contentDocument.body,e);if(e){const t=r.getAttribute("aria-activedescendant");if(t){const e=n(r).getElementById(t);if(e)return e}}return r}function r(t,e){return t===e||t.contains(e)}function i(t){return"IFRAME"===t.tagName}const u=["button","color","file","image","reset","submit"];function c(t,e){return"matches"in t?t.matches(e):"msMatchesSelector"in t?t.msMatchesSelector(e):t.webkitMatchesSelector(e)}function s(t){const e=t;return e.offsetWidth>0||e.offsetHeight>0||t.getClientRects().length>0}function a(t,e){if("closest"in t)return t.closest(e);do{if(c(t,e))return t;t=t.parentElement||t.parentNode}while(null!==t&&1===t.nodeType);return null}function l(t){try{const e=t instanceof HTMLInputElement&&null!==t.selectionStart,n="TEXTAREA"===t.tagName;return e||n||!1}catch(t){return!1}}function f(t,e){const n=t?.getAttribute("role");return n&&-1!==["dialog","menu","listbox","tree","grid"].indexOf(n)?n:e}function d(t){if(!t)return null;if(t.clientHeight&&t.scrollHeight>t.clientHeight){const{overflowY:e}=getComputedStyle(t);if("visible"!==e&&"hidden"!==e)return t}return d(t.parentElement)||document.scrollingElement||document.body}function b(t){const e=t.getBoundingClientRect(),n=d(t);if(!n)return!1;const o=n.getBoundingClientRect(),r="HTML"===n.tagName,i=r?o.top+n.scrollTop:o.top,u=r?n.clientHeight:o.bottom,c=r?o.left+n.scrollLeft:o.left,s=r?n.clientWidth:o.right,a=e.top<i,l=e.left<c,f=e.bottom>u,b=e.right>s;return a||l||f||b}function g(){return e&&!!navigator.maxTouchPoints}function m(){return!!e&&/mac|iphone|ipad|ipod/i.test(navigator.platform)}const p="input:not([type='hidden']):not([disabled]), select:not([disabled]), textarea:not([disabled]), a[href], button:not([disabled]), [tabindex], iframe, object, embed, area[href], audio[controls], video[controls], [contenteditable]:not([contenteditable='false'])";function h(t){return c(t,p)&&s(t)}function y(t){return h(t)&&!function(t){return parseInt(t.getAttribute("tabindex")||"0",10)<0}(t)}function v(t,e){const n=Array.from(t.querySelectorAll(p));e&&n.unshift(t);const o=n.filter(h);return o.forEach(((t,e)=>{if(i(t)&&t.contentDocument){const n=t.contentDocument.body;o.splice(e,1,...v(n))}})),o}function E(t,e){const[n]=v(t,e);return n||null}function A(t,e,n){const o=Array.from(t.querySelectorAll(p)),r=o.filter(y);return e&&y(t)&&r.unshift(t),r.forEach(((t,e)=>{if(i(t)&&t.contentDocument){const o=A(t.contentDocument.body,!1,n);r.splice(e,1,...o)}})),!r.length&&n?o:r}function w(t,e,n){const[o]=A(t,e,n);return o||null}function T(t,e,n){const o=A(t,e,n);return o[o.length-1]||null}function x(t,e,n,r){const i=o(t),u=v(t,e),c=u.indexOf(i),s=u.slice(c+1);return s.find(y)||(n?u.find(y):null)||(r?s[0]:null)||null}function F(t,e,n,r){const i=o(t),u=v(t,e).reverse(),c=u.indexOf(i),s=u.slice(c+1);return s.find(y)||(n?u.find(y):null)||(r?s[0]:null)||null}function I(t){const e=o(t);if(!e)return!1;if(r(t,e))return!0;const n=e.getAttribute("aria-activedescendant");return!!n&&("id"in t&&(n===t.id||!!t.querySelector(`#${CSS.escape(n)}`)))}function S(t){const e=t.getAttribute("tabindex")??"";t.setAttribute("data-tabindex",e),t.setAttribute("tabindex","-1")}function N(t){return"object"==typeof t&&null!=t}t.addGlobalEventListener=function t(e,n,o,r){void 0===r&&(r=window);try{r.document.addEventListener(e,n,o)}catch(t){}const i=[];for(let u=0;u<r.frames?.length;u+=1){const c=r.frames[u];c&&i.push(t(e,n,o,c))}return()=>{try{r.document.removeEventListener(e,n,o)}catch(t){}i.forEach((t=>t()))}},t.addItemToArray=function(t,e,n){return void 0===n&&(n=-1),n in t?[...t.slice(0,n),e,...t.slice(n)]:[...t,e]},t.applyState=function(t,e){if(function(t){return"function"==typeof t}(t)){return t(function(t){return"function"==typeof t}(e)?e():e)}return t},t.canUseDOM=e,t.chain=function(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];return function(){for(const t of e)"function"==typeof t&&t(...arguments)}},t.closest=a,t.contains=r,t.cx=function(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];return e.filter(Boolean).join(" ")||void 0},t.disableFocus=S,t.disableFocusIn=function(t,e){A(t,e).forEach(S)},t.fireBlurEvent=function(t,e){const n=new FocusEvent("blur",e),o=t.dispatchEvent(n),r={...e,bubbles:!0};return t.dispatchEvent(new FocusEvent("focusout",r)),o},t.fireClickEvent=function(t,e){const n=new MouseEvent("click",e);return t.dispatchEvent(n)},t.fireEvent=function(t,e,n){const o=new Event(e,n);return t.dispatchEvent(o)},t.fireFocusEvent=function(t,e){const n=new FocusEvent("focus",e),o=t.dispatchEvent(n),r={...e,bubbles:!0};return t.dispatchEvent(new FocusEvent("focusin",r)),o},t.fireKeyboardEvent=function(t,e,n){const o=new KeyboardEvent(e,n);return t.dispatchEvent(o)},t.flatten2DArray=function(t){const e=[];for(const n of t)e.push(...n);return e},t.focusIfNeeded=function(t){!I(t)&&h(t)&&t.focus()},t.focusIntoView=function(t,e){"scrollIntoView"in t?(t.focus({preventScroll:!0}),t.scrollIntoView({block:"nearest",inline:"nearest",...e})):t.focus()},t.getActiveElement=o,t.getAllFocusable=function(t){return v(document.body,t)},t.getAllFocusableIn=v,t.getAllTabbable=function(t){return A(document.body,!1,t)},t.getAllTabbableIn=A,t.getClosestFocusable=function(t){for(;t&&!h(t);)t=a(t,p);return t||null},t.getDocument=n,t.getFirstFocusable=function(t){return E(document.body,t)},t.getFirstFocusableIn=E,t.getFirstTabbable=function(t){return w(document.body,!1,t)},t.getFirstTabbableIn=w,t.getLastTabbable=function(t){return T(document.body,!1,t)},t.getLastTabbableIn=T,t.getNextTabbable=function(t,e){return x(document.body,!1,t,e)},t.getNextTabbableIn=x,t.getPopupItemRole=function(t,e){const n=f(t);return n?{menu:"menuitem",listbox:"option",tree:"treeitem",grid:"gridcell"}[n]??e:e},t.getPopupRole=f,t.getPreviousTabbable=function(t,e){return F(document.body,!1,t,e)},t.getPreviousTabbableIn=F,t.getScrollingElement=d,t.getTextboxSelection=function(t){let e=0,o=0;if(l(t))e=t.selectionStart||0,o=t.selectionEnd||0;else if(t.isContentEditable){const i=n(t).getSelection();if(i?.rangeCount&&i.anchorNode&&r(t,i.anchorNode)&&i.focusNode&&r(t,i.focusNode)){const n=i.getRangeAt(0),r=n.cloneRange();r.selectNodeContents(t),r.setEnd(n.startContainer,n.startOffset),e=r.toString().length,r.setEnd(n.endContainer,n.endOffset),o=r.toString().length}}return{start:e,end:o}},t.getWindow=function(t){return n(t).defaultView||window},t.hasFocus=function(t){const e=o(t);if(!e)return!1;if(e===t)return!0;const n=e.getAttribute("aria-activedescendant");return!!n&&n===t.id},t.hasFocusWithin=I,t.hasOwnProperty=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},t.isApple=m,t.isButton=function(t){const e=t.tagName.toLowerCase();return"button"===e||!("input"!==e||!t.type)&&-1!==u.indexOf(t.type)},t.isDownloading=function(t){const e=t.currentTarget;if(!e)return!1;const n=e.tagName.toLowerCase();return!!t.altKey&&("a"===n||("button"===n&&"submit"===e.type||"input"===n&&"submit"===e.type))},t.isEmpty=function(t){return Array.isArray(t)?!t.length:N(t)?!Object.keys(t).length:null==t||""===t},t.isFirefox=function(){return e&&/firefox\//i.test(navigator.userAgent)},t.isFocusEventOutside=function(t,e){const n=e||t.currentTarget,o=t.relatedTarget;return!o||!r(n,o)},t.isFocusable=h,t.isFrame=i,t.isInteger=function(t){return"number"==typeof t?Math.floor(t)===t:String(Math.floor(Number(t)))===t},t.isMac=function(){return e&&navigator.platform.startsWith("Mac")&&!g()},t.isObject=N,t.isOpeningInNewTab=function(t){const e=t.currentTarget;if(!e)return!1;const n=m();if(n&&!t.metaKey)return!1;if(!n&&!t.ctrlKey)return!1;const o=e.tagName.toLowerCase();return"a"===o||("button"===o&&"submit"===e.type||"input"===o&&"submit"===e.type)},t.isPartiallyHidden=b,t.isPortalEvent=function(t){return Boolean(t.currentTarget&&!r(t.currentTarget,t.target))},t.isPromise=function(t){return Boolean(t&&"then"in t&&"function"==typeof t.then)},t.isSafari=function(){return e&&m()&&/apple/i.test(navigator.vendor)},t.isSelfTarget=function(t){return t.target===t.currentTarget},t.isTabbable=y,t.isTextField=l,t.isTouchDevice=g,t.isVisible=s,t.matches=c,t.noop=function(){},t.normalizeString=function(t){return t.normalize("NFD").replace(/[\u0300-\u036f]/g,"")},t.queueBeforeEvent=function(t,e,n){const o=requestAnimationFrame((()=>{t.removeEventListener(e,r,!0),n()})),r=()=>{cancelAnimationFrame(o),n()};return t.addEventListener(e,r,{once:!0,capture:!0}),o},t.queueMicrotask=function(t){if(window.queueMicrotask)return window.queueMicrotask(t);Promise.resolve().then(t)},t.restoreFocusIn=function(t){const e=t.querySelectorAll("[data-tabindex]"),n=t=>{const e=t.getAttribute("data-tabindex");t.removeAttribute("data-tabindex"),e?t.setAttribute("tabindex",e):t.removeAttribute("tabindex")};t.hasAttribute("data-tabindex")&&n(t),e.forEach(n)},t.reverseArray=function(t){return t.slice().reverse()},t.scrollIntoViewIfNeeded=function(t,e){b(t)&&"scrollIntoView"in t&&t.scrollIntoView(e)},t.shallowEqual=function(t,e){if(t===e)return!0;if(!t)return!1;if(!e)return!1;if("object"!=typeof t)return!1;if("object"!=typeof e)return!1;const n=Object.keys(t),o=Object.keys(e),{length:r}=n;if(o.length!==r)return!1;for(const o of n)if(t[o]!==e[o])return!1;return!0},t.toArray=function(t){return Array.isArray(t)?t:void 0!==t?[t]:[]}}));