Skip to content

Commit b48509d

Browse files
committed
refactor: removed jquery from modal and sidenav
1 parent 20c2606 commit b48509d

File tree

3 files changed

+103
-162
lines changed

3 files changed

+103
-162
lines changed

docs/js/materialize.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/modal.ts

+63-108
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { Component } from "./component";
2-
import $ from "cash-dom";
32
import anim from "animejs";
43
import { M } from "./global";
54

6-
let _defaults = {
5+
const _defaults = {
76
opacity: 0.5,
87
inDuration: 250,
98
outDuration: 250,
@@ -18,12 +17,13 @@ let _defaults = {
1817
};
1918

2019
export class Modal extends Component {
20+
el: HTMLElement;
2121
static _modalsOpen: number;
2222
static _count: number;
2323
isOpen: boolean;
24-
id: any;
24+
id: string;
2525
private _openingTrigger: any;
26-
$overlay: any;
26+
private _overlay: HTMLElement;
2727
private _nthModalOpened: number;
2828
private _handleOverlayClickBound: any;
2929
private _handleModalCloseClickBound: any;
@@ -35,10 +35,11 @@ let _defaults = {
3535
(this.el as any).M_Modal = this;
3636
this.options = {...Modal.defaults, ...options};
3737
this.isOpen = false;
38-
this.id = this.$el.attr('id');
38+
this.id = this.el.id;
3939
this._openingTrigger = undefined;
40-
this.$overlay = $('<div class="modal-overlay"></div>');
41-
(this.el as HTMLElement).tabIndex = 0;
40+
this._overlay = document.createElement('div');
41+
this._overlay.classList.add('modal-overlay');
42+
this.el.tabIndex = 0;
4243
this._nthModalOpened = 0;
4344
Modal._count++;
4445
this._setupEventHandlers();
@@ -53,15 +54,15 @@ let _defaults = {
5354
}
5455

5556
static getInstance(el) {
56-
let domElem = !!el.jquery ? el[0] : el;
57+
const domElem = !!el.jquery ? el[0] : el;
5758
return domElem.M_Modal;
5859
}
5960

6061
destroy() {
6162
Modal._count--;
6263
this._removeEventHandlers();
6364
this.el.removeAttribute('style');
64-
this.$overlay.remove();
65+
this._overlay.remove();
6566
(this.el as any).M_Modal = undefined;
6667
}
6768

@@ -71,76 +72,63 @@ let _defaults = {
7172
if (Modal._count === 1) {
7273
document.body.addEventListener('click', this._handleTriggerClick);
7374
}
74-
this.$overlay[0].addEventListener('click', this._handleOverlayClickBound);
75+
this._overlay.addEventListener('click', this._handleOverlayClickBound);
7576
this.el.addEventListener('click', this._handleModalCloseClickBound);
7677
}
7778

7879
_removeEventHandlers() {
7980
if (Modal._count === 0) {
8081
document.body.removeEventListener('click', this._handleTriggerClick);
8182
}
82-
this.$overlay[0].removeEventListener('click', this._handleOverlayClickBound);
83+
this._overlay.removeEventListener('click', this._handleOverlayClickBound);
8384
this.el.removeEventListener('click', this._handleModalCloseClickBound);
8485
}
8586

8687
_handleTriggerClick(e) {
87-
let $trigger = $(e.target).closest('.modal-trigger');
88-
if ($trigger.length) {
89-
let modalId = M.getIdFromTrigger($trigger[0]);
90-
let modalInstance = (document.getElementById(modalId) as any).M_Modal;
91-
if (modalInstance) {
92-
modalInstance.open($trigger);
93-
}
94-
e.preventDefault();
95-
}
88+
const trigger = e.target.closest('.modal-trigger');
89+
if (!trigger) return;
90+
const modalId = M.getIdFromTrigger(trigger);
91+
const modalInstance = (document.getElementById(modalId) as any).M_Modal;
92+
if (modalInstance) modalInstance.open(trigger);
93+
e.preventDefault();
9694
}
9795

9896
_handleOverlayClick() {
99-
if (this.options.dismissible) {
100-
this.close();
101-
}
97+
if (this.options.dismissible) this.close();
10298
}
10399

104100
_handleModalCloseClick(e) {
105-
let $closeTrigger = $(e.target).closest('.modal-close');
106-
if ($closeTrigger.length) {
107-
this.close();
108-
}
101+
const closeTrigger = e.target.closest('.modal-close');
102+
if (closeTrigger) this.close();
109103
}
110104

111105
_handleKeydown(e) {
112106
// ESC key
113-
if (e.keyCode === 27 && this.options.dismissible) {
114-
this.close();
115-
}
107+
if (e.keyCode === 27 && this.options.dismissible) this.close();
116108
}
117109

118110
_handleFocus(e) {
119111
// Only trap focus if this modal is the last model opened (prevents loops in nested modals).
120112
if (!this.el.contains(e.target) && this._nthModalOpened === Modal._modalsOpen) {
121-
(this.el as HTMLElement).focus();
113+
this.el.focus();
122114
}
123115
}
124116

125117
_animateIn() {
126-
// Set initial styles
127-
$.extend((this.el as HTMLElement).style, {
128-
display: 'block',
129-
opacity: 0
130-
});
131-
$.extend(this.$overlay[0].style, {
132-
display: 'block',
133-
opacity: 0
134-
});
118+
// Set initial styles
119+
this.el.style.display = 'block';
120+
this.el.style.opacity = '0';
121+
this._overlay.style.display = 'block';
122+
this._overlay.style.opacity = '0';
135123
// Animate overlay
136124
anim({
137-
targets: this.$overlay[0],
125+
targets: this._overlay,
138126
opacity: this.options.opacity,
139127
duration: this.options.inDuration,
140128
easing: 'easeOutQuad'
141129
});
142130
// Define modal animation options
143-
let enterAnimOptions = {
131+
const enterAnimOptions = {
144132
targets: this.el,
145133
duration: this.options.inDuration,
146134
easing: 'easeOutCubic',
@@ -151,45 +139,38 @@ let _defaults = {
151139
}
152140
}
153141
};
154-
155142
// Bottom sheet animation
156143
if (this.el.classList.contains('bottom-sheet')) {
157-
$.extend(enterAnimOptions, {
158-
bottom: 0,
159-
opacity: 1
160-
});
161-
anim(enterAnimOptions);
162-
163-
// Normal modal animation
164-
} else {
165-
$.extend(enterAnimOptions, {
166-
top: [this.options.startingTop, this.options.endingTop],
167-
opacity: 1,
168-
scaleX: [0.8, 1],
169-
scaleY: [0.8, 1]
170-
});
171-
anim(enterAnimOptions);
144+
enterAnimOptions['bottom'] = 0;
145+
enterAnimOptions['opacity'] = 1;
146+
}
147+
// Normal modal animation
148+
else {
149+
enterAnimOptions['top'] = [this.options.startingTop, this.options.endingTop];
150+
enterAnimOptions['opacity'] = 1;
151+
enterAnimOptions['scaleX'] = [0.8, 1];
152+
enterAnimOptions['scaleY'] = [0.8, 1];
172153
}
154+
anim(enterAnimOptions);
173155
}
174156

175157
_animateOut() {
176158
// Animate overlay
177159
anim({
178-
targets: this.$overlay[0],
160+
targets: this._overlay,
179161
opacity: 0,
180162
duration: this.options.outDuration,
181163
easing: 'easeOutQuart'
182164
});
183-
184165
// Define modal animation options
185-
let exitAnimOptions = {
166+
const exitAnimOptions = {
186167
targets: this.el,
187168
duration: this.options.outDuration,
188169
easing: 'easeOutCubic',
189170
// Handle modal ready callback
190171
complete: () => {
191-
(this.el as HTMLElement).style.display = 'none';
192-
this.$overlay.remove();
172+
this.el.style.display = 'none';
173+
this._overlay.remove();
193174
// Call onCloseEnd callback
194175
if (typeof this.options.onCloseEnd === 'function') {
195176
this.options.onCloseEnd.call(this, this.el);
@@ -198,104 +179,78 @@ let _defaults = {
198179
};
199180
// Bottom sheet animation
200181
if (this.el.classList.contains('bottom-sheet')) {
201-
$.extend(exitAnimOptions, {
202-
bottom: '-100%',
203-
opacity: 0
204-
});
205-
anim(exitAnimOptions);
206-
// Normal modal animation
182+
exitAnimOptions['bottom'] = '-100%';
183+
exitAnimOptions['opacity'] = 0;
207184
}
185+
// Normal modal animation
208186
else {
209-
$.extend(exitAnimOptions, {
210-
top: [this.options.endingTop, this.options.startingTop],
211-
opacity: 0,
212-
scaleX: 0.8,
213-
scaleY: 0.8
214-
});
215-
anim(exitAnimOptions);
187+
exitAnimOptions['top'] = [this.options.endingTop, this.options.startingTop];
188+
exitAnimOptions['opacity'] = 0;
189+
exitAnimOptions['scaleX'] = 0.8;
190+
exitAnimOptions['scaleY'] = 0.8;
216191
}
192+
anim(exitAnimOptions);
217193
}
218194

219-
open($trigger) {
220-
if (this.isOpen) {
221-
return;
222-
}
195+
open(trigger: HTMLElement|undefined): Modal {
196+
if (this.isOpen) return;
223197
this.isOpen = true;
224198
Modal._modalsOpen++;
225199
this._nthModalOpened = Modal._modalsOpen;
226-
227200
// Set Z-Index based on number of currently open modals
228-
this.$overlay[0].style.zIndex = 1000 + Modal._modalsOpen * 2;
229-
(this.el as HTMLElement).style.zIndex = (1000 + Modal._modalsOpen * 2 + 1).toString();
230-
201+
this._overlay.style.zIndex = (1000 + Modal._modalsOpen * 2).toString();
202+
this.el.style.zIndex = (1000 + Modal._modalsOpen * 2 + 1).toString();
231203
// Set opening trigger, undefined indicates modal was opened by javascript
232-
this._openingTrigger = !!$trigger ? $trigger[0] : undefined;
233-
204+
this._openingTrigger = !!trigger ? trigger : undefined;
234205
// onOpenStart callback
235206
if (typeof this.options.onOpenStart === 'function') {
236207
this.options.onOpenStart.call(this, this.el, this._openingTrigger);
237208
}
238-
239209
if (this.options.preventScrolling) {
240210
document.body.style.overflow = 'hidden';
241211
}
242-
243212
this.el.classList.add('open');
244-
this.el.insertAdjacentElement('afterend', this.$overlay[0]);
245-
213+
this.el.insertAdjacentElement('afterend', this._overlay);
246214
if (this.options.dismissible) {
247215
this._handleKeydownBound = this._handleKeydown.bind(this);
248216
this._handleFocusBound = this._handleFocus.bind(this);
249217
document.addEventListener('keydown', this._handleKeydownBound);
250218
document.addEventListener('focus', this._handleFocusBound, true);
251219
}
252-
253220
anim.remove(this.el);
254-
anim.remove(this.$overlay[0]);
221+
anim.remove(this._overlay);
255222
this._animateIn();
256-
257223
// Focus modal
258-
(this.el as HTMLElement).focus();
224+
this.el.focus();
259225
return this;
260226
}
261227

262228
close() {
263-
if (!this.isOpen) {
264-
return;
265-
}
229+
if (!this.isOpen) return;
266230
this.isOpen = false;
267231
Modal._modalsOpen--;
268232
this._nthModalOpened = 0;
269-
270233
// Call onCloseStart callback
271234
if (typeof this.options.onCloseStart === 'function') {
272235
this.options.onCloseStart.call(this, this.el);
273236
}
274-
275237
this.el.classList.remove('open');
276-
277238
// Enable body scrolling only if there are no more modals open.
278239
if (Modal._modalsOpen === 0) {
279240
document.body.style.overflow = '';
280241
}
281-
282242
if (this.options.dismissible) {
283243
document.removeEventListener('keydown', this._handleKeydownBound);
284244
document.removeEventListener('focus', this._handleFocusBound, true);
285245
}
286-
287246
anim.remove(this.el);
288-
anim.remove(this.$overlay[0]);
247+
anim.remove(this._overlay);
289248
this._animateOut();
290249
return this;
291250
}
292-
251+
293252
static{
294253
Modal._modalsOpen = 0;
295254
Modal._count = 0;
296255
}
297-
298256
}
299-
300-
301-

0 commit comments

Comments
 (0)