作者头像

按钮组件 - MaterialButton link

link

MaterialButton 是一个符合 Material Design 3 Expressive 规范的基础交互组件。它支持多种变体、尺寸和形状,并且能够自动透传所有 HTML 属性和事件监听器。

核心特性 link

link
  • 高度可定制:支持多种变体(Button, Chip)、形状(Round, Square)和颜色主题。
  • 自动属性透传:通过 v-bind="$attrs",可以像使用原生 buttona 标签一样传递属性和监听事件。
  • 智能渲染:根据是否提供 href 属性,自动切换渲染为 <a> 标签或 <button> 标签。
  • 内置交互反馈:集成 StateLayer,提供 Material 风格的悬停、聚焦和激活状态反馈。

组件属性 (Props) link

link
属性名类型默认值可选值说明
variantstring"button""button", "chip"组件变体
shapestring"button"变体默认"round""chip"变体默认"square""round", "square"按钮形状
sizestring"s""xs", "s", "m", "l", "xl"按钮尺寸
colorstring"filled""elevated", "filled", "tonal", "outlined", "standard", "text"颜色变体
iconstring--图标名称(Material Symbols)
hrefstring--链接地址(提供时渲染为 <a>
targetstring"_blank""_blank", "_self", etc.链接打开方式
currentStatestring"none""hover", "focus", "active", "none"手动强制指定状态

组件事件 (Events) link

link

由于启用了属性透传,组件支持所有原生 HTML 事件。

事件名说明
@click点击事件
@mouseenter鼠标进入事件
@mousedown鼠标按下事件
...其他标准鼠标及键盘事件

使用示例 link

link

1. 样式与配置全览 link

link

下表展示了 MaterialButton 在不同属性组合下的视觉表现。

变体形状大小颜色图标内容演示
buttonroundsfilled-Filled
buttonroundstonal-Tonal
buttonroundsoutlined-Outlined
buttonroundselevated-Elevated
buttonroundstext-Text
buttonroundsfilledhome发送
buttonroundstonalbuild-
buttonroundxsfilled-Extra Small
buttonsquarelfilled-Large Square
buttonroundxlfilledaddExtra Large
chipsquaresfilled-Chip Button

4. 手动状态控制 link

link

通过 currentState 属性,可以强制按钮显示特定的交互状态(如在演示或联动显示时)。

vue
<MaterialButton currentState="hover">强制悬停状态</MaterialButton>
<MaterialButton currentState="active" color="tonal">强制激活状态</MaterialButton>

5. 事件绑定示例 link

link

您可以像使用普通按钮一样绑定事件。

6. 链接模式 link

link

当提供 href 时,组件会渲染为 <a> 标签。

vue
<MaterialButton href="https://google.com" target="_blank" icon="open_in_new">访问 Google</MaterialButton>
open_in_new访问 Google