Badge 徽章
徽章组件会在其子项(们)的右上角生成一个小徽章。
4
<Badge badgeContent={4} color="primary">
<MailIcon color="action" />
</Badge>
自定义徽章
以下是自定义组件的一个示例。 您可以在 重写文档页面 中了解更多有关此内容的信息。
<IconButton aria-label="cart">
<StyledBadge badgeContent={4} color="secondary">
<ShoppingCartIcon />
</StyledBadge>
</IconButton>
1
当 badgeContent 为零时,徽章组件将会自动隐藏。 您可以使用 showZero
属性覆盖它。
0
<Badge color="secondary" badgeContent={0}>
<MailIcon />
</Badge>
<Badge color="secondary" badgeContent={0} showZero>
<MailIcon />
</Badge>
9999+999+
<Badge color="secondary" badgeContent={99}>
<MailIcon />
</Badge>
<Badge color="secondary" badgeContent={100}>
<MailIcon />
</Badge>
<Badge color="secondary" badgeContent={1000} max={999}>
<MailIcon />
</Badge>
<Badge color="secondary" variant="dot">
<MailIcon />
</Badge>
<Badge color="secondary" badgeContent=" ">
{rectangle}
</Badge>
<Badge color="secondary" badgeContent=" " variant="dot">
{rectangle}
</Badge>
<Badge color="secondary" overlap="circular" badgeContent=" ">
{circle}
</Badge>
<Badge color="secondary" overlap="circular" badgeContent=" " variant="dot">
{circle}
</Badge>
11299+999+
<Badge
anchorOrigin={{
vertical: 'top',
horizontal: 'right',
}}
>
Unstyled
The badge also comes with an unstyled version. It's ideal for doing heavy customizations and minimizing bundle size.
import BadgeUnstyled from '@material-ui/unstyled/BadgeUnstyled';
5
<StyledBadge badgeContent={5} overlap="circular">
<BadgeContent />
</StyledBadge>
<StyledBadge badgeContent={5} variant="dot" overlap="circular">
<BadgeContent />
</StyledBadge>
无障碍设计
You can't rely on the content of the badge to be announced correctly. You should provide a full description, for instance, with aria-label
:
<IconButton aria-label={notificationsLabel(100)}>
<Badge badgeContent={100} color="secondary">
<MailIcon />
</Badge>
</IconButton>