Commit e4e6e6e
authored
feat: keyboard tracking view (#1047)
## 📜 Description
Introduce compat `KeyboardTrackingView` layer.
## 💡 Motivation and Context
Starting from iOS beta 4 we got breaking changes that breaks
`react-native-keyboard-controller`.
### 1️⃣ `layer.presentation` doesn't update its values during animation
When we read `print("kv:
\(KeyboardViewLocator.shared.resolve()?.layer.presentation()?.frame.origin.y)")`
it will always give us values as "keyboard already opened":
```bash
kv: Optional(528.0)
kv: Optional(528.0)
kv: Optional(528.0)
kv: Optional(528.0)
kv: Optional(528.0)
kv: Optional(528.0)
kv: Optional(528.0)
```
So it will cause "instant" animations and it's not something that
developers expect to see.
### 2️⃣ `layer` doesn't contain the information about its `CAAnimation`
While this problem is not such a critical, because we use fallback
approach: if `animation` is `nil` then we just previous keyboard frame
without additional frame correction. But it'll produce unsynchronized
animation (we literally will come back to
`react-native-keyboard-controller@1.11` version or earlier). So
eventually we need to have an access to these variables.
<hr>
It's obvious that we need to get a tracking view to deliver smooth
animations. I had few ideas how to add it:
### 1️⃣ Use `Notification` approach
The first approach was to use keyboard notifications, put tracking view
outside of visible screen area and track it. It works, but it'll give
`CASpringAnimation` after interactive gesture dismissal (when keyboard
goes up). I've tried various ideas, like changing frame without
`UIView.animate` etc., but in all cases I got the same outcome:
- animation can be `nil`;
- animation may be `CASpringAnimation` instead of `CABasicAnimation`.
### 2️⃣ Use `inputAccessoryView` as tracking view
The other idea was to setup listener on `inputAccessoryView`.
In reality this view doesn't have any animation - I assume it's because
of the fact, that it's attached to keyboard and keyboard drives the
animation.
_**Theoretically**_ we could lookup to `superview` and track its
animation, but I didn't verify it - that solution would be pretty
complex because we had to inject `inputAccessoryView` for each
`TextInput`...
### 3️⃣ Use `keyboardLayoutGuide` for creating tracking view
The other idea to try was usage of `keyboardLayoutGuide`. The idea the
same as with `Notifications`. BUT! Fortunately that approach always
gives proper animations and I can see `CABasicAnimation` for interactive
dismissal case. This is how it looks if we make the view visible:
https://github.com/user-attachments/assets/efef1976-1940-4a98-82d9-114f10df4022
The benefit of this approach is that we always can see how a native view
behaves - react-native just replicates the native view.
One limitation is that `keyboardLayoutGuide` is available from iOS 15.
Moreover we use `usesBottomSafeArea` property, which is available
starting from iOS 17. Taking all these facts into consideration and
keeping in mind, that everything was pretty stable until iOS 26, I
decided to use old implementation for all iOS version < 26. I don't want
to break old code, but discovering bugs in new code/iOS is kind of
expected process 😅
<hr>
With the new concept of a compat layer (`KeyboardTrackingView`) we can
separate concepts and move all interpolation code into
`KeyboardTrackingView` class itself. As a proof of concept I fixed
interactive keyboard dismissal for iOS 26+ - now we can embed all math
for calculating keyboard position into this layer 😎 (so this PR will
automatically close
#975
- note it doesn't mean that I fully fixed interactive dismissal: this PR
is a first step into making `react-native-keyboard-controller` codebase
more predictable/enhanceable etc.):
https://github.com/user-attachments/assets/5dda7a96-42d1-43ad-8cbd-7ca24828d0d1
Closes
#975
## 📢 Changelog
<!-- High level overview of important changes -->
<!-- For example: fixed status bar manipulation; added new types
declarations; -->
<!-- If your changes don't affect one of platform/language below - then
remove this platform/language -->
### iOS
- created `KeyboardTrackingView` as a compat layer (iOS < 26 and iOS
26+)
- use `KeyboardTrackingView` in `KeyboardMovementObserver` and avoid
direct usages of `keyboardView` instance;
- move interactive interpolation code into `KeyboardTrackingView` from
`KeyboardMovementObserver`.
## 🤔 How Has This Been Tested?
Tested manually on:
- iPhone 6s (iOS 15.8, real device);
- iPhone 11 (iOS 26, real device)
- iPhone 16 Pro (iOS 26, real device)
- iPhone 14 Pro (iOS 18.4, real device)
- iPhone 15 Pro (iOS 17.5, simulator)
- iPhone 16 Pro (iOS 26, simulator)
## 📸 Screenshots (if appropriate):
|Before|After|
|-------|-----|
|<video
src="https://github.com/user-attachments/assets/e2c808dc-2aa5-4768-8974-4df429abba57">|<video
src="https://github.com/user-attachments/assets/04448303-be1a-4c0b-b705-fe0c936726ad">|
> captured with a fix for `did` events (dispatch them only after
keyboard finish its animation) for both before/after cases. Captured on
iOS 26 for better proof/visibility.
## 📝 Checklist
- [x] CI successfully passed
- [x] I added new mocks and corresponding unit-tests if library API was
changed1 parent 3ae5e72 commit e4e6e6e
File tree
2 files changed
+145
-19
lines changed- ios/observers
2 files changed
+145
-19
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
93 | 94 | | |
94 | 95 | | |
95 | 96 | | |
96 | | - | |
| 97 | + | |
97 | 98 | | |
98 | 99 | | |
99 | 100 | | |
| |||
115 | 116 | | |
116 | 117 | | |
117 | 118 | | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | 119 | | |
124 | | - | |
125 | | - | |
126 | | - | |
| 120 | + | |
127 | 121 | | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
133 | 126 | | |
134 | 127 | | |
135 | 128 | | |
| |||
192 | 185 | | |
193 | 186 | | |
194 | 187 | | |
| 188 | + | |
195 | 189 | | |
196 | 190 | | |
197 | 191 | | |
198 | | - | |
| 192 | + | |
199 | 193 | | |
200 | 194 | | |
201 | 195 | | |
| |||
251 | 245 | | |
252 | 246 | | |
253 | 247 | | |
254 | | - | |
| 248 | + | |
255 | 249 | | |
256 | 250 | | |
257 | 251 | | |
| |||
263 | 257 | | |
264 | 258 | | |
265 | 259 | | |
266 | | - | |
| 260 | + | |
267 | 261 | | |
268 | 262 | | |
269 | 263 | | |
270 | | - | |
| 264 | + | |
271 | 265 | | |
272 | 266 | | |
273 | 267 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
0 commit comments