KeyboardToolbar
KeyboardToolbar is a view that sticky to the keyboard and has next and previous buttons for switching between inputs as well as Done button for closing the keyboard.
This component is fully customizable and allows you to define any behavior for provided buttons and also allows you to render additional content.
Features
- Fully customizable UI 🎨: Tailor the appearance of the toolbar to match your app's design.
- Supports dark/light theme 🌓: Adapts to the theme settings of the user's device for a seamless experience.
- Extended accessibility support 🔍: Ensures that all users, including those with disabilities, can navigate through inputs effectively.
- Full control over the buttons behavior 🔧: Customize the actions triggered by the next, previous, and done buttons according to your needs.
- Extends ViewProps 📜: Supports all the props that
Viewcomponent has. - Compound component pattern 🔌: Mix and match sub-components for granular control over the toolbar's structure.
Compound Components
The new API uses sub-components as children of KeyboardToolbar. These allow for precise customization, such as conditional rendering of buttons or injecting custom elements.
<KeyboardToolbar.Background>
Renders a custom background (e.g., blur effect) that overlays the entire toolbar. Accepts any React node as children.
import { Platform } from "react-native";
import { KeyboardToolbar } from "react-native-keyboard-controller";
import { BlurView } from "@react-native-community/blur";
<KeyboardToolbar opacity="4F">
<KeyboardToolbar.Background>
<BlurView
blurAmount={32}
blurType={Platform.OS === "ios" ? "chromeMaterial" : "light"}
reducedTransparencyFallbackColor="white"
style={{ position: "absolute", top: 0, left: 0, bottom: 0, right: 0 }}
/>
</KeyboardToolbar.Background>
{/* Other sub-components */}
</KeyboardToolbar>;
Please, note, that you need to specify opacity prop for this prop to work. Because otherwise you will not see a blur effect.
<KeyboardToolbar.Content>
Renders a custom content (e.g., yours UI elements) in the middle of the toolbar. Accepts any React node as children.
import { KeyboardToolbar } from "react-native-keyboard-controller";
<KeyboardToolbar>
<KeyboardToolbar.Content>
{showAutoFill ? (
<AutoFillContacts onContactSelected={onContactSelected} />
) : null}
</KeyboardToolbar.Content>
{/* Other sub-components */}
</KeyboardToolbar>;
<KeyboardToolbar.Prev>
button
This property allows to render custom touchable component.
import { TouchableOpacity } from "react-native-gesture-handler";
import {
KeyboardToolbar,
KeyboardToolbarProps,
} from "react-native-keyboard-controller";
const CustomButton: KeyboardToolbarProps["button"] = ({
children,
onPress,
}) => <TouchableOpacity onPress={onPress}>{children}</TouchableOpacity>;
// ...
<KeyboardToolbar>
<KeyboardToolbar.Prev button={CustomButton} />
</KeyboardToolbar>;
icon
icon property allows to render custom icons.
import { Text } from "react-native";
import {
KeyboardToolbar,
KeyboardToolbarProps,
} from "react-native-keyboard-controller";
const Icon: KeyboardToolbarProps["icon"] = ({ type }) => {
return <Text>{type === "next" ? "⬇️" : "⬆️"}</Text>;
};
// ...
<KeyboardToolbar>
<KeyboardToolbar.Prev icon={Icon} />
</KeyboardToolbar>;
onPress
A callback that is called when the user presses the previous button. The callback receives an instance of GestureResponderEvent which can be used to cancel the default action (for advanced use-cases).
import { Platform } from "react-native";
import { KeyboardToolbar } from "react-native-keyboard-controller";
import { trigger } from "react-native-haptic-feedback";
const options = {
enableVibrateFallback: true,
ignoreAndroidSystemSettings: false,
};
const haptic = () =>
trigger(Platform.OS === "ios" ? "impactLight" : "keyboardTap", options);
// ...
<KeyboardToolbar>
<KeyboardToolbar.Prev onPress={haptic} />
</KeyboardToolbar>;
To prevent the default action, call e.preventDefault() inside the callback:
<KeyboardToolbar>
<KeyboardToolbar.Prev
onPress={(e) => {
// the focus will not be moved to the prev input
e.preventDefault();
}}
/>
</KeyboardToolbar>
<KeyboardToolbar.Next>
button
This property allows to render custom touchable component.
import { TouchableOpacity } from "react-native-gesture-handler";
import {
KeyboardToolbar,
KeyboardToolbarProps,
} from "react-native-keyboard-controller";
const CustomButton: KeyboardToolbarProps["button"] = ({
children,
onPress,
}) => <TouchableOpacity onPress={onPress}>{children}</TouchableOpacity>;
// ...
<KeyboardToolbar>
<KeyboardToolbar.Next button={CustomButton} />
</KeyboardToolbar>;
icon
icon property allows to render custom icons.
import { Text } from "react-native";
import {
KeyboardToolbar,
KeyboardToolbarProps,
} from "react-native-keyboard-controller";
const Icon: KeyboardToolbarProps["icon"] = ({ type }) => {
return <Text>{type === "next" ? "⬇️" : "⬆️"}</Text>;
};
// ...
<KeyboardToolbar>
<KeyboardToolbar.Next icon={Icon} />
</KeyboardToolbar>;
onPress
A callback that is called when the user presses the next button. The callback receives an instance of GestureResponderEvent which can be used to cancel the default action (for advanced use-cases).
import { Platform } from "react-native";
import { KeyboardToolbar } from "react-native-keyboard-controller";
import { trigger } from "react-native-haptic-feedback";
const options = {
enableVibrateFallback: true,
ignoreAndroidSystemSettings: false,
};
const haptic = () =>
trigger(Platform.OS === "ios" ? "impactLight" : "keyboardTap", options);
// ...
<KeyboardToolbar>
<KeyboardToolbar.Next onPress={haptic} />
</KeyboardToolbar>;
To prevent the default action, call e.preventDefault() inside the callback:
<KeyboardToolbar>
<KeyboardToolbar.Next
onPress={(e) => {
// the focus will not be moved to the next input
e.preventDefault();
}}
/>
</KeyboardToolbar>
<KeyboardToolbar.Done>
button
This property allows to render custom touchable component.
import { TouchableOpacity } from "react-native-gesture-handler";
import {
KeyboardToolbar,
KeyboardToolbarProps,
} from "react-native-keyboard-controller";
const CustomButton: KeyboardToolbarProps["button"] = ({
children,
onPress,
}) => <TouchableOpacity onPress={onPress}>{children}</TouchableOpacity>;
// ...
<KeyboardToolbar>
<KeyboardToolbar.Next button={CustomButton} />
</KeyboardToolbar>;
onPress
A callback that is called when the user presses the done button. The callback receives an instance of GestureResponderEvent which can be used to cancel the default action (for advanced use-cases).
import { Platform } from "react-native";
import { KeyboardToolbar } from "react-native-keyboard-controller";
import { trigger } from "react-native-haptic-feedback";
const options = {
enableVibrateFallback: true,
ignoreAndroidSystemSettings: false,
};
const haptic = () =>
trigger(Platform.OS === "ios" ? "impactLight" : "keyboardTap", options);
// ...
<KeyboardToolbar>
<KeyboardToolbar.Done onPress={haptic} />
</KeyboardToolbar>;
To prevent the default action, call e.preventDefault() inside the callback:
<KeyboardToolbar>
<KeyboardToolbar.Done
onPress={(e) => {
// keyboard will not be dismissed, since we cancelled the default action
e.preventDefault();
}}
/>
</KeyboardToolbar>