Skip to content

Commit 972787d

Browse files
authored
feat: new KeyboardController.isVisible() method (#694)
## 📜 Description Added `KeyboardController.isVisible()` method. ## 💡 Motivation and Context I decided to match `Keyboard` API from `react-native` more closely, and I thought that `isVisible()` method can be useful in many places. So in this PR I'm adding `isVisible` method. ## 📢 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 --> ### Docs - added API spec for new method. ### JS - added `isVisible` method to `KeyboardController` API. ## 🤔 How Has This Been Tested? Tested manually. ## 📝 Checklist - [x] CI successfully passed - [x] I added new mocks and corresponding unit-tests if library API was changed
1 parent 80d8972 commit 972787d

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

docs/docs/api/keyboard-controller.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ The equivalent method from `react-native` relies on specific internal components
7070
In contrast, the described method enables keyboard dismissal for any focused input, extending functionality beyond the limitations of the default implementation.
7171
:::
7272

73+
### `isVisible`
74+
75+
```ts
76+
static isVisible(): boolean;
77+
```
78+
79+
This method returns `true` if keyboard is currently visible and `false` otherwise.
80+
81+
```ts
82+
if (KeyboardController.isVisible()) {
83+
// do something
84+
}
85+
```
86+
7387
### `setFocusTo`
7488

7589
```ts

jest/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const mock = {
5151
setDefaultMode: jest.fn(),
5252
dismiss: jest.fn().mockReturnValue(Promise.resolve()),
5353
setFocusTo: jest.fn(),
54+
isVisible: jest.fn().mockReturnValue(false),
5455
},
5556
AndroidSoftInputModes: {
5657
SOFT_INPUT_ADJUST_NOTHING: 48,

src/module.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ import { KeyboardControllerNative, KeyboardEvents } from "./bindings";
22

33
import type { KeyboardControllerModule } from "./types";
44

5-
let isVisible = false;
5+
let isClosed = false;
66

77
KeyboardEvents.addListener("keyboardDidHide", () => {
8-
isVisible = false;
8+
isClosed = true;
99
});
1010

1111
KeyboardEvents.addListener("keyboardDidShow", () => {
12-
isVisible = true;
12+
isClosed = false;
1313
});
1414

1515
const dismiss = async (): Promise<void> => {
1616
return new Promise((resolve) => {
17-
if (!isVisible) {
17+
if (isClosed) {
1818
resolve();
1919

2020
return;
@@ -28,10 +28,12 @@ const dismiss = async (): Promise<void> => {
2828
KeyboardControllerNative.dismiss();
2929
});
3030
};
31+
const isVisible = () => !isClosed;
3132

3233
export const KeyboardController: KeyboardControllerModule = {
3334
setDefaultMode: KeyboardControllerNative.setDefaultMode,
3435
setInputMode: KeyboardControllerNative.setInputMode,
3536
setFocusTo: KeyboardControllerNative.setFocusTo,
3637
dismiss: dismiss,
38+
isVisible,
3739
};

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export type KeyboardControllerModule = {
121121
// all platforms
122122
dismiss: () => Promise<void>;
123123
setFocusTo: (direction: Direction) => void;
124+
isVisible: () => boolean;
124125
};
125126
export type KeyboardControllerNativeModule = {
126127
// android only

0 commit comments

Comments
 (0)