BannerPRO

在你的网站顶部显示一个横幅,用于向用户通知重要信息。

用法

标题

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

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

Icon

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

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

颜色

使用 color prop 更改横幅的颜色。

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

关闭

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

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

关闭图标

使用 close-icon prop 自定义关闭按钮 图标。默认为 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 prop 为横幅添加一些 按钮 操作。

<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://github.com/nuxt/ui-pro"
    target="_blank"
    title="Purchase Nuxt UI Pro and get access to all components."
    color="primary"
  />
</template>
NuxtLink 组件将继承你传递给 User 组件的所有其他属性。

示例

app.vue

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

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

    <UHeader />

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

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

API

Props

Prop默认值类型
as

'div'

any

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

icon

string

标题旁边显示的图标。

close

false

boolean | Partial<ButtonProps>

显示一个关闭按钮以关闭横幅。 { size: 'md', color: 'neutral', variant: 'ghost' }

color

'primary'

"error" | "primary" | "secondary" | "success" | "info" | "warning" | "neutral"

target

null | "_blank" | "_parent" | "_self" | "_top" | string & {}

to

string | RouteLocationAsRelativeGeneric | RouteLocationAsPathGeneric

title

string

id

'1'

string

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

actions

ButtonProps[]

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

关闭图标

appConfig.ui.icons.close

string

关闭按钮中显示的图标。

UI 配置

{ root?: ClassNameValue; container?: ClassNameValue; left?: ClassNameValue; center?: ClassNameValue; right?: ClassNameValue; icon?: ClassNameValue; title?: ClassNameValue; actions?: ClassNameValue; close?: ClassNameValue; }

插槽

插槽类型
leading

{}

title

{}

actions

{}

close

{ ui: any; }

事件

事件类型
close

any[]

主题

app.config.ts
export default defineAppConfig({
  uiPro: {
    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({
      uiPro: {
        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 uiPro from '@nuxt/ui-pro/vite'

export default defineConfig({
  plugins: [
    vue(),
    uiPro({
      uiPro: {
        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 上的源代码。