14

How do I hide the drawer when the user clicks on an item? Or when a button is clicked?

<div class="mdl-layout__drawer">
        <span class="mdl-layout-title">Title</span>
        <button class="mdl-button mdl-js-button mdl-button--fab mdl-button--mini-fab mdl-js-ripple-effect" id="clickme">
          <i class="material-icons">add</i>
        </button>
</div>

How do I do it that when the button is clicked, the drawer will be hidden as if I clicked outside of the drawer? I tried simulating a click event outside of the drawer but it still does not hide.

krato
  • 1,226
  • 4
  • 14
  • 30

12 Answers12

23

toggleDrawer is now a public function since @be54f78.

var layout = document.querySelector('.mdl-layout');
layout.MaterialLayout.toggleDrawer();

Not currently available with v1.0.6, so you will need to build from source (as of today).

Benjamin
  • 594
  • 1
  • 5
  • 12
10

I believe you can remove the is-visible class from .mdl-layout__drawer. I tried modifying a codepen example from their site: demo. My pure javascript event binding is rusty, but as I mentioned, you just need to remove the .is-visible class from the drawer.

Update

The code I provided was for v1.0.0 of mdl and is not actual anymore. Starting at v1.1.0 there is a public API provided for toggling the drawer, as described in Benjamin's answer. If you're between v1.0.6 and v1.1.0, have a look at idleherb's answer.

Community
  • 1
  • 1
jdepypere
  • 3,453
  • 6
  • 54
  • 84
  • Please check one of the other answers too. ```.mdl-layout__obfuscator ``` need to be removed as well. I think that should be the accepted answer – Magesh khanna Mar 14 '16 at 02:59
  • @Mageshkhanna obviously a lot has changed since July last year, but my code sample obviously works so I don't quite see why you would need to downvote this. My code works for version 1.0.0, the version that was active when the question was asked. – jdepypere Mar 14 '16 at 09:56
  • sorry mate! Updated now :) – Magesh khanna Mar 15 '16 at 16:45
  • Does anyone know how to check if the drawer is opened or not. I don't want to trigger the toggleDrawer if the drawer is already close as it would then open it. – Roberto Nov 14 '16 at 15:26
  • @Lancelot I know it is a bit late, but I covered that in my answer below. – Stephen Jun 16 '17 at 19:44
7

Based on GitHub discourse, I have a couple of working solutions to the (hopefully soon to be resolved) issue of having a MDL drawer close when link is clicked. At the moment I'm using:

function close() {
  var d = document.querySelector('.mdl-layout');
  d.MaterialLayout.toggleDrawer();
}

document.querySelector('.mdl-layout__drawer').addEventListener('click', close);

Other variations are:

1.

document.querySelector('.mdl-layout__drawer').addEventListener('click', function () {
  document.querySelector('.mdl-layout__obfuscator').classList.remove('is-visible');
  this.classList.remove('is-visible');
}, false);

2.

function close() {
  var d = document.querySelector('.mdl-layout');
  d.MaterialLayout.toggleDrawer();
}
var drawer_container = document.getElementsByClassName('mdl-layout')[0];
drawer_container.querySelector('.mdl-layout__drawer').addEventListener('click', 'close');

Someone in the discussion mentioned the idea of targeting the querySelector so as not to require looking through the entire document and I came up with the following two variations:

3.

var drawer_container = document.getElementsByClassName('mdl-layout')[0]; 
# no IDs in the html code.
drawer_container.querySelector('.mdl-layout__drawer').addEventListener('click', function () {
  var obfuscator = document.querySelector('.mdl-layout__obfuscator');
  if (obfuscator.classList.contains('is-visible')) {
    obfuscator.classList.remove('is-visible');
    this.classList.remove('is-visible');
  }
}, false);

4.

function close() {
  var d = document.getElementsByClassName('mdl-layout__container')[0].querySelector('.mdl-layout');
  d.MaterialLayout.toggleDrawer();
}
var drawer_container = document.getElementsByClassName('mdl-layout')[0];
drawer_container.querySelector('.mdl-layout__drawer').addEventListener('click', 'close');

In both of my versions the browser has to run document.getElementsByClassName as well as a targeted querySelector call.

In my first version there is also the check: classList.contains('is-visible') which someone had recommended, but which seems unnecessary as, the function is being called from an item that is only visible if classList.contains('is-visible') is true.

I added the following calls to each of my variations (#3 and 4), within the functions:

console.time("anonymous");
console.timeEnd("anonymous");
console.time("close function");
console.timeEnd("close function");

And the one with the if statement runs in .39ms. Without the if statement they both run in .19ms. But I'm also not measuring the getElementsByClassName and querySelector methods that, if I understand correctly, are running on page load.

When I ran console.time("first"); and console.timeEnd("first"); through the first, and to me, prettiest code, the time was 23ms.

Apparently ie8, which I want to support, doesn't support getElementsByClassName.

I'm hoping someone can provide and explain an optimal solution to this relatively simple problem.

Here's a CodePen (not mine).

MikeiLL
  • 6,282
  • 5
  • 37
  • 68
4

For version 1.0.6, you have to remove the before mentioned class from two elements:

$( '.mdl-layout__drawer, .mdl-layout__obfuscator' ).removeClass( 'is-visible' );
idleherb
  • 1,072
  • 11
  • 23
3

I'm using this jquery command:

$( 'div[class^="mdl-layout__obfuscator"]' ).trigger( "click" );
0

Showing and hiding the menu is as easy as adding and removing the .is-visible class as it can be seen in the source:

MaterialLayout.prototype.drawerToggleHandler_ = function() {
  'use strict';

  this.drawer_.classList.toggle(this.CssClasses_.IS_DRAWER_OPEN);
};

So you would have something like this:

function toggle_drawer() {
  var drawer = document.getElementsByClassName('mdl-layout__drawer')[0];
  drawer.classList.toggle("is-visible");
}

I was hoping for a more convenient method of the MaterialLayout widget, but the best that I came up with was:

var layout = document.getElementsByClassName('mdl-layout')[0];
layout.MaterialLayout.drawerToggleHandler_();

although this happens to be working, that _ at the end of the method name shows that this function is not supposed to be (mis)used as a public API method.

Yan Foto
  • 10,850
  • 6
  • 57
  • 88
0

Do this:

HTML

<div class="mdl-layout__drawer" id="mobile-left-menu">
    <span class="mdl-layout-title">Whatever</span>
    <nav class="mdl-navigation inject-navigation">
          <a class="mdl-navigation__link" href="#" page="home">Home</a>
          <a class="mdl-navigation__link" href="#About" page="about">About</a>
    </nav>
</div>

JS

    $('.mdl-navigation__link').on('click', function () {

        // close the drawer the button is clicked
        $('.mdl-layout__drawer').toggleClass('is-visible')
    });

CSS

/* prevent the dark transparent background over the page with the drawer is open */
.mdl-layout__obfuscator.is-visible{
    background-color: transparent;
}
Mahmoud Zalt
  • 30,478
  • 7
  • 87
  • 83
0

Auto Hide the Navigation Drawer in Material Design Lite Framework.

Just include this code in the script tag of your web page

Must Include jQuery to get this run... :D

<script>
$(document).ready(function(){
    $(".mdl-layout__drawer a").click(function(){
        $(".mdl-layout__drawer,.mdl-layout__obfuscator").toggleClass("is-visible");
    });
});
</script>
Chandra Shekhar
  • 3,550
  • 2
  • 14
  • 22
0

To close it you need to check that it is open first, as there is no "closeDrawer". This is helpful when you cannot assume it is already open, like if you have a logout button within the drawer, and also outside, or in some session timeout function. You just need it closed to show the log-back-in form.

closeDrawer() {
    let drawer = document.querySelector('.mdl-layout__drawer');
    if (drawer && drawer.className.indexOf("is-visible")>-1) {
        toggleDrawer();
    }
}
toggleDrawer() {
    let layout = document.querySelector('.mdl-layout');
    if (layout && layout.MaterialLayout) {
        layout.MaterialLayout.toggleDrawer();
    }
}
Stephen
  • 1,603
  • 16
  • 19
0

In Angular ^4.0.0 you can use this workaround instead of using toggleDrawer() if you are having some problems with having MaterialLayout undefined as I'm.

(
  document
    .querySelector('.mdl-layout__obfuscator') as HTMLDivElement
).click();
AndreaM16
  • 3,917
  • 4
  • 35
  • 74
0

I have no idea how to get the "MaterialLayout" inside my Angular 6 project, but I took their prototype function and used it in my component:

  toggleDrawer = function () {
    var is_drawer_open = 'is-visible'
    var drawerButton = document.querySelector('.mdl-layout__drawer-button');
    var drawer_ = document.querySelector('.mdl-layout__drawer');
    var obfuscator_ = document.querySelector('.mdl-layout__obfuscator');
    drawer_.classList.toggle(is_drawer_open);
    obfuscator_.classList.toggle(is_drawer_open);
    // Set accessibility properties.
    if (drawer_.classList.contains(is_drawer_open)) {
      drawer_.setAttribute('aria-hidden', 'false');
      drawerButton.setAttribute('aria-expanded', 'true');
    } else {
      drawer_.setAttribute('aria-hidden', 'true');
      drawerButton.setAttribute('aria-expanded', 'false');
    }
  };
Stef
  • 644
  • 1
  • 8
  • 25
0

add this code to a custom button 'click' event (tested on version 1.3.0)

$(".mdl-layout__drawer, .mdl-layout__obfuscator").toggleClass("is-visible");
Farzad.Kamali
  • 553
  • 4
  • 10