Animated Alert
Vue 已实现 · 还原验收 0/1 · 上游演示 · 固定版本源码
预览
A little heads up
Your changes are saved automatically as you work.
用法
vue
<script setup lang="ts">
import { Alert, AlertAction, AlertClose, AlertContent, AlertDescription, AlertIcon, AlertTitle, ButtonBase, Tabs } from 'beui-vue'
import { computed, shallowRef } from 'vue'
const messages = {
info: { label: 'Info', title: 'A little heads up', description: 'Your changes are saved automatically as you work.' },
success: { label: 'Success', title: 'All set', description: 'Your changes have been saved.' },
warning: { label: 'Warning', title: 'Almost at the limit', description: "You're getting close to your storage limit. Free up some space before uploading more files." },
destructive: { label: 'Error', title: 'Something went wrong', description: "We couldn't save your changes. Give it another try." },
}
const items = Object.entries(messages).map(([value, { label }]) => ({ value, label }))
const variant = shallowRef<keyof typeof messages>('info')
const open = shallowRef(true)
const showButton = shallowRef<HTMLButtonElement | null>(null)
const message = computed(() => messages[variant.value])
function select(value: string) {
variant.value = value as keyof typeof messages
open.value = true
}
</script>
<template>
<div class="alert-demo">
<Tabs :model-value="variant" :items="items" :labels="{ list: 'Alert variant' }" @update:model-value="select" />
<Alert v-model:open="open" :variant="variant" :return-focus-ref="showButton">
<AlertIcon /><AlertContent>
<AlertTitle>{{ message.title }}</AlertTitle><AlertDescription>{{ message.description }}</AlertDescription>
<AlertAction v-if="variant === 'destructive'"><ButtonBase size="sm" variant="outline" @click="select('success')">Try again</ButtonBase></AlertAction>
</AlertContent><AlertClose />
</Alert>
<button ref="showButton" type="button" :disabled="open" @click="open = true">Show alert</button>
</div>
</template>
<style scoped>
.alert-demo { display: flex; flex-direction: column; align-items: center; gap: 1.5rem; width: 100%; max-width: 28rem; }
.alert-demo > button { padding: .4rem .75rem; border-radius: 999px; font: inherit; font-size: .75rem; cursor: pointer; }
.alert-demo > button:focus-visible { outline: 2px solid var(--beui-ring); }
.alert-demo > button:disabled { opacity: .5; cursor: default; }
</style>API
variant 支持 default、info、success、warning、destructive。用 v-model:open 或 defaultOpen 控制显隐,@open-change 接收变化。组合 AlertIcon、AlertContent、AlertTitle、AlertDescription、AlertAction 和 AlertClose。returnFocusRef 指向关闭后的焦点,labels.dismiss 定制按钮名称。useAlert() 为后代提供响应式状态和 dismiss()。