Bug 254882

Summary: iPadOS: window loses focus when dismissing the keyboard, breaks Page Lifecycle API
Product: WebKit Reporter: ik
Component: UI EventsAssignee: Nobody <webkit-unassigned>
Status: NEW ---    
Severity: Normal CC: megan_gardner, webkit-bug-importer, wenson_hsieh
Priority: P2 Keywords: InRadar
Version: Safari 16   
Hardware: iPhone / iPad   
OS: iOS 16   

Description ik 2023-04-02 02:02:57 PDT
** Short description: **

On iPadOS, the window.blur event fires when the software keyboard is dismissed via the dedicated button (bottom right corner).

** Reproduce: **

1. On an iPad Mini (5th gen), open twitter log in and open a DM / chat
2. Connect with dev tools and paste the code below (logs focus and blur events to console)
2. Tap the message input at the bottom of the chat UI
3. Dismiss the keyboard using the dedicated button (bottom right corner)

The same steps can performed on this testcase I built for another issue (plz ignore the included steps ;))
https://testcase.rejh.nl/webkit-safe-area-inset-does-not-update/

** Code snippet: **

```
// Log focus and blur events to console
function log(eventName) {
  return (event) => console.log(eventName, event.target === event.currentTarget, event.target);
}
window.addEventListener('focus', e => log('focus')(e));
window.addEventListener('blur', e => log('blur')(e));
```

** What happens: **

The window.blur event fires. This may be problematic for sites using the Page Lifecycle API (https://developer.chrome.com/blog/page-lifecycle-api/) to determine the state of the page.

- Scrolling the page does not restore focus
- AFAIK you can't do it programatically (window.focus() does not, in face, focus the window) 
- Tapping somewhere in the page does restore focus. 

** Additional notes: **

1. I have only been able to reproduce this on my iPad Mini. I don't have an actual iPhone to test with at the moment, but I have noticed that the 'Done' button on iPhone behaves differently from the button on iPadOS. iPhone and iPad simulator seem to behave as expected (no window.blur event). 

2. It seems that there are a number of bugs related to the software keyboard, especially on iPadOS:
- iOS: safe-area-inset-bottom still set when keyboard is open: https://bugs.webkit.org/show_bug.cgi?id=217754
- iPadOS: viewport doesn't correctly restore when dismissing keyboard: https://bugs.webkit.org/show_bug.cgi?id=254861

Would be great to see this area getting some attention
Comment 1 Radar WebKit Bug Importer 2023-04-09 02:03:17 PDT
<rdar://problem/107802120>