Skip to content

Instantly share code, notes, and snippets.

@reinink
Last active June 14, 2022 03:29
Show Gist options
  • Save reinink/db2700d3deb4234cc5605c42fa723c0f to your computer and use it in GitHub Desktop.
Save reinink/db2700d3deb4234cc5605c42fa723c0f to your computer and use it in GitHub Desktop.
Global Inertia.js click event handler
import { Inertia } from '@inertiajs/inertia'
document.addEventListener('click', (event) => {
if (
event.target.tagName.toLowerCase() === 'a' &&
event.target.hasAttribute('inertia') &&
!event.target.isContentEditable &&
!event.defaultPrevented &&
!event.shiftKey &&
!event.ctrlKey &&
!event.metaKey &&
!event.altKey
) {
event.preventDefault()
const options = JSON.parse(event.target.getAttribute('inertia') || '{}')
Inertia.visit(event.target.href, options)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment