open modals as pages on mobile devices
This commit is contained in:
committed by
Pete Matsyburka
parent
66862e93c8
commit
807e82b21e
@@ -0,0 +1,15 @@
|
||||
export default class extends HTMLElement {
|
||||
connectedCallback () {
|
||||
if (!this.isMobile()) return
|
||||
|
||||
document.querySelectorAll('a[data-turbo-frame="modal"]').forEach((link) => {
|
||||
link.dataset.turboFrame = '_top'
|
||||
})
|
||||
}
|
||||
|
||||
isMobile () {
|
||||
const isTouchWebkit = 'ontouchstart' in window && navigator.maxTouchPoints > 0 && /AppleWebKit|android/i.test(navigator.userAgent)
|
||||
|
||||
return isTouchWebkit || /iPhone|iPad|iPod/i.test(navigator.userAgent)
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ export default actionable(class extends HTMLElement {
|
||||
|
||||
document.addEventListener('keyup', this.onEscKey)
|
||||
|
||||
document.addEventListener('turbo:before-cache', this.close)
|
||||
document.addEventListener('turbo:before-cache', this.onBeforeCache)
|
||||
|
||||
if (this.dataset.closeAfterSubmit !== 'false') {
|
||||
document.addEventListener('turbo:submit-end', this.onSubmit)
|
||||
@@ -18,7 +18,7 @@ export default actionable(class extends HTMLElement {
|
||||
|
||||
document.removeEventListener('keyup', this.onEscKey)
|
||||
document.removeEventListener('turbo:submit-end', this.onSubmit)
|
||||
document.removeEventListener('turbo:before-cache', this.close)
|
||||
document.removeEventListener('turbo:before-cache', this.onBeforeCache)
|
||||
}
|
||||
|
||||
onSubmit = (e) => {
|
||||
@@ -33,9 +33,19 @@ export default actionable(class extends HTMLElement {
|
||||
}
|
||||
}
|
||||
|
||||
onBeforeCache = () => {
|
||||
if (this.closest('turbo-frame')?.src) {
|
||||
this.remove()
|
||||
}
|
||||
}
|
||||
|
||||
close = (e) => {
|
||||
e?.preventDefault()
|
||||
|
||||
this.remove()
|
||||
if (!this.closest('turbo-frame')?.src && window.history.length > 1) {
|
||||
window.history.back()
|
||||
} else {
|
||||
this.remove()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user