Skip to content

Commit

Permalink
fix: updated android keyboard handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhom committed Jun 29, 2021
1 parent 177bf4c commit f53306d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 41 deletions.
97 changes: 58 additions & 39 deletions src/components/bottomSheet/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
SHEET_STATE,
SCROLLABLE_STATE,
KEYBOARD_BLUR_BEHAVIOR,
KEYBOARD_DISMISS_THRESHOLD,
KEYBOARD_INPUT_MODE,
SCROLLABLE_TYPE,
WINDOW_HEIGHT,
} from '../../constants';
Expand All @@ -65,6 +65,7 @@ import {
DEFAULT_ANIMATE_ON_MOUNT,
DEFAULT_KEYBOARD_BEHAVIOR,
DEFAULT_KEYBOARD_BLUR_BEHAVIOR,
DEFAULT_KEYBOARD_INPUT_MODE,
INITIAL_CONTAINER_HEIGHT,
INITIAL_HANDLE_HEIGHT,
INITIAL_POSITION,
Expand Down Expand Up @@ -112,6 +113,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
// keyboard
keyboardBehavior = DEFAULT_KEYBOARD_BEHAVIOR,
keyboardBlurBehavior = DEFAULT_KEYBOARD_BLUR_BEHAVIOR,
android_keyboardInputMode = DEFAULT_KEYBOARD_INPUT_MODE,

// layout
handleHeight: _providedHandleHeight,
Expand Down Expand Up @@ -282,19 +284,27 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
/**
* Returns keyboard height that in the root container.
*/
const getKeyboardHeightInContainer = useWorkletCallback(
() =>
$modal
? Math.abs(
animatedKeyboardHeight.value -
Math.abs(bottomInset - animatedContainerOffset.value.bottom)
)
: Math.abs(
animatedKeyboardHeight.value -
animatedContainerOffset.value.bottom
),
[$modal, bottomInset]
);
const getKeyboardHeightInContainer = useWorkletCallback(() => {
/**
* if android software input mode is not `adjustPan`, than keyboard
* height will be 0 all the time.
*/
if (
Platform.OS === 'android' &&
android_keyboardInputMode === KEYBOARD_INPUT_MODE.adjustResize
) {
return 0;
}

return $modal
? Math.abs(
animatedKeyboardHeight.value -
Math.abs(bottomInset - animatedContainerOffset.value.bottom)
)
: Math.abs(
animatedKeyboardHeight.value - animatedContainerOffset.value.bottom
);
}, [$modal, bottomInset]);
//#endregion

//#region state/dynamic variables
Expand Down Expand Up @@ -568,11 +578,26 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
},
[_providedOnAnimate, animatedSnapPoints, animatedCurrentIndex]
);
const animateToPositionCompleted = useWorkletCallback(() => {
animatedAnimationState.value = ANIMATION_STATE.STOPPED;
animatedNextPosition.value = Number.NEGATIVE_INFINITY;
animatedNextPositionIndex.value = Number.NEGATIVE_INFINITY;
});
const animateToPositionCompleted = useWorkletCallback(
function animateToPositionCompleted(isFinished: boolean) {
if (!isFinished) {
return;
}
runOnJS(print)({
component: BottomSheet.name,
method: animateToPositionCompleted.name,
params: {
animatedCurrentIndex: animatedCurrentIndex.value,
animatedNextPosition: animatedNextPosition.value,
animatedNextPositionIndex: animatedNextPositionIndex.value,
},
});
animatedAnimationState.value = ANIMATION_STATE.STOPPED;
animatedCurrentIndex.value = animatedNextPositionIndex.value;
animatedNextPosition.value = Number.NEGATIVE_INFINITY;
animatedNextPositionIndex.value = Number.NEGATIVE_INFINITY;
}
);
const animateToPosition = useWorkletCallback(
function animateToPosition(
position: number,
Expand Down Expand Up @@ -1002,22 +1027,6 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
context.isScrollablePositionLocked = false;
}

/**
* dismiss the keyboard when panning down, when:
* - keyboard is shown.
* - distance is more than threshold.
* - scrollable content is on the top.
* -
*/
if (
animatedKeyboardState.value === KEYBOARD_STATE.SHOWN &&
translationY > KEYBOARD_DISMISS_THRESHOLD &&
scrollableContentOffsetY.value === 0 &&
Platform.OS === 'android'
) {
runOnJS(Keyboard.dismiss)();
}

/**
* over-drag implementation.
*/
Expand Down Expand Up @@ -1405,7 +1414,6 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
animatedNextPositionIndex.value = -1;
} else {
nextPosition = animatedSnapPoints.value[_providedIndex];
animatedNextPositionIndex.value = _providedIndex;
}

/**
Expand Down Expand Up @@ -1434,7 +1442,6 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
if (animateOnMount) {
animateToPosition(nextPosition);
} else {
animatedNextPosition.value = nextPosition;
animatedPosition.value = nextPosition;
}
isAnimatedOnMount.value = true;
Expand Down Expand Up @@ -1508,7 +1515,14 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
* if new keyboard state is hidden and blur behavior is none, then exit the method
*/
(_keyboardState === KEYBOARD_STATE.HIDDEN &&
keyboardBlurBehavior === KEYBOARD_BLUR_BEHAVIOR.none)
keyboardBlurBehavior === KEYBOARD_BLUR_BEHAVIOR.none) ||
/**
* if platform is android and the input mode is resize, then exit the method
*/
(Platform.OS === 'android' &&
(keyboardBehavior === KEYBOARD_BEHAVIOR.interactive ||
keyboardBehavior === KEYBOARD_BEHAVIOR.none) &&
android_keyboardInputMode === KEYBOARD_INPUT_MODE.adjustResize)
) {
return;
}
Expand All @@ -1528,7 +1542,12 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
const nextPosition = getNextPosition();
animateToPosition(nextPosition, 0, animationConfigs);
},
[keyboardBlurBehavior, getNextPosition]
[
keyboardBehavior,
keyboardBlurBehavior,
android_keyboardInputMode,
getNextPosition,
]
);

/**
Expand Down
8 changes: 7 additions & 1 deletion src/components/bottomSheet/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Animated, { Easing } from 'react-native-reanimated';
import {
KEYBOARD_BEHAVIOR,
KEYBOARD_BLUR_BEHAVIOR,
KEYBOARD_INPUT_MODE,
WINDOW_HEIGHT,
} from '../../constants';
import { exp } from '../../utilities/easingExp';
Expand All @@ -25,8 +26,11 @@ const DEFAULT_ENABLE_HANDLE_PANNING_GESTURE = true;
const DEFAULT_ENABLE_OVER_DRAG = true;
const DEFAULT_ENABLE_PAN_DOWN_TO_CLOSE = false;
const DEFAULT_ANIMATE_ON_MOUNT = false;

// keyboard
const DEFAULT_KEYBOARD_BEHAVIOR = KEYBOARD_BEHAVIOR.none;
const DEFAULT_KEYBOARD_BLUR_BEHAVIOR = KEYBOARD_BLUR_BEHAVIOR.none;
const DEFAULT_KEYBOARD_INPUT_MODE = KEYBOARD_INPUT_MODE.adjustPan;

// initial values
const INITIAL_SNAP_POINT = -999;
Expand All @@ -51,9 +55,11 @@ export {
DEFAULT_ENABLE_OVER_DRAG,
DEFAULT_ENABLE_PAN_DOWN_TO_CLOSE,
DEFAULT_ANIMATE_ON_MOUNT,
// keyboard
DEFAULT_KEYBOARD_BEHAVIOR,
DEFAULT_KEYBOARD_BLUR_BEHAVIOR,
// initial
DEFAULT_KEYBOARD_INPUT_MODE,
// layout
INITIAL_POSITION,
INITIAL_CONTAINER_HEIGHT,
INITIAL_CONTAINER_OFFSET,
Expand Down
9 changes: 9 additions & 0 deletions src/components/bottomSheet/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { BottomSheetBackgroundProps } from '../bottomSheetBackground';
import type {
KEYBOARD_BEHAVIOR,
KEYBOARD_BLUR_BEHAVIOR,
KEYBOARD_INPUT_MODE,
} from '../../constants';

export interface BottomSheetProps
Expand Down Expand Up @@ -152,6 +153,14 @@ export interface BottomSheetProps
* - `restore`: restore sheet position.
*/
keyboardBlurBehavior?: keyof typeof KEYBOARD_BLUR_BEHAVIOR;
/**
* Defines keyboard input mode for Android only.
* @link {https://developer.android.com/guide/topics/manifest/activity-element#wsoft}
* @type `adjustPan` | `adjustResize`
* @default `adjustPan`
*/
android_keyboardInputMode?: keyof typeof KEYBOARD_INPUT_MODE;

//#endregion

/**
Expand Down
6 changes: 6 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ const KEYBOARD_BLUR_BEHAVIOR = {
restore: 'restore',
} as const;

const KEYBOARD_INPUT_MODE = {
adjustPan: 'adjustPan',
adjustResize: 'adjustResize',
} as const;

const KEYBOARD_DISMISS_THRESHOLD = 12.5;

export {
Expand All @@ -88,5 +93,6 @@ export {
MODAL_STACK_BEHAVIOR,
KEYBOARD_BEHAVIOR,
KEYBOARD_BLUR_BEHAVIOR,
KEYBOARD_INPUT_MODE,
KEYBOARD_DISMISS_THRESHOLD,
};
2 changes: 1 addition & 1 deletion src/utilities/animate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const animate = (
point: number,
configs: Animated.WithSpringConfig | Animated.WithTimingConfig,
velocity: number = 0,
onComplete?: () => void
onComplete?: (isFinished: boolean) => void
) => {
'worklet';

Expand Down

0 comments on commit f53306d

Please sign in to comment.