-
Couldn't load subscription status.
- Fork 6
feat: Stimulus controller to show a confirm modal before link or form… #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1708f1f
feat: Stimulus controller to show a confirm modal before link or form…
tijsverkoyen f064f8e
fix: fixed typo
tijsverkoyen 377fb12
fix: more grammaticaly correct sentance
tijsverkoyen 0dcc1cd
chore: removed useless separate method
tijsverkoyen b5c560c
chore: removed useless fallback
tijsverkoyen 323e227
fix: only bind on button inside confirmation modal
tijsverkoyen 67b72f8
feedback: use translation variables
tijsverkoyen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,134 @@ | ||
| import { Controller } from '@hotwired/stimulus' | ||
| import { Modal } from 'bootstrap' | ||
|
|
||
| export default class extends Controller { | ||
| static values = { | ||
| modalTitle: String, | ||
| confirmationMessage: { type: String, default: 'Are you sure?' }, | ||
| cancelButtonText: { type: String, default: 'Cancel' }, | ||
| confirmButtonText: { type: String, default: 'Ok' }, | ||
| closeButtonText: { type: String, default: 'Close' }, | ||
| } | ||
| static targets = ['element'] | ||
|
|
||
| connect () { | ||
| if (!this.isForm() && !this.isLink()) { | ||
| console.error('Confirm controller: Only form or a elements are supported.') | ||
| } | ||
|
|
||
| let eventName = '' | ||
| if (this.isForm()) eventName = 'submit' | ||
| if (this.isLink()) eventName = 'click' | ||
|
|
||
| this.elementTarget.addEventListener(eventName, (e) => { | ||
| e.preventDefault() | ||
| e.stopPropagation() | ||
| this.showModal() | ||
| }) | ||
| } | ||
|
|
||
| isForm () { | ||
| return this.elementTarget && this.elementTarget.tagName.toLowerCase() === 'form' | ||
| } | ||
|
|
||
| isLink () { | ||
| return this.elementTarget && this.elementTarget.tagName.toLowerCase() === 'a' | ||
| } | ||
|
|
||
| showModal () { | ||
| // remove existing modal | ||
| const existingModal = document.querySelector('div[data-role="confirm-modal"]') | ||
| if (existingModal) { | ||
| existingModal.parentNode.removeChild(existingModal) | ||
| } | ||
|
|
||
| // create modal | ||
| const modal = document.createElement('div') | ||
| modal.className = 'modal fade' | ||
| modal.tabIndex = -1 | ||
| modal.setAttribute('aria-labelledby', 'confirmModalTitle') | ||
| modal.setAttribute('aria-hidden', 'true') | ||
| modal.setAttribute('data-role', 'confirm-modal') | ||
| const modalDialog = document.createElement('div') | ||
| modalDialog.className = 'modal-dialog' | ||
| modal.appendChild(modalDialog) | ||
| const modalContent = document.createElement('div') | ||
| modalContent.className = 'modal-content' | ||
| modalDialog.appendChild(modalContent) | ||
| const modalHeader = document.createElement('div') | ||
| modalHeader.className = 'modal-header' | ||
| modalContent.appendChild(modalHeader) | ||
| if (this.modalTitleValue !== '') { | ||
| const modalTitle = document.createElement('h5') | ||
| modalTitle.className = 'modal-title' | ||
| modalTitle.id = 'confirmModalTitle' | ||
| modalTitle.textContent = this.modalTitleValue | ||
| modalHeader.appendChild(modalTitle) | ||
| } | ||
| const closeButton = document.createElement('button') | ||
| closeButton.type = 'button' | ||
| closeButton.className = 'btn-close' | ||
| closeButton.setAttribute('data-bs-dismiss', 'modal') | ||
| closeButton.setAttribute('aria-label', this.closeButtonTextValue) | ||
| modalHeader.appendChild(closeButton) | ||
| const modalBody = document.createElement('div') | ||
| modalBody.className = 'modal-body' | ||
| modalBody.textContent = this.confirmationMessageValue | ||
| modalContent.appendChild(modalBody) | ||
| const modalFooter = document.createElement('div') | ||
| modalFooter.className = 'modal-footer' | ||
| modalContent.appendChild(modalFooter) | ||
| const cancelButton = document.createElement('button') | ||
| cancelButton.type = 'button' | ||
| cancelButton.className = 'btn btn-secondary' | ||
| cancelButton.setAttribute('data-bs-dismiss', 'modal') | ||
| cancelButton.textContent = this.cancelButtonTextValue | ||
| modalFooter.appendChild(cancelButton) | ||
| const confirmButton = document.createElement('button') | ||
| confirmButton.type = 'button' | ||
| confirmButton.className = 'btn btn-primary' | ||
| confirmButton.textContent = this.confirmButtonTextValue | ||
| confirmButton.setAttribute('data-role', 'confirm-button') | ||
| modalFooter.appendChild(confirmButton) | ||
| document.body.appendChild(modal) | ||
|
|
||
| // show modal | ||
| const bootstrapModal = new Modal(modal, { backdrop: 'static' }) | ||
| bootstrapModal.show() | ||
|
|
||
| // remove modal from DOM after hiding | ||
| modal.addEventListener('hidden.bs.modal', () => { | ||
| modal.parentNode && modal.parentNode.removeChild(modal) | ||
| }) | ||
|
|
||
| // bind on confirm button click | ||
| const confirmBtn = modal.querySelector('[data-role="confirm-button"]') | ||
| confirmBtn.addEventListener('click', () => { | ||
| this.handleConfirm() | ||
| bootstrapModal.hide() | ||
| }) | ||
| } | ||
|
|
||
| handleConfirm () { | ||
| if (this.isForm()) { | ||
| this.elementTarget.submit() | ||
| return | ||
| } | ||
|
|
||
| if (this.isLink()) { | ||
| if (this.elementTarget.target) { | ||
| window.open(this.elementTarget.href, this.elementTarget.target) | ||
| return | ||
| } | ||
|
|
||
| window.location = this.elementTarget.href | ||
| } | ||
sourcery-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| disconnect () { | ||
| const existingModal = document.querySelector('div[data-role="confirm-modal"]') | ||
| if (existingModal) { | ||
| existingModal.parentNode.removeChild(existingModal) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.