fix(footer): remove toolbar bottom padding if near bottom slot tabs or keyboard is open (#25746)

Co-authored-by: EinfachHans <EinfachHans@users.noreply.github.com>
This commit is contained in:
Amanda Johnston
2022-08-16 13:22:43 -05:00
committed by GitHub
parent 79c65dc382
commit bb37446032
13 changed files with 190 additions and 25 deletions

View File

@@ -0,0 +1,48 @@
import { win } from '../window';
/**
* Creates a controller that tracks and reacts to opening or closing the keyboard.
*
* @internal
* @param keyboardChangeCallback A function to call when the keyboard opens or closes.
*/
export const createKeyboardController = (
keyboardChangeCallback?: (keyboardOpen: boolean) => void
): KeyboardController => {
let keyboardWillShowHandler: (() => void) | undefined;
let keyboardWillHideHandler: (() => void) | undefined;
let keyboardVisible: boolean;
const init = () => {
keyboardWillShowHandler = () => {
keyboardVisible = true;
if (keyboardChangeCallback) keyboardChangeCallback(true);
};
keyboardWillHideHandler = () => {
keyboardVisible = false;
if (keyboardChangeCallback) keyboardChangeCallback(false);
};
win?.addEventListener('keyboardWillShow', keyboardWillShowHandler);
win?.addEventListener('keyboardWillHide', keyboardWillHideHandler);
};
const destroy = () => {
win?.removeEventListener('keyboardWillShow', keyboardWillShowHandler!);
win?.removeEventListener('keyboardWillHide', keyboardWillHideHandler!);
keyboardWillShowHandler = keyboardWillHideHandler = undefined;
};
const isKeyboardVisible = () => keyboardVisible;
init();
return { init, destroy, isKeyboardVisible };
};
export type KeyboardController = {
init: () => void;
destroy: () => void;
isKeyboardVisible: () => boolean;
};

View File

@@ -0,0 +1,24 @@
import { createKeyboardController } from '../keyboard-controller';
describe('Keyboard Controller', () => {
it('should update isKeyboardVisible', () => {
const keyboardCtrl = createKeyboardController();
window.dispatchEvent(new Event('keyboardWillShow'));
expect(keyboardCtrl.isKeyboardVisible()).toBe(true);
window.dispatchEvent(new Event('keyboardWillHide'));
expect(keyboardCtrl.isKeyboardVisible()).toBe(false);
});
it('should run the callback', () => {
const callbackMock = jest.fn();
createKeyboardController(callbackMock);
window.dispatchEvent(new Event('keyboardWillShow'));
expect(callbackMock).toHaveBeenCalledWith(true);
window.dispatchEvent(new Event('keyboardWillHide'));
expect(callbackMock).toHaveBeenCalledWith(false);
});
});