diff --git a/core/src/utils/browser/index.ts b/core/src/utils/browser/index.ts index 5b3e914a45..ed70843de1 100644 --- a/core/src/utils/browser/index.ts +++ b/core/src/utils/browser/index.ts @@ -20,6 +20,26 @@ * Note: Code inside of this if-block will * not run in an SSR environment. */ -export const win: Window | undefined = typeof window !== 'undefined' ? window : undefined; + +/** + * Even listeners on the window typically expect + * Event types for the listener parameter. If you want to listen + * on the window for certain CustomEvent types you can add that definition + * here as long as you are using the "win" utility below. + */ +type IonicWindow = Window & { + addEventListener( + type: 'ionKeyboardDidShow', + listener: (ev: CustomEvent<{ keyboardHeight: number }>) => void, + options?: boolean | AddEventListenerOptions + ): void; + removeEventListener( + type: 'ionKeyboardDidShow', + listener: (ev: CustomEvent<{ keyboardHeight: number }>) => void, + options?: boolean | AddEventListenerOptions + ): void; +}; + +export const win: IonicWindow | undefined = typeof window !== 'undefined' ? window : undefined; export const doc: Document | undefined = typeof document !== 'undefined' ? document : undefined; diff --git a/core/src/utils/input-shims/hacks/scroll-assist.ts b/core/src/utils/input-shims/hacks/scroll-assist.ts index 93d84d8090..1b35367ce9 100644 --- a/core/src/utils/input-shims/hacks/scroll-assist.ts +++ b/core/src/utils/input-shims/hacks/scroll-assist.ts @@ -13,18 +13,6 @@ let currentPadding = 0; const SKIP_SCROLL_ASSIST = 'data-ionic-skip-scroll-assist'; -/** - * addEventListener typically expects an Event - * type in the event listener callbacks. In this case - * we have a CustomEvent, so we update the window event map - * to register our specific event as well as the type of that event. - */ -declare global { - interface WindowEventMap { - ionKeyboardDidShow: CustomEvent<{ keyboardHeight: number }>; - } -} - export const enableScrollAssist = ( componentEl: HTMLElement, inputEl: HTMLInputElement | HTMLTextAreaElement, @@ -135,7 +123,7 @@ export const enableScrollAssist = ( */ const focusOut = () => { hasKeyboardBeenPresentedForTextField = false; - window.removeEventListener('ionKeyboardDidShow', keyboardShow); + win?.removeEventListener('ionKeyboardDidShow', keyboardShow); componentEl.removeEventListener('focusout', focusOut, true); }; @@ -166,7 +154,7 @@ export const enableScrollAssist = ( platformHeight ); - window.addEventListener('ionKeyboardDidShow', keyboardShow); + win?.addEventListener('ionKeyboardDidShow', keyboardShow); componentEl.addEventListener('focusout', focusOut, true); }; @@ -174,7 +162,7 @@ export const enableScrollAssist = ( return () => { componentEl.removeEventListener('focusin', focusIn, true); - window.removeEventListener('ionKeyboardDidShow', keyboardShow); + win?.removeEventListener('ionKeyboardDidShow', keyboardShow); componentEl.removeEventListener('focusout', focusOut, true); }; };