From c7c24a88604ee7398eb788e631ee304d9176cc94 Mon Sep 17 00:00:00 2001 From: Jack Sullivan Date: Sun, 27 Aug 2023 21:09:56 -0400 Subject: [PATCH 1/3] Livewire 3 --- composer.json | 2 +- phpunit.xml.dist | 3 ++ public/modal.js | 2 +- resources/js/modal.js | 63 +++++++++++++--------- resources/views/modal.blade.php | 20 ++++--- src/LivewireModalServiceProvider.php | 16 +++--- src/Modal.php | 80 +++++++++++++++++++++++++--- src/ModalComponent.php | 49 +++++------------ tests/Components/DemoModal.php | 9 +++- tests/LivewireModalComponentTest.php | 27 +++++----- tests/LivewireModalTest.php | 32 ++++++----- 11 files changed, 190 insertions(+), 113 deletions(-) diff --git a/composer.json b/composer.json index 935ce4e..e1cf224 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "php": "^8.1", "spatie/laravel-package-tools": "^1.14.0", "illuminate/contracts": "^10.0", - "livewire/livewire": "^2.11" + "livewire/livewire": "^3.0" }, "require-dev": { "laravel/pint": "^1.0", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 95ff701..1d108d5 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,4 +9,7 @@ ./tests + + + \ No newline at end of file diff --git a/public/modal.js b/public/modal.js index c9e1d9a..da49033 100644 --- a/public/modal.js +++ b/public/modal.js @@ -1 +1 @@ -(()=>{var t,e={331:()=>{function t(t){return function(t){if(Array.isArray(t))return e(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,o){if(!t)return;if("string"==typeof t)return e(t,o);var n=Object.prototype.toString.call(t).slice(8,-1);"Object"===n&&t.constructor&&(n=t.constructor.name);if("Map"===n||"Set"===n)return Array.from(t);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return e(t,o)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function e(t,e){(null==e||e>t.length)&&(e=t.length);for(var o=0,n=new Array(e);o0&&void 0!==arguments[0]&&arguments[0],e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,o=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(!0===this.getActiveComponentModalAttribute("dispatchCloseEvent")){var n=this.$wire.get("components")[this.activeComponent].name;Livewire.emit("modalClosed",n)}if(!0===this.getActiveComponentModalAttribute("destroyOnClose")&&Livewire.emit("destroyComponent",this.activeComponent),e>0)for(var i=0;i1&&void 0!==arguments[1]&&arguments[1];if(this.show=!0,this.activeComponent!==t){!1!==this.activeComponent&&!1===o&&this.componentHistory.push(this.activeComponent);var n=50;!1===this.activeComponent?(this.activeComponent=t,this.activeComponentType=this.getActiveComponentModalAttribute("type"),this.showActiveComponent=!0,this.modalWidth=this.getActiveComponentModalAttribute("maxWidthClass"),this.fullScreen=this.getActiveComponentModalAttribute("fullScreen")):(this.showActiveComponent=!1,n=400,setTimeout((function(){e.activeComponent=t,e.activeComponentType=e.getActiveComponentModalAttribute("type"),e.showActiveComponent=!0,e.modalWidth=e.getActiveComponentModalAttribute("maxWidthClass"),e.fullScreen=e.getActiveComponentModalAttribute("fullScreen")}),300)),this.$nextTick((function(){var o,i=null===(o=e.$refs[t])||void 0===o?void 0:o.querySelector("[autofocus]");i&&setTimeout((function(){i.focus()}),n)}))}},focusables:function(){return t(this.$el.querySelectorAll("a, button, input, textarea, select, details, [tabindex]:not([tabindex='-1'])")).filter((function(t){return!t.hasAttribute("disabled")}))},firstFocusable:function(){return this.focusables()[0]},lastFocusable:function(){return this.focusables().slice(-1)[0]},nextFocusable:function(){return this.focusables()[this.nextFocusableIndex()]||this.firstFocusable()},prevFocusable:function(){return this.focusables()[this.prevFocusableIndex()]||this.lastFocusable()},nextFocusableIndex:function(){return(this.focusables().indexOf(document.activeElement)+1)%(this.focusables().length+1)},prevFocusableIndex:function(){return Math.max(0,this.focusables().indexOf(document.activeElement))-1},init:function(){var t=this;this.modalWidth=this.getActiveComponentModalAttribute("maxWidthClass"),this.fullScreen=this.getActiveComponentModalAttribute("fullScreen"),this.$watch("show",(function(e){e?document.body.classList.add("overflow-y-hidden"):(document.body.classList.remove("overflow-y-hidden"),setTimeout((function(){t.activeComponent=!1,t.activeComponentType=null,t.$wire.resetState()}),300))})),Livewire.on("closeModal",(function(){var e=arguments.length>0&&void 0!==arguments[0]&&arguments[0],o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=arguments.length>2&&void 0!==arguments[2]&&arguments[2];t.closeModal(e,o,n)})),Livewire.on("activeModalComponentChanged",(function(e){t.setActiveModalComponent(e)}))}}}},754:()=>{}},o={};function n(t){var i=o[t];if(void 0!==i)return i.exports;var r=o[t]={exports:{}};return e[t](r,r.exports,n),r.exports}n.m=e,t=[],n.O=(e,o,i,r)=>{if(!o){var s=1/0;for(u=0;u=r)&&Object.keys(n.O).every((t=>n.O[t](o[c])))?o.splice(c--,1):(a=!1,r0&&t[u-1][2]>r;u--)t[u]=t[u-1];t[u]=[o,i,r]},n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{var t={387:0,109:0};n.O.j=e=>0===t[e];var e=(e,o)=>{var i,r,[s,a,c]=o,l=0;if(s.some((e=>0!==t[e]))){for(i in a)n.o(a,i)&&(n.m[i]=a[i]);if(c)var u=c(n)}for(e&&e(o);ln(331)));var i=n.O(void 0,[109],(()=>n(754)));i=n.O(i)})(); \ No newline at end of file +(()=>{var t,e={331:()=>{function t(t){return function(t){if(Array.isArray(t))return e(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,o){if(!t)return;if("string"==typeof t)return e(t,o);var n=Object.prototype.toString.call(t).slice(8,-1);"Object"===n&&t.constructor&&(n=t.constructor.name);if("Map"===n||"Set"===n)return Array.from(t);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return e(t,o)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function e(t,e){(null==e||e>t.length)&&(e=t.length);for(var o=0,n=new Array(e);o0&&void 0!==arguments[0]&&arguments[0],e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,o=arguments.length>2&&void 0!==arguments[2]&&arguments[2];if(!1!==this.show){if(!0===this.getActiveComponentModalAttribute("dispatchCloseEvent")){var n=this.$wire.get("components")[this.activeComponent].name;Livewire.dispatch("modalClosed",n)}if(!0===this.getActiveComponentModalAttribute("destroyOnClose")&&Livewire.dispatch("destroyComponent",this.activeComponent),e>0)for(var i=0;i1&&void 0!==arguments[1]&&arguments[1];if(this.setShowPropertyTo(!0),this.activeComponent!==t){!1!==this.activeComponent&&!1===o&&this.componentHistory.push(this.activeComponent);var n=50;!1===this.activeComponent?(this.activeComponent=t,this.activeComponentType=this.getActiveComponentModalAttribute("type"),this.showActiveComponent=!0,this.modalWidth=this.getActiveComponentModalAttribute("maxWidthClass"),this.fullScreen=this.getActiveComponentModalAttribute("fullScreen")):(this.showActiveComponent=!1,n=400,setTimeout((function(){e.activeComponent=t,e.activeComponentType=e.getActiveComponentModalAttribute("type"),e.showActiveComponent=!0,e.modalWidth=e.getActiveComponentModalAttribute("maxWidthClass"),e.fullScreen=e.getActiveComponentModalAttribute("fullScreen")}),300)),this.$nextTick((function(){var o,i=null===(o=e.$refs[t])||void 0===o?void 0:o.querySelector("[autofocus]");i&&setTimeout((function(){i.focus()}),n)}))}},focusables:function(){return t(this.$el.querySelectorAll("a, button, input:not([type='hidden'], textarea, select, details, [tabindex]:not([tabindex='-1'])")).filter((function(t){return!t.hasAttribute("disabled")}))},firstFocusable:function(){return this.focusables()[0]},lastFocusable:function(){return this.focusables().slice(-1)[0]},nextFocusable:function(){return this.focusables()[this.nextFocusableIndex()]||this.firstFocusable()},prevFocusable:function(){return this.focusables()[this.prevFocusableIndex()]||this.lastFocusable()},nextFocusableIndex:function(){return(this.focusables().indexOf(document.activeElement)+1)%(this.focusables().length+1)},prevFocusableIndex:function(){return Math.max(0,this.focusables().indexOf(document.activeElement))-1},setShowPropertyTo:function(t){var e=this;this.show=t,t?document.body.classList.add("overflow-y-hidden"):(document.body.classList.remove("overflow-y-hidden"),setTimeout((function(){e.activeComponent=!1,e.$wire.resetState()}),300))},init:function(){var t=this;this.modalWidth=this.getActiveComponentModalAttribute("maxWidthClass"),this.fullScreen=this.getActiveComponentModalAttribute("fullScreen"),Livewire.on("closeModal",(function(e){var o,n,i;t.closeModal(null!==(o=null==e?void 0:e.force)&&void 0!==o&&o,null!==(n=null==e?void 0:e.skipPreviousModals)&&void 0!==n?n:0,null!==(i=null==e?void 0:e.destroySkipped)&&void 0!==i&&i)})),Livewire.on("activeModalComponentChanged",(function(e){var o=e.id;t.setActiveModalComponent(o)}))}}}},754:()=>{}},o={};function n(t){var i=o[t];if(void 0!==i)return i.exports;var r=o[t]={exports:{}};return e[t](r,r.exports,n),r.exports}n.m=e,t=[],n.O=(e,o,i,r)=>{if(!o){var s=1/0;for(u=0;u=r)&&Object.keys(n.O).every((t=>n.O[t](o[l])))?o.splice(l--,1):(a=!1,r0&&t[u-1][2]>r;u--)t[u]=t[u-1];t[u]=[o,i,r]},n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{var t={387:0,109:0};n.O.j=e=>0===t[e];var e=(e,o)=>{var i,r,[s,a,l]=o,c=0;if(s.some((e=>0!==t[e]))){for(i in a)n.o(a,i)&&(n.m[i]=a[i]);if(l)var u=l(n)}for(e&&e(o);cn(331)));var i=n.O(void 0,[109],(()=>n(754)));i=n.O(i)})(); \ No newline at end of file diff --git a/resources/js/modal.js b/resources/js/modal.js index 33096bf..87d5943 100644 --- a/resources/js/modal.js +++ b/resources/js/modal.js @@ -28,21 +28,24 @@ window.LivewireUIModal = () => { this.closeModal(true); }, closeModal(force = false, skipPreviousModals = 0, destroySkipped = false) { + if(this.show === false) { + return; + } if (this.getActiveComponentModalAttribute('dispatchCloseEvent') === true) { const componentName = this.$wire.get('components')[this.activeComponent].name; - Livewire.emit('modalClosed', componentName); + Livewire.dispatch('modalClosed', componentName); } if (this.getActiveComponentModalAttribute('destroyOnClose') === true) { - Livewire.emit('destroyComponent', this.activeComponent); + Livewire.dispatch('destroyComponent', this.activeComponent); } if (skipPreviousModals > 0) { for (var i = 0; i < skipPreviousModals; i++) { if (destroySkipped) { const id = this.componentHistory[this.componentHistory.length - 1]; - Livewire.emit('destroyComponent', id); + Livewire.dispatch('destroyComponent', {id: id}); } this.componentHistory.pop(); } @@ -54,14 +57,14 @@ window.LivewireUIModal = () => { if (id) { this.setActiveModalComponent(id, true); } else { - this.show = false; + this.setShowPropertyTo(false); } } else { - this.show = false; + this.setShowPropertyTo(false); } }, setActiveModalComponent(id, skip = false) { - this.show = true; + this.setShowPropertyTo(true); if (this.activeComponent === id) { return; @@ -104,7 +107,7 @@ window.LivewireUIModal = () => { }); }, focusables() { - let selector = 'a, button, input, textarea, select, details, [tabindex]:not([tabindex=\'-1\'])' + let selector = 'a, button, input:not([type=\'hidden\'], textarea, select, details, [tabindex]:not([tabindex=\'-1\'])' return [...this.$el.querySelectorAll(selector)] .filter(el => !el.hasAttribute('disabled')) @@ -127,34 +130,46 @@ window.LivewireUIModal = () => { prevFocusableIndex() { return Math.max(0, this.focusables().indexOf(document.activeElement)) - 1 }, + setShowPropertyTo(show) { + this.show = show; + + if (show) { + document.body.classList.add('overflow-y-hidden'); + } else { + document.body.classList.remove('overflow-y-hidden'); + + setTimeout(() => { + this.activeComponent = false; + this.$wire.resetState(); + }, 300); + } + }, init() { this.modalWidth = this.getActiveComponentModalAttribute('maxWidthClass'); this.fullScreen = this.getActiveComponentModalAttribute('fullScreen'); - this.$watch('show', value => { - if (value) { - document.body.classList.add('overflow-y-hidden'); - } else { - document.body.classList.remove('overflow-y-hidden'); + // this.$watch('show', value => { + // if (value) { + // document.body.classList.add('overflow-y-hidden'); + // } else { + // document.body.classList.remove('overflow-y-hidden'); - setTimeout(() => { - this.activeComponent = false; - this.activeComponentType = null; + // setTimeout(() => { + // this.activeComponent = false; + // this.activeComponentType = null; - this.$wire.resetState(); - }, 300); - } - }); + // this.$wire.resetState(); + // }, 300); + // } + // }); - Livewire.on('closeModal', (force = false, skipPreviousModals = 0, destroySkipped = false) => { - this.closeModal(force, skipPreviousModals, destroySkipped); + Livewire.on('closeModal', (data) => { + this.closeModal(data?.force ?? false, data?.skipPreviousModals ?? 0, data?.destroySkipped ?? false); }); - Livewire.on('activeModalComponentChanged', (id) => { + Livewire.on('activeModalComponentChanged', ({id}) => { this.setActiveModalComponent(id); }); - - } }; } diff --git a/resources/views/modal.blade.php b/resources/views/modal.blade.php index b37b309..c0c1a41 100644 --- a/resources/views/modal.blade.php +++ b/resources/views/modal.blade.php @@ -9,26 +9,25 @@