How to show full text and wrap in menu item in angular material

Deadly 提交于 2020-02-01 04:36:48

问题


I am trying to make a notification system, So I am using the mat-menu from angular material. I've increased the width of menu-item but the problem is that it is not showing the full content. It is only showing the text which comes in one line.

but I want to wrap the text on a new line if it exceeds that line like here

my compontent.html

  <button mat-button [matMenuTriggerFor]="notification"><mat-icon>add_alert</mat-icon></button>
  <mat-menu #notification="matMenu" [overlapTrigger]="false" class="my-full-width-menu">
    <button mat-menu-item style="white-space: normal">
      Learn one way to build applications with Angular and reuse your code and abilities to build
      apps for any deployment target. For web, mobile web, native mobile and native desktop.
    </button>
    <button mat-menu-item>Item 2</button>
  </mat-menu>

and CSS is

.mat-menu-panel.my-full-width-menu {
  max-width: none;
  width: 100vw;
  margin-left: -8px;
  margin-top: 24px;
}

Please tell me how to do that.


回答1:


Material spec does not allow for word wrap on buttons, with that said, if you still want to do this you will need to modify the mat-menu-item class in addition to the style="white-space: normal" on your button....

as the mat-menu-item class has static line height and static height to 48px that is preventing it from word wrapping.

::ng-deep button.mat-menu-item {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    cursor: pointer;
    outline: 0;
    border: none;
    -webkit-tap-highlight-color: transparent;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
    /** set line height to desired height */
    line-height: 18px;
    /** set height to auto */
    height: auto;
    padding: 0 16px;
    text-align: left;
    text-decoration: none;
    max-width: 100%;
    position: relative;
}

To set menu width, use the following.

::ng-deep div.mat-menu-panel {
    min-width: 112px;
      /** set width to 500px */
    max-width: 500px;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    max-height: calc(100vh - 48px);
    border-radius: 4px;
    outline: 0;
}

Stackblitz

https://stackblitz.com/edit/angular-qzbutg?embed=1&file=app/menu-overview-example.css


Please reference this issue, material spec does not allow for word wrap on buttons.

https://github.com/angular/material/issues/582


Reference Text label

Don’t. Don’t wrap text. For maximum legibility, a text label should remain on a single line.

https://material.io/design/components/buttons.html#anatomy



来源:https://stackoverflow.com/questions/54022588/how-to-show-full-text-and-wrap-in-menu-item-in-angular-material

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!