diff --git a/docs/docs/api/keyboard-controller.md b/docs/docs/api/keyboard-controller.md index 6d3b2158b..4eb9342f5 100644 --- a/docs/docs/api/keyboard-controller.md +++ b/docs/docs/api/keyboard-controller.md @@ -70,6 +70,20 @@ The equivalent method from `react-native` relies on specific internal components In contrast, the described method enables keyboard dismissal for any focused input, extending functionality beyond the limitations of the default implementation. ::: +### `isVisible` + +```ts +static isVisible(): boolean; +``` + +This method returns `true` if keyboard is currently visible and `false` otherwise. + +```ts +if (KeyboardController.isVisible()) { + // do something +} +``` + ### `setFocusTo` ```ts diff --git a/jest/index.js b/jest/index.js index dbbe4cd63..55da80c89 100644 --- a/jest/index.js +++ b/jest/index.js @@ -51,6 +51,7 @@ const mock = { setDefaultMode: jest.fn(), dismiss: jest.fn().mockReturnValue(Promise.resolve()), setFocusTo: jest.fn(), + isVisible: jest.fn().mockReturnValue(false), }, AndroidSoftInputModes: { SOFT_INPUT_ADJUST_NOTHING: 48, diff --git a/src/module.ts b/src/module.ts index d66c210b3..dbc3d3b1d 100644 --- a/src/module.ts +++ b/src/module.ts @@ -2,19 +2,19 @@ import { KeyboardControllerNative, KeyboardEvents } from "./bindings"; import type { KeyboardControllerModule } from "./types"; -let isVisible = false; +let isClosed = false; KeyboardEvents.addListener("keyboardDidHide", () => { - isVisible = false; + isClosed = true; }); KeyboardEvents.addListener("keyboardDidShow", () => { - isVisible = true; + isClosed = false; }); const dismiss = async (): Promise => { return new Promise((resolve) => { - if (!isVisible) { + if (isClosed) { resolve(); return; @@ -28,10 +28,12 @@ const dismiss = async (): Promise => { KeyboardControllerNative.dismiss(); }); }; +const isVisible = () => !isClosed; export const KeyboardController: KeyboardControllerModule = { setDefaultMode: KeyboardControllerNative.setDefaultMode, setInputMode: KeyboardControllerNative.setInputMode, setFocusTo: KeyboardControllerNative.setFocusTo, dismiss: dismiss, + isVisible, }; diff --git a/src/types.ts b/src/types.ts index 8e712f412..b2801519c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -121,6 +121,7 @@ export type KeyboardControllerModule = { // all platforms dismiss: () => Promise; setFocusTo: (direction: Direction) => void; + isVisible: () => boolean; }; export type KeyboardControllerNativeModule = { // android only