Skip to content

Commit

Permalink
Merge branch 'sidenav-accessibility' of https://github.com/gseldersla…
Browse files Browse the repository at this point in the history
…ghs/materialize into gselderslaghs-sidenav-accessibility
  • Loading branch information
wuda-io committed Nov 13, 2024
2 parents c144a6f + 4df4a94 commit 559073e
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/sidenav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ export class Sidenav extends Component<SidenavOptions> implements Openable {
if (this.isFixed) {
window.addEventListener('resize', this._handleWindowResize);
}
/* Set aria-hidden state */
this._setAriaHidden();
this._setTabIndex();
}

private _removeEventHandlers() {
Expand Down Expand Up @@ -406,6 +409,9 @@ export class Sidenav extends Component<SidenavOptions> implements Openable {
else {
if (this.options.preventScrolling) this._preventBodyScrolling();
if (!this.isDragged || this.percentOpen != 1) this._animateIn();
/* Set aria-hidden state */
this._setAriaHidden();
this._setTabIndex();
}
}

Expand All @@ -432,6 +438,9 @@ export class Sidenav extends Component<SidenavOptions> implements Openable {
} else {
this._overlay.style.display = 'none';
}
/* Set aria-hidden state */
this._setAriaHidden();
this._setTabIndex();
}
}

Expand Down Expand Up @@ -488,7 +497,7 @@ export class Sidenav extends Component<SidenavOptions> implements Openable {

private _animateOverlayIn() {
let start = 0;
if (this.isDragged)
if (this.isDragged)
start = this.percentOpen;
else
this._overlay.style.display = 'block';
Expand All @@ -501,7 +510,7 @@ export class Sidenav extends Component<SidenavOptions> implements Openable {
setTimeout(() => {
this._overlay.style.transition = `opacity ${duration}ms ease`;
// to
this._overlay.style.opacity = '1';
this._overlay.style.opacity = '1';
}, 1);
}

Expand All @@ -510,12 +519,27 @@ export class Sidenav extends Component<SidenavOptions> implements Openable {
// easeOutQuad
this._overlay.style.transition = `opacity ${duration}ms ease`;
// to
this._overlay.style.opacity = '0';
this._overlay.style.opacity = '0';
setTimeout(() => {
this._overlay.style.display = 'none';
}, duration);
}

private _setAriaHidden = () => {
this.el.ariaHidden = this.isOpen ? 'false' : 'true';
const navWrapper = document.querySelector('.nav-wrapper ul') as any;
if (navWrapper) navWrapper.ariaHidden = this.isOpen;
}

private _setTabIndex = () => {
const navLinks = document.querySelectorAll('.nav-wrapper ul li a');
const sideNavLinks = document.querySelectorAll('.sidenav li a');
if (navLinks)
navLinks.forEach((navLink: HTMLAnchorElement) => { navLink.tabIndex = this.isOpen ? -1 : 0});
if (sideNavLinks)
sideNavLinks.forEach((sideNavLink: HTMLAnchorElement) => { sideNavLink.tabIndex = this.isOpen ? 0 : -1;});
}

static {
Sidenav._sidenavs = [];
}
Expand Down

0 comments on commit 559073e

Please sign in to comment.