I'm trying to solve the problem different. Instead of waiting until the element is in viewport. I collect the scroll positions of all ancestors, focus the element and then restore the scroll positions.
This approach is far more efficient than calculating the target element's visible area on each animation frame. For this to work the element has to be focusable right away (which is not the case for ally.when.focusable), therefore this feature can't replaceally.when.focusable completely.
In WICG: Improve element.focus method @aFarkas explains his approach to get around having to use ally.when.focusable:
This approach is far more efficient than calculating the target element's visible area on each animation frame. For this to work the element has to be focusable right away (which is not the case for
ally.when.focusable
), therefore this feature can't replaceally.when.focusable
completely.This would be an optional behavior:
Before we
.focus()
the element, we collect the parents'.scrollTop
and.scrollLeft
and restore those values in thefocus
event handler.