imacrayon / alpine-ajax

An Alpine.js plugin for building server-powered frontends.
https://alpine-ajax.js.org
MIT License
558 stars 11 forks source link

Improve focus management #56

Closed imacrayon closed 6 months ago

imacrayon commented 7 months ago

The problem

I'm not totally satisfied with using x-focus for focus management, it falls short in some cases:

It's worth noting that Alpine has a first party Focus plugin, it would be nice to have better support for those utilities.

x-autofocus to the rescue

This PR introduces a new x-autofocus attribute and deprecates x-focus. After an AJAX request is issued and content is merged on the page, the first element with the attribute x-autofocus, will be automatically focused. The same is true for the browser-standard autofocus attribute. The only difference between x-autofocus and autofocus is that x-autofocus does not trigger focus on initial page loads, only when AJAX content loads.

If multiple elements are targeted in an AJAX request each target is checked for an x-autofocus attribute in the order specified by x-target, so the first target containing an x-autofocus will receive focus.

Fixes #52