@alpinejs/focus
Advanced tools
Comparing version 3.9.0 to 3.9.1
@@ -738,3 +738,3 @@ (() => { | ||
}); | ||
Alpine.directive("trap", Alpine.skipDuringClone((el, {expression, modifiers}, {effect, evaluateLater}) => { | ||
Alpine.directive("trap", Alpine.skipDuringClone((el, {expression, modifiers}, {effect, evaluateLater, cleanup}) => { | ||
let evaluator = evaluateLater(expression); | ||
@@ -751,2 +751,13 @@ let oldValue = false; | ||
}; | ||
const releaseFocus = () => { | ||
undoInert(); | ||
undoInert = () => { | ||
}; | ||
undoDisableScrolling(); | ||
undoDisableScrolling = () => { | ||
}; | ||
trap.deactivate({ | ||
returnFocus: !modifiers.includes("noreturn") | ||
}); | ||
}; | ||
effect(() => evaluator((value) => { | ||
@@ -765,14 +776,7 @@ if (oldValue === value) | ||
if (!value && oldValue) { | ||
undoInert(); | ||
undoInert = () => { | ||
}; | ||
undoDisableScrolling(); | ||
undoDisableScrolling = () => { | ||
}; | ||
trap.deactivate({ | ||
returnFocus: !modifiers.includes("noreturn") | ||
}); | ||
releaseFocus(); | ||
} | ||
oldValue = !!value; | ||
})); | ||
cleanup(releaseFocus); | ||
}, (el, {expression, modifiers}, {evaluate}) => { | ||
@@ -779,0 +783,0 @@ if (modifiers.includes("inert") && evaluate(expression)) |
@@ -1,2 +0,2 @@ | ||
(()=>{var j=["input","select","textarea","a[href]","button","[tabindex]","audio[controls]","video[controls]",'[contenteditable]:not([contenteditable="false"])',"details>summary:first-of-type","details"],_=j.join(","),D=typeof Element=="undefined"?function(){}:Element.prototype.matches||Element.prototype.msMatchesSelector||Element.prototype.webkitMatchesSelector,G=function(e,t,n){var u=Array.prototype.slice.apply(e.querySelectorAll(_));return t&&D.call(e,_)&&u.unshift(e),u=u.filter(n),u},Z=function(e){return e.contentEditable==="true"},M=function(e){var t=parseInt(e.getAttribute("tabindex"),10);return isNaN(t)?Z(e)||(e.nodeName==="AUDIO"||e.nodeName==="VIDEO"||e.nodeName==="DETAILS")&&e.getAttribute("tabindex")===null?0:e.tabIndex:t},$=function(e,t){return e.tabIndex===t.tabIndex?e.documentOrder-t.documentOrder:e.tabIndex-t.tabIndex},k=function(e){return e.tagName==="INPUT"},ee=function(e){return k(e)&&e.type==="hidden"},te=function(e){var t=e.tagName==="DETAILS"&&Array.prototype.slice.apply(e.children).some(function(n){return n.tagName==="SUMMARY"});return t},re=function(e,t){for(var n=0;n<e.length;n++)if(e[n].checked&&e[n].form===t)return e[n]},ae=function(e){if(!e.name)return!0;var t=e.form||e.ownerDocument,n=function(l){return t.querySelectorAll('input[type="radio"][name="'+l+'"]')},u;if(typeof window!="undefined"&&typeof window.CSS!="undefined"&&typeof window.CSS.escape=="function")u=n(window.CSS.escape(e.name));else try{u=n(e.name)}catch(s){return console.error("Looks like you have a radio button with a name attribute containing invalid CSS selector characters and need the CSS.escape polyfill: %s",s.message),!1}var r=re(u,e.form);return!r||r===e},ne=function(e){return k(e)&&e.type==="radio"},ie=function(e){return ne(e)&&!ae(e)},ue=function(e,t){if(getComputedStyle(e).visibility==="hidden")return!0;var n=D.call(e,"details>summary:first-of-type"),u=n?e.parentElement:e;if(D.call(u,"details:not([open]) *"))return!0;if(!t||t==="full")for(;e;){if(getComputedStyle(e).display==="none")return!0;e=e.parentElement}else if(t==="non-zero-area"){var r=e.getBoundingClientRect(),s=r.width,l=r.height;return s===0&&l===0}return!1},oe=function(e){if(k(e)||e.tagName==="SELECT"||e.tagName==="TEXTAREA"||e.tagName==="BUTTON")for(var t=e.parentElement;t;){if(t.tagName==="FIELDSET"&&t.disabled){for(var n=0;n<t.children.length;n++){var u=t.children.item(n);if(u.tagName==="LEGEND")return!u.contains(e)}return!0}t=t.parentElement}return!1},C=function(e,t){return!(t.disabled||ee(t)||ue(t,e.displayCheck)||te(t)||oe(t))},se=function(e,t){return!(!C(e,t)||ie(t)||M(t)<0)},W=function(e,t){t=t||{};var n=[],u=[],r=G(e,t.includeContainer,se.bind(null,t));r.forEach(function(l,h){var d=M(l);d===0?n.push(l):u.push({documentOrder:h,tabIndex:d,node:l})});var s=u.sort($).map(function(l){return l.node}).concat(n);return s},q=function(e,t){t=t||{};var n=G(e,t.includeContainer,C.bind(null,t));return n};var ce=j.concat("iframe").join(","),A=function(e,t){if(t=t||{},!e)throw new Error("No node provided");return D.call(e,ce)===!1?!1:C(t,e)};function B(i,e){var t=Object.keys(i);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(i);e&&(n=n.filter(function(u){return Object.getOwnPropertyDescriptor(i,u).enumerable})),t.push.apply(t,n)}return t}function fe(i){for(var e=1;e<arguments.length;e++){var t=arguments[e]!=null?arguments[e]:{};e%2?B(Object(t),!0).forEach(function(n){le(i,n,t[n])}):Object.getOwnPropertyDescriptors?Object.defineProperties(i,Object.getOwnPropertyDescriptors(t)):B(Object(t)).forEach(function(n){Object.defineProperty(i,n,Object.getOwnPropertyDescriptor(t,n))})}return i}function le(i,e,t){return e in i?Object.defineProperty(i,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):i[e]=t,i}var H=function(){var i=[];return{activateTrap:function(t){if(i.length>0){var n=i[i.length-1];n!==t&&n.pause()}var u=i.indexOf(t);u===-1||i.splice(u,1),i.push(t)},deactivateTrap:function(t){var n=i.indexOf(t);n!==-1&&i.splice(n,1),i.length>0&&i[i.length-1].unpause()}}}(),de=function(e){return e.tagName&&e.tagName.toLowerCase()==="input"&&typeof e.select=="function"},be=function(e){return e.key==="Escape"||e.key==="Esc"||e.keyCode===27},ve=function(e){return e.key==="Tab"||e.keyCode===9},U=function(e){return setTimeout(e,0)},O=function(e,t){var n=-1;return e.every(function(u,r){return t(u)?(n=r,!1):!0}),n},N=function(e){for(var t=arguments.length,n=new Array(t>1?t-1:0),u=1;u<t;u++)n[u-1]=arguments[u];return typeof e=="function"?e.apply(void 0,n):e},K=function(e,t){var n=document,u=fe({returnFocusOnDeactivate:!0,escapeDeactivates:!0,delayInitialFocus:!0},t),r={containers:[],tabbableGroups:[],nodeFocusedBeforeActivation:null,mostRecentlyFocusedNode:null,active:!1,paused:!1,delayInitialFocusTimer:void 0},s,l=function(a,o,c){return a&&a[o]!==void 0?a[o]:u[c||o]},h=function(a){return r.containers.some(function(o){return o.contains(a)})},d=function(a){var o=u[a];if(!o)return null;var c=o;if(typeof o=="string"&&(c=n.querySelector(o),!c))throw new Error("`".concat(a,"` refers to no known node"));if(typeof o=="function"&&(c=o(),!c))throw new Error("`".concat(a,"` did not return a node"));return c},g=function(){var a;if(l({},"initialFocus")===!1)return!1;if(d("initialFocus")!==null)a=d("initialFocus");else if(h(n.activeElement))a=n.activeElement;else{var o=r.tabbableGroups[0],c=o&&o.firstTabbableNode;a=c||d("fallbackFocus")}if(!a)throw new Error("Your focus-trap needs to have at least one focusable element");return a},v=function(){if(r.tabbableGroups=r.containers.map(function(a){var o=W(a);if(o.length>0)return{container:a,firstTabbableNode:o[0],lastTabbableNode:o[o.length-1]}}).filter(function(a){return!!a}),r.tabbableGroups.length<=0&&!d("fallbackFocus"))throw new Error("Your focus-trap must have at least one container with at least one tabbable node in it at all times")},p=function f(a){if(a!==!1&&a!==n.activeElement){if(!a||!a.focus){f(g());return}a.focus({preventScroll:!!u.preventScroll}),r.mostRecentlyFocusedNode=a,de(a)&&a.select()}},y=function(a){var o=d("setReturnFocus");return o||a},S=function(a){if(!h(a.target)){if(N(u.clickOutsideDeactivates,a)){s.deactivate({returnFocus:u.returnFocusOnDeactivate&&!A(a.target)});return}N(u.allowOutsideClick,a)||a.preventDefault()}},L=function(a){var o=h(a.target);o||a.target instanceof Document?o&&(r.mostRecentlyFocusedNode=a.target):(a.stopImmediatePropagation(),p(r.mostRecentlyFocusedNode||g()))},Q=function(a){v();var o=null;if(r.tabbableGroups.length>0){var c=O(r.tabbableGroups,function(F){var T=F.container;return T.contains(a.target)});if(c<0)a.shiftKey?o=r.tabbableGroups[r.tabbableGroups.length-1].lastTabbableNode:o=r.tabbableGroups[0].firstTabbableNode;else if(a.shiftKey){var b=O(r.tabbableGroups,function(F){var T=F.firstTabbableNode;return a.target===T});if(b<0&&r.tabbableGroups[c].container===a.target&&(b=c),b>=0){var m=b===0?r.tabbableGroups.length-1:b-1,w=r.tabbableGroups[m];o=w.lastTabbableNode}}else{var E=O(r.tabbableGroups,function(F){var T=F.lastTabbableNode;return a.target===T});if(E<0&&r.tabbableGroups[c].container===a.target&&(E=c),E>=0){var X=E===r.tabbableGroups.length-1?0:E+1,J=r.tabbableGroups[X];o=J.firstTabbableNode}}}else o=d("fallbackFocus");o&&(a.preventDefault(),p(o))},R=function(a){if(be(a)&&N(u.escapeDeactivates)!==!1){a.preventDefault(),s.deactivate();return}if(ve(a)){Q(a);return}},x=function(a){N(u.clickOutsideDeactivates,a)||h(a.target)||N(u.allowOutsideClick,a)||(a.preventDefault(),a.stopImmediatePropagation())},I=function(){if(!!r.active)return H.activateTrap(s),r.delayInitialFocusTimer=u.delayInitialFocus?U(function(){p(g())}):p(g()),n.addEventListener("focusin",L,!0),n.addEventListener("mousedown",S,{capture:!0,passive:!1}),n.addEventListener("touchstart",S,{capture:!0,passive:!1}),n.addEventListener("click",x,{capture:!0,passive:!1}),n.addEventListener("keydown",R,{capture:!0,passive:!1}),s},P=function(){if(!!r.active)return n.removeEventListener("focusin",L,!0),n.removeEventListener("mousedown",S,!0),n.removeEventListener("touchstart",S,!0),n.removeEventListener("click",x,!0),n.removeEventListener("keydown",R,!0),s};return s={activate:function(a){if(r.active)return this;var o=l(a,"onActivate"),c=l(a,"onPostActivate"),b=l(a,"checkCanFocusTrap");b||v(),r.active=!0,r.paused=!1,r.nodeFocusedBeforeActivation=n.activeElement,o&&o();var m=function(){b&&v(),I(),c&&c()};return b?(b(r.containers.concat()).then(m,m),this):(m(),this)},deactivate:function(a){if(!r.active)return this;clearTimeout(r.delayInitialFocusTimer),r.delayInitialFocusTimer=void 0,P(),r.active=!1,r.paused=!1,H.deactivateTrap(s);var o=l(a,"onDeactivate"),c=l(a,"onPostDeactivate"),b=l(a,"checkCanReturnFocus");o&&o();var m=l(a,"returnFocus","returnFocusOnDeactivate"),w=function(){U(function(){m&&p(y(r.nodeFocusedBeforeActivation)),c&&c()})};return m&&b?(b(y(r.nodeFocusedBeforeActivation)).then(w,w),this):(w(),this)},pause:function(){return r.paused||!r.active?this:(r.paused=!0,P(),this)},unpause:function(){return!r.paused||!r.active?this:(r.paused=!1,v(),I(),this)},updateContainerElements:function(a){var o=[].concat(a).filter(Boolean);return r.containers=o.map(function(c){return typeof c=="string"?n.querySelector(c):c}),r.active&&v(),this}},s.updateContainerElements(e),s};function Y(i){let e,t;window.addEventListener("focusin",()=>{e=t,t=document.activeElement}),i.magic("focus",n=>{let u=n;return{__noscroll:!1,__wrapAround:!1,within(r){return u=r,this},withoutScrolling(){return this.__noscroll=!0,this},noscroll(){return this.__noscroll=!0,this},withWrapAround(){return this.__wrapAround=!0,this},wrap(){return this.withWrapAround()},focusable(r){return A(r)},previouslyFocused(){return e},lastFocused(){return e},focused(){return t},focusables(){return Array.isArray(u)?u:q(u,{displayCheck:"none"})},all(){return this.focusables()},isFirst(r){let s=this.all();return s[0]&&s[0].isSameNode(r)},isLast(r){let s=this.all();return s.length&&s.slice(-1)[0].isSameNode(r)},getFirst(){return this.all()[0]},getLast(){return this.all().slice(-1)[0]},getNext(){let r=this.all(),s=document.activeElement;if(r.indexOf(s)!==-1)return this.__wrapAround&&r.indexOf(s)===r.length-1?r[0]:r[r.indexOf(s)+1]},getPrevious(){let r=this.all(),s=document.activeElement;if(r.indexOf(s)!==-1)return this.__wrapAround&&r.indexOf(s)===0?r.slice(-1)[0]:r[r.indexOf(s)-1]},first(){this.focus(this.getFirst())},last(){this.focus(this.getLast())},next(){this.focus(this.getNext())},previous(){this.focus(this.getPrevious())},prev(){return this.previous()},focus(r){!r||setTimeout(()=>{r.hasAttribute("tabindex")||r.setAttribute("tabindex","0"),r.focus({preventScroll:this._noscroll})})}}}),i.directive("trap",i.skipDuringClone((n,{expression:u,modifiers:r},{effect:s,evaluateLater:l})=>{let h=l(u),d=!1,g=K(n,{escapeDeactivates:!1,allowOutsideClick:!0,fallbackFocus:()=>n}),v=()=>{},p=()=>{};s(()=>h(y=>{d!==y&&(y&&!d&&setTimeout(()=>{r.includes("inert")&&(v=V(n)),r.includes("noscroll")&&(p=pe()),g.activate()}),!y&&d&&(v(),v=()=>{},p(),p=()=>{},g.deactivate({returnFocus:!r.includes("noreturn")})),d=!!y)}))},(n,{expression:u,modifiers:r},{evaluate:s})=>{r.includes("inert")&&s(u)&&V(n)}))}function V(i){let e=[];return z(i,t=>{let n=t.hasAttribute("aria-hidden");t.setAttribute("aria-hidden","true"),e.push(()=>n||t.removeAttribute("aria-hidden"))}),()=>{for(;e.length;)e.pop()()}}function z(i,e){i.isSameNode(document.body)||!i.parentNode||Array.from(i.parentNode.children).forEach(t=>{t.isSameNode(i)||e(t),z(i.parentNode,e)})}function pe(){let i=document.documentElement.style.overflow,e=document.documentElement.style.paddingRight,t=window.innerWidth-document.documentElement.clientWidth;return document.documentElement.style.overflow="hidden",document.documentElement.style.paddingRight=`${t}px`,()=>{document.documentElement.style.overflow=i,document.documentElement.style.paddingRight=e}}document.addEventListener("alpine:init",()=>{window.Alpine.plugin(Y)});})(); | ||
(()=>{var j=["input","select","textarea","a[href]","button","[tabindex]","audio[controls]","video[controls]",'[contenteditable]:not([contenteditable="false"])',"details>summary:first-of-type","details"],_=j.join(","),A=typeof Element=="undefined"?function(){}:Element.prototype.matches||Element.prototype.msMatchesSelector||Element.prototype.webkitMatchesSelector,G=function(e,t,n){var u=Array.prototype.slice.apply(e.querySelectorAll(_));return t&&A.call(e,_)&&u.unshift(e),u=u.filter(n),u},Z=function(e){return e.contentEditable==="true"},M=function(e){var t=parseInt(e.getAttribute("tabindex"),10);return isNaN(t)?Z(e)||(e.nodeName==="AUDIO"||e.nodeName==="VIDEO"||e.nodeName==="DETAILS")&&e.getAttribute("tabindex")===null?0:e.tabIndex:t},$=function(e,t){return e.tabIndex===t.tabIndex?e.documentOrder-t.documentOrder:e.tabIndex-t.tabIndex},C=function(e){return e.tagName==="INPUT"},ee=function(e){return C(e)&&e.type==="hidden"},te=function(e){var t=e.tagName==="DETAILS"&&Array.prototype.slice.apply(e.children).some(function(n){return n.tagName==="SUMMARY"});return t},re=function(e,t){for(var n=0;n<e.length;n++)if(e[n].checked&&e[n].form===t)return e[n]},ae=function(e){if(!e.name)return!0;var t=e.form||e.ownerDocument,n=function(l){return t.querySelectorAll('input[type="radio"][name="'+l+'"]')},u;if(typeof window!="undefined"&&typeof window.CSS!="undefined"&&typeof window.CSS.escape=="function")u=n(window.CSS.escape(e.name));else try{u=n(e.name)}catch(s){return console.error("Looks like you have a radio button with a name attribute containing invalid CSS selector characters and need the CSS.escape polyfill: %s",s.message),!1}var r=re(u,e.form);return!r||r===e},ne=function(e){return C(e)&&e.type==="radio"},ie=function(e){return ne(e)&&!ae(e)},ue=function(e,t){if(getComputedStyle(e).visibility==="hidden")return!0;var n=A.call(e,"details>summary:first-of-type"),u=n?e.parentElement:e;if(A.call(u,"details:not([open]) *"))return!0;if(!t||t==="full")for(;e;){if(getComputedStyle(e).display==="none")return!0;e=e.parentElement}else if(t==="non-zero-area"){var r=e.getBoundingClientRect(),s=r.width,l=r.height;return s===0&&l===0}return!1},oe=function(e){if(C(e)||e.tagName==="SELECT"||e.tagName==="TEXTAREA"||e.tagName==="BUTTON")for(var t=e.parentElement;t;){if(t.tagName==="FIELDSET"&&t.disabled){for(var n=0;n<t.children.length;n++){var u=t.children.item(n);if(u.tagName==="LEGEND")return!u.contains(e)}return!0}t=t.parentElement}return!1},O=function(e,t){return!(t.disabled||ee(t)||ue(t,e.displayCheck)||te(t)||oe(t))},se=function(e,t){return!(!O(e,t)||ie(t)||M(t)<0)},W=function(e,t){t=t||{};var n=[],u=[],r=G(e,t.includeContainer,se.bind(null,t));r.forEach(function(l,h){var b=M(l);b===0?n.push(l):u.push({documentOrder:h,tabIndex:b,node:l})});var s=u.sort($).map(function(l){return l.node}).concat(n);return s},q=function(e,t){t=t||{};var n=G(e,t.includeContainer,O.bind(null,t));return n};var ce=j.concat("iframe").join(","),k=function(e,t){if(t=t||{},!e)throw new Error("No node provided");return A.call(e,ce)===!1?!1:O(t,e)};function B(i,e){var t=Object.keys(i);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(i);e&&(n=n.filter(function(u){return Object.getOwnPropertyDescriptor(i,u).enumerable})),t.push.apply(t,n)}return t}function fe(i){for(var e=1;e<arguments.length;e++){var t=arguments[e]!=null?arguments[e]:{};e%2?B(Object(t),!0).forEach(function(n){le(i,n,t[n])}):Object.getOwnPropertyDescriptors?Object.defineProperties(i,Object.getOwnPropertyDescriptors(t)):B(Object(t)).forEach(function(n){Object.defineProperty(i,n,Object.getOwnPropertyDescriptor(t,n))})}return i}function le(i,e,t){return e in i?Object.defineProperty(i,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):i[e]=t,i}var H=function(){var i=[];return{activateTrap:function(t){if(i.length>0){var n=i[i.length-1];n!==t&&n.pause()}var u=i.indexOf(t);u===-1||i.splice(u,1),i.push(t)},deactivateTrap:function(t){var n=i.indexOf(t);n!==-1&&i.splice(n,1),i.length>0&&i[i.length-1].unpause()}}}(),de=function(e){return e.tagName&&e.tagName.toLowerCase()==="input"&&typeof e.select=="function"},be=function(e){return e.key==="Escape"||e.key==="Esc"||e.keyCode===27},ve=function(e){return e.key==="Tab"||e.keyCode===9},U=function(e){return setTimeout(e,0)},L=function(e,t){var n=-1;return e.every(function(u,r){return t(u)?(n=r,!1):!0}),n},D=function(e){for(var t=arguments.length,n=new Array(t>1?t-1:0),u=1;u<t;u++)n[u-1]=arguments[u];return typeof e=="function"?e.apply(void 0,n):e},K=function(e,t){var n=document,u=fe({returnFocusOnDeactivate:!0,escapeDeactivates:!0,delayInitialFocus:!0},t),r={containers:[],tabbableGroups:[],nodeFocusedBeforeActivation:null,mostRecentlyFocusedNode:null,active:!1,paused:!1,delayInitialFocusTimer:void 0},s,l=function(a,o,c){return a&&a[o]!==void 0?a[o]:u[c||o]},h=function(a){return r.containers.some(function(o){return o.contains(a)})},b=function(a){var o=u[a];if(!o)return null;var c=o;if(typeof o=="string"&&(c=n.querySelector(o),!c))throw new Error("`".concat(a,"` refers to no known node"));if(typeof o=="function"&&(c=o(),!c))throw new Error("`".concat(a,"` did not return a node"));return c},v=function(){var a;if(l({},"initialFocus")===!1)return!1;if(b("initialFocus")!==null)a=b("initialFocus");else if(h(n.activeElement))a=n.activeElement;else{var o=r.tabbableGroups[0],c=o&&o.firstTabbableNode;a=c||b("fallbackFocus")}if(!a)throw new Error("Your focus-trap needs to have at least one focusable element");return a},m=function(){if(r.tabbableGroups=r.containers.map(function(a){var o=W(a);if(o.length>0)return{container:a,firstTabbableNode:o[0],lastTabbableNode:o[o.length-1]}}).filter(function(a){return!!a}),r.tabbableGroups.length<=0&&!b("fallbackFocus"))throw new Error("Your focus-trap must have at least one container with at least one tabbable node in it at all times")},p=function f(a){if(a!==!1&&a!==n.activeElement){if(!a||!a.focus){f(v());return}a.focus({preventScroll:!!u.preventScroll}),r.mostRecentlyFocusedNode=a,de(a)&&a.select()}},E=function(a){var o=b("setReturnFocus");return o||a},y=function(a){if(!h(a.target)){if(D(u.clickOutsideDeactivates,a)){s.deactivate({returnFocus:u.returnFocusOnDeactivate&&!k(a.target)});return}D(u.allowOutsideClick,a)||a.preventDefault()}},w=function(a){var o=h(a.target);o||a.target instanceof Document?o&&(r.mostRecentlyFocusedNode=a.target):(a.stopImmediatePropagation(),p(r.mostRecentlyFocusedNode||v()))},Q=function(a){m();var o=null;if(r.tabbableGroups.length>0){var c=L(r.tabbableGroups,function(N){var S=N.container;return S.contains(a.target)});if(c<0)a.shiftKey?o=r.tabbableGroups[r.tabbableGroups.length-1].lastTabbableNode:o=r.tabbableGroups[0].firstTabbableNode;else if(a.shiftKey){var d=L(r.tabbableGroups,function(N){var S=N.firstTabbableNode;return a.target===S});if(d<0&&r.tabbableGroups[c].container===a.target&&(d=c),d>=0){var g=d===0?r.tabbableGroups.length-1:d-1,F=r.tabbableGroups[g];o=F.lastTabbableNode}}else{var T=L(r.tabbableGroups,function(N){var S=N.lastTabbableNode;return a.target===S});if(T<0&&r.tabbableGroups[c].container===a.target&&(T=c),T>=0){var X=T===r.tabbableGroups.length-1?0:T+1,J=r.tabbableGroups[X];o=J.firstTabbableNode}}}else o=b("fallbackFocus");o&&(a.preventDefault(),p(o))},R=function(a){if(be(a)&&D(u.escapeDeactivates)!==!1){a.preventDefault(),s.deactivate();return}if(ve(a)){Q(a);return}},x=function(a){D(u.clickOutsideDeactivates,a)||h(a.target)||D(u.allowOutsideClick,a)||(a.preventDefault(),a.stopImmediatePropagation())},I=function(){if(!!r.active)return H.activateTrap(s),r.delayInitialFocusTimer=u.delayInitialFocus?U(function(){p(v())}):p(v()),n.addEventListener("focusin",w,!0),n.addEventListener("mousedown",y,{capture:!0,passive:!1}),n.addEventListener("touchstart",y,{capture:!0,passive:!1}),n.addEventListener("click",x,{capture:!0,passive:!1}),n.addEventListener("keydown",R,{capture:!0,passive:!1}),s},P=function(){if(!!r.active)return n.removeEventListener("focusin",w,!0),n.removeEventListener("mousedown",y,!0),n.removeEventListener("touchstart",y,!0),n.removeEventListener("click",x,!0),n.removeEventListener("keydown",R,!0),s};return s={activate:function(a){if(r.active)return this;var o=l(a,"onActivate"),c=l(a,"onPostActivate"),d=l(a,"checkCanFocusTrap");d||m(),r.active=!0,r.paused=!1,r.nodeFocusedBeforeActivation=n.activeElement,o&&o();var g=function(){d&&m(),I(),c&&c()};return d?(d(r.containers.concat()).then(g,g),this):(g(),this)},deactivate:function(a){if(!r.active)return this;clearTimeout(r.delayInitialFocusTimer),r.delayInitialFocusTimer=void 0,P(),r.active=!1,r.paused=!1,H.deactivateTrap(s);var o=l(a,"onDeactivate"),c=l(a,"onPostDeactivate"),d=l(a,"checkCanReturnFocus");o&&o();var g=l(a,"returnFocus","returnFocusOnDeactivate"),F=function(){U(function(){g&&p(E(r.nodeFocusedBeforeActivation)),c&&c()})};return g&&d?(d(E(r.nodeFocusedBeforeActivation)).then(F,F),this):(F(),this)},pause:function(){return r.paused||!r.active?this:(r.paused=!0,P(),this)},unpause:function(){return!r.paused||!r.active?this:(r.paused=!1,m(),I(),this)},updateContainerElements:function(a){var o=[].concat(a).filter(Boolean);return r.containers=o.map(function(c){return typeof c=="string"?n.querySelector(c):c}),r.active&&m(),this}},s.updateContainerElements(e),s};function Y(i){let e,t;window.addEventListener("focusin",()=>{e=t,t=document.activeElement}),i.magic("focus",n=>{let u=n;return{__noscroll:!1,__wrapAround:!1,within(r){return u=r,this},withoutScrolling(){return this.__noscroll=!0,this},noscroll(){return this.__noscroll=!0,this},withWrapAround(){return this.__wrapAround=!0,this},wrap(){return this.withWrapAround()},focusable(r){return k(r)},previouslyFocused(){return e},lastFocused(){return e},focused(){return t},focusables(){return Array.isArray(u)?u:q(u,{displayCheck:"none"})},all(){return this.focusables()},isFirst(r){let s=this.all();return s[0]&&s[0].isSameNode(r)},isLast(r){let s=this.all();return s.length&&s.slice(-1)[0].isSameNode(r)},getFirst(){return this.all()[0]},getLast(){return this.all().slice(-1)[0]},getNext(){let r=this.all(),s=document.activeElement;if(r.indexOf(s)!==-1)return this.__wrapAround&&r.indexOf(s)===r.length-1?r[0]:r[r.indexOf(s)+1]},getPrevious(){let r=this.all(),s=document.activeElement;if(r.indexOf(s)!==-1)return this.__wrapAround&&r.indexOf(s)===0?r.slice(-1)[0]:r[r.indexOf(s)-1]},first(){this.focus(this.getFirst())},last(){this.focus(this.getLast())},next(){this.focus(this.getNext())},previous(){this.focus(this.getPrevious())},prev(){return this.previous()},focus(r){!r||setTimeout(()=>{r.hasAttribute("tabindex")||r.setAttribute("tabindex","0"),r.focus({preventScroll:this._noscroll})})}}}),i.directive("trap",i.skipDuringClone((n,{expression:u,modifiers:r},{effect:s,evaluateLater:l,cleanup:h})=>{let b=l(u),v=!1,m=K(n,{escapeDeactivates:!1,allowOutsideClick:!0,fallbackFocus:()=>n}),p=()=>{},E=()=>{},y=()=>{p(),p=()=>{},E(),E=()=>{},m.deactivate({returnFocus:!r.includes("noreturn")})};s(()=>b(w=>{v!==w&&(w&&!v&&setTimeout(()=>{r.includes("inert")&&(p=V(n)),r.includes("noscroll")&&(E=pe()),m.activate()}),!w&&v&&y(),v=!!w)})),h(y)},(n,{expression:u,modifiers:r},{evaluate:s})=>{r.includes("inert")&&s(u)&&V(n)}))}function V(i){let e=[];return z(i,t=>{let n=t.hasAttribute("aria-hidden");t.setAttribute("aria-hidden","true"),e.push(()=>n||t.removeAttribute("aria-hidden"))}),()=>{for(;e.length;)e.pop()()}}function z(i,e){i.isSameNode(document.body)||!i.parentNode||Array.from(i.parentNode.children).forEach(t=>{t.isSameNode(i)||e(t),z(i.parentNode,e)})}function pe(){let i=document.documentElement.style.overflow,e=document.documentElement.style.paddingRight,t=window.innerWidth-document.documentElement.clientWidth;return document.documentElement.style.overflow="hidden",document.documentElement.style.paddingRight=`${t}px`,()=>{document.documentElement.style.overflow=i,document.documentElement.style.paddingRight=e}}document.addEventListener("alpine:init",()=>{window.Alpine.plugin(Y)});})(); | ||
/*! | ||
@@ -3,0 +3,0 @@ * focus-trap 6.6.1 |
@@ -799,3 +799,3 @@ var __create = Object.create; | ||
}); | ||
Alpine.directive("trap", Alpine.skipDuringClone((el, {expression, modifiers}, {effect, evaluateLater}) => { | ||
Alpine.directive("trap", Alpine.skipDuringClone((el, {expression, modifiers}, {effect, evaluateLater, cleanup}) => { | ||
let evaluator = evaluateLater(expression); | ||
@@ -812,2 +812,13 @@ let oldValue = false; | ||
}; | ||
const releaseFocus = () => { | ||
undoInert(); | ||
undoInert = () => { | ||
}; | ||
undoDisableScrolling(); | ||
undoDisableScrolling = () => { | ||
}; | ||
trap.deactivate({ | ||
returnFocus: !modifiers.includes("noreturn") | ||
}); | ||
}; | ||
effect(() => evaluator((value) => { | ||
@@ -826,14 +837,7 @@ if (oldValue === value) | ||
if (!value && oldValue) { | ||
undoInert(); | ||
undoInert = () => { | ||
}; | ||
undoDisableScrolling(); | ||
undoDisableScrolling = () => { | ||
}; | ||
trap.deactivate({ | ||
returnFocus: !modifiers.includes("noreturn") | ||
}); | ||
releaseFocus(); | ||
} | ||
oldValue = !!value; | ||
})); | ||
cleanup(releaseFocus); | ||
}, (el, {expression, modifiers}, {evaluate}) => { | ||
@@ -840,0 +844,0 @@ if (modifiers.includes("inert") && evaluate(expression)) |
@@ -789,3 +789,3 @@ var __create = Object.create; | ||
}); | ||
Alpine.directive("trap", Alpine.skipDuringClone((el, {expression, modifiers}, {effect, evaluateLater}) => { | ||
Alpine.directive("trap", Alpine.skipDuringClone((el, {expression, modifiers}, {effect, evaluateLater, cleanup}) => { | ||
let evaluator = evaluateLater(expression); | ||
@@ -802,2 +802,13 @@ let oldValue = false; | ||
}; | ||
const releaseFocus = () => { | ||
undoInert(); | ||
undoInert = () => { | ||
}; | ||
undoDisableScrolling(); | ||
undoDisableScrolling = () => { | ||
}; | ||
trap.deactivate({ | ||
returnFocus: !modifiers.includes("noreturn") | ||
}); | ||
}; | ||
effect(() => evaluator((value) => { | ||
@@ -816,14 +827,7 @@ if (oldValue === value) | ||
if (!value && oldValue) { | ||
undoInert(); | ||
undoInert = () => { | ||
}; | ||
undoDisableScrolling(); | ||
undoDisableScrolling = () => { | ||
}; | ||
trap.deactivate({ | ||
returnFocus: !modifiers.includes("noreturn") | ||
}); | ||
releaseFocus(); | ||
} | ||
oldValue = !!value; | ||
})); | ||
cleanup(releaseFocus); | ||
}, (el, {expression, modifiers}, {evaluate}) => { | ||
@@ -830,0 +834,0 @@ if (modifiers.includes("inert") && evaluate(expression)) |
{ | ||
"name": "@alpinejs/focus", | ||
"version": "3.9.0", | ||
"version": "3.9.1", | ||
"description": "Manage focus within a page", | ||
@@ -5,0 +5,0 @@ "author": "Caleb Porzio", |
@@ -6,3 +6,3 @@ import { createFocusTrap } from 'focus-trap' | ||
let lastFocused | ||
let currentFocused | ||
let currentFocused | ||
@@ -16,6 +16,6 @@ window.addEventListener('focusin', () => { | ||
let within = el | ||
return { | ||
__noscroll: false, | ||
__wrapAround: false, | ||
__noscroll: false, | ||
__wrapAround: false, | ||
within(el) { within = el; return this }, | ||
@@ -39,3 +39,3 @@ withoutScrolling() { this.__noscroll = true; return this }, | ||
focusables() { | ||
if (Array.isArray(within)) return within | ||
if (Array.isArray(within)) return within | ||
@@ -46,9 +46,9 @@ return focusable(within, { displayCheck: 'none' }) | ||
isFirst(el) { | ||
let els = this.all() | ||
let els = this.all() | ||
return els[0] && els[0].isSameNode(el) | ||
}, | ||
isLast(el) { | ||
let els = this.all() | ||
let els = this.all() | ||
return els.length && els.slice(-1)[0].isSameNode(el) | ||
@@ -92,3 +92,3 @@ }, | ||
focus(el) { | ||
if (! el) return | ||
if (! el) return | ||
@@ -105,3 +105,3 @@ setTimeout(() => { | ||
Alpine.directive('trap', Alpine.skipDuringClone( | ||
(el, { expression, modifiers }, { effect, evaluateLater }) => { | ||
(el, { expression, modifiers }, { effect, evaluateLater, cleanup }) => { | ||
let evaluator = evaluateLater(expression) | ||
@@ -120,2 +120,14 @@ | ||
const releaseFocus = () => { | ||
undoInert() | ||
undoInert = () => {} | ||
undoDisableScrolling() | ||
undoDisableScrolling = () => {} | ||
trap.deactivate({ | ||
returnFocus: !modifiers.includes('noreturn') | ||
}) | ||
} | ||
effect(() => evaluator(value => { | ||
@@ -136,11 +148,3 @@ if (oldValue === value) return | ||
if (! value && oldValue) { | ||
undoInert() | ||
undoInert = () => {} | ||
undoDisableScrolling() | ||
undoDisableScrolling = () => {} | ||
trap.deactivate({ | ||
returnFocus: !modifiers.includes('noreturn') | ||
}) | ||
releaseFocus() | ||
} | ||
@@ -150,2 +154,4 @@ | ||
})) | ||
cleanup(releaseFocus) | ||
}, | ||
@@ -152,0 +158,0 @@ // When cloning, we only want to add aria-hidden attributes to the |
107232
2777