Hi there.
I’ve got a form with inputs outside of the view on small screens. I am adding an event listener to phx:update
that then scrolls to the first object with .has_errors
. This only seems to work if I add a 1ms delay to my event listener (presumably to let the dom finish updating). Without it, the target node is correctly queried, but the call to scrollIntoView
isn’t executed. Should I be listening to a separate event? It seems there is no phx:updated
.
I import and call the following hook on the parent form using phx-hook=""
:
// assets/js/hooks/error-scroll.js
const errorScroll = {
mounted() {
window.addEventListener("phx:update", (event) => {
let firstError = document.querySelector("label.has-error");
if (firstError) {
setTimeout(function () {
firstError.scrollIntoView({ behavior: "smooth", block: "end", inline: "nearest" });
console.log("scrolling to:", firstError);
}, 1)
}
});
}
}
export default errorScroll;
What am I missing?