横幅

GitHub
在您网站顶部显示横幅,通知用户重要信息。

用法

标题

使用 title 属性在横幅上显示标题。

<template>
  <UBanner title="This is a banner with an important message." />
</template>

Icon

使用 icon 属性在横幅上显示图标。

<template>
  <UBanner icon="i-lucide-info" title="This is a banner with an icon." />
</template>

颜色

使用 color 属性更改横幅的颜色。

<template>
  <UBanner color="neutral" icon="i-lucide-info" title="This is a banner with an icon." />
</template>

关闭

使用 close 属性显示一个 按钮 以关闭横幅。默认为 false

当点击关闭按钮时,将发出一个 close 事件。
<template>
  <UBanner id="example" title="This is a closable banner." close />
</template>
关闭时,banner-${id} 将存储在本地存储中,以防止它再次显示。
对于上面的示例,banner-example 将存储在本地存储中。

关闭图标

使用 close-icon 属性自定义关闭按钮的 图标。默认为 i-lucide-x

<template>
  <UBanner
    title="This is a closable banner with a custom close icon."
    close
    close-icon="i-lucide-x-circle"
  />
</template>
你可以在 app.config.ts 中的 ui.icons.close 键下全局自定义此图标。
你可以在 vite.config.ts 中的 ui.icons.close 键下全局自定义此图标。

操作

使用 actions 属性为横幅添加一些 按钮 操作。

<script setup lang="ts">
const actions = ref([
  {
    label: 'Action 1',
    variant: 'outline'
  },
  {
    label: 'Action 2',
    trailingIcon: 'i-lucide-arrow-right'
  }
])
</script>

<template>
  <UBanner title="This is a banner with actions." :actions="actions" />
</template>
操作按钮默认为 color="neutral"size="xs"。你可以通过直接将这些值传递给每个操作按钮来定制它们。

您可以传递<NuxtLink>组件,例如 totargetrel 等。

<template>
  <UBanner
    to="https://nuxtlabs.com/"
    target="_blank"
    title="NuxtLabs is joining Vercel!"
    color="primary"
  />
</template>
当不是链接时,此组件应渲染为的元素或组件。默认为 button

示例

app.vue

在你的 app.vue 或布局中使用横幅组件

app.vue
<template>
  <UApp>
    <UBanner icon="i-lucide-construction" title="Nuxt UI v4 has been released!" />

    <UHeader />

    <UMain>
      <NuxtLayout>
        <NuxtPage />
      </NuxtLayout>
    </UMain>

    <UFooter />
  </UApp>
</template>

API

属性

属性默认值类型
as

'div'

any

此组件应渲染为的元素或组件。

id

'1'

string

一个保存到本地存储的唯一 ID,用于记住横幅是否已被关闭。更改此值以再次显示横幅。

图标

字符串 | 对象

显示在标题旁边的图标。

title

string

actions

ButtonProps[]

在标题旁边显示操作列表。 { color: '中性', size: 'xs' }

过渡到

字符串 | RouteLocationAsRelativeGeneric | RouteLocationAsPathGeneric

target

null | 字符串 & {} | "_blank" | "_parent" | "_self" | "_top"

color

'主要'

"错误" | "中性" | "主要" | "次要" | "成功" | "信息" | "警告"

close

false

布尔值 | 部分<ButtonProps>

显示一个关闭按钮以关闭横幅。 { 大小: 'md', 颜色: '中性', 变体: 'ghost' }

closeIcon

appConfig.ui.icons.close

字符串 | 对象

关闭按钮中显示的图标。

ui

{?: ClassNameValue; 容器?: ClassNameValue;?: ClassNameValue; 中心?: ClassNameValue;?: ClassNameValue; 图标?: ClassNameValue; 标题?: ClassNameValue; 操作?: ClassNameValue; 关闭?: ClassNameValue; }

插槽

插槽类型
前置

{}

title

{}

actions

{}

close

{ ui: 任何; }

事件

事件类型
close

[]

主题

app.config.ts
export default defineAppConfig({
  ui: {
    banner: {
      slots: {
        root: [
          'relative z-50 w-full',
          'transition-colors'
        ],
        container: 'flex items-center justify-between gap-3 h-12',
        left: 'hidden lg:flex-1 lg:flex lg:items-center',
        center: 'flex items-center gap-1.5 min-w-0',
        right: 'lg:flex-1 flex items-center justify-end',
        icon: 'size-5 shrink-0 text-inverted pointer-events-none',
        title: 'text-sm text-inverted font-medium truncate',
        actions: 'flex gap-1.5 shrink-0 isolate',
        close: 'text-inverted hover:bg-default/10 focus-visible:bg-default/10 -me-1.5 lg:me-0'
      },
      variants: {
        color: {
          primary: {
            root: 'bg-primary'
          },
          secondary: {
            root: 'bg-secondary'
          },
          success: {
            root: 'bg-success'
          },
          info: {
            root: 'bg-info'
          },
          warning: {
            root: 'bg-warning'
          },
          error: {
            root: 'bg-error'
          },
          neutral: {
            root: 'bg-inverted'
          }
        },
        to: {
          true: ''
        }
      },
      compoundVariants: [
        {
          color: 'primary',
          to: true,
          class: {
            root: 'hover:bg-primary/90'
          }
        },
        {
          color: 'neutral',
          to: true,
          class: {
            root: 'hover:bg-inverted/90'
          }
        }
      ],
      defaultVariants: {
        color: 'primary'
      }
    }
  }
})
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: {
        banner: {
          slots: {
            root: [
              'relative z-50 w-full',
              'transition-colors'
            ],
            container: 'flex items-center justify-between gap-3 h-12',
            left: 'hidden lg:flex-1 lg:flex lg:items-center',
            center: 'flex items-center gap-1.5 min-w-0',
            right: 'lg:flex-1 flex items-center justify-end',
            icon: 'size-5 shrink-0 text-inverted pointer-events-none',
            title: 'text-sm text-inverted font-medium truncate',
            actions: 'flex gap-1.5 shrink-0 isolate',
            close: 'text-inverted hover:bg-default/10 focus-visible:bg-default/10 -me-1.5 lg:me-0'
          },
          variants: {
            color: {
              primary: {
                root: 'bg-primary'
              },
              secondary: {
                root: 'bg-secondary'
              },
              success: {
                root: 'bg-success'
              },
              info: {
                root: 'bg-info'
              },
              warning: {
                root: 'bg-warning'
              },
              error: {
                root: 'bg-error'
              },
              neutral: {
                root: 'bg-inverted'
              }
            },
            to: {
              true: ''
            }
          },
          compoundVariants: [
            {
              color: 'primary',
              to: true,
              class: {
                root: 'hover:bg-primary/90'
              }
            },
            {
              color: 'neutral',
              to: true,
              class: {
                root: 'hover:bg-inverted/90'
              }
            }
          ],
          defaultVariants: {
            color: 'primary'
          }
        }
      }
    })
  ]
})
为便于阅读,compoundVariants 中的某些颜色已省略。请在 GitHub 上查看源代码。

更新日志

5d6e1— 修复:确保 actions 插槽渲染 (#4946)

61b60— 功能:允许传递组件而不是名称 (#4766)

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