UNPKG

carbon-react

Version:

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

2 lines (1 loc) 1.76 kB
"use strict";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"),o=(t,e)=>{const n=document.querySelectorAll(`input[type="radio"][name="${t}"]`);return[...n].find((t=>t.checked))||n[e?n.length-1:0]},i=(t,e,i)=>{const r=e.indexOf(t);if(-1===r)return;const l=i?-1:1;let u,s=r;for(;!u;){s+=l,s<0&&(s+=e.length),s>=e.length&&(s-=e.length);const r=e[s];if(r===t)return n(t)&&document.activeElement!==t?o(t.getAttribute("name"),i):t;if(n(r)){const e=r.getAttribute("name");n(t)?e!==t.getAttribute("name")&&(u=o(e,i)):u=o(e,i)}else u=r}return u};exports.defaultFocusableSelectors=t,exports.getNextElement=i,exports.onTabGuardFocus=(i,r,l)=>u=>()=>{const s="top"===l,d=i.indexOf(u);let c,a,f=d;do{var b;f+=s?-1:1,f<0&&(f+=i.length),f>=i.length&&(f-=i.length),c=i[f],a=Array.from((null==c||null===(b=c.current)||void 0===b?void 0:b.querySelectorAll(r||t))||[]).filter((t=>-1!==Number(t.tabIndex)))}while(f!==d&&!(null==a?void 0:a.length));const h=null==a?void 0:a[s?a.length-1:0];if(n(h)){const t=o(h.getAttribute("name"),s);e(t)}else e(h)},exports.setElementFocus=e,exports.trapFunction=(t,n,o,r,l)=>{const u=r?n.filter((t=>t.matches(r))):n,s=u[0],d=u[u.length-1];if(l)return void l(t,s,d);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(s);if(!r)return;const a=t.shiftKey?s:d,f=i(o?a:c,u,t.shiftKey),b=i(o?a:c,n,t.shiftKey);f&&f!==b&&(e(f),t.preventDefault())};