I am facing a challenge on my webpage where I have implemented two Material Design Component drawers with identical items. One is set to be permanent for desktop and tablet displays, while the other is designed to be hidden or modal for mobile devices.
<aside class="mdc-drawer mdc-drawer--permanent">
<div class="mdc-drawer__header">
<h3 class="mdc-drawer__title">App</h3>
<h6 class="mdc-drawer__subtitle">@username</h6>
</div>
<div class="mdc-drawer__content">
<nav class="mdc-list--permanent">@menu_drawer_content</nav>
</div>
</aside>
<aside class="mdc-drawer mdc-drawer--modal">
<div class="mdc-drawer__header">
<h3 class="mdc-drawer__title">App</h3>
<h6 class="mdc-drawer__subtitle">@username</h6>
</div>
<div class="mdc-drawer__content">
<nav class="mdc-list">@menu_drawer_content</nav>
</div>
</aside>
Both drawers are initialized as follows:
modalDrawer = mdc.drawer.MDCDrawer.attachTo(document.querySelector('.mdc-drawer--modal'));
let list = mdc.list.MDCList.attachTo(document.querySelector('.mdc-list--permanent'));
list.wrapFocus = true;
To toggle between the two drawers based on screen size, I use the following JavaScript code:
let smallForm = window.matchMedia("(max-width: 767px)").matches;
function resized() {
let smallForm_ = window.matchMedia("(max-width: 767px)").matches;
if (smallForm !== smallForm_) {
smallForm = smallForm_;
changedMedia();
}
}
function changedMedia() {
let drawerButton = $('.mdc-top-app-bar__row > section > button');
if (smallForm) {
$('.mdc-drawer--permanent').hide();
drawerButton.show();
} else {
$('.mdc-drawer--permanent').show();
drawerButton.hide();
modalDrawer.open = false;
}
}
However, there is a bug that persists when selecting an item in one drawer does not sync with the same item in the other drawer. It leads to inconsistencies especially when transitioning between screen sizes.
Is there a way to link these two drawers so that selection in one will affect the state of the other without causing any unwanted recursive loop?
Edit: Added bounty. Full source.