Skip to content

Commit dd268fc

Browse files
authored
Merge pull request #550 from gselderslaghs/materialbox-accessibility
accessibility(Materialbox) tabindex
2 parents 123e856 + 0ed1584 commit dd268fc

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/materialbox.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class Materialbox extends Component<MaterialboxOptions> {
7474
...Materialbox.defaults,
7575
...options
7676
};
77-
77+
7878
this.overlayActive = false;
7979
this.doneAnimating = true;
8080
this.placeholder = document.createElement('div');
@@ -83,6 +83,7 @@ export class Materialbox extends Component<MaterialboxOptions> {
8383
this.originalHeight = 0;
8484
this.originInlineStyles = this.el.getAttribute('style');
8585
this.caption = this.el.getAttribute('data-caption') || '';
86+
this.el.tabIndex = 0;
8687
// Wrap
8788
this.el.before(this.placeholder);
8889
this.placeholder.append(this.el);
@@ -129,19 +130,31 @@ export class Materialbox extends Component<MaterialboxOptions> {
129130

130131
private _setupEventHandlers() {
131132
this.el.addEventListener('click', this._handleMaterialboxClick);
133+
this.el.addEventListener('keypress', this._handleMaterialboxKeypress);
132134
}
133135

134136
private _removeEventHandlers() {
135137
this.el.removeEventListener('click', this._handleMaterialboxClick);
138+
this.el.removeEventListener('keypress', this._handleMaterialboxKeypress);
136139
}
137140

138141
private _handleMaterialboxClick = () => {
142+
this._handleMaterialboxToggle();
143+
};
144+
145+
private _handleMaterialboxKeypress = (e: KeyboardEvent) => {
146+
if (Utils.keys.ENTER.includes(e.key)) {
147+
this._handleMaterialboxToggle();
148+
}
149+
};
150+
151+
private _handleMaterialboxToggle = () => {
139152
// If already modal, return to original
140153
if (this.doneAnimating === false || (this.overlayActive && this.doneAnimating))
141154
this.close();
142155
else
143156
this.open();
144-
}
157+
};
145158

146159
private _handleWindowScroll = () => {
147160
if (this.overlayActive) this.close();
@@ -237,7 +250,7 @@ export class Materialbox extends Component<MaterialboxOptions> {
237250
easing: 'easeOutQuad',
238251
complete: () => {
239252
this.doneAnimating = true;
240-
if (typeof this.options.onOpenEnd === 'function') this.options.onOpenEnd.call(this, this.el);
253+
if (typeof this.options.onOpenEnd === 'function') this.options.onOpenEnd.call(this, this.el);
241254
}
242255
});
243256
*/
@@ -272,7 +285,7 @@ export class Materialbox extends Component<MaterialboxOptions> {
272285
// Remove overflow overrides on ancestors
273286
this._changedAncestorList.forEach(anchestor => anchestor.style.overflow = '');
274287
// onCloseEnd callback
275-
if (typeof this.options.onCloseEnd === 'function') this.options.onCloseEnd.call(this, this.el);
288+
if (typeof this.options.onCloseEnd === 'function') this.options.onCloseEnd.call(this, this.el);
276289
}, duration);
277290
}
278291

0 commit comments

Comments
 (0)