File

packages/ui/src/lib/pop/pop.component.ts

Description

A Pop is an area of content whose visibility can be toggled. It can be used e.g as dialog, drawer or dialog. https://components.entrecode.de/ui/pop?e=1

Metadata

changeDetection ChangeDetectionStrategy.OnPush
selector ec-pop
templateUrl ./pop.component.html

Index

Properties
Methods
Inputs
Outputs
HostListeners

Constructor

constructor(popService: PopService, elementRef: ElementRef, cdr: ChangeDetectorRef)
Parameters :
Name Type Optional
popService PopService No
elementRef ElementRef No
cdr ChangeDetectorRef No

Inputs

active
Type : boolean

If true, .ec-pop is part of the DOM (*ngIf) + .active is set on .ec-pop-container.

hideOnClickOutside
Default value : false

If set to true, the pop will hide when a click happens outside the pop.

hideOnEscape
Default value : true

If set to false, esc will not close the pop

type
Type : string

The used type on the host element

Outputs

toggle
Type : EventEmitter<boolean>

HostListeners

document:click
Arguments : '$event'
document:click($event)

Listens for document:click and hides

Methods

Public hide
hide()

Hides the pop. Sets active false and removes pop from popService.stack

Returns : void
isOutside
isOutside(element)

yields true if the given element is outside the pop / or is the wrapper element itself (the backdrop)

Parameters :
Name Optional
element No
Returns : boolean
Public show
show(e?)

Shows the pop. Sets active true and adds pop to popService.stack

Parameters :
Name Optional
e Yes
Returns : void
Public toggle
toggle(e?)

Shows if not active, hides if active.

Parameters :
Name Optional
e Yes
Returns : void

Properties

activated
Default value : false

Flip and stays true after first show

clickEvent
Public elementRef
Type : ElementRef

Pop

A Pop is not just a modal, but a section of markup that appears (pops) anywhere and with any style on your page.

Usage

<ec-pop class="modal-wrapper" #myPop>
  <div class="modal">
    <a (click)="myPop.hide()" class="modal__dismiss">Close</a>
    <header class="modal__header">
    </header>
    <div class="modal__body">
        Some body content
    </div>
    <footer class="modal__footer">
        This is the footer
    </footer>
  </div>
</ec-pop>
<button (click)="myPop.show()">Show pop</button>

The default pop classes can be looked up here.

import {
  Component,
  EventEmitter,
  Input,
  Output,
  HostBinding,
  HostListener,
  ElementRef,
  ChangeDetectionStrategy,
  ChangeDetectorRef,
} from '@angular/core';
import { PopService } from './pop.service';

/** A Pop is an area of content whose visibility can be toggled.
 * It can be used e.g as dialog, drawer or dialog.
 * <example-url>https://components.entrecode.de/ui/pop?e=1</example-url>
 * */
@Component({
  selector: 'ec-pop',
  templateUrl: './pop.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class PopComponent {
  /** If true, .ec-pop is part of the DOM (*ngIf) + .active is set on .ec-pop-container.  */
  @Input() @HostBinding('class.is-active') active: boolean;
  /** Flip and stays true after first show */
  activated = false;
  /** The used type on the host element */
  @Input() type: string;
  /** If set to true, the pop will hide when a click happens outside the pop. */
  @Input() hideOnClickOutside = false;
  /** If set to false, esc will not close the pop */
  @Input() hideOnEscape = true;
  // tslint:disable-next-line:no-output-rename
  @Output('toggle') _toggle: EventEmitter<boolean> = new EventEmitter();

  clickEvent;

  /** Listens for document:click and hides */
  @HostListener('document:click', ['$event']) clickedOutside($event) {
    if (
      this.hideOnClickOutside &&
      this.active &&
      (!this.clickEvent || $event !== this.clickEvent) && // to ensure the show event wont hide immediately
      this.elementRef &&
      this.isOutside($event.target)
    ) {
      this.hide();
    }
  }

  constructor(protected popService: PopService, public elementRef: ElementRef, protected cdr: ChangeDetectorRef) {}

  /** yields true if the given element is outside the pop / or is the wrapper element itself (the backdrop) */
  isOutside(element) {
    return !this.elementRef.nativeElement.contains(element) /*  ||
      element === this.elementRef.nativeElement */;
  } // active: boolean = !this.active, emit: boolean = false

  /** Shows if not active, hides if active. */ public toggle(e?) {
    if (this.active) {
      this.hide();
    } else {
      this.show(e);
    }
  }

  /** Shows the pop. Sets active true and adds pop to popService.stack */
  public show(e?) {
    this.active = true;
    this.activated = true;
    if (this.hideOnEscape) {
      this.popService.stack.add(this);
    }
    if (e) {
      this.clickEvent = e;
    } else if (this.hideOnClickOutside) {
      // console.warn('To use hideOnClickOutside, you need to pass the click event to the show method of ec-pop!');
    }
    this._toggle.emit(this.active);
    this.cdr.markForCheck();
  }

  /** Hides the pop. Sets active false and removes pop from popService.stack */
  public hide() {
    if (this.hideOnEscape) {
      this.popService.stack.remove(this);
    }
    this.active = false;
    this._toggle.emit(this.active);
    this.cdr.markForCheck();
  }
}
<ng-content *ngIf="active"></ng-content>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""