Skip to content

Commit f8da6c3

Browse files
thiagobrezokwasniewski
authored andcommitted
fix: remove keyboard listerners and mark as unsupported (#25)
* fix: remove keyboard listerners and mark as unsupported * chore: replace warn with warnOnce
1 parent 6fc08a1 commit f8da6c3

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

packages/react-native/Libraries/Components/Keyboard/Keyboard.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import LayoutAnimation from '../../LayoutAnimation/LayoutAnimation';
1515
import dismissKeyboard from '../../Utilities/dismissKeyboard';
1616
import Platform from '../../Utilities/Platform';
1717
import NativeKeyboardObserver from './NativeKeyboardObserver';
18+
import warnOnce from '../../Utilities/warnOnce';
1819

1920
export type KeyboardEventName = $Keys<KeyboardEventDefinitions>;
2021

@@ -114,6 +115,14 @@ class Keyboard {
114115
);
115116

116117
constructor() {
118+
if (Platform.isVisionOS) {
119+
warnOnce(
120+
'Keyboard-unavailable',
121+
'Keyboard is not available on visionOS platform. The system displays the keyboard in a separate window, leaving the app’s window unaffected by the keyboard’s appearance and disappearance',
122+
);
123+
return;
124+
}
125+
117126
this.addListener('keyboardDidShow', ev => {
118127
this._currentlyShowing = ev;
119128
});
@@ -151,6 +160,10 @@ class Keyboard {
151160
listener: (...$ElementType<KeyboardEventDefinitions, K>) => mixed,
152161
context?: mixed,
153162
): EventSubscription {
163+
if (Platform.isVisionOS) {
164+
return;
165+
}
166+
154167
return this._emitter.addListener(eventType, listener);
155168
}
156169

@@ -160,6 +173,10 @@ class Keyboard {
160173
* @param {string} eventType The native event string listeners are watching which will be removed.
161174
*/
162175
removeAllListeners<K: $Keys<KeyboardEventDefinitions>>(eventType: ?K): void {
176+
if (Platform.isVisionOS) {
177+
return;
178+
}
179+
163180
this._emitter.removeAllListeners(eventType);
164181
}
165182

packages/react-native/Libraries/Components/Keyboard/KeyboardAvoidingView.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import AccessibilityInfo from '../AccessibilityInfo/AccessibilityInfo';
2424
import View from '../View/View';
2525
import Keyboard from './Keyboard';
2626
import * as React from 'react';
27+
import warnOnce from '../../Utilities/warnOnce';
2728

2829
type Props = $ReadOnly<{|
2930
...ViewProps,
@@ -176,6 +177,13 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
176177

177178
componentDidMount(): void {
178179
if (Platform.OS === 'ios') {
180+
if (Platform.isVisionOS) {
181+
warnOnce(
182+
'KeyboardAvoidingView-unavailable',
183+
'KeyboardAvoidingView is not available on visionOS platform. The system displays the keyboard in a separate window, leaving the app’s window unaffected by the keyboard’s appearance and disappearance',
184+
);
185+
return;
186+
}
179187
this._subscriptions = [
180188
Keyboard.addListener('keyboardWillChangeFrame', this._onKeyboardChange),
181189
];
@@ -205,6 +213,16 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
205213
onLayout,
206214
...props
207215
} = this.props;
216+
217+
if (Platform.isVisionOS) {
218+
// KeyboardAvoidingView is not supported on VisionOS, so we return a simple View without the onLayout handler
219+
return (
220+
<View ref={this.viewRef} style={style} {...props}>
221+
{children}
222+
</View>
223+
);
224+
}
225+
208226
const bottomHeight = enabled === true ? this.state.bottom : 0;
209227
switch (behavior) {
210228
case 'height':

packages/react-native/React/CoreModules/RCTKeyboardObserver.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ @implementation RCTKeyboardObserver
2323

2424
- (void)startObserving
2525
{
26+
#if !TARGET_OS_VISION
2627
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
2728

2829
#define ADD_KEYBOARD_HANDLER(NAME, SELECTOR) [nc addObserver:self selector:@selector(SELECTOR:) name:NAME object:nil]
@@ -35,6 +36,7 @@ - (void)startObserving
3536
ADD_KEYBOARD_HANDLER(UIKeyboardDidChangeFrameNotification, keyboardDidChangeFrame);
3637

3738
#undef ADD_KEYBOARD_HANDLER
39+
#endif
3840
}
3941

4042
- (NSArray<NSString *> *)supportedEvents
@@ -51,9 +53,12 @@ - (void)startObserving
5153

5254
- (void)stopObserving
5355
{
56+
#if !TARGET_OS_VISION
5457
[[NSNotificationCenter defaultCenter] removeObserver:self];
58+
#endif
5559
}
5660

61+
#if !TARGET_OS_VISION
5762
// Bridge might be already invalidated by the time the keyboard is about to be dismissed.
5863
// This might happen, for example, when reload from the packager is performed.
5964
// Thus we need to check against nil here.
@@ -72,6 +77,7 @@ -(void)EVENT : (NSNotification *)notification
7277
IMPLEMENT_KEYBOARD_HANDLER(keyboardDidHide)
7378
IMPLEMENT_KEYBOARD_HANDLER(keyboardWillChangeFrame)
7479
IMPLEMENT_KEYBOARD_HANDLER(keyboardDidChangeFrame)
80+
#endif
7581

7682
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
7783
(const facebook::react::ObjCTurboModule::InitParams &)params

0 commit comments

Comments
 (0)