Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(modal): move keyboard behavior for gestures into component #24650

Merged
merged 1 commit into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 8 additions & 41 deletions core/src/components/modal/gestures/sheet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Animation } from '../../../interface';
import { GestureDetail, createGesture } from '../../../utils/gesture';
import { clamp, raf } from '../../../utils/helpers';
import { KEYBOARD_DID_OPEN } from '../../../utils/keyboard/keyboard';
import { getBackdropValueForSheet } from '../utils';

export const createSheetGesture = (
Expand Down Expand Up @@ -43,27 +42,6 @@ export const createSheetGesture = (
const backdropAnimation = animation.childAnimations.find(ani => ani.id === 'backdropAnimation');
const maxBreakpoint = breakpoints[breakpoints.length - 1];

const keyboardOpenCallback = () => {
/**
* When the native keyboard is opened and the webview
* is resized, the gesture implementation will become unresponsive
* and enter a free-scroll mode.
*
* When the keyboard is opened, we disable the gesture for
* a single frame and re-enable once the contents have repositioned
* from the keyboard placement.
*/
gesture.enable(false);
raf(() => {
gesture.enable(true)
});
};

/* tslint:disable-next-line */
if (typeof window !== 'undefined') {
window.addEventListener(KEYBOARD_DID_OPEN, keyboardOpenCallback);
}

/**
* After the entering animation completes,
* we need to set the animation to go from
Expand Down Expand Up @@ -115,16 +93,13 @@ export const createSheetGesture = (
contentEl.scrollY = false;
}

/* tslint:disable-next-line */
if (typeof document !== 'undefined') {
const activeElement = baseEl.ownerDocument.activeElement as HTMLElement;
if (activeElement.matches('input,ion-input,textarea,ion-textarea')) {
raf(() => {
// Dismisses the open keyboard when the sheet drag gesture is started.
activeElement.blur();
});
}
}
raf(() => {
/**
* Dismisses the open keyboard when the sheet drag gesture is started.
* Sets the focus onto the modal element.
*/
baseEl.focus();
});

animation.progressStart(true, 1 - currentBreakpoint);
};
Expand Down Expand Up @@ -234,13 +209,6 @@ export const createSheetGesture = (
}
};

const onDestroy = () => {
/* tslint:disable-next-line */
if (typeof window !== 'undefined') {
window.removeEventListener(KEYBOARD_DID_OPEN, keyboardOpenCallback);
}
}

const gesture = createGesture({
el: wrapperEl,
gestureName: 'modalSheet',
Expand All @@ -250,8 +218,7 @@ export const createSheetGesture = (
canStart,
onStart,
onMove,
onEnd,
onDestroy
onEnd
});
return gesture;
};
31 changes: 31 additions & 0 deletions core/src/components/modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getIonMode } from '../../global/ionic-global';
import { Animation, AnimationBuilder, ComponentProps, ComponentRef, FrameworkDelegate, Gesture, ModalAttributes, OverlayEventDetail, OverlayInterface } from '../../interface';
import { CoreDelegate, attachComponent, detachComponent } from '../../utils/framework-delegate';
import { raf } from '../../utils/helpers';
import { KEYBOARD_DID_OPEN } from '../../utils/keyboard/keyboard';
import { BACKDROP, activeAnimations, dismiss, eventMethod, prepareOverlay, present } from '../../utils/overlays';
import { getClassMap } from '../../utils/theme';
import { deepReady } from '../../utils/transition';
Expand Down Expand Up @@ -44,6 +45,7 @@ export class Modal implements ComponentInterface, OverlayInterface {
private currentBreakpoint?: number;
private wrapperEl?: HTMLElement;
private backdropEl?: HTMLIonBackdropElement;
private keyboardOpenCallback?: () => void;

private inline = false;
private workingDelegate?: FrameworkDelegate;
Expand Down Expand Up @@ -380,6 +382,30 @@ export class Modal implements ComponentInterface, OverlayInterface {
this.initSwipeToClose();
}

/* tslint:disable-next-line */
if (typeof window !== 'undefined') {
this.keyboardOpenCallback = () => {
if (this.gesture) {
/**
* When the native keyboard is opened and the webview
* is resized, the gesture implementation will become unresponsive
* and enter a free-scroll mode.
*
* When the keyboard is opened, we disable the gesture for
* a single frame and re-enable once the contents have repositioned
* from the keyboard placement.
*/
this.gesture.enable(false);
raf(() => {
if (this.gesture) {
this.gesture.enable(true)
}
});
}
}
window.addEventListener(KEYBOARD_DID_OPEN, this.keyboardOpenCallback);
}

this.currentTransition = undefined;
}

Expand Down Expand Up @@ -474,6 +500,11 @@ export class Modal implements ComponentInterface, OverlayInterface {
return false;
}

/* tslint:disable-next-line */
if (typeof window !== 'undefined' && this.keyboardOpenCallback) {
window.removeEventListener(KEYBOARD_DID_OPEN, this.keyboardOpenCallback);
}

/**
* When using an inline modal
* and presenting a modal it is possible to
Expand Down
8 changes: 0 additions & 8 deletions core/src/utils/gesture/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,6 @@ export const createGesture = (config: GestureConfig): Gesture => {
destroy() {
gesture.destroy();
pointerEvents.destroy();
if (config.onDestroy !== undefined) {
config.onDestroy();
}
}
};
};
Expand Down Expand Up @@ -328,11 +325,6 @@ export interface GestureConfig {
onStart?: GestureCallback;
onMove?: GestureCallback;
onEnd?: GestureCallback;
/**
* Callback to extend the behavior when a gesture
* handler is destroyed.
*/
onDestroy?: () => void;
notCaptured?: GestureCallback;
}

Expand Down