Further simplify post-route handler.

* Use `HtmlElement.tabIndex` directly rather than going through attributes.
* Always restore `tabIndex` after focus.
* Remove `setTimeout` to simplify implementation.

This does have a disadvantage in that it leaves unnecessary `tabindex="1"` instances around. On the other hand, it does simplify the implementation and get the job done' so is probably a more clear example of how to do this correctly.
This commit is contained in:
Nolan Darilek 2021-12-22 12:41:19 -06:00
parent 7d09845b8c
commit 7b48c2fbaf

View file

@ -180,16 +180,10 @@ let liveSocket = new LiveSocket("/live", Socket, {
let routeUpdated = () => { let routeUpdated = () => {
let target = document.querySelector("main h1") || document.querySelector("main") let target = document.querySelector("main h1") || document.querySelector("main")
if (target) { if (target) {
let origTabIndex = target.getAttribute("tabindex") let origTabIndex = target.tabIndex
target.setAttribute("tabindex", "-1") target.tabIndex = -1
target.focus() target.focus()
window.setTimeout(() => { target.tabIndex = origTabIndex
if (origTabIndex) {
target.setAttribute("tabindex", origTabIndex)
} else {
target.removeAttribute("tabindex")
}
}, 1000)
} }
} }