-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Remove Wire Elements modal dependencies and import modal into t…
…he project
- Loading branch information
1 parent
d4fca09
commit 75c0d2f
Showing
25 changed files
with
745 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
const Modal = () => { | ||
return { | ||
show: false, | ||
showActiveComponent: true, | ||
activeComponent: false, | ||
componentHistory: [], | ||
modalWidth: null , | ||
listeners: [], | ||
getActiveComponentModalAttribute(key) { | ||
if (this.$wire.get('components')[this.activeComponent] !== undefined) { | ||
return this.$wire.get('components')[this.activeComponent]['modalAttributes'][key]; | ||
} | ||
}, | ||
closeModalOnEscape(trigger) { | ||
if (this.getActiveComponentModalAttribute('closeOnEscape') === false) { | ||
return; | ||
} | ||
|
||
let force = this.getActiveComponentModalAttribute('closeOnEscapeIsForceful') === true; | ||
this.closeModal(force); | ||
}, | ||
closeModalOnClickAway(trigger) { | ||
if (this.getActiveComponentModalAttribute('closeOnClickAway') === false) { | ||
return; | ||
} | ||
|
||
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.dispatch('modalClosed', {name: componentName}); | ||
} | ||
|
||
if (this.getActiveComponentModalAttribute('destroyOnClose') === true) { | ||
Livewire.dispatch('destroyComponent', {id: this.activeComponent}); | ||
} | ||
|
||
if (skipPreviousModals > 0) { | ||
for (var i = 0; i < skipPreviousModals; i++) { | ||
if (destroySkipped) { | ||
const id = this.componentHistory[this.componentHistory.length - 1]; | ||
Livewire.dispatch('destroyComponent', {id: id}); | ||
} | ||
this.componentHistory.pop(); | ||
} | ||
} | ||
|
||
const id = this.componentHistory.pop(); | ||
|
||
if (id && !force) { | ||
if (id) { | ||
this.setActiveModalComponent(id, true); | ||
} else { | ||
this.setShowPropertyTo(false); | ||
} | ||
} else { | ||
this.setShowPropertyTo(false); | ||
} | ||
}, | ||
setActiveModalComponent(id, skip = false) { | ||
this.setShowPropertyTo(true); | ||
|
||
if (this.activeComponent === id) { | ||
return; | ||
} | ||
|
||
if (this.activeComponent !== false && skip === false) { | ||
this.componentHistory.push(this.activeComponent); | ||
} | ||
|
||
let focusableTimeout = 50; | ||
|
||
if (this.activeComponent === false) { | ||
this.activeComponent = id | ||
this.showActiveComponent = true; | ||
this.modalWidth = this.getActiveComponentModalAttribute('maxWidthClass'); | ||
} else { | ||
this.showActiveComponent = false; | ||
|
||
focusableTimeout = 400; | ||
|
||
setTimeout(() => { | ||
this.activeComponent = id; | ||
this.showActiveComponent = true; | ||
this.modalWidth = this.getActiveComponentModalAttribute('maxWidthClass'); | ||
}, 300); | ||
} | ||
|
||
this.$nextTick(() => { | ||
let focusable = this.$refs[id]?.querySelector('[autofocus]'); | ||
if (focusable) { | ||
setTimeout(() => { | ||
focusable.focus(); | ||
}, focusableTimeout); | ||
} | ||
}); | ||
}, | ||
focusables() { | ||
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')) | ||
}, | ||
firstFocusable() { | ||
return this.focusables()[0] | ||
}, | ||
lastFocusable() { | ||
return this.focusables().slice(-1)[0] | ||
}, | ||
nextFocusable() { | ||
return this.focusables()[this.nextFocusableIndex()] || this.firstFocusable() | ||
}, | ||
prevFocusable() { | ||
return this.focusables()[this.prevFocusableIndex()] || this.lastFocusable() | ||
}, | ||
nextFocusableIndex() { | ||
return (this.focusables().indexOf(document.activeElement) + 1) % (this.focusables().length + 1) | ||
}, | ||
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.listeners.push( | ||
Livewire.on('closeModal', (data) => { | ||
this.closeModal(data?.force ?? false, data?.skipPreviousModals ?? 0, data?.destroySkipped ?? false); | ||
}) | ||
); | ||
|
||
this.listeners.push( | ||
Livewire.on('activeModalComponentChanged', ({id}) => { | ||
this.setActiveModalComponent(id); | ||
}) | ||
); | ||
}, | ||
destroy() { | ||
this.listeners.forEach((listener) => { | ||
listener(); | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
export default Modal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.