(function(u,g){typeof exports=="object"&&typeof module<"u"?module.exports=g(require("vue")):typeof define=="function"&&define.amd?define(["vue"],g):(u=typeof globalThis<"u"?globalThis:u||self,u.VueFocusLock=g(u.Vue))})(this,function(u){"use strict";var g="data-focus-lock",D="data-focus-lock-disabled",W="data-no-focus-lock",G="data-autofocus-inside",R="data-no-autofocus";const ce=Object.freeze(Object.defineProperty({__proto__:null,FOCUS_ALLOW:W,FOCUS_AUTO:G,FOCUS_DISABLED:D,FOCUS_GROUP:g,FOCUS_NO_AUTOFOCUS:R},Symbol.toStringTag,{value:"Module"}));var b=function(e){for(var n=Array(e.length),t=0;t=0}).sort(pe)},ye=["button:enabled","select:enabled","textarea:enabled","input:enabled","a[href]","area[href]","summary","iframe","object","embed","audio[controls]","video[controls]","[tabindex]","[contenteditable]","[autofocus]"],_=ye.join(","),Fe="".concat(_,", [data-focus-guard]"),X=function(e,n){return b((e.shadowRoot||e).children).reduce(function(t,r){return t.concat(r.matches(n?Fe:_)?[r]:[],X(r))},[])},Ie=function(e,n){var t;return e instanceof HTMLIFrameElement&&(!((t=e.contentDocument)===null||t===void 0)&&t.body)?S([e.contentDocument.body],n):[e]},S=function(e,n){return e.reduce(function(t,r){var o,a=X(r,n),i=(o=[]).concat.apply(o,a.map(function(l){return Ie(l,n)}));return t.concat(i,r.parentNode?b(r.parentNode.querySelectorAll(_)).filter(function(l){return l===r}):[])},[])},Ae=function(e){var n=e.querySelectorAll("[".concat(G,"]"));return b(n).map(function(t){return S([t])}).reduce(function(t,r){return t.concat(r)},[])},Z=function(e,n){return b(e).filter(function(t){return j(n,t)}).filter(function(t){return me(t)})},ee=function(e,n){return n===void 0&&(n=new Map),b(e).filter(function(t){return z(n,t)})},ne=function(e,n){return Oe(Z(S(e),n),!1)},xe=function(e,n){return Z(Ae(e),n)},y=function(e,n){return e.shadowRoot?y(e.shadowRoot,n):Object.getPrototypeOf(e).contains!==void 0&&Object.getPrototypeOf(e).contains.call(e,n)?!0:b(e.children).some(function(t){var r;if(t instanceof HTMLIFrameElement){var o=(r=t.contentDocument)===null||r===void 0?void 0:r.body;return o?y(o,n):!1}return y(t,n)})},Ee=function(e){for(var n=new Set,t=e.length,r=0;r0&&n.add(o),(a&Node.DOCUMENT_POSITION_CONTAINS)>0&&n.add(r)}return e.filter(function(i,l){return!n.has(l)})},te=function(e){return e.parentNode?te(e.parentNode):e},re=function(e){var n=E(e);return n.filter(Boolean).reduce(function(t,r){var o=r.getAttribute(g);return t.push.apply(t,o?Ee(b(te(r).querySelectorAll("[".concat(g,'="').concat(o,'"]:not([').concat(D,'="disabled"])')))):[r]),t},[])},Te=function(e){try{return e()}catch{return}},I=function(e){if(e===void 0&&(e=document),!(!e||!e.activeElement)){var n=e.activeElement;return n.shadowRoot?I(n.shadowRoot):n instanceof HTMLIFrameElement&&Te(function(){return n.contentWindow.document})?I(n.contentWindow.document):n}},_e=function(e,n){return e===n},Se=function(e,n){return!!b(e.querySelectorAll("iframe")).some(function(t){return _e(t,n)})},Ne=function(e,n){return n===void 0&&(n=I(H(e).ownerDocument)),!n||n.dataset&&n.dataset.focusGuard?!1:re(e).some(function(t){return y(t,n)||Se(t,n)})},we=function(e){e===void 0&&(e=document);var n=I(e);return n?b(e.querySelectorAll("[".concat(W,"]"))).some(function(t){return y(t,n)}):!1},Ce=function(e,n){return n.filter($).filter(function(t){return t.name===e.name}).filter(function(t){return t.checked})[0]||e},N=function(e,n){return $(e)&&e.name?Ce(e,n):e},ke=function(e){var n=new Set;return e.forEach(function(t){return n.add(N(t,e))}),e.filter(function(t){return n.has(t)})},oe=function(e){return e[0]&&e.length>1?N(e[0],e):e[0]},ue=function(e,n){return e.indexOf(N(n,e))},w="NEW_FOCUS",Be=function(e,n,t,r,o){var a=e.length,i=e[0],l=e[a-1],h=J(r);if(!(r&&e.indexOf(r)>=0)){var c=r!==void 0?t.indexOf(r):-1,A=o?t.indexOf(o):c,v=o?e.indexOf(o):-1;if(c===-1)return v!==-1?v:w;if(v===-1)return w;var m=c-A,p=t.indexOf(i),d=t.indexOf(l),s=ke(t),x=r!==void 0?s.indexOf(r):-1,P=x-(o?s.indexOf(o):c);if(!m&&v>=0||n.length===0)return v;var T=ue(e,n[0]),O=ue(e,n[n.length-1]);if(c<=p&&h&&Math.abs(m)>1)return O;if(c>=d&&h&&Math.abs(m)>1)return T;if(m&&Math.abs(P)>1)return v;if(c<=p)return O;if(c>d)return T;if(m)return Math.abs(m)>1?v:(a+v+m)%a}},Me=function(e){return function(n){var t,r=(t=K(n))===null||t===void 0?void 0:t.autofocus;return n.autofocus||r!==void 0&&r!=="false"||e.indexOf(n)>=0}},Pe=function(e,n,t){var r=e.map(function(a){var i=a.node;return i}),o=ee(r.filter(Me(t)));return o&&o.length?oe(o):oe(ee(n))},C=function(e,n){return n===void 0&&(n=[]),n.push(e),e.parentNode&&C(e.parentNode.host||e.parentNode,n),n},k=function(e,n){for(var t=C(e),r=C(n),o=0;o=0)return a}return!1},Ue=function(e,n,t){var r=E(e),o=E(n),a=r[0],i=!1;return o.filter(Boolean).forEach(function(l){i=k(i||l,l)||i,t.filter(Boolean).forEach(function(h){var c=k(a,h);c&&(!i||y(c,i)?i=c:i=k(c,i))})}),i},Le=function(e,n){return e.reduce(function(t,r){return t.concat(xe(r,n))},[])},De=function(e,n){var t=new Map;return n.forEach(function(r){return t.set(r.node,r)}),e.map(function(r){return t.get(r)}).filter(he)},We=function(e,n){var t=I(E(e).length>0?document:H(e).ownerDocument),r=re(e).filter(Q),o=Ue(t||e,e,r),a=new Map,i=ne(r,a),l=i.filter(function(d){var s=d.node;return Q(s)});if(l[0]){var h=ne([o],a).map(function(d){var s=d.node;return s}),c=De(h,l),A=c.map(function(d){var s=d.node;return s}),v=c.filter(function(d){var s=d.tabIndex;return s>=0}).map(function(d){var s=d.node;return s}),m=Be(A,v,h,t,n);if(m===w){var p=Pe(i,v,Le(r,a));if(p)return{node:p};console.warn("focus-lock: cannot find any node to move focus into");return}return m===void 0?m:c[m]}},Ge=function(e,n){e&&("focus"in e&&e.focus(n),"contentWindow"in e&&e.contentWindow&&e.contentWindow.focus())},B=0,M=!1,Re=function(e,n,t){t===void 0&&(t={});var r=We(e,n);if(!M&&r){if(B>2){console.error("FocusLock: focus-fighting detected. Only one focus management system could be active. See https://github.com/theKashey/focus-lock/#focus-fighting"),M=!0,setTimeout(function(){M=!1},1);return}B++,Ge(r.node,t.focusOptions),B--}},He=ce;const Ve=["tabIndex"],qe=["tabIndex"];return u.defineComponent({__name:"FocusLock",props:{returnFocus:{type:Boolean},disabled:{type:Boolean},noFocusGuards:{type:[Boolean,String]},group:{}},setup(e){let n=[],t;const r=e,{returnFocus:o,disabled:a,noFocusGuards:i,group:l}=u.toRefs(r),h=u.ref(null),c=u.ref({disabled:!0,onActivation:()=>{}}),A=u.computed(()=>({[He.FOCUS_GROUP]:l.value})),v=u.computed(()=>i.value!==!0),m=u.computed(()=>v.value&&i.value!=="tail");u.watch(a,()=>{c.value.disabled=a.value,U()}),u.onMounted(()=>{const f=u.getCurrentInstance();f&&(c.value={instance:f.proxy,observed:h.value.querySelector("[data-lock]"),disabled:a.value,onActivation:()=>{t=t||document&&document.activeElement}},n.length||Ke(),n.push(c.value),U())}),u.onUnmounted(()=>{const f=u.getCurrentInstance();f&&(n=n.filter(({instance:F})=>F!==f.proxy),n.length||Ye(),o.value&&t&&t.focus&&t.focus(),U())});function p(f){setTimeout(f,0)}let d=null,s=null,x=!1;const P=()=>document&&document.activeElement===document.body,T=()=>P()||we(),O=()=>{if(d){const{observed:f,onActivation:F}=d;(x||!T()||!s)&&(f&&!Ne(f)&&(F(),Re(f,s)),x=!1,s=document&&document.activeElement)}},je=f=>f.filter(({disabled:F})=>!F).slice(-1)[0],ze=f=>{d!==f&&(d=null),d=f,f&&(O(),p(O))},U=()=>{ze(je(n))},ae=()=>{O()},L=()=>{p(O)},ie=()=>{x=!0,s=null},Ke=()=>{document.addEventListener("focusin",ae,!0),document.addEventListener("focusout",L),window.addEventListener("blur",ie)},Ye=()=>{document.removeEventListener("focusin",ae,!0),document.removeEventListener("focusout",L),window.removeEventListener("blur",ie)};return(f,F)=>(u.openBlock(),u.createElementBlock("div",{ref_key:"rootEl",ref:h},[v.value?(u.openBlock(),u.createElementBlock("div",{key:0,tabIndex:u.unref(a)?-1:0,"aria-hidden":"true"},null,8,Ve)):u.createCommentVNode("",!0),u.createElementVNode("div",u.mergeProps({onFocusout:L},A.value,{"data-lock":""}),[u.renderSlot(f.$slots,"default")],16),m.value?(u.openBlock(),u.createElementBlock("div",{key:1,tabIndex:u.unref(a)?-1:0,"aria-hidden":"true"},null,8,qe)):u.createCommentVNode("",!0)],512))}})});