Switching drawer variants dynamically at runtime
Hi,
I was wondering if you have any opinion on using drawers dynamically. I would like to use temporary for mobile, persistent for tablet and permanent for desktop.
However, a single drawer types class seems to be used throughout the contents of the drawer. For example in a persistent drawer as seen here I can count mdc-persistent-drawer__drawer, mdc-persistent-drawer__toolbar-spacer and mdc-persistent-drawer--selected all used in different child components of the parent mdc-persistent-drawer.
Doesn't this make switching from one drawer type to another really difficult? Maybe you have some tips on how to solve a problem like this.
As far as I know, you just need to write a method within your app to easily generate the contents which can be included within multiple drawer markups. The drawers aren't meant to be changed dynamically but used statically depending on the device. This is because, it isn't really often that the user changes the viewport size in most apps.
If you wanted to handle the case of size changes though and keep the drawer contents as consistent as possible. Then you'd want to look into using a two-day data binding setup and generate the drawer contents client-side from an array of data. That way as any given drawer is needed you just generate the contents based on the data object.
I dunno, I feel like generating the drawer again client side would be overly complex for anything but SPAs. And screen size has to be checked client side so a simple parent level drawer type class seems like it would be much simpler. Not that i know if BEM allows stuff like that but just a thought.
At least toolbars use a parent level mdc-toolbar--fixed class.
After speaking with the rest of the team, we think the easiest solution is probably the best solution.
Create a component for each context, then use CSS to display the correct component for the correct size. We have a plan in motion for helpers to make this pattern simpler for our users, but until then, simply styling the components to be display: none and switching that rule at appropriate break points seems to be the correct solution.
It is of course possible to engineer the components to change based on screen size automatically, but we feel that this is too restrictive for most common cases of mdc-drawer
Reopening this issue because it was brought up in https://github.com/material-components/material-components-web-codelabs/issues/78, and seems like a fairly common use case.
I'm using the following approach to switch between the two versions:
import { MDCDrawer } from "@material/drawer";
const element = document.querySelector(".mdc-drawer");
const mobileClass = "mdc-drawer--modal";
const desktopClass = "mdc-drawer--dismissible";
let drawer;
// Criterium for switching to mobile
const isMobile = window.matchMedia(`(max-width: 720px)`);
// Prepare scrim/overlay element
const mobileOverlay = document.createElement("div");
mobileOverlay.classList.add("mdc-drawer-scrim");
// Switch between mobile and desktop
function toggleMobile(event) {
const isMobile = event.matches;
if (isMobile) {
element.classList.add(mobileClass);
element.classList.remove(desktopClass);
// Insert scrim/overlay element right after drawer
element.insertAdjacentElement("afterend", mobileOverlay);
} else {
element.classList.add(desktopClass);
element.classList.remove(mobileClass);
// Remove scrim/overlay element
mobileOverlay.remove();
}
drawer = MDCDrawer.attachTo(element);
drawer.open = !isMobile;
}
// Toggle when media query matches / stops matching
isMobile.addListener(toggleMobile);
// Toggle initially
toggleMobile(isMobile);
export default drawer;
The corresponding markup:
<aside class="mdc-drawer">
...
</aside>
<div class="mdc-drawer-app-content">
...
</div>
Please see this demo on Glitch for complete example of dynamically switching between 2 drawer variants based on device size.
I played around with the demo on Glitch but see several a11y issues. Specifically, the tabindex values for the links continue to change stay permanently on -1, making it not accessible by keyboard. This appears to happen only when your window changes between the two types.
@backflip, I saw the same behavior in your example. Any suggestions?
Not sure if this will help, but I came up with a different solution (@mui v5), please let me know what you think.
Here's the codesandbox: https://codesandbox.io/s/responsivedrawer-material-demo-2-hnu6g
Key aspects:
import { useTheme } from "@mui/material/styles";
import useMediaQuery from "@mui/material/useMediaQuery";
//...
const theme = useTheme();
const isGreaterThanSmallBreakpoint = useMediaQuery(
theme.breakpoints.up("sm")
);
//...
<Drawer
variant={isGreaterThanSmallBreakpoint ? "permanent" : "temporary"}
//...
/>