UNPKG

carbon-react

Version:

A library of reusable React components for easily building user interfaces.

2 lines (1 loc) 1.74 kB
const t='button:not([disabled]), [href], input:not([type="hidden"]):not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]',e=t=>{let e=0;const n=()=>{"visible"===window.getComputedStyle(t).visibility?t.focus():e<100&&(setTimeout(n,10),e+=10)};n()},n=t=>t.hasAttribute("type")&&"radio"===t.getAttribute("type"),i=(t,e)=>{const n=document.querySelectorAll(`input[type="radio"][name="${t}"]`);return[...n].find((t=>t.checked))||n[e?n.length-1:0]},l=(t,e,l)=>{const r=e.indexOf(t);if(-1===r)return;const o=l?-1:1;let u,d=r;for(;!u;){d+=o,d<0&&(d+=e.length),d>=e.length&&(d-=e.length);const r=e[d];if(r===t)return n(t)&&document.activeElement!==t?i(t.getAttribute("name"),l):t;if(n(r)){const e=r.getAttribute("name");n(t)?e!==t.getAttribute("name")&&(u=i(e,l)):u=i(e,l)}else u=r}return u},r=(l,r,o)=>u=>()=>{const d="top"===o,s=l.indexOf(u);let c,a,f=s;do{var h;f+=d?-1:1,f<0&&(f+=l.length),f>=l.length&&(f-=l.length),c=l[f],a=Array.from((null==c||null===(h=c.current)||void 0===h?void 0:h.querySelectorAll(r||t))||[]).filter((t=>-1!==Number(t.tabIndex)))}while(f!==s&&!(null==a?void 0:a.length));const b=null==a?void 0:a[d?a.length-1:0];if(n(b)){const t=i(b.getAttribute("name"),d);e(t)}else e(b)},o=(t,n,i,r,o)=>{const u=r?n.filter((t=>t.matches(r))):n,d=u[0],s=u[u.length-1];if(o)return void o(t,d,s);if("Tab"!==t.key)return;if(!(null==u?void 0:u.length))return void t.preventDefault();const c=document.activeElement;if(c===document.body)return t.preventDefault(),void e(d);if(!r)return;const a=t.shiftKey?d:s,f=l(i?a:c,u,t.shiftKey),h=l(i?a:c,n,t.shiftKey);f&&f!==h&&(e(f),t.preventDefault())};export{t as defaultFocusableSelectors,l as getNextElement,r as onTabGuardFocus,e as setElementFocus,o as trapFunction};