Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accessibility(FloatingActionButton) implemented tab index and aria expanded #553

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions src/buttons.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, BaseOptions, InitElements, MElement, Openable } from './component';
import { Component, BaseOptions, InitElements, MElement, Openable } from "./component";
import { Utils } from './utils';

export interface FloatingActionButtonOptions extends BaseOptions {
/**
Expand Down Expand Up @@ -62,10 +63,16 @@ export class FloatingActionButton
this.offsetX = 0;

this.el.classList.add(`direction-${this.options.direction}`);
if (this.options.direction === 'top') this.offsetY = 40;
else if (this.options.direction === 'right') this.offsetX = -40;
else if (this.options.direction === 'bottom') this.offsetY = -40;
else this.offsetX = 40;
this._anchor.tabIndex = 0;
this._menu.ariaExpanded = 'false';
if (this.options.direction === 'top')
this.offsetY = 40;
else if (this.options.direction === 'right')
this.offsetX = -40;
else if (this.options.direction === 'bottom')
this.offsetY = -40;
else
this.offsetX = 40;
this._setupEventHandlers();
}

Expand Down Expand Up @@ -119,6 +126,7 @@ export class FloatingActionButton
} else {
this.el.addEventListener('click', this._handleFABClick);
}
this.el.addEventListener('keypress', this._handleFABKeyPress);
}

_removeEventHandlers() {
Expand All @@ -128,9 +136,20 @@ export class FloatingActionButton
} else {
this.el.removeEventListener('click', this._handleFABClick);
}
this.el.removeEventListener('keypress', this._handleFABKeyPress);
}

_handleFABClick = () => {
this._handleFABToggle()
}

_handleFABKeyPress = (e) => {
if(Utils.keys.ENTER.includes(e.key)) {
this._handleFABToggle();
}
}

_handleFABToggle = () => {
if (this.isOpen) {
this.close();
} else {
Expand Down Expand Up @@ -169,6 +188,7 @@ export class FloatingActionButton

_animateInFAB() {
this.el.classList.add('active');
this._menu.ariaExpanded = 'true';
const delayIncrement = 40;
const duration = 275;

Expand All @@ -186,19 +206,24 @@ export class FloatingActionButton
el.style.transition = `opacity ${duration}ms ease, transform ${duration}ms ease`;
el.style.opacity = '1';
el.style.transform = 'translate(0, 0) scale(1)';
el.tabIndex = 0;
}, 1);
}, delay);
});
}

_animateOutFAB() {
const duration = 175;
setTimeout(() => this.el.classList.remove('active'), duration);
setTimeout(() => {
this.el.classList.remove('active'), duration;
this._menu.ariaExpanded = 'false';
});
this._floatingBtnsReverse.forEach((el) => {
el.style.transition = `opacity ${duration}ms ease, transform ${duration}ms ease`;
// to
el.style.opacity = '0';
el.style.transform = `translate(${this.offsetX}px, ${this.offsetY}px) scale(0.4)`;
el.tabIndex = -1;
});
}

Expand Down Expand Up @@ -228,6 +253,7 @@ export class FloatingActionButton
this.el.style.left = '0';
this.el.style.transform = 'translateX(' + this.offsetX + 'px)';
this.el.style.transition = 'none';
this._menu.ariaExpanded = 'true';

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

this._menu
.querySelectorAll('li > a')
.forEach((a: HTMLAnchorElement) => (a.style.opacity = '1'));
this._menu.querySelectorAll('li > a').forEach((a: HTMLAnchorElement) => {
a.style.opacity = '1';
a.tabIndex = 0;
});

// Scroll to close.
window.addEventListener('scroll', this.close, true);
Expand Down
Loading