-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## 📜 Description Added `useKeyboardController` hook that returns `setEnabled` + `enabled` (which allows to disable and enable module on demand). ## 💡 Motivation and Context Fixes #233 #198 ## 📢 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 `useKeyboardController` hook that gives an access to `setEnabled` method and `enabled` variable (that indicates current state); - added `enabled` property to specs; - manage `enabled` property via state (from react) and passing methods via context to change the value of this variable; - apply and revert monkey patch depending on `enabled` value. ### iOS - paper: added property setter that calls `mount`/`unmount` depends on the value that comes; - fabric: detect prop changes in `updateProps` method and call `mount`/`unmount` depends on the value that comes; ### Android - go to `edge-to-edge` when prop is set (before we did it in `onAttachedToWindow` method); - enable `edge-to-edge` mode if `onAttachedToWindow` is called more than one time; ## 🤔 How Has This Been Tested? Tested on: - Pixel 3a (Android 13, emulator) - Pixel 6 Pro (Android 9, emulator) - Pixel 7 Pro (Android 13, real device); - Xiaomi Redmi Note 5 Pro (Android 9, real device); - iPhone 14 Pro (iOS 16.4, iOS 17, simulator); - iPhone 6S (iOS 15.6, real device). ## 📸 Screenshots (if appropriate): |Android|iOS| |--------|---| |<video src="https://github.com/kirillzyusko/react-native-keyboard-controller/assets/22820318/6fcece94-8252-4fc8-9ab5-6d9b946168e7">|<video src="https://github.com/kirillzyusko/react-native-keyboard-controller/assets/22820318/9e91d79f-cf42-4074-aeed-0f063896e500">| ## 📝 Checklist - [x] CI successfully passed
- Loading branch information
1 parent
91a06d0
commit ef0f625
Showing
38 changed files
with
366 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
FabricExample/src/screens/Examples/EnabledDisabled/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react'; | ||
import { Button, View } from 'react-native'; | ||
import { useKeyboardController } from 'react-native-keyboard-controller'; | ||
import KeyboardAnimationTemplate from '../../../components/KeyboardAnimation'; | ||
|
||
export default function EnabledDisabled() { | ||
const { enabled, setEnabled } = useKeyboardController(); | ||
|
||
return ( | ||
<View style={{ flex: 1, paddingTop: 50 }}> | ||
<Button | ||
title={enabled ? 'Enabled' : 'Disabled'} | ||
onPress={() => setEnabled(!enabled)} | ||
/> | ||
<KeyboardAnimationTemplate /> | ||
</View> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"label": "⌨️ Keyboard", | ||
"position": 1 | ||
} |
File renamed without changes.
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"label": "📂 Module", | ||
"position": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
keywords: [react-native-keyboard-controller, useKeyboardController, enabled, disabled, setEnabled] | ||
--- | ||
|
||
# useKeyboardController | ||
|
||
`useKeyboardController` is a hook which gives an access to the state of the `react-native-keyboard-controller` library. It return two values: | ||
|
||
- `enabled` - boolean value which indicates whether the library is enabled in app; | ||
- `setEnabled` - function that changes state of `enabled` property. | ||
|
||
This hook can be handy in situations when your app is relying on default window resizing behavior on Android. Once the module is enabled - it moves the app in [edge-to-edge](https://developer.android.com/training/gestures/edge-to-edge) and prevents window from being resized (works as iOS). However if you need default Android behavior you can disable this module where needed and make a gradual integration of this library into your application. | ||
|
||
:::caution Use it only when you really need it | ||
Nonetheless that you can fallback to default Android behavior I still strongly recommend you not to go with this approach just because you'll loose all attractiveness of smooth animated keyboard transitions and your app will not look as great as it possibly can. | ||
|
||
Consider to use [KeyboardAvoidingView](../../components/keyboard-avoiding-view.mdx) which also resize the window, but does it with beautiful animated transitions that makes your interactions with app smooth and pleasant. | ||
::: | ||
|
||
## Example | ||
|
||
```tsx | ||
import { useKeyboardController } from "react-native-keyboard-controller"; | ||
|
||
const { enabled, setEnabled } = useKeyboardController(); | ||
|
||
setEnabled(false); | ||
``` | ||
|
||
Also have a look on [example](https://github.com/kirillzyusko/react-native-keyboard-controller/tree/main/example) app for more comprehensive usage. | ||
|
||
## Using with class component | ||
|
||
```tsx | ||
import { | ||
KeyboardController, | ||
KeyboardContext, | ||
AndroidSoftInputModes, | ||
} from "react-native-keyboard-controller"; | ||
|
||
class KeyboardAnimation extends React.PureComponent { | ||
// 1. use context value | ||
static contextType = KeyboardContext; | ||
|
||
componentDidMount() { | ||
// 2. get an access to `enabled` and `setEnabled` props | ||
const { enabled, setEnabled } = this.context; | ||
|
||
// 3. disable a module on demand in your app | ||
setEnabled(false); | ||
} | ||
} | ||
``` |
Oops, something went wrong.