packages/ui/src/lib/pop/pop.component.ts
A Pop is an area of content whose visibility can be toggled.
It can be used e.g as dialog, drawer or dialog.
changeDetection | ChangeDetectionStrategy.OnPush |
selector | ec-pop |
templateUrl | ./pop.component.html |
Properties |
|
Methods |
Inputs |
Outputs |
HostListeners |
constructor(popService: PopService, elementRef: ElementRef, cdr: ChangeDetectorRef)
|
||||||||||||
Defined in packages/ui/src/lib/pop/pop.component.ts:53
|
||||||||||||
Parameters :
|
active | |
Type : boolean
|
|
Defined in packages/ui/src/lib/pop/pop.component.ts:28
|
|
If true, .ec-pop is part of the DOM (*ngIf) + .active is set on .ec-pop-container. |
hideOnClickOutside | |
Default value : false
|
|
Defined in packages/ui/src/lib/pop/pop.component.ts:34
|
|
If set to true, the pop will hide when a click happens outside the pop. |
hideOnEscape | |
Default value : true
|
|
Defined in packages/ui/src/lib/pop/pop.component.ts:36
|
|
If set to false, esc will not close the pop |
type | |
Type : string
|
|
Defined in packages/ui/src/lib/pop/pop.component.ts:32
|
|
The used type on the host element |
toggle | |
Type : EventEmitter<boolean>
|
|
Defined in packages/ui/src/lib/pop/pop.component.ts:38
|
document:click |
Arguments : '$event'
|
document:click($event)
|
Defined in packages/ui/src/lib/pop/pop.component.ts:43
|
Listens for document:click and hides |
Public hide |
hide()
|
Defined in packages/ui/src/lib/pop/pop.component.ts:88
|
Hides the pop. Sets active false and removes pop from popService.stack
Returns :
void
|
isOutside | ||||
isOutside(element)
|
||||
Defined in packages/ui/src/lib/pop/pop.component.ts:58
|
||||
yields true if the given element is outside the pop / or is the wrapper element itself (the backdrop)
Parameters :
Returns :
boolean
|
Public show | ||||
show(e?)
|
||||
Defined in packages/ui/src/lib/pop/pop.component.ts:72
|
||||
Shows the pop. Sets active true and adds pop to popService.stack
Parameters :
Returns :
void
|
Public toggle | ||||
toggle(e?)
|
||||
Defined in packages/ui/src/lib/pop/pop.component.ts:63
|
||||
Shows if not active, hides if active.
Parameters :
Returns :
void
|
activated |
Default value : false
|
Defined in packages/ui/src/lib/pop/pop.component.ts:30
|
Flip and stays true after first show |
clickEvent |
Defined in packages/ui/src/lib/pop/pop.component.ts:40
|
Public elementRef |
Type : ElementRef
|
Defined in packages/ui/src/lib/pop/pop.component.ts:55
|
A Pop is not just a modal, but a section of markup that appears (pops) anywhere and with any style on your page.
<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>