Skip to content

Commit

Permalink
focus fix, shorter scoped variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Fred Heusschen committed Apr 2, 2022
1 parent fe19ccc commit 51a1b82
Show file tree
Hide file tree
Showing 36 changed files with 133 additions and 127 deletions.
6 changes: 1 addition & 5 deletions demo/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ <h1>This is a demo.</h1>
<!-- mmenu scripts -->
<script src="../dist/mmenu.js"></script>
<script>
new Mmenu(document.querySelector("#menu"), {
offCanvas: {
position: "left-front",
},
});
new Mmenu(document.querySelector("#menu"));

document.addEventListener("click", function (evnt) {
var anchor = evnt.target.closest('a[href="#/"]');
Expand Down
2 changes: 1 addition & 1 deletion dist/addons/sectionindexer/mmenu.sectionindexer.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/core/offcanvas/configs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const configs = {
clone: false,
menu: {
insertMethod: 'prepend',
insertMethod: 'append',
insertSelector: 'body'
},
page: {
Expand Down
2 changes: 1 addition & 1 deletion dist/core/offcanvas/mmenu.offcanvas.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 20 additions & 17 deletions dist/core/offcanvas/mmenu.offcanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,6 @@ export default function () {
}
// Add methods to the API.
this._api.push('open', 'close', 'setPage');
// Setup the UI blocker.
if (!Mmenu.node.blck) {
this.bind('initMenu:before', () => {
/** The UI blocker node. */
const blocker = DOM.create('a.mm-wrapper__blocker.mm-blocker.mm-slideout');
blocker.id = uniqueId();
blocker.title = this.i18n(configs.screenReader.closeMenu);
// Make the blocker able to receive focus.
blocker.setAttribute('tabindex', '-1');
// Append the blocker node to the body.
document.querySelector(configs.menu.insertSelector).append(blocker);
// Store the blocker node.
Mmenu.node.blck = blocker;
});
}
// Clone menu and prepend it to the <body>.
this.bind('initMenu:before', () => {
// Clone if needed.
Expand All @@ -59,6 +44,21 @@ export default function () {
// Prepend to the <body>
this.node.wrpr[configs.menu.insertMethod](this.node.menu);
});
// Setup the UI blocker.
if (!Mmenu.node.blck) {
this.bind('initMenu:before', () => {
/** The UI blocker node. */
const blocker = DOM.create('a.mm-wrapper__blocker.mm-blocker.mm-slideout');
blocker.id = uniqueId();
blocker.title = this.i18n(configs.screenReader.closeMenu);
// Make the blocker able to receive focus.
blocker.tabIndex = 0;
// Append the blocker node to the body.
document.querySelector(configs.menu.insertSelector).append(blocker);
// Store the blocker node.
Mmenu.node.blck = blocker;
});
}
this.bind('initMenu:after', () => {
// Setup the page.
this.setPage(Mmenu.node.page);
Expand Down Expand Up @@ -142,8 +142,11 @@ Mmenu.prototype.close = function () {
this.node.wrpr.classList.remove('mm-wrapper--opened');
Mmenu.node.blck.classList.remove('mm-blocker--blocking');
// Focus opening link or page.
const focus = this.node.open || document.querySelector(`[href="#${this.node.menu.id}"]`) || this.node.page || null;
const focus = this.node.open || document.querySelector(`[href="#${this.node.menu.id}"]`) || Mmenu.node.page || null;
(_a = focus) === null || _a === void 0 ? void 0 : _a.focus();
// Prevent html/body from scrolling due to focus.
document.documentElement.scrollLeft = 0;
document.body.scrollLeft = 0;
// Invoke "after" hook.
this.trigger('close:after');
};
Expand Down Expand Up @@ -180,7 +183,7 @@ Mmenu.prototype.setPage = function (page) {
// Invoke "before" hook.
this.trigger('setPage:before', [page]);
// Make the page able to receive focus.
page.setAttribute('tabindex', '-1');
page.tabIndex = -1;
// Set the classes
page.classList.add('mm-page', 'mm-slideout');
// Set the ID.
Expand Down

0 comments on commit 51a1b82

Please sign in to comment.