颜色模式按钮

ButtonGitHub
一个用于在浅色和深色模式之间切换的按钮。

用法

ColorModeButton 组件扩展了 Button 组件,因此您可以传递任何属性,例如 colorvariantsize 等。

<template>
  <UColorModeButton />
</template>
按钮默认使用 color="neutral"variant="ghost"

示例

使用自定义图标

使用 app.config.ts 通过 ui.icons 属性来自定义图标。

app.config.ts
export default defineAppConfig({
  ui: {
    icons: {
      light: 'i-ph-sun',
      dark: 'i-ph-moon'
    }
  }
})

使用 vite.config.ts 通过 ui.icons 属性来自定义图标。

vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'

export default defineConfig({
  plugins: [
    vue(),
    ui({
      ui: {
        icons: {
          light: 'i-ph-sun',
          dark: 'i-ph-moon'
        }
      }
    })
  ]
})

带回退插槽

由于按钮被包装在ClientOnly组件中,您可以传递一个 fallback 插槽来在组件加载时显示一个占位符。

<template>
  <UColorModeButton>
    <template #fallback>
      <UButton loading variant="ghost" color="neutral" />
    </template>
  </UColorModeButton>
</template>

API

属性

属性默认值类型
as'button'any

此组件在不是链接时应呈现的元素或组件。

color'neutral'"primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral"
variant'ghost'"link" | "ghost" | "solid" | "outline" | "soft" | "subtle"
trailingIconany

在右侧显示图标。

namestring
尾部boolean

当为 true 时,图标将显示在右侧。

loadingboolean

当为 true 时,将显示加载图标。

图标any

根据 leadingtrailing 属性显示图标。

尺寸'md'"md" | "xs" | "sm" | "lg" | "xl"
avatarAvatarProps

在左侧显示头像。

autofocusfalse | true | "true" | "false"
disabledboolean
type'button'"reset" | "submit" | "button"

当不是链接时,按钮的类型。

labelstring
activeColor"primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral"
activeVariant"link" | "ghost" | "solid" | "outline" | "soft" | "subtle"
正方形boolean

以四边等距内边距渲染按钮。

blockboolean

渲染全宽按钮。

loadingAutoboolean

根据 @click promise 状态自动设置加载状态

前置boolean

当为 true 时,图标将显示在左侧。

leadingIconany

在左侧显示图标。

loadingIconappConfig.ui.icons.loadingany

loading prop 为 true 时显示的图标。

ui{ base?: ClassNameValue; label?: ClassNameValue; leadingIcon?: ClassNameValue; leadingAvatar?: ClassNameValue; leadingAvatarSize?: ClassNameValue; trailingIcon?: ClassNameValue; }
此组件还支持所有原生 <button> HTML 属性。

插槽

插槽类型

更新日志

184ea— chore: 减少类型冗余,通过省略操作按钮中的链接属性

2ce9a— 修复:改进图标类合并

5f30c— 修复:缺少图标导入

1d1c6— 修复:使用 CSS 显示颜色模式图标 (#5394)

5b177— feat: 扩展原生 HTML 属性 (#5348)

5cb65— 特性:导入 @nuxt/ui-pro 组件