Alert
Displays a persistent notice within the page, such as instructions, a risk warning or an operation result. It does not disappear automatically; an optional close button lets users dismiss it.
Import
import { HuiAlert } from '@nscode/handy-ui'
Basic Usage
title sets the title. type defaults to 'info'.
<script setup lang="ts">
import { HuiAlert } from '@nscode/handy-ui'
</script>
<template>
<HuiAlert title="This is a notice" />
</template>
Types
type accepts success, warning, info, error and help. The color and leading icon change accordingly.
<script setup lang="ts">
import { HuiAlert, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiAlert title="Success alert (success)" type="success" />
<HuiAlert title="Warning alert (warning)" type="warning" />
<HuiAlert title="Information alert (info)" type="info" />
<HuiAlert title="Error alert (error)" type="error" />
<HuiAlert title="Help alert (help)" type="help" />
</HuiSpace>
</template>
Dark Effect
effect defaults to 'light', with a pale background and colored icon. Set it to 'dark' for a solid background, white text and no border. All five types support the dark effect:
<script setup lang="ts">
import { HuiAlert, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiAlert title="Dark success alert (success)" type="success" effect="dark" />
<HuiAlert title="Dark warning alert (warning)" type="warning" effect="dark" />
<HuiAlert title="Dark information alert (info)" type="info" effect="dark" />
<HuiAlert title="Dark error alert (error)" type="error" effect="dark" />
<HuiAlert title="Dark help alert (help)" type="help" effect="dark" />
<HuiAlert
title="Dark alerts also support descriptions"
description="The description is also white."
type="success"
effect="dark"
closable
/>
</HuiSpace>
</template>
Description
description appears below the title in a smaller font. When there is a description without a title, the icon and text are vertically centered.
You can also provide the description through the default slot or #description. Priority is #description slot > default slot > description prop. The default slot acts as a fallback and is not shown when #description is provided.
<script setup lang="ts">
import { HuiAlert, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiAlert
title="Notice with a description"
description="Here is a more detailed explanation."
type="success"
/>
<HuiAlert
title="Warning with a description"
description="Here is a more detailed explanation."
type="warning"
/>
<HuiAlert
title="Error with a description"
description="Here is a more detailed explanation."
type="error"
/>
<HuiAlert description="Description without a title" />
<HuiAlert title="Description from the default slot">
<span
>Descriptions can include <a href="https://example.com" target="_blank">links</a> and other
content.</span
>
</HuiAlert>
<HuiAlert>
<template #title>
<strong>Title from a slot</strong>
</template>
<template #description>Description from a slot</template>
</HuiAlert>
<HuiAlert title="Both slots provided" description="The description prop is not shown">
<template #description>The #description slot takes precedence</template>
The default slot is not shown either
</HuiAlert>
</HuiSpace>
</template>
Closable
closable defaults to false. Enable it to show a close button on the right. close-text replaces the close icon with text.
Clicking the close button immediately emits close and starts fading out. After the 0.3-second fade-out ends and the element is removed, after-close is emitted. Further clicks during fade-out do not emit duplicate events.
A dismissed alert does not reappear. The Show all again button below changes key to render the alerts again:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiAlert, HuiButton, HuiSpace } from '@nscode/handy-ui'
const log = ref<string[]>([])
const key = ref(0)
const handleClose = (event: MouseEvent) => {
log.value.push(`close(${event.type})`)
}
const handleAfterClose = () => {
log.value.push('after-close: removed from the page')
}
const reset = () => {
key.value++
log.value = []
}
</script>
<template>
<HuiSpace :key="key" direction="vertical" fill>
<HuiAlert
title="Closable notice"
closable
@close="handleClose"
@after-close="handleAfterClose"
/>
<HuiAlert title="Close button with text" type="success" closable close-text="Got it" />
<HuiAlert
title="Closable notice with a description"
description="The close button aligns with the title."
type="warning"
closable
/>
<HuiAlert title="Closable dark alert" type="error" effect="dark" closable />
</HuiSpace>
<HuiSpace wrap style="margin-top: 12px">
<HuiButton size="small" @click="reset">Show all again</HuiButton>
<span>{{ log.length ? log.join(' → ') : 'Close the first alert to see its events' }}</span>
</HuiSpace>
</template>
Alignment
align controls the alignment of the icon, title and description: left (default), center or right. The earlier center prop is equivalent to align="center" and takes precedence when both are provided. The close button stays at the top right when centered.
<script setup lang="ts">
import { HuiAlert, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiAlert title="Left aligned (left)" description="Left aligned by default" />
<HuiAlert title="Centered (center)" description="align is center" align="center" />
<HuiAlert title="Right aligned (right)" description="align is right" align="right" />
<HuiAlert
title="center prop"
description="Equivalent to align being center"
type="success"
center
/>
<HuiAlert
title="Centered and closable"
description="The close button is at the top right"
type="warning"
center
closable
/>
</HuiSpace>
</template>
Icons
show-icondefaults totrue. Set it tofalseto hide the icon.icon-positionplaces the icon to the'left'(default) or'right'of the content.- The
#iconslot replaces the default icon.icon-positionstill controls its position, andshow-icon="false"also hides this slot.
<script setup lang="ts">
import { HuiAlert, HuiIcon, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiAlert title="Icon hidden" :show-icon="false" />
<HuiAlert title="Icon on the left (left)" type="success" />
<HuiAlert title="Icon on the right (right)" type="success" icon-position="right" />
<HuiAlert title="Custom icon" type="warning">
<template #icon>
<HuiIcon name="star" size="md" />
</template>
</HuiAlert>
<HuiAlert title="Custom icon on the right" type="help" icon-position="right">
<template #icon>
<HuiIcon name="bell" size="md" />
</template>
</HuiAlert>
</HuiSpace>
</template>
Without a Border
border defaults to true. Set it to false to remove the border. All five light-effect types support this option; dark-effect alerts already have no border.
<script setup lang="ts">
import { HuiAlert, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiAlert title="With a border (default)" type="info" />
<HuiAlert title="Borderless success alert" type="success" :border="false" />
<HuiAlert title="Borderless warning alert" type="warning" :border="false" />
<HuiAlert title="Borderless information alert" type="info" :border="false" />
<HuiAlert title="Borderless error alert" type="error" :border="false" />
<HuiAlert title="Borderless help alert" type="help" :border="false" />
</HuiSpace>
</template>
Marquee
Set marquee to true to display the description on one line and scroll it repeatedly from right to left. Each cycle takes 10 seconds, allowing long descriptions to scroll fully into view. The title remains stationary.
<script setup lang="ts">
import { HuiAlert, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiAlert
type="warning"
marquee
description="The system will be offline for maintenance tonight from 22:00 to 24:00. Save your work in advance."
/>
<HuiAlert
title="Announcement"
type="info"
effect="dark"
marquee
description="Only the description scrolls; the title remains fixed above it."
/>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | '' | Title. Can also be provided through the #title slot |
| description | string | '' | Description. Overridden by the #description or default slot |
| type | 'success' | 'warning' | 'info' | 'error' | 'help' | 'info' | Type, which determines the color and icon |
| effect | 'light' | 'dark' | 'light' | Light or dark effect |
| closable | boolean | false | Whether to show a close button |
| closeText | string | '' | Text replacing the close icon when nonempty |
| align | 'left' | 'center' | 'right' | 'left' | Alignment |
| center | boolean | false | Equivalent to align="center"; takes precedence when both are provided |
| showIcon | boolean | true | Whether to show the icon |
| iconPosition | 'left' | 'right' | 'left' | Whether the icon appears to the left or right of the content |
| border | boolean | true | Whether to show a border |
| marquee | boolean | false | Whether the description scrolls repeatedly on a single line |
Events
| Event | Callback Arguments | Description |
|---|---|---|
| close | (event: MouseEvent) | Emitted immediately when the close button is clicked, with the click event |
| after-close | — | Emitted after the fade-out ends and the alert is removed from the page |
Slots
| Slot | Description |
|---|---|
| default | Fallback description content. Not shown when #description is provided |
| title | Title, overriding the title prop |
| description | Description, overriding the default slot and description prop |
| icon | Replaces the default icon. Positioned by iconPosition and hidden when showIcon is false |
Types
import type {
AlertType,
AlertEffect,
AlertAlign,
AlertIconPosition,
AlertProps,
AlertEmits
} from '@nscode/handy-ui'
type AlertType = 'success' | 'warning' | 'info' | 'error' | 'help'
type AlertEffect = 'dark' | 'light'
type AlertAlign = 'left' | 'center' | 'right'
type AlertIconPosition = 'left' | 'right'
interface AlertProps {
title?: string
description?: string
type?: AlertType
closable?: boolean
center?: boolean
showIcon?: boolean
effect?: AlertEffect
closeText?: string
align?: AlertAlign
border?: boolean
marquee?: boolean
iconPosition?: AlertIconPosition
}
interface AlertEmits {
close: [event: MouseEvent]
afterClose: []
}