generated from VLADIMIR/template_frontend
1 line
3.3 KiB
JavaScript
1 line
3.3 KiB
JavaScript
import{A as e,D as t,L as n,X as r,it as i,p as a,z as o}from"./vue-router-bPs2Bq2h.js";import{A as s,M as c,j as l}from"./Card-DAWa6XIj.js";import{o as u}from"./light-WRuBODwl.js";function d(e){return typeof e==`string`?document.querySelector(e):e()||null}function f(e){return e instanceof HTMLElement}function p(e){for(let t=0;t<e.childNodes.length;t++){let n=e.childNodes[t];if(f(n)&&(h(n)||p(n)))return!0}return!1}function m(e){for(let t=e.childNodes.length-1;t>=0;t--){let n=e.childNodes[t];if(f(n)&&(h(n)||m(n)))return!0}return!1}function h(e){if(!g(e))return!1;try{e.focus({preventScroll:!0})}catch{}return document.activeElement===e}function g(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`SELECT`:case`TEXTAREA`:return!0;default:return!1}}var _=[],v=t({name:`FocusTrap`,props:{disabled:Boolean,active:Boolean,autoFocus:{type:Boolean,default:!0},onEsc:Function,initialFocusTo:[String,Function],finalFocusTo:[String,Function],returnFocusOnDeactivated:{type:Boolean,default:!0}},setup(e){let t=u(),a=i(null),f=i(null),h=!1,g=!1,v=typeof document>`u`?null:document.activeElement;function y(){return _[_.length-1]===t}function b(t){var n;t.code===`Escape`&&y()&&((n=e.onEsc)==null||n.call(e,t))}o(()=>{r(()=>e.active,e=>{e?(C(),l(`keydown`,document,b)):(s(`keydown`,document,b),h&&w())},{immediate:!0})}),n(()=>{s(`keydown`,document,b),h&&w()});function x(e){if(!g&&y()){let t=S();if(t===null||t.contains(c(e)))return;T(`first`)}}function S(){let e=a.value;if(e===null)return null;let t=e;for(;t=t.nextSibling,!(t===null||t instanceof Element&&t.tagName===`DIV`););return t}function C(){var n;if(!e.disabled){if(_.push(t),e.autoFocus){let{initialFocusTo:t}=e;t===void 0?T(`first`):(n=d(t))==null||n.focus({preventScroll:!0})}h=!0,document.addEventListener(`focus`,x,!0)}}function w(){var n;if(e.disabled||(document.removeEventListener(`focus`,x,!0),_=_.filter(e=>e!==t),y()))return;let{finalFocusTo:r}=e;r===void 0?e.returnFocusOnDeactivated&&v instanceof HTMLElement&&(g=!0,v.focus({preventScroll:!0}),g=!1):(n=d(r))==null||n.focus({preventScroll:!0})}function T(t){if(y()&&e.active){let e=a.value,n=f.value;if(e!==null&&n!==null){let r=S();if(r==null||r===n){g=!0,e.focus({preventScroll:!0}),g=!1;return}g=!0;let i=t===`first`?p(r):m(r);g=!1,i||(g=!0,e.focus({preventScroll:!0}),g=!1)}}}function E(e){if(g)return;let t=S();t!==null&&(e.relatedTarget!==null&&t.contains(e.relatedTarget)?T(`last`):T(`first`))}function D(e){g||(e.relatedTarget!==null&&e.relatedTarget===a.value?T(`last`):T(`first`))}return{focusableStartRef:a,focusableEndRef:f,focusableStyle:`position: absolute; height: 0; width: 0;`,handleStartFocus:E,handleEndFocus:D}},render(){let{default:t}=this.$slots;if(t===void 0)return null;if(this.disabled)return t();let{active:n,focusableStyle:r}=this;return e(a,null,[e(`div`,{"aria-hidden":`true`,tabindex:n?`0`:`-1`,ref:`focusableStartRef`,style:r,onFocus:this.handleStartFocus}),t(),e(`div`,{"aria-hidden":`true`,style:r,ref:`focusableEndRef`,tabindex:n?`0`:`-1`,onFocus:this.handleEndFocus})])}}),y=new WeakSet;function b(e){y.add(e)}function x(e){return!y.has(e)}function S(e,t=[],n){let r={};return t.forEach(t=>{r[t]=e[t]}),Object.assign(r,n)}export{v as i,x as n,b as r,S as t}; |