-
Notifications
You must be signed in to change notification settings - Fork 13.5k
/
Copy pathmodal.tsx
154 lines (129 loc) · 3.97 KB
/
modal.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { Component, h, Ionic, Listen, Prop } from '../index';
import { AnimationBuilder, Animation, Modal as IModal, ModalEvent } from '../../util/interfaces';
import { createThemedClasses } from '../../util/theme';
import iOSEnterAnimation from './animations/ios.enter';
import iOSLeaveAnimation from './animations/ios.leave';
@Component({
tag: 'ion-modal',
styleUrls: {
ios: 'modal.ios.scss',
md: 'modal.md.scss',
wp: 'modal.wp.scss'
}
})
export class Modal implements IModal {
$el: HTMLElement;
animation: Animation;
@Prop() mode: string;
@Prop() color: string;
@Prop() component: string;
@Prop() componentProps: any = {};
@Prop() cssClass: string;
@Prop() enableBackdropDismiss: boolean = true;
@Prop() enterAnimation: AnimationBuilder;
@Prop() exitAnimation: AnimationBuilder;
@Prop() id: string;
@Prop() showBackdrop: boolean = true;
@Listen('ionDismiss')
onDismiss(ev: UIEvent) {
ev.stopPropagation();
ev.preventDefault();
this.dismiss();
}
ionViewDidLoad() {
Ionic.emit(this, 'ionModalDidLoad', { detail: { modal: this } } as ModalEvent);
}
present() {
return new Promise<void>(resolve => {
this._present(resolve);
});
}
private _present(resolve: Function) {
if (this.animation) {
this.animation.destroy();
this.animation = null;
}
Ionic.emit(this, 'ionModalWillPresent', { detail: { modal: this } } as ModalEvent);
// get the user's animation fn if one was provided
let animationBuilder = this.enterAnimation;
if (!animationBuilder) {
// user did not provide a custom animation fn
// decide from the config which animation to use
// TODO!!
animationBuilder = iOSEnterAnimation;
}
// build the animation and kick it off
this.animation = animationBuilder(this.$el);
this.animation.onFinish((a: any) => {
a.destroy();
Ionic.emit(this, 'ionModalDidPresent', { detail: { modal: this } } as ModalEvent);
resolve();
}).play();
}
dismiss() {
if (this.animation) {
this.animation.destroy();
this.animation = null;
}
return new Promise<void>(resolve => {
Ionic.emit(this, 'ionModalWillDismiss', { detail: { modal: this } } as ModalEvent);
// get the user's animation fn if one was provided
let animationBuilder = this.exitAnimation;
if (!animationBuilder) {
// user did not provide a custom animation fn
// decide from the config which animation to use
// TODO!!
animationBuilder = iOSLeaveAnimation;
}
// build the animation and kick it off
this.animation = animationBuilder(this.$el);
this.animation.onFinish((a: any) => {
a.destroy();
Ionic.emit(this, 'ionModalDidDismiss', { detail: { modal: this } } as ModalEvent);
Ionic.dom.write(() => {
this.$el.parentNode.removeChild(this.$el);
});
resolve();
}).play();
});
}
ionViewDidUnload() {
Ionic.emit(this, 'ionModalDidUnload', { detail: { modal: this } } as ModalEvent);
}
backdropClick() {
if (this.enableBackdropDismiss) {
// const opts: NavOptions = {
// minClickBlockDuration: 400
// };
this.dismiss();
}
}
render() {
const ThisComponent = this.component;
let userCssClasses = 'modal-content';
if (this.cssClass) {
userCssClasses += ` ${this.cssClass}`;
}
const dialogClasses = createThemedClasses(this.mode, this.color, 'modal-wrapper');
const thisComponentClasses = createThemedClasses(this.mode, this.color, userCssClasses);
return [
<div
onClick={this.backdropClick.bind(this)}
class={{
'modal-backdrop': true,
'hide-backdrop': !this.showBackdrop
}}
></div>,
<div
role='dialog'
class={dialogClasses}
>
<ThisComponent
props={this.componentProps}
class={thisComponentClasses}
>
</ThisComponent>
</div>
];
}
}