Skip to content

Commit e2bc985

Browse files
authored
Merge pull request #553 from gselderslaghs/fab-accessibility
accessibility(FloatingActionButton) implemented tab index and aria expanded
2 parents 8c54388 + 20de238 commit e2bc985

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/buttons.ts

+36-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Component, BaseOptions, InitElements, MElement, Openable } from './component';
1+
import { Component, BaseOptions, InitElements, MElement, Openable } from "./component";
2+
import { Utils } from './utils';
23

34
export interface FloatingActionButtonOptions extends BaseOptions {
45
/**
@@ -62,10 +63,16 @@ export class FloatingActionButton
6263
this.offsetX = 0;
6364

6465
this.el.classList.add(`direction-${this.options.direction}`);
65-
if (this.options.direction === 'top') this.offsetY = 40;
66-
else if (this.options.direction === 'right') this.offsetX = -40;
67-
else if (this.options.direction === 'bottom') this.offsetY = -40;
68-
else this.offsetX = 40;
66+
this._anchor.tabIndex = 0;
67+
this._menu.ariaExpanded = 'false';
68+
if (this.options.direction === 'top')
69+
this.offsetY = 40;
70+
else if (this.options.direction === 'right')
71+
this.offsetX = -40;
72+
else if (this.options.direction === 'bottom')
73+
this.offsetY = -40;
74+
else
75+
this.offsetX = 40;
6976
this._setupEventHandlers();
7077
}
7178

@@ -119,6 +126,7 @@ export class FloatingActionButton
119126
} else {
120127
this.el.addEventListener('click', this._handleFABClick);
121128
}
129+
this.el.addEventListener('keypress', this._handleFABKeyPress);
122130
}
123131

124132
_removeEventHandlers() {
@@ -128,9 +136,20 @@ export class FloatingActionButton
128136
} else {
129137
this.el.removeEventListener('click', this._handleFABClick);
130138
}
139+
this.el.removeEventListener('keypress', this._handleFABKeyPress);
131140
}
132141

133142
_handleFABClick = () => {
143+
this._handleFABToggle()
144+
}
145+
146+
_handleFABKeyPress = (e) => {
147+
if(Utils.keys.ENTER.includes(e.key)) {
148+
this._handleFABToggle();
149+
}
150+
}
151+
152+
_handleFABToggle = () => {
134153
if (this.isOpen) {
135154
this.close();
136155
} else {
@@ -169,6 +188,7 @@ export class FloatingActionButton
169188

170189
_animateInFAB() {
171190
this.el.classList.add('active');
191+
this._menu.ariaExpanded = 'true';
172192
const delayIncrement = 40;
173193
const duration = 275;
174194

@@ -186,19 +206,24 @@ export class FloatingActionButton
186206
el.style.transition = `opacity ${duration}ms ease, transform ${duration}ms ease`;
187207
el.style.opacity = '1';
188208
el.style.transform = 'translate(0, 0) scale(1)';
209+
el.tabIndex = 0;
189210
}, 1);
190211
}, delay);
191212
});
192213
}
193214

194215
_animateOutFAB() {
195216
const duration = 175;
196-
setTimeout(() => this.el.classList.remove('active'), duration);
217+
setTimeout(() => {
218+
this.el.classList.remove('active'), duration;
219+
this._menu.ariaExpanded = 'false';
220+
});
197221
this._floatingBtnsReverse.forEach((el) => {
198222
el.style.transition = `opacity ${duration}ms ease, transform ${duration}ms ease`;
199223
// to
200224
el.style.opacity = '0';
201225
el.style.transform = `translate(${this.offsetX}px, ${this.offsetY}px) scale(0.4)`;
226+
el.tabIndex = -1;
202227
});
203228
}
204229

@@ -228,6 +253,7 @@ export class FloatingActionButton
228253
this.el.style.left = '0';
229254
this.el.style.transform = 'translateX(' + this.offsetX + 'px)';
230255
this.el.style.transition = 'none';
256+
this._menu.ariaExpanded = 'true';
231257

232258
this._anchor.style.transform = `translateY(${this.offsetY}px`;
233259
this._anchor.style.transition = 'none';
@@ -248,9 +274,10 @@ export class FloatingActionButton
248274
backdrop.style.transform = 'scale(' + scaleFactor + ')';
249275
backdrop.style.transition = 'transform .2s cubic-bezier(0.550, 0.055, 0.675, 0.190)';
250276

251-
this._menu
252-
.querySelectorAll('li > a')
253-
.forEach((a: HTMLAnchorElement) => (a.style.opacity = '1'));
277+
this._menu.querySelectorAll('li > a').forEach((a: HTMLAnchorElement) => {
278+
a.style.opacity = '1';
279+
a.tabIndex = 0;
280+
});
254281

255282
// Scroll to close.
256283
window.addEventListener('scroll', this.close, true);

0 commit comments

Comments
 (0)