2 lines
3.6 KiB
JavaScript
2 lines
3.6 KiB
JavaScript
import{disposables as p}from'./disposables.js';import*as y from'./dom.js';import{match as L}from'./match.js';import{getActiveElement as b,getOwnerDocument as F,getRootNode as f}from'./owner.js';let E=["[contentEditable=true]","[tabindex]","a[href]","area[href]","button:not([disabled])","iframe","input:not([disabled])","select:not([disabled])","details>summary","textarea:not([disabled])"].map(e=>`${e}:not([tabindex='-1'])`).join(","),S=["[data-autofocus]"].map(e=>`${e}:not([tabindex='-1'])`).join(",");var T=(o=>(o[o.First=1]="First",o[o.Previous=2]="Previous",o[o.Next=4]="Next",o[o.Last=8]="Last",o[o.WrapAround=16]="WrapAround",o[o.NoScroll=32]="NoScroll",o[o.AutoFocus=64]="AutoFocus",o))(T||{}),A=(n=>(n[n.Error=0]="Error",n[n.Overflow=1]="Overflow",n[n.Success=2]="Success",n[n.Underflow=3]="Underflow",n))(A||{}),O=(t=>(t[t.Previous=-1]="Previous",t[t.Next=1]="Next",t))(O||{});function x(e=document.body){return e==null?[]:Array.from(e.querySelectorAll(E)).sort((r,t)=>Math.sign((r.tabIndex||Number.MAX_SAFE_INTEGER)-(t.tabIndex||Number.MAX_SAFE_INTEGER)))}function h(e=document.body){return e==null?[]:Array.from(e.querySelectorAll(S)).sort((r,t)=>Math.sign((r.tabIndex||Number.MAX_SAFE_INTEGER)-(t.tabIndex||Number.MAX_SAFE_INTEGER)))}var I=(t=>(t[t.Strict=0]="Strict",t[t.Loose=1]="Loose",t))(I||{});function H(e,r=0){var t;return e===((t=F(e))==null?void 0:t.body)?!1:L(r,{[0](){return e.matches(E)},[1](){let l=e;for(;l!==null;){if(l.matches(E))return!0;l=l.parentElement}return!1}})}function K(e){p().nextFrame(()=>{let r=b(e);r&&y.isHTMLorSVGElement(r)&&!H(r,0)&&w(e)})}var g=(t=>(t[t.Keyboard=0]="Keyboard",t[t.Mouse=1]="Mouse",t))(g||{});typeof window!="undefined"&&typeof document!="undefined"&&(document.addEventListener("keydown",e=>{e.metaKey||e.altKey||e.ctrlKey||(document.documentElement.dataset.headlessuiFocusVisible="")},!0),document.addEventListener("click",e=>{e.detail===1?delete document.documentElement.dataset.headlessuiFocusVisible:e.detail===0&&(document.documentElement.dataset.headlessuiFocusVisible="")},!0));function w(e){e==null||e.focus({preventScroll:!0})}let _=["textarea","input"].join(",");function P(e){var r,t;return(t=(r=e==null?void 0:e.matches)==null?void 0:r.call(e,_))!=null?t:!1}function G(e,r=t=>t){return e.slice().sort((t,l)=>{let n=r(t),a=r(l);if(n===null||a===null)return 0;let u=n.compareDocumentPosition(a);return u&Node.DOCUMENT_POSITION_FOLLOWING?-1:u&Node.DOCUMENT_POSITION_PRECEDING?1:0})}function R(e,r,t=e===null?document.body:f(e)){return v(x(t),r,{relativeTo:e})}function v(e,r,{sorted:t=!0,relativeTo:l=null,skipElements:n=[]}={}){let a=Array.isArray(e)?e.length>0?f(e[0]):document:f(e),u=Array.isArray(e)?t?G(e):e:r&64?h(e):x(e);n.length>0&&u.length>1&&(u=u.filter(i=>!n.some(d=>d!=null&&"current"in d?(d==null?void 0:d.current)===i:d===i))),l=l!=null?l:a==null?void 0:a.activeElement;let o=(()=>{if(r&5)return 1;if(r&10)return-1;throw new Error("Missing Focus.First, Focus.Previous, Focus.Next or Focus.Last")})(),M=(()=>{if(r&1)return 0;if(r&2)return Math.max(0,u.indexOf(l))-1;if(r&4)return Math.max(0,u.indexOf(l))+1;if(r&8)return u.length-1;throw new Error("Missing Focus.First, Focus.Previous, Focus.Next or Focus.Last")})(),N=r&32?{preventScroll:!0}:{},m=0,c=u.length,s;do{if(m>=c||m+c<=0)return 0;let i=M+m;if(r&16)i=(i+c)%c;else{if(i<0)return 3;if(i>=c)return 1}s=u[i],s==null||s.focus(N),m+=o}while(s!==b(s));return r&6&&P(s)&&s.select(),2}export{T as Focus,A as FocusResult,I as FocusableMode,w as focusElement,R as focusFrom,v as focusIn,E as focusableSelector,h as getAutoFocusableElements,x as getFocusableElements,H as isFocusableElement,K as restoreFocusIfNecessary,G as sortByDomNode};
|