Skip to content

Commit

Permalink
feat: merge offset properties for KeyboardToolbar (#702)
Browse files Browse the repository at this point in the history
## 📜 Description

Added an ability to customize `offset` for `KeyboardToolbar`.

## 💡 Motivation and Context

If you use `KeyboardToolbar` with `bottom-tabs` (or in general if the
bottom of the screen is not touching the bottom of window) then
`KeyboardToolbar` will not be sticky to the keyboard.

To fix that we have to position toolbar in the end of the window. Since
`KeyboardToolbar` is based on `KeyboardStickyView` (and
`KeyboardStickyView` already has `offset` property) I decided to merge
these propeprties.

Now `KeyboardToolbar` accepts `offset` property and merges it with
default value (for closed state).

Closes
#622

## 📢 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

- mention that `KeyboardToolbar` inherits properties from
`KeyboardStickyView`.

### JS

- merge `offset` property with default value and pass it to
`keyboardStickyView`;

## 🤔 How Has This Been Tested?

Tested manually in reproduction example.

## 📸 Screenshots (if appropriate):

|Before|After|
|-------|-----|

|![image](https://github.com/user-attachments/assets/79bd6116-a73f-44c6-ad98-14dd1e08d350)|![image](https://github.com/user-attachments/assets/c98dda17-dc0b-44c7-89ef-b146eaafb42f)|

## 📝 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 21, 2024
1 parent 74124b8 commit a300735
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/docs/api/components/keyboard-toolbar/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ import toolbar from "./toolbar.lottie.json";

Inherits [View Props](https://reactnative.dev/docs/view#props).

### [`KeyboardStickyViewProps`](./keyboard-sticky-view)

Inherits [KeyboardStickyViewProps](./keyboard-sticky-view).

### `button`

This property allows to render custom touchable component for next, previous and done button.
Expand Down
9 changes: 7 additions & 2 deletions src/components/KeyboardToolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Button from "./Button";
import { colors } from "./colors";

import type { HEX, KeyboardToolbarTheme } from "./types";
import type { KeyboardStickyViewProps } from "../KeyboardStickyView";
import type { ReactNode } from "react";
import type { GestureResponderEvent, ViewProps } from "react-native";

Expand Down Expand Up @@ -53,7 +54,7 @@ export type KeyboardToolbarProps = Omit<
* A value for container opacity in hexadecimal format (e.g. `ff`). Default value is `ff`.
*/
opacity?: HEX;
};
} & Pick<KeyboardStickyViewProps, "offset">;

const TEST_ID_KEYBOARD_TOOLBAR = "keyboard.toolbar";
const TEST_ID_KEYBOARD_TOOLBAR_PREVIOUS = `${TEST_ID_KEYBOARD_TOOLBAR}.previous`;
Expand All @@ -63,7 +64,6 @@ const TEST_ID_KEYBOARD_TOOLBAR_DONE = `${TEST_ID_KEYBOARD_TOOLBAR}.done`;

const KEYBOARD_TOOLBAR_HEIGHT = 42;
const DEFAULT_OPACITY: HEX = "FF";
const offset = { closed: KEYBOARD_TOOLBAR_HEIGHT };

/**
* `KeyboardToolbar` is a component that is shown above the keyboard with `Prev`/`Next` and
Expand All @@ -81,6 +81,7 @@ const KeyboardToolbar: React.FC<KeyboardToolbarProps> = ({
onDoneCallback,
blur = null,
opacity = DEFAULT_OPACITY,
offset: { closed = 0, opened = 0 } = {},
...rest
}) => {
const colorScheme = useColorScheme();
Expand Down Expand Up @@ -111,6 +112,10 @@ const KeyboardToolbar: React.FC<KeyboardToolbarProps> = ({
],
[colorScheme, opacity, theme],
);
const offset = useMemo(
() => ({ closed: closed + KEYBOARD_TOOLBAR_HEIGHT, opened }),
[closed, opened],
);
const ButtonContainer = button || Button;
const IconContainer = icon || Arrow;

Expand Down

0 comments on commit a300735

Please sign in to comment.