|
|
import{f as k}from"./flatten-7z_8MaI9.js";import{w as b}from"./light-Cua7cqmF.js";import{c as B}from"./index-L98eAIry.js";import{a as S,o as C}from"./delegate-CI_DDEu_.js";import{g as M}from"./get-precise-event-target-DhO46Lpt.js";import{d as R,s as T,v as V,t as H,S as O,G as m,F as U}from"./index-bVOhw2NF.js";function j(e,r="default",u=void 0){const a=e[r];if(!a)return b("getFirstSlotVNode",`slot[${r}] is empty`),null;const i=k(a(u));return i.length===1?i[0]:(b("getFirstSlotVNode",`slot[${r}] should have exactly one child`),null)}function y(e){return typeof e=="string"?document.querySelector(e):e()}function p(e){return e instanceof HTMLElement}function w(e){for(let r=0;r<e.childNodes.length;r++){const u=e.childNodes[r];if(p(u)&&(N(u)||w(u)))return!0}return!1}function I(e){for(let r=e.childNodes.length-1;r>=0;r--){const u=e.childNodes[r];if(p(u)&&(N(u)||I(u)))return!0}return!1}function N(e){if(!_(e))return!1;try{e.focus({preventScroll:!0})}catch{}return document.activeElement===e}function _(e){if(e.tabIndex>0||e.tabIndex===0&&e.getAttribute("tabIndex")!==null)return!0;if(e.getAttribute("disabled"))return!1;switch(e.nodeName){case"A":return!!e.href&&e.rel!=="ignore";case"INPUT":return e.type!=="hidden"&&e.type!=="file";case"BUTTON":case"SELECT":case"TEXTAREA":return!0;default:return!1}}let s=[];const z=R({name:"FocusTrap",props:{disabled:Boolean,active:Boolean,autoFocus:{type:Boolean,default:!0},onEsc:Function,initialFocusTo:String,finalFocusTo:String,returnFocusOnDeactivated:{type:Boolean,default:!0}},setup(e){const r=B(),u=T(null),a=T(null);let i=!1,o=!1;const E=typeof document>"u"?null:document.activeElement;function c(){return s[s.length-1]===r}function v(t){var n;t.code==="Escape"&&c()&&((n=e.onEsc)===null||n===void 0||n.call(e,t))}V(()=>{H(()=>e.active,t=>{t?(x(),C("keydown",document,v)):(S("keydown",document,v),i&&g())},{immediate:!0})}),O(()=>{S("keydown",document,v),i&&g()});function F(t){if(!o&&c()){const n=h();if(n===null||n.contains(M(t)))return;l("first")}}function h(){const t=u.value;if(t===null)return null;let n=t;for(;n=n.nextSibling,!(n===null||n instanceof Element&&n.tagName==="DIV"););return n}function x(){var t;if(!e.disabled){if(s.push(r),e.autoFocus){const{initialFocusTo:n}=e;n===void 0?l("first"):(t=y(n))===null||t===void 0||t.focus({preventScroll:!0})}i=!0,document.addEventListener("focus",F,!0)}}function g(){var t;if(e.disabled||(document.removeEventListener("focus",F,!0),s=s.filter(f=>f!==r),c()))return;const{finalFocusTo:n}=e;n!==void 0?(t=y(n))===null||t===void 0||t.focus({preventScroll:!0}):e.returnFocusOnDeactivated&&E instanceof HTMLElement&&(o=!0,E.focus({preventScroll:!0}),o=!1)}function l(t){if(c()&&e.active){const n=u.value,f=a.value;if(n!==null&&f!==null){const d=h();if(d==null||d===f){o=!0,n.focus({preventScroll:!0}),o=!1;return}o=!0;const L=t==="first"?w(d):I(d);o=!1,L||(o=!0,n.focus({preventScroll:!0}),o=!1)}}}function A(t){if(o)return;const n=h();n!==null&&(t.relatedTarget!==null&&n.contains(t.relatedTarget)?l("last"):l("first"))}function D(t){o||(t.relatedTarget!==null&&t.relatedTarget===u.value?l("last"):l("first"))}return{focusableStartRef:u,focusableEndRef:a,focusableStyle:"position: absolute; height: 0; width: 0;",handleStartFocus:A,handleEndFocus:D}},render(){const{default:e}=this.$slots;if(e===void 0)return null;if(this.disabled)return e();const{active:r,focusableStyle:u}=this;return m(U,null,[m("div",{"aria-hidden":"true",tabindex:r?"0":"-1",ref:"focusableStartRef",style:u,onFocus:this.handleStartFocus}),e(),m("div",{"aria-hidden":"true",style:u,ref:"focusableEndRef",tabindex:r?"0":"-1",onFocus:this.handleEndFocus})])}});export{z as F,j as g};
|