-
-
Save cryptocoinserver/b95741fb67209564efcef74aae66e28a to your computer and use it in GitHub Desktop.
Ionic 2 Keyboard Attach Directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Directive, ElementRef, Input } from '@angular/core'; | |
import { Content, Platform } from 'ionic-angular'; | |
import { Keyboard } from 'ionic-native'; | |
/** | |
* @name KeyboardAttachDirective | |
* @description | |
* The `keyboardAttach` directive will cause an element to float above the | |
* keyboard when the keyboard shows. Currently only supports the `ion-footer` element. | |
* | |
* ### Notes | |
* - This directive requires [Ionic Native](https://github.com/driftyco/ionic-native) | |
* and the [Ionic Keyboard Plugin](https://github.com/driftyco/ionic-plugin-keyboard). | |
* - Currently only tested to work on iOS. | |
* - If there is an input in your footer, you will need to set | |
* `Keyboard.disableScroll(true)`. | |
* | |
* @usage | |
* | |
* ```html | |
* <ion-content #content> | |
* </ion-content> | |
* | |
* <ion-footer [keyboardAttach]="content"> | |
* <ion-toolbar> | |
* <ion-item> | |
* <ion-input></ion-input> | |
* </ion-item> | |
* </ion-toolbar> | |
* </ion-footer> | |
* ``` | |
*/ | |
@Directive({ | |
selector: '[keyboardAttach]' | |
}) | |
export class KeyboardAttachDirective { | |
@Input('keyboardAttach') content: Content; | |
private onShowSubscription; | |
private onHideSubscription; | |
constructor( | |
private elementRef: ElementRef, | |
private platform: Platform | |
) { | |
if (this.platform.is('cordova') && this.platform.is('ios')) { | |
this.onShowSubscription = Keyboard.onKeyboardShow().subscribe(e => this.onShow(e)); | |
this.onHideSubscription = Keyboard.onKeyboardHide().subscribe(() => this.onHide()); | |
} | |
} | |
ngOnDestroy() { | |
if (this.onShowSubscription) { | |
this.onShowSubscription.unsubscribe(); | |
} | |
if (this.onHideSubscription) { | |
this.onHideSubscription.unsubscribe(); | |
} | |
} | |
private onShow(e) { | |
let keyboardHeight: number = e.keyboardHeight || (e.detail && e.detail.keyboardHeight); | |
this.setElementPosition(keyboardHeight); | |
}; | |
private onHide() { | |
this.setElementPosition(0); | |
}; | |
private setElementPosition(pixels: number) { | |
this.elementRef.nativeElement.style.paddingBottom = pixels + 'px'; | |
this.content.resize(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks so much for this!!