To set the focus on the h1 element on every route change using Laravel, Inertia, and Vue 3, you can create a global event listener in your app.js file.
First, you need to listen for the start event in Inertia. This event is fired before the page transition begins. You can use the Inertia.on method to listen for this event.
Inside the event listener, you can use the document.querySelector method to select the h1 element and then call the focus method on it to set the focus.
Here's an example of how you can implement this solution in your app.js file:
import { Inertia } from '@inertiajs/inertia';
Inertia.on('start', () => {
const h1Element = document.querySelector('h1');
if (h1Element) {
h1Element.focus();
}
});
Make sure to import the Inertia object from @inertiajs/inertia at the top of your app.js file.
With this solution, the focus will be set on the h1 element on every route change. You don't need to add the tabindex="-1" attribute to all h1 elements unless you have specific accessibility requirements.
Note: If you're using Vue 2 instead of Vue 3, you can still use this solution. Just make sure to import the Inertia object from @inertiajs/inertia-vue2 instead.