Skip to content

Commit a3bec0d

Browse files
authored
feat: state (#721)
## 📜 Description Added `KeyboardController.state()` method. ## 💡 Motivation and Context To achieve a parity with `Keyboard` API from `react-native` I decided to add `KeyboardController.state()` method. By design it should act as `Keyboard.metrics()` method. The reason why I choose `state` name instead of `metrics` is because our data structure returns more like state rather than a metrics - instead of `height` and layout information we also return additional data, such as `duration` of last animation, `timestamp` when last open happened, `target` of focused input and in future potentially `type`, `appearance` and maybe other properties. ## 📢 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 --> ### JS - added `KeyboardController.state()` method; ### Docs - added a reference about `KeyboardController.state()` method; ## 🤔 How Has This Been Tested? Tested manually in example app. ## 📸 Screenshots (if appropriate): <img width="898" alt="image" src="https://github.com/user-attachments/assets/338b6d9d-e82a-4f69-a023-01a1f41c1d20"> ## 📝 Checklist - [x] CI successfully passed - [x] I added new mocks and corresponding unit-tests if library API was changed
1 parent 6ea4506 commit a3bec0d

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

docs/docs/api/keyboard-controller.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ if (KeyboardController.isVisible()) {
8484
}
8585
```
8686

87+
### `state`
88+
89+
```ts
90+
static state(): KeyboardEventData | null;
91+
```
92+
93+
This method returns the last keyboard state. It returns `null` if keyboard was not shown in the app yet.
94+
8795
### `setFocusTo`
8896

8997
```ts

jest/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const mock = {
5555
dismiss: jest.fn().mockReturnValue(Promise.resolve()),
5656
setFocusTo: jest.fn(),
5757
isVisible: jest.fn().mockReturnValue(false),
58+
state: jest.fn().mockReturnValue(null),
5859
},
5960
AndroidSoftInputModes: {
6061
SOFT_INPUT_ADJUST_NOTHING: 48,

src/module.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { KeyboardControllerNative, KeyboardEvents } from "./bindings";
22

3-
import type { KeyboardControllerModule } from "./types";
3+
import type { KeyboardControllerModule, KeyboardEventData } from "./types";
44

55
let isClosed = false;
6+
let lastEvent: KeyboardEventData | null = null;
67

7-
KeyboardEvents.addListener("keyboardDidHide", () => {
8+
KeyboardEvents.addListener("keyboardDidHide", (e) => {
89
isClosed = true;
10+
lastEvent = e;
911
});
1012

11-
KeyboardEvents.addListener("keyboardDidShow", () => {
13+
KeyboardEvents.addListener("keyboardDidShow", (e) => {
1214
isClosed = false;
15+
lastEvent = e;
1316
});
1417

1518
const dismiss = async (): Promise<void> => {
@@ -29,11 +32,13 @@ const dismiss = async (): Promise<void> => {
2932
});
3033
};
3134
const isVisible = () => !isClosed;
35+
const state = () => lastEvent;
3236

3337
export const KeyboardController: KeyboardControllerModule = {
3438
setDefaultMode: KeyboardControllerNative.setDefaultMode,
3539
setInputMode: KeyboardControllerNative.setInputMode,
3640
setFocusTo: KeyboardControllerNative.setFocusTo,
37-
dismiss: dismiss,
41+
dismiss,
3842
isVisible,
43+
state,
3944
};

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export type KeyboardControllerModule = {
122122
dismiss: () => Promise<void>;
123123
setFocusTo: (direction: Direction) => void;
124124
isVisible: () => boolean;
125+
state: () => KeyboardEventData | null;
125126
};
126127
export type KeyboardControllerNativeModule = {
127128
// android only

0 commit comments

Comments
 (0)