问题
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 staticline height
and staticheight
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