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

Mobile nav dropdown - focusTrap #2465

Merged
merged 5 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Draft
- Adding mobile nav dropdown focusTrap [#2465](https://github.com/bigcommerce/cornerstone/pull/2465)
- Remove remote_api_scripts to avoid double firing Meta Pixel analytics [#2467](https://github.com/bigcommerce/cornerstone/pull/2467)
- Prevent flow outside page container on account pages [#2462](https://github.com/bigcommerce/cornerstone/pull/2462)
- Account.js - Fixed jquery selector to be template literal [#2464](https://github.com/bigcommerce/cornerstone/pull/2464)
Expand Down
33 changes: 33 additions & 0 deletions assets/js/theme/global/mobile-menu-toggle.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as focusTrap from 'focus-trap';
import _ from 'lodash';
import mediaQueryListFactory from '../common/media-query-list';
import { CartPreviewEvents } from './cart-preview';
Expand Down Expand Up @@ -37,6 +38,8 @@ export class MobileMenuToggle {
this.$subMenus = this.$navList.find('.navPages-action');
this.$toggle = $toggle;
this.mediumMediaQueryList = mediaQueryListFactory('medium');
this.$preModalFocusedEl = null;
this.focusTrap = null;

// Auto-bind
this.onToggleClick = this.onToggleClick.bind(this);
Expand Down Expand Up @@ -77,6 +80,28 @@ export class MobileMenuToggle {
}
}

setupFocusTrap() {
if (!this.$preModalFocusedEl) this.$preModalFocusedEl = $(document.activeElement);

if (!this.focusTrap) {
this.focusTrap = focusTrap.createFocusTrap(this.$header[0], {
escapeDeactivates: false,
returnFocusOnDeactivate: false,
allowOutsideClick: true,
fallbackFocus: () => {
const fallbackNode = this.$preModalFocusedEl && this.$preModalFocusedEl.length
? this.$preModalFocusedEl[0]
: $('[data-mobile-menu-toggle="menu"]')[0];

return fallbackNode;
},
});
}

this.focusTrap.deactivate();
this.focusTrap.activate();
}

toggle() {
if (this.isOpen) {
this.hide();
Expand All @@ -98,6 +123,8 @@ export class MobileMenuToggle {
this.$scrollView.scrollTop(0);

this.resetSubMenus();

this.setupFocusTrap();
}

hide() {
Expand All @@ -112,6 +139,12 @@ export class MobileMenuToggle {
this.$header.removeClass('is-open');

this.resetSubMenus();

if (this.focusTrap) this.focusTrap.deactivate();

if (this.$preModalFocusedEl) this.$preModalFocusedEl.focus();

this.$preModalFocusedEl = null;
}

// Private
Expand Down
Loading