Files
Manu MA 48a27636c7 fix(all): component reusage (#18963)
Use new stencil APIs to allow ionic elements to be reused once removed from the DOM.

fixes #18843
fixes #17344
fixes #16453
fixes #15879
fixes #15788
fixes #15484
fixes #17890
fixes #16364
2019-08-27 16:29:37 +02:00

91 lines
2.0 KiB
TypeScript

import { Component, ComponentInterface, Event, EventEmitter, Host, Listen, Prop, h } from '@stencil/core';
import { getIonMode } from '../../global/ionic-global';
import { GESTURE_CONTROLLER } from '../../utils/gesture';
import { now } from '../../utils/helpers';
@Component({
tag: 'ion-backdrop',
styleUrls: {
ios: 'backdrop.ios.scss',
md: 'backdrop.md.scss'
},
shadow: true
})
export class Backdrop implements ComponentInterface {
private lastClick = -10000;
private blocker = GESTURE_CONTROLLER.createBlocker({
disableScroll: true
});
/**
* If `true`, the backdrop will be visible.
*/
@Prop() visible = true;
/**
* If `true`, the backdrop will can be clicked and will emit the `ionBackdropTap` event.
*/
@Prop() tappable = true;
/**
* If `true`, the backdrop will stop propagation on tap.
*/
@Prop() stopPropagation = true;
/**
* Emitted when the backdrop is tapped.
*/
@Event() ionBackdropTap!: EventEmitter<void>;
connectedCallback() {
if (this.stopPropagation) {
this.blocker.block();
}
}
disconnectedCallback() {
this.blocker.unblock();
}
@Listen('touchstart', { passive: false, capture: true })
protected onTouchStart(ev: TouchEvent) {
this.lastClick = now(ev);
this.emitTap(ev);
}
@Listen('click', { passive: false, capture: true })
@Listen('mousedown', { passive: false, capture: true })
protected onMouseDown(ev: TouchEvent) {
if (this.lastClick < now(ev) - 2500) {
this.emitTap(ev);
}
}
private emitTap(ev: Event) {
if (this.stopPropagation) {
ev.preventDefault();
ev.stopPropagation();
}
if (this.tappable) {
this.ionBackdropTap.emit();
}
}
render() {
const mode = getIonMode(this);
return (
<Host
tabindex="-1"
class={{
[mode]: true,
'backdrop-hide': !this.visible,
'backdrop-no-tappable': !this.tappable,
}}
>
</Host>
);
}
}