最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Prevent MDC Menu from closing after clicking first menu item - Stack Overflow

programmeradmin9浏览0评论

We are using MDC menu ponent. I am looking for a way to not close the menu when clicked on the first item in the menu.

I tried applying a class to mdc-list-item and specifying cursor as auto, but it is not working.

.menu-do-not-close {
   cursor: auto !important;
}

below is the example fiddle

/

Could you provide some direction on how to achieve this.

We are using MDC menu ponent. I am looking for a way to not close the menu when clicked on the first item in the menu.

I tried applying a class to mdc-list-item and specifying cursor as auto, but it is not working.

.menu-do-not-close {
   cursor: auto !important;
}

below is the example fiddle

https://jsfiddle/phani25485/Lt6q2gxa/2/

Could you provide some direction on how to achieve this.

Share Improve this question edited Oct 25, 2018 at 19:12 benvc 15.2k4 gold badges38 silver badges57 bronze badges asked Jul 6, 2018 at 13:30 user804401user804401 1,9949 gold badges43 silver badges75 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7 +50

You can stop the menu from closing after a specific item is selected by calling event.stopPropagation() on the mdc-list-item click event when the event target has a specific class assigned. You will need to add some additional code to handle the list item click event since this prevents the normal MDC menu click handling.

const menu = mdc.menu.MDCMenu.attachTo(document.querySelector('.mdc-menu'));
document.querySelector('.mdc-button').addEventListener('click', () => {
  menu.open = !menu.open;
});

//Prevent menu close when option with 'prevent-menu-close' class clicked
for (const option of document.querySelectorAll('.mdc-list-item')) {
  option.addEventListener('click', (event) => {
    const prevent = event.currentTarget.classList.contains('prevent-menu-close');
    if (prevent) {
      event.stopPropagation();
      // handle 'prevent-menu-close' list item click event
    }
  });
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Material Select</title>
    <link rel="stylesheet" href="https://fonts.googleapis./icon?family=Material+Icons">
    <link rel="stylesheet" href="https://fonts.googleapis./css?family=Roboto:300,400,500,700">
    <link href="https://unpkg./material-ponents-web@latest/dist/material-ponents-web.min.css" rel="stylesheet">
    <script src="https://unpkg./material-ponents-web@latest/dist/material-ponents-web.min.js"></script>
  </head>
  <body>
    <div class="mdc-menu-surface--anchor">
      <button class="mdc-button">
        <span class="mdc-button__label">Open Menu</span>
      </button>
      <div class="mdc-menu mdc-menu-surface">
        <ul class="mdc-list" role="menu" aria-hidden="true" aria-orientation="vertical" tabindex="-1">
          <li class="mdc-list-item prevent-menu-close" role="menuitem">
            <span class="mdc-list-item__text">Prevent Close</span>
          </li>
          <li class="mdc-list-item" role="menuitem">
            <span class="mdc-list-item__text">Another Menu Item</span>
          </li>
        </ul>
      </div>
    </div>
  </body>
</html>

发布评论

评论列表(0)

  1. 暂无评论