Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Directive, ElementRef, OnDestroy, Renderer2 } from '@angular/core';
  2. import { Keyboard } from '@ionic-native/keyboard';
  3. import { Subscription } from 'rxjs/Subscription';
  4.  
  5. @Directive({
  6.   selector: '[hideWhenKeyboardIsOpen]'
  7. })
  8. export class HideWhenKeyboardIsOpenDirective implements OnDestroy {
  9.   private show: Subscription;
  10.   private hide: Subscription;
  11.  
  12.   constructor(
  13.     public element: ElementRef,
  14.     public renderer: Renderer2,
  15.     public keyboard: Keyboard
  16.   ) {
  17.     this.show = keyboard.onKeyboardShow().subscribe(() => {
  18.       renderer.setStyle(element.nativeElement, 'visibility', 'hidden');
  19.     });
  20.  
  21.     this.hide = keyboard.onKeyboardHide().subscribe(() => {
  22.       renderer.removeStyle(element.nativeElement, 'visibility');
  23.     });
  24.   }
  25.  
  26.   ngOnDestroy(): void {
  27.     if (this.hide !== undefined) this.hide.unsubscribe();
  28.  
  29.     if (this.show !== undefined) this.show.unsubscribe();
  30.   }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement