Skip to content

Commit

Permalink
feat: new KeyboardController.isVisible() method (#694)
Browse files Browse the repository at this point in the history
## 📜 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
  • Loading branch information
kirillzyusko authored Nov 18, 2024
1 parent 80d8972 commit 972787d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
14 changes: 14 additions & 0 deletions docs/docs/api/keyboard-controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions jest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
return new Promise((resolve) => {
if (!isVisible) {
if (isClosed) {
resolve();

return;
Expand All @@ -28,10 +28,12 @@ const dismiss = async (): Promise<void> => {
KeyboardControllerNative.dismiss();
});
};
const isVisible = () => !isClosed;

export const KeyboardController: KeyboardControllerModule = {
setDefaultMode: KeyboardControllerNative.setDefaultMode,
setInputMode: KeyboardControllerNative.setInputMode,
setFocusTo: KeyboardControllerNative.setFocusTo,
dismiss: dismiss,
isVisible,
};
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export type KeyboardControllerModule = {
// all platforms
dismiss: () => Promise<void>;
setFocusTo: (direction: Direction) => void;
isVisible: () => boolean;
};
export type KeyboardControllerNativeModule = {
// android only
Expand Down

0 comments on commit 972787d

Please sign in to comment.