按钮

使用 Bootstrap 的自定义按钮样式,支持多种尺寸、状态等,用于表单、对话框及更多场景中的操作。

Layer: components

基类

按钮有一个标准的 .btn 类,用于设置内边距和内容对齐等基本样式,以及用于基本无障碍的回退视觉样式。将此改为另一个按钮类,即可为特定的按钮变体和主题应用不同的视觉样式。

HTML
<button type="button" class="btn">Base class</button>

.btn 类旨在作为你自定义按钮样式的起点,而我们提供的按钮变体可与 .theme-* 类一起使用,或作为你自定义样式的基础。

演练场

Bootstrap 包含若干按钮变体,每种都有其自己的语义用途,并附加了一些额外选项以实现更多控制。结合我们的工具类,你拥有一组非常强大的默认值可供选择。

HTML
<button type="button" class="btn-solid theme-primary btn-sm" data-button-type="text">Button</button>
<button type="button" class="btn-solid theme-primary" data-button-type="left-icon">
  <svg class="bi" width="16" height="16" aria-hidden="true">
    <use href="#arrow-left" />
  </svg>
  Left icon
</button>
<button type="button" class="btn-solid theme-primary btn-sm" data-button-type="right-icon">
  Right icon
  <svg class="bi" width="16" height="16" aria-hidden="true">
    <use href="#arrow-right" />
  </svg>
</button>
<button type="button" class="btn-solid btn-icon theme-primary btn-sm" data-button-type="icon-only" aria-label="Icon only">
  <svg class="bi" width="16" height="16" aria-hidden="true">
    <use href="#plus-lg" />
  </svg>
</button>

变体

一次性比较所有按钮变体和样式:

HTML
<button type="button" class="btn-solid theme-primary">Primary</button>
<button type="button" class="btn-outline theme-primary">Primary</button>
<button type="button" class="btn-subtle theme-primary">Primary</button>
<button type="button" class="btn-text theme-primary">Primary</button>
<button type="button" class="btn-solid theme-accent">Accent</button>
<button type="button" class="btn-outline theme-accent">Accent</button>
<button type="button" class="btn-subtle theme-accent">Accent</button>
<button type="button" class="btn-text theme-accent">Accent</button>
<button type="button" class="btn-solid theme-success">Success</button>
<button type="button" class="btn-outline theme-success">Success</button>
<button type="button" class="btn-subtle theme-success">Success</button>
<button type="button" class="btn-text theme-success">Success</button>
<button type="button" class="btn-solid theme-danger">Danger</button>
<button type="button" class="btn-outline theme-danger">Danger</button>
<button type="button" class="btn-subtle theme-danger">Danger</button>
<button type="button" class="btn-text theme-danger">Danger</button>
<button type="button" class="btn-solid theme-warning">Warning</button>
<button type="button" class="btn-outline theme-warning">Warning</button>
<button type="button" class="btn-subtle theme-warning">Warning</button>
<button type="button" class="btn-text theme-warning">Warning</button>
<button type="button" class="btn-solid theme-info">Info</button>
<button type="button" class="btn-outline theme-info">Info</button>
<button type="button" class="btn-subtle theme-info">Info</button>
<button type="button" class="btn-text theme-info">Info</button>
<button type="button" class="btn-solid theme-inverse">Inverse</button>
<button type="button" class="btn-outline theme-inverse">Inverse</button>
<button type="button" class="btn-subtle theme-inverse">Inverse</button>
<button type="button" class="btn-text theme-inverse">Inverse</button>
<button type="button" class="btn-solid theme-secondary">Secondary</button>
<button type="button" class="btn-outline theme-secondary">Secondary</button>
<button type="button" class="btn-subtle theme-secondary">Secondary</button>
<button type="button" class="btn-text theme-secondary">Secondary</button>
无障碍提示:使用颜色传达含义

仅使用颜色来增添含义只能提供视觉上的提示,无法传达给屏幕阅读器等辅助技术的用户。请确保含义本身从内容中就能显而易见(例如,具有_足够_颜色对比度的可见文本),或通过其他方式包含,例如使用 .visually-hidden 类隐藏的附加文本。

链接按钮

使用 .btn-link 类创建看起来像链接的按钮。

HTML
<button type="button" class="btn btn-link">Link</button>

链接按钮也可以使用主题颜色设置样式。

HTML
<button type="button" class="btn btn-link theme-primary">Primary</button>
<button type="button" class="btn btn-link theme-accent">Accent</button>
<button type="button" class="btn btn-link theme-success">Success</button>
<button type="button" class="btn btn-link theme-danger">Danger</button>
<button type="button" class="btn btn-link theme-warning">Warning</button>
<button type="button" class="btn btn-link theme-info">Info</button>
<button type="button" class="btn btn-link theme-inverse">Inverse</button>
<button type="button" class="btn btn-link theme-secondary">Secondary</button>

带样式的按钮

使用 .btn-styled 修饰符类,通过渐变和阴影为实心按钮添加视觉深度。这提供了更具三维感的外观,可以使用 CSS 变量进一步自定义。

HTML
<button class="btn-solid btn-styled theme-primary">Primary</button>
<button class="btn-solid btn-styled theme-accent">Accent</button>
<button class="btn-solid btn-styled theme-success">Success</button>
<button class="btn-solid btn-styled theme-danger">Danger</button>
<button class="btn-solid btn-styled theme-warning">Warning</button>
<button class="btn-solid btn-styled theme-info">Info</button>
<button class="btn-solid btn-styled theme-inverse">Inverse</button>
<button class="btn-solid btn-styled theme-secondary">Secondary</button>

渐变和阴影可以通过 CSS 变量进行自定义:

变量描述
--bs-btn-gradient-start渐变顶部(默认:rgb(255 255 255 / 12.5%))
--bs-btn-gradient-end渐变底部(默认:rgb(0 0 0 / 7.5%))
--bs-btn-border-mix-color要与按钮背景混合的颜色(默认:#000)
--bs-btn-border-mix-amount要与按钮背景混合的颜色量(默认:10%)
--bs-btn-border-hover-mix-amount悬停时要与按钮背景混合的颜色量(默认:12.5%)
--bs-btn-border-active-mix-amount激活时要与按钮背景混合的颜色量(默认:20%)
--bs-btn-shadow静止状态阴影
--bs-btn-active-shadow按下/激活状态阴影

按钮标签

.btn 类设计为与 <button> 元素一起使用。不过,你也可以在 <a> 或 <input> 元素上使用这些类(尽管某些浏览器可能会应用略微不同的渲染)。

当在用于触发页内功能(如折叠内容)的 <a> 元素上使用按钮类时,而不是链接到新页面或当前页面内的区块,应为这些链接提供 role="button",以适当地向屏幕阅读器等辅助技术传达其用途。

Link
HTML
<a class="btn-solid theme-primary" href="#" role="button">Link</a>
<button class="btn-solid theme-primary" type="submit">Button</button>
<input class="btn-solid theme-primary" type="button" value="Input">
<input class="btn-solid theme-primary" type="submit" value="Submit">
<input class="btn-solid theme-primary" type="reset" value="Reset">

尺寸

想要更大或更小的按钮?添加 .btn-lg 或 .btn-sm 以获得额外的尺寸。

HTML
<button type="button" class="btn-solid theme-primary btn-lg">Large button</button>
<button type="button" class="btn-solid theme-secondary btn-lg">Large button</button>
HTML
<button type="button" class="btn-solid theme-primary btn-sm">Small button</button>
<button type="button" class="btn-solid theme-secondary btn-sm">Small button</button>
HTML
<button type="button" class="btn-solid theme-primary btn-xs">Extra small button</button>
<button type="button" class="btn-solid theme-secondary btn-xs">Extra small button</button>

你甚至可以使用 CSS 变量自定义尺寸:

HTML
<button type="button" class="btn-solid theme-primary"
        style="--bs-btn-padding-y: .25rem; --bs-btn-padding-x: .5rem; --bs-btn-font-size: .75rem;">
  Custom button
</button>

图标按钮

使用 .btn-icon 表示仅包含图标的方形按钮。该类设置 aspect-ratio: 1 并移除内边距,根据其 min-height 使按钮成为完美的正方形。请务必在仅图标按钮上包含 aria-label 属性,以确保屏幕阅读器用户可访问。

HTML
<button type="button" class="btn-icon" aria-label="Magic">
  <svg class="bi" width="16" height="16" fill="currentColor" aria-hidden="true"><use href="#magic"></use></svg>
</button>
<button type="button" class="btn-solid theme-primary btn-icon" aria-label="Confirm">
  <svg class="bi" width="16" height="16" fill="currentColor" aria-hidden="true"><use href="#check2"></use></svg>
</button>
<button type="button" class="btn-outline theme-secondary btn-icon" aria-label="Auto theme">
  <svg class="bi" width="16" height="16" fill="currentColor" aria-hidden="true"><use href="#circle-half"></use></svg>
</button>
<button type="button" class="btn-subtle theme-warning btn-icon" aria-label="Light theme">
  <svg class="bi" width="16" height="16" fill="currentColor" aria-hidden="true"><use href="#sun-fill"></use></svg>
</button>

结合尺寸类以获得不同的图标按钮尺寸:

HTML
<button type="button" class="btn-solid theme-primary btn-icon btn-xs" aria-label="Confirm">
  <svg class="bi" width="12" height="12" fill="currentColor" aria-hidden="true"><use href="#check2"></use></svg>
</button>
<button type="button" class="btn-solid theme-primary btn-icon btn-sm" aria-label="Confirm">
  <svg class="bi" width="14" height="14" fill="currentColor" aria-hidden="true"><use href="#check2"></use></svg>
</button>
<button type="button" class="btn-solid theme-primary btn-icon" aria-label="Confirm">
  <svg class="bi" width="16" height="16" fill="currentColor" aria-hidden="true"><use href="#check2"></use></svg>
</button>
<button type="button" class="btn-solid theme-primary btn-icon btn-lg" aria-label="Confirm">
  <svg class="bi" width="20" height="20" fill="currentColor" aria-hidden="true"><use href="#check2"></use></svg>
</button>

禁用状态

通过为任何 <button> 元素添加 disabled 布尔属性,使按钮看起来处于非活动状态。禁用的按钮具有 pointer-events: none,阻止触发悬停和激活状态,并有一些额外的样式来指示禁用状态。

HTML
<button type="button" class="btn-solid theme-primary" disabled>Primary button</button>
<button type="button" class="btn-solid theme-secondary" disabled>Button</button>
<button type="button" class="btn-outline theme-primary" disabled>Primary button</button>
<button type="button" class="btn-outline theme-secondary" disabled>Button</button>

使用 <a> 元素的禁用按钮行为略有不同。在 <a> 元素上使用 .disabled 类而不是属性,以使其视觉上呈现为禁用状态。如果你需要保留 href 属性,可能需要额外的 HTML 来完全禁用链接功能。

HTML
<a class="btn-solid theme-primary disabled" role="button" aria-disabled="true">Primary link</a>
<a class="btn-solid theme-secondary disabled" role="button" aria-disabled="true">Link</a>

如果存在 href 属性,请添加 tabindex="-1" 和 aria-disabled="true",以防止键盘焦点和辅助技术与该链接交互。

HTML
<a href="#" class="btn-solid theme-primary disabled" tabindex="-1" role="button" aria-disabled="true">Primary link</a>
<a href="#" class="btn-solid theme-secondary disabled" tabindex="-1" role="button" aria-disabled="true">Link</a>

你可能还想使用自定义 JavaScript 完全禁用链接功能。

块级按钮

使用 .d-grid 工具类创建响应式的全宽“块级按钮”堆栈。使用 gap 工具类将它们间隔开。

HTML
<div class="d-grid gap-2">
  <button class="btn-solid theme-primary" type="button">Button</button>
  <button class="btn-solid theme-primary" type="button">Button</button>
</div>

这里我们创建一个响应式变体,从垂直堆叠的按钮开始,直到 md 断点,此时 .md:d-block 替换 .d-grid 类,从而使 gap-2 工具类失效。调整浏览器大小以查看它们的变化。

HTML
<div class="d-grid gap-2 md:d-block">
  <button class="btn-solid theme-primary" type="button">Button</button>
  <button class="btn-solid theme-primary" type="button">Button</button>
</div>

你可以使用网格列宽类调整块级按钮的宽度。例如,对于半宽的“块级按钮”,使用 .col-6。也可以使用 .mx-auto 将其水平居中。

HTML
<div class="d-grid gap-2 col-6 mx-auto">
  <button class="btn-solid theme-primary" type="button">Button</button>
  <button class="btn-solid theme-primary" type="button">Button</button>
</div>

当按钮水平排列时,可以使用额外的工具类来调整按钮的对齐方式。这里,我们采用了之前响应式示例,并在按钮上添加了一些 flex 工具类和 margin 工具类,以便在它们不再堆叠时右对齐。

HTML
<div class="d-grid gap-2 md:d-flex md:justify-content-end">
  <button class="btn-solid theme-primary md:me-2" type="button">Button</button>
  <button class="btn-solid theme-primary" type="button">Button</button>
</div>

切换按钮

通过在带有按钮类的 <label> 元素上添加 .btn-check,并在其中嵌套复选框或单选输入框,创建类似按钮的复选框和单选按钮。输入框在视觉上被隐藏,而标签提供按钮的外观。这些切换按钮如果需要,可以进一步分组到按钮组中。

在视觉上,这些切换按钮与我们的 JavaScript 切换按钮 完全相同,但辅助技术对它们的传达方式不同。由于复选框在底层是复选框,屏幕阅读器会将复选框开关朗读为 “checked”/“not checked”。JavaScript 切换按钮被朗读为 “button”/“button pressed”。你可以自行决定哪种方法更适合你的用例。

复选框

复选框可以被选中、取消选中或禁用。基于复选框的切换按钮只切换单个按钮的状态。

HTML
<label class="btn-check btn-solid theme-primary">
  <input type="checkbox" checked autocomplete="off">
  Checked
</label>

<label class="btn-check btn-solid theme-primary">
  <input type="checkbox" autocomplete="off">
  Toggle
</label>

<label class="btn-check btn-solid theme-primary">
  <input type="checkbox" disabled autocomplete="off">
  Disabled
</label>

单选按钮

单选按钮可以被选中、取消选中或禁用。使用基于单选按钮的切换按钮在一组按钮之间切换状态,这些按钮包含具有相同 name 属性值的输入元素。

HTML
<label class="btn-check btn-solid theme-primary">
  <input type="radio" name="options" autocomplete="off" checked>
  Checked
</label>

<label class="btn-check btn-solid theme-primary">
  <input type="radio" name="options" autocomplete="off">
  Toggle
</label>

<label class="btn-check btn-solid theme-primary">
  <input type="radio" name="options" disabled autocomplete="off">
  Disabled
</label>

<label class="btn-check btn-solid theme-primary">
  <input type="radio" name="options" autocomplete="off">
  Toggle
</label>

按钮插件

按钮插件允许你创建简单的开/关切换按钮。

在视觉上,这些切换按钮与我们的输入切换按钮完全相同,但辅助技术对它们的传达方式不同。由于复选框在底层是复选框,屏幕阅读器会将复选框开关朗读为 “checked”/“not checked”。JavaScript 切换按钮被朗读为 “button”/“button pressed”。你可以自行决定哪种方法更适合你的用例。

切换状态

添加 data-bs-toggle="button" 以切换按钮的 active 状态。还要添加 aria-pressed,以便辅助技术将该控件朗读为切换按钮。对于默认状态使用 aria-pressed="false"。如果你预先切换按钮,请添加 .active 类并设置 aria-pressed="true"。

HTML
<button type="button" class="btn-solid theme-primary" data-bs-toggle="button" aria-pressed="false">Toggle button</button>
<button type="button" class="btn-solid theme-primary active" data-bs-toggle="button" aria-pressed="true">Active toggle button</button>
<button type="button" class="btn-solid theme-primary" disabled data-bs-toggle="button" aria-pressed="false">Disabled toggle button</button>
HTML
<a href="#" class="btn-solid theme-primary" role="button" data-bs-toggle="button" aria-pressed="false">Toggle link</a>
<a href="#" class="btn-solid theme-primary active" role="button" data-bs-toggle="button" aria-pressed="true">Active toggle link</a>
<a class="btn-solid theme-primary disabled" aria-disabled="true" role="button" data-bs-toggle="button" aria-pressed="false">Disabled toggle link</a>

aria-pressed 没有默认值。没有该属性的按钮会被朗读为普通按钮,而不是切换按钮。作为安全网,Bootstrap 会在 DOMContentLoaded 时为每个 [data-bs-toggle="button"] 添加缺失的属性,使用 .active 类来选择值。无论如何,请在你的标记中写入它,以便在我们的 JavaScript 运行之前状态就是正确的。

通过数据属性

属性描述
data-bs-toggle="button"通过按钮插件切换按钮的激活(pressed)状态。

依赖

The button plugin requires the following JavaScript files if you’re building Bootstrap’s JS from source:

FileDescription
js/src/button.tsMain button component
js/src/base-component.tsBase component class
js/src/dom/data.tsElement data store
js/src/dom/event-handler.tsEvent handling utilities
js/src/dom/manipulator.tsData attribute manipulation
js/src/dom/selector-engine.tsDOM selector utilities
js/src/util/component-functions.tsShared component helpers
js/src/util/config.tsConfiguration base class
js/src/util/index.tsCore utility functions

方法

你可以使用按钮构造函数创建按钮实例,例如:

JavaScript
          const bsButton = new bootstrap.Button('#myButton')
        
方法描述
dispose销毁元素的按钮。(移除 DOM 元素上存储的数据)
getInstance静态方法,允许你获取与 DOM 元素关联的按钮实例,你可以这样使用:bootstrap.Button.getInstance(element)。
getOrCreateInstance静态方法,返回与 DOM 元素关联的按钮实例,如果尚未初始化则创建一个新实例。你可以这样使用:bootstrap.Button.getOrCreateInstance(element)。
toggle切换按下状态。使按钮呈现已被激活的外观。

例如,切换所有按钮

JavaScript
          document.querySelectorAll('.btn').forEach(buttonElement => {
  const button = bootstrap.Button.getOrCreateInstance(buttonElement)
  button.toggle()
})
        

CSS

变量

按钮使用 CSS 变量进行实时自定义。这些变量设置在基础 .btn 类上,并由所有按钮变体继承。

$button-tokens: defaults(
  (
    --btn-gap: var(--btn-input-gap),
    --btn-min-height: var(--btn-input-min-height),
    --btn-padding-x: var(--btn-input-padding-x),
    --btn-padding-y: var(--btn-input-padding-y),
    --btn-font-size: var(--btn-input-font-size),
    --btn-font-weight: var(--btn-input-font-weight),
    --btn-line-height: var(--btn-input-line-height),
    --btn-color: var(--fg-body),
    --btn-white-space: nowrap,
    --btn-border-width: var(--border-width),
    --btn-border-color: transparent,
    --btn-border-radius: var(--radius-5),
    --btn-hover-border-color: transparent,
    --btn-disabled-opacity: .65,
    --btn-transition-property: "color, background-color, border-color, box-shadow",
    --btn-transition-duration: .15s,
    --btn-transition-timing: ease-in-out,
  ),
  $button-tokens
);

每个变体类(.btn-solid、.btn-outline 等)根据当前主题颜色更新这些变量,使按钮在与 .theme-* 类一起使用时能够自动适应。

带样式的按钮使用 $button-styled-tokens Sass 映射。

$button-styled-tokens: defaults(
  (
    --btn-gradient-start: rgb(255 255 255 / 12.5%),
    --btn-gradient-end: rgb(0 0 0 / 7.5%) ,
    --btn-border-mix-color: #000,
    --btn-border-mix-amount: 10%,
    --btn-border-hover-mix-amount: 12.5%,
    --btn-border-active-mix-amount: 20%,
    --btn-shadow: "0 1px 2px rgb(0 0 0 / 15%), inset 0 1px 0 rgb(255 255 255 / 10%)",
    --btn-active-shadow: inset 0 2px 4px rgb(0 0 0 / .15),
  ),
  $button-styled-tokens
);

链接按钮使用 $button-link-tokens Sass 映射。

$button-link-tokens: defaults(
  (
    --btn-font-weight: var(--font-weight-normal),
    --btn-color: var(--link-color),
    --btn-bg: transparent,
    --btn-border-color: transparent,
    --btn-hover-color: var(--link-hover-color),
    --btn-hover-bg: transparent,
    --btn-hover-border-color: transparent,
    --btn-active-color: var(--link-hover-color),
    --btn-active-bg: transparent,
    --btn-active-border-color: transparent,
    --btn-disabled-color: var(--fg-3),
    --btn-disabled-border-color: transparent,
  ),
  $button-link-tokens
);

Sass 映射

尺寸

按钮尺寸在 $button-sizes Sass 映射中定义。每个尺寸通过 Sass 循环指定若干变量。这些大多与文本表单控件一致。

$button-sizes: ();
$button-sizes: defaults(
  ("xs", "sm", "lg"),
  $button-sizes
);

变体

按钮变体在 $button-variants Sass 映射中定义。每个变体指定其基础、悬停和激活状态要使用的主题颜色 tokens。该映射使用 token 名称(如 "bg"、"contrast"、"border"),它们引用当前主题的颜色调色板。

$button-variants: defaults(
  (
    "solid": (
      "base": (
        "bg": "bg",
        "color": "contrast",
        "border-color": "bg"
      ),
      "hover": (
        "bg": "bg",
        "border-color": "bg",
        "color": "contrast"
      ),
      "active": (
        "bg": "bg",
        "border-color": "bg",
        "color": "contrast"
      )
    ),
    "outline": (
      "base": (
        "bg": "transparent",
        "color": "fg",
        "border-color": "border"
      ),
      "hover": (
        "bg": "bg",
        "color": "contrast",
        "border-color": "bg"
      ),
      "active": (
        "bg": "bg",
        "color": "contrast",
        "border-color": "bg"
      )
    ),
    "subtle": (
      "base": (
        "bg": "bg-subtle",
        "color": "fg",
        "border-color": "transparent"
      ),
      "hover": (
        "bg": ("bg-muted", "bg-subtle"),
        "color": "fg-emphasis"
      ),
      "active": (
        "bg": "bg-subtle",
        "color": "fg-emphasis"
      )
    ),
    "text": (
      "base": (
        "color": "fg",
        "bg": "transparent",
        "border-color": "transparent"
      ),
      "hover": (
        "color": "fg",
        "bg": "bg-subtle"
      ),
      "active": (
        "color": "fg",
        "bg": "bg-subtle"
      )
    )
  ),
  $button-variants
);

要添加自定义变体,请在导入 Bootstrap 的按钮之前扩展该映射:

SCSS
          @use "bootstrap/scss/buttons/button" with (
  $button-variants: (
    "solid": ( /* ... */ ),
    "outline": ( /* ... */ ),
    "subtle": ( /* ... */ ),
    "text": ( /* ... */ ),
    "ghost": (
      "base": (
        "bg": "transparent",
        "color": "text",
        "border-color": "transparent"
      ),
      "hover": (
        "bg": "bg-subtle",
        "color": "text-emphasis"
      ),
      "active": (
        "bg": "bg-subtle",
        "color": "text-emphasis"
      )
    )
  )
);
        

要移除你不需要的变体,请使用 Sass 的 map.remove():

SCSS
          @use "sass:map";

@use "bootstrap/scss/buttons/button" as *button* with (
  $button-variants: map.remove($button-variants, "text", "subtle")
);
        

选择器

$btn-variant-selectors 列表控制哪些选择器接收基础按钮样式(tokens、display、padding、font 等)。默认情况下,它包括 .btn、.btn-link、.btn-icon 以及从 $button-variants 映射生成的所有类。这意味着像 .btn-solid 这样的变体类可以独立工作,而无需 .btn。

$btn-variant-selectors: (string.unquote(".btn"), string.unquote(".btn-link"), string.unquote(".btn-icon"));
@each $variant, $config in $button-variants {
  $btn-variant-selectors: list.append($btn-variant-selectors, string.unquote(".btn-#{$variant}"), comma);
}

Sass 循环

变体循环

变体循环遍历 $button-variants 以生成每个变体类。它使用映射中定义的主题颜色 tokens 为基础、悬停、激活和禁用状态设置 CSS 变量。

@each $variant, $config in $button-variants {
  .btn-#{$variant} {
    @each $property, $value in map.get($button-variants, $variant, "base") {
      @if $value == "transparent" {
        --btn-#{$property}: transparent;
      } @else {
        --btn-#{$property}: var(--theme-#{$value});
      }
    }

    @each $property, $value in map.get($button-variants, $variant, "active") {
      @if $value == "transparent" {
        --btn-active-#{$property}: transparent;
      } @else if $value == "bg-subtle" {
        --btn-active-#{$property}: var(--theme-#{$value});
      } @else {
        --btn-active-#{$property}: oklch(from var(--theme-#{$value}) calc(l * .9) calc(c * 1.15) h);
      }
    }
    @each $property, $value in map.get($button-variants, $variant, "base") {
      @if $value == "transparent" {
        --btn-disabled-#{$property}: transparent;
      } @else {
        --btn-disabled-#{$property}: var(--theme-#{$value});
      }
    }

    &:hover {
      @each $property, $value in map.get($button-variants, $variant, "hover") {
        @if $value == "transparent" {
          --btn-hover-#{$property}: transparent;
        } @else if meta.type-of($value) == "list" {
          $first-value: list.nth($value, 1);
          $second-value: list.nth($value, 2);
          --btn-hover-#{$property}: color-mix(in oklch, var(--theme-#{$first-value}) 50%, var(--theme-#{$second-value}));
        } @else if $value == "bg-subtle" {
          --btn-hover-#{$property}: var(--theme-#{$value});
        } @else {
          --btn-hover-#{$property}: oklch(from var(--theme-#{$value}) calc(l * .95) calc(c * 1.1) h);
        }
      }
    }

    &:focus-visible {
      outline-color: var(--theme-focus-ring);
    }

    &:active,
    &.active,
    &.btn-check:has(input:checked) {
      @each $property, $value in map.get($button-variants, $variant, "active") {
        @if $value == "transparent" {
          --btn-active-#{$property}: transparent;
        } @else if $value == "bg-subtle" {
          --btn-active-#{$property}: var(--theme-#{$value});
        } @else {
          --btn-active-#{$property}: oklch(from var(--theme-#{$value}) calc(l * .9) calc(c * 1.15) h);
        }
      }
    }

    // Disabled state for toggle buttons
    &:disabled,
    &.disabled,
    &.btn-check:has(input:disabled) {
      @each $property, $value in map.get($button-variants, $variant, "base") {
        @if $value == "transparent" {
          --btn-disabled-#{$property}: transparent;
        } @else {
          --btn-disabled-#{$property}: var(--theme-#{$value});
        }
      }
    }
  }
}

尺寸循环

尺寸循环遍历 $button-sizes 以生成尺寸修饰符类,为每个尺寸设置适当的 CSS 变量,包括按钮组。

@each $size, $_ in $button-sizes {
  .btn-#{$size},
  .btn-group-#{$size} > [class*="btn-"] {
    --btn-gap: var(--btn-input-#{$size}-gap);
    --btn-min-height: var(--btn-input-#{$size}-min-height);
    --btn-padding-y: var(--btn-input-#{$size}-padding-y);
    --btn-padding-x: var(--btn-input-#{$size}-padding-x);
    --btn-font-size: var(--btn-input-#{$size}-font-size);
    --btn-line-height: var(--btn-input-#{$size}-line-height);
    --btn-border-radius: var(--btn-input-#{$size}-border-radius);
  }
}