Material-components-web: MDC-Menu cannot be pinned

Created on 7 Oct 2020  路  3Comments  路  Source: material-components/material-components-web

Hello, I am trying to anchor a menu to a button to open the menu, I followed the instructions in the document but there is no effect, is there something I am missing?

I leave the example

https://codepen.io/jimmyhdolores/pen/QWEWaWy

Versions:

"material-components-web": "^7.0.0"

bug

All 3 comments

I've had similar woes. Anchoring using the JS api just doesn't seem to work that well, and the documentation for this library is absolutely shocking. I found the one way to get it to work was this

menu.setFixedPosition(true);

however things get a bit weird if you resize with the menu open.

A more reliable way is to wrap the anchor element and the menu surface together inside another element with the class mdc-menu-surface--anchor (and remove the setAnchorElement call). The library will automatically anchor the menu based on this markup: https://codepen.io/dabrowne/pen/RwRwJLo

This is documented here: https://github.com/material-components/material-components-web/tree/master/packages/mdc-menu#anchors-and-positioning

Hello @dabrowne thank you very much for the information and for example I am going to test it.

Hi @jimyhdolores ! It is unclear what you mean by _pinned_.

For your use case you'll have to follow the documentation mentioned in Anchor To Element Within Wrapper section. Menu wrapper (i.e., mdc-menu-surface--anchor) should only contain the Menu and menu button.

Here is the updated codepen with working example.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

traviskaufman picture traviskaufman  路  4Comments

kimurakenshi picture kimurakenshi  路  3Comments

traviskaufman picture traviskaufman  路  3Comments

mether picture mether  路  4Comments

patrickrodee picture patrickrodee  路  3Comments