Skip to content

Commit 485c349

Browse files
committed
don't overuse requestAnimationFrame (#3657)
This ensures that JS.focus works on iOS. Fixes: #3563
1 parent 1397b5e commit 485c349

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

assets/js/phoenix_live_view/hooks.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,12 @@ let Hooks = {
6767
ARIA.focusFirst(this.el)
6868
}
6969
})
70-
this.el.addEventListener("phx:show-end", () => this.el.focus())
71-
if(window.getComputedStyle(this.el).display !== "none"){
72-
ARIA.focusFirst(this.el)
70+
// only try to change the focus if it is not already inside
71+
if(!this.el.contains(document.activeElement)){
72+
this.el.addEventListener("phx:show-end", () => this.el.focus())
73+
if(window.getComputedStyle(this.el).display !== "none"){
74+
ARIA.focusFirst(this.el)
75+
}
7376
}
7477
}
7578
}

assets/js/phoenix_live_view/js.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ let JS = {
9797
},
9898

9999
exec_focus(e, eventType, phxEvent, view, sourceEl, el){
100-
window.requestAnimationFrame(() => ARIA.attemptFocus(el))
100+
ARIA.attemptFocus(el)
101101
},
102102

103103
exec_focus_first(e, eventType, phxEvent, view, sourceEl, el){
104-
window.requestAnimationFrame(() => ARIA.focusFirstInteractive(el) || ARIA.focusFirst(el))
104+
ARIA.focusFirstInteractive(el) || ARIA.focusFirst(el)
105105
},
106106

107107
exec_push_focus(e, eventType, phxEvent, view, sourceEl, el){
@@ -219,18 +219,14 @@ let JS = {
219219
}
220220
} else {
221221
if(this.isVisible(el)){
222-
window.requestAnimationFrame(() => {
223-
el.dispatchEvent(new Event("phx:hide-start"))
224-
DOM.putSticky(el, "toggle", currentEl => currentEl.style.display = "none")
225-
el.dispatchEvent(new Event("phx:hide-end"))
226-
})
222+
el.dispatchEvent(new Event("phx:hide-start"))
223+
DOM.putSticky(el, "toggle", currentEl => currentEl.style.display = "none")
224+
el.dispatchEvent(new Event("phx:hide-end"))
227225
} else {
228-
window.requestAnimationFrame(() => {
229-
el.dispatchEvent(new Event("phx:show-start"))
230-
let stickyDisplay = display || this.defaultDisplay(el)
231-
DOM.putSticky(el, "toggle", currentEl => currentEl.style.display = stickyDisplay)
232-
el.dispatchEvent(new Event("phx:show-end"))
233-
})
226+
el.dispatchEvent(new Event("phx:show-start"))
227+
let stickyDisplay = display || this.defaultDisplay(el)
228+
DOM.putSticky(el, "toggle", currentEl => currentEl.style.display = stickyDisplay)
229+
el.dispatchEvent(new Event("phx:show-end"))
234230
}
235231
}
236232
},

0 commit comments

Comments
 (0)