mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
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:
48
core/src/utils/keyboard/keyboard-controller.ts
Normal file
48
core/src/utils/keyboard/keyboard-controller.ts
Normal 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;
|
||||
};
|
||||
24
core/src/utils/keyboard/test/keyboard-controller.spec.ts
Normal file
24
core/src/utils/keyboard/test/keyboard-controller.spec.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user