(fn: QueuedCallback)
| 74 | * Guarantees an invocation if an expected transition did not finish within 300ms. |
| 75 | */ |
| 76 | export function runAfterKeyboard(fn: QueuedCallback): () => void { |
| 77 | // Flush synchronously when keyboard is unsupported. This is default for non-touch devices |
| 78 | // or devices which did not open their OSK within our opening timeout. |
| 79 | if (!supportsKeyboard()) { |
| 80 | return (fn(false), () => {}); |
| 81 | } |
| 82 | |
| 83 | // Wait two frames to see if focus lands on an input. |
| 84 | let frame = window.requestAnimationFrame(() => { |
| 85 | frame = window.requestAnimationFrame(() => { |
| 86 | let activeElement = document.hasFocus() ? getActiveElement() : null; |
| 87 | let willKeyboardOpen = willOpenKeyboard(activeElement); |
| 88 | |
| 89 | // If keyboard won't change, call the function immediately. |
| 90 | if (isKeyboardOpen() === willKeyboardOpen) { |
| 91 | return fn(willKeyboardOpen); |
| 92 | } |
| 93 | |
| 94 | // On close, fire immediately since consumers may assert the ICB. |
| 95 | if (isKeyboardOpen() && !willKeyboardOpen) { |
| 96 | return fn(willKeyboardOpen); |
| 97 | } |
| 98 | |
| 99 | resizeCallbacks.add(fn); |
| 100 | |
| 101 | if (!listenersByWindow.has(window)) { |
| 102 | listenersByWindow.set(window, setupGlobalListeners()); |
| 103 | } |
| 104 | }); |
| 105 | }); |
| 106 | |
| 107 | return () => { |
| 108 | window.cancelAnimationFrame(frame); |
| 109 | resizeCallbacks.delete(fn); |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Delays a callback execution until the on-screen keyboard has finished its transition. |
no test coverage detected