본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

배너

원문 보기 위키 갱신

배너 (Banner)

Banner는 웹사이트 상단에 중요한 정보를 사용자에게 알리는 데 사용하는 컴포넌트예요. title, icon, color 같은 다양한 prop으로 내용을 꾸밀 수 있고, 닫기 버튼이나 액션 버튼도 손쉽게 추가할 수 있어요.

출처: Nuxt UI

본문

사용법 (Usage)

제목 (Title)

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)

color prop을 사용하면 배너의 색상을 바꿀 수 있어요.

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

닫기 (Close)

close prop을 사용하면 배너를 닫을 수 있는 Button을 표시할 수 있어요. 기본값은 false예요.

[!TIP]

닫기 버튼을 클릭하면 close 이벤트가 발생해요.

<script setup lang="ts">
import type { BannerProps } from '@nuxt/ui'

const { id = 'example' } = defineProps<{
  id?: string
  title?: string
  color?: BannerProps['color']
  closeIcon?: string
}>()

function onClose() {
  localStorage.removeItem(`banner-${id}`)

  setTimeout(() => {
    document.querySelector('html')?.classList.remove('hide-banner')
  }, 1000)
}

onBeforeMount(() => {
  localStorage.removeItem(`banner-${id}`)
})
</script>

<template>
  <UBanner
    :id="id"
    :title="title || 'This is a closable banner'"
    :color="color"
    :close-icon="closeIcon"
    close
    @close="onClose"
  />
</template>

[!NOTE]

닫으면 banner-${id}가 local storage에 저장되어 다시 표시되지 않아요. :br 위 예시에서는 banner-example이 local storage에 저장돼요.

[!CAUTION]

닫은 상태를 페이지를 새로 고쳐도 유지하려면 반드시 id prop을 지정해야 해요. id를 명시하지 않으면 현재 세션에서만 숨겨지고, 페이지를 새로 고치면 다시 나타나요.

닫기 아이콘 (Close Icon)

close-icon prop을 사용하면 닫기 버튼의 Icon을 커스터마이즈할 수 있어요. 기본값은 i-lucide-x예요.

<script setup lang="ts">
import type { BannerProps } from '@nuxt/ui'

const { id = 'example' } = defineProps<{
  id?: string
  title?: string
  color?: BannerProps['color']
  closeIcon?: string
}>()

function onClose() {
  localStorage.removeItem(`banner-${id}`)

  setTimeout(() => {
    document.querySelector('html')?.classList.remove('hide-banner')
  }, 1000)
}

onBeforeMount(() => {
  localStorage.removeItem(`banner-${id}`)
})
</script>

<template>
  <UBanner
    :id="id"
    :title="title || 'This is a closable banner'"
    :color="color"
    :close-icon="closeIcon"
    close
    @close="onClose"
  />
</template>

Nuxt:

[!TIP] See: /docs/getting-started/integrations/icons/nuxt#theme

app.config.ts 안의 ui.icons.close 키에서 이 아이콘을 전역으로 커스터마이즈할 수 있어요.

Vue:

[!TIP] See: /docs/getting-started/integrations/icons/vue#theme

vite.config.ts 안의 ui.icons.close 키에서 이 아이콘을 전역으로 커스터마이즈할 수 있어요.

액션 (Actions)

actions prop을 사용하면 배너에 몇 가지 Button 액션을 추가할 수 있어요.

<script setup lang="ts">
import type { ButtonProps } from '@nuxt/ui'

const actions = ref<ButtonProps[]>([
  {
    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>

[!NOTE]

액션 버튼은 기본적으로 color="neutral", size="xs"로 설정돼요. 각 액션 버튼에 직접 값을 전달하면 이 값을 커스터마이즈할 수 있어요.

<NuxtLink> 컴포넌트의 속성을 그대로 전달할 수 있어요. 예를 들어 to, target, rel 등이 있죠.

<template>
  <UBanner to="https://nuxtlabs.com/" target="_blank" title="NuxtLabs is joining Vercel!" color="primary" />
</template>

[!NOTE]

NuxtLink 컴포넌트는 User 컴포넌트에 전달한 나머지 모든 속성을 상속받아요.

예시 (Examples)

app.vue 안에서

Banner 컴포넌트는 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

Props

/**
 * Props for the Banner component
 */
interface BannerProps {
  /**
   * The element or component this component should render as.
   * @default 'div'
   */
  as?: any;
  /**
   * A unique id saved to local storage to remember if the banner has been dismissed.
   * Without an explicit id, the banner will not be persisted and will reappear on page reload.
   */
  id?: string | undefined;
  /**
   * The icon displayed next to the title.
   */
  icon?: any;
  title?: string | undefined;
  /**
   * Display a list of actions next to the title.
   * `{ color: 'neutral', size: 'xs' }`{lang="ts-type"}
   */
  actions?: ButtonProps[] | undefined;
  to?: string | it | et | undefined;
  target?: null | "_blank" | "_parent" | "_self" | "_top" | string & {} | undefined;
  /**
   * @default 'primary'
   */
  color?: "error" | "primary" | "secondary" | "success" | "info" | "warning" | "neutral" | undefined;
  /**
   * Display a close button to dismiss the banner.
   * `{ size: 'md', color: 'neutral', variant: 'ghost' }`{lang="ts-type"}
   * @default false
   */
  close?: boolean | Omit<ButtonProps, LinkPropsKeys> | undefined;
  /**
   * The icon displayed in the close button.
   * @default appConfig.ui.icons.close
   */
  closeIcon?: any;
  ui?: { root?: SlotClass; container?: SlotClass; left?: SlotClass; center?: SlotClass; right?: SlotClass; icon?: SlotClass; title?: SlotClass; actions?: SlotClass; close?: SlotClass; } | undefined;
}

Slots

/**
 * Slots for the Banner component
 */
interface BannerSlots {
  leading(): any;
  title(): any;
  actions(): any;
  close(): any;
}

Emits

/**
 * Emitted events for the Banner component
 */
interface BannerEmits {
  close: (payload: []) => void;
}

테마 (Theme)

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: {
            root: 'outline-(--ui-bg)/25 -outline-offset-3 has-[>a:focus-visible]:outline-3'
          }
        }
      },
      compoundVariants: [
        {
          color: 'primary',
          to: true,
          class: {
            root: 'hover:bg-primary/90'
          }
        },
        {
          color: 'secondary',
          to: true,
          class: {
            root: 'hover:bg-secondary/90'
          }
        },
        {
          color: 'success',
          to: true,
          class: {
            root: 'hover:bg-success/90'
          }
        },
        {
          color: 'info',
          to: true,
          class: {
            root: 'hover:bg-info/90'
          }
        },
        {
          color: 'warning',
          to: true,
          class: {
            root: 'hover:bg-warning/90'
          }
        },
        {
          color: 'error',
          to: true,
          class: {
            root: 'hover:bg-error/90'
          }
        },
        {
          color: 'neutral',
          to: true,
          class: {
            root: 'hover:bg-inverted/90'
          }
        }
      ],
      defaultVariants: {
        color: 'primary'
      }
    }
  }
})

더 알아보기 (Learn more)