Skip to content

Notification 通知

悬浮出现在页面角落,显示全局的通知提醒消息。

名称分类说明类型默认值
type? 风格通知类型'success' | 'warning' | 'info' | 'danger' | 'error'
position? 风格弹出位置'top-right' | 'top-left' | 'bottom-right' | 'bottom-left''top-right'
offset? 风格距离窗口边缘的偏移量number20
plain? 风格朴素模式(浅色背景,彩色文字)booleanfalse
ghost? 风格幽灵模式(透明背景,仅边框)booleanfalse
duration? 行为显示时间(毫秒),设为 0 则不会自动关闭number3000
showClose? 行为是否显示关闭按钮booleantrue
showTimer? 行为是否显示计时进度条booleantrue
max? 行为同一位置最大可见数量number
onClick? 行为点击回调() => void
onClose? 行为关闭回调() => void
title? 内容标题string
message? 内容通知内容string | VNode
icon? 内容自定义图标string
PxNotification? 方法显示通知(options: NotificationOptions) => NotificationHandler
PxNotification.success? 方法显示成功通知(options: NotificationOptions) => NotificationHandler
PxNotification.warning? 方法显示警告通知(options: NotificationOptions) => NotificationHandler
PxNotification.info? 方法显示信息通知(options: NotificationOptions) => NotificationHandler
PxNotification.danger? 方法显示危险通知(options: NotificationOptions) => NotificationHandler
PxNotification.error? 方法显示错误通知(options: NotificationOptions) => NotificationHandler
PxNotification.closeAll? 方法关闭所有通知(type?: NotificationType) => void

基础用法

<template>
  <px-button @click="showNotification">Show Notification</px-button>
</template>

<script setup lang="ts">
import { PxNotification } from 'sakana-element';

const showNotification = () => {
  PxNotification({
    title: 'Title',
    message: 'This is a notification message',
  });
};
</script>

不同类型

用来显示「成功、警告、消息、错误」类的通知。

<template>
  <div class="demo-notification">
    <px-button @click="showSuccess">Success</px-button>
    <px-button @click="showWarning">Warning</px-button>
    <px-button @click="showInfo">Info</px-button>
    <px-button @click="showDanger">Danger</px-button>
  </div>
</template>

<script setup lang="ts">
import { PxNotification } from 'sakana-element';

const showSuccess = () => {
  PxNotification.success({
    title: 'Success',
    message: 'Operation completed successfully',
  });
};
const showWarning = () => {
  PxNotification.warning({
    title: 'Warning',
    message: 'This is a warning message',
  });
};
const showInfo = () => {
  PxNotification.info({
    title: 'Info',
    message: 'This is an info message',
  });
};
const showDanger = () => {
  PxNotification.danger({
    title: 'Danger',
    message: 'This is a danger message',
  });
};
</script>

<style scoped>
.demo-notification {
  display: flex;
  gap: 12px;
}
</style>

朴素模式

使用 plain 获取浅色背景、彩色文字的样式。

<template>
  <div class="demo-notification">
    <px-button v-for="t in types" :key="t" :type="t === 'danger' ? 'danger' : t" @click="show(t)">
      {{ t }}
    </px-button>
  </div>
</template>

<script setup lang="ts">
import { PxNotification } from 'sakana-element';

const types = ['info', 'success', 'warning', 'danger'] as const;

const show = (type: (typeof types)[number]) => {
  PxNotification({
    title: 'Plain Notification',
    message: `This is a ${type} plain notification.`,
    type,
    plain: true,
  });
};
</script>

<style scoped>
.demo-notification {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}
</style>

幽灵模式

使用 ghost 获取透明背景、仅边框的样式。

<template>
  <div class="demo-notification">
    <px-button v-for="t in types" :key="t" :type="t === 'danger' ? 'danger' : t" @click="show(t)">
      {{ t }}
    </px-button>
  </div>
</template>

<script setup lang="ts">
import { PxNotification } from 'sakana-element';

const types = ['info', 'success', 'warning', 'danger'] as const;

const show = (type: (typeof types)[number]) => {
  PxNotification({
    title: 'Ghost Notification',
    message: `This is a ${type} ghost notification.`,
    type,
    ghost: true,
  });
};
</script>

<style scoped>
.demo-notification {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}
</style>

不同位置

可以让通知从屏幕四角中的任意一角弹出。

<template>
  <div class="demo-notification">
    <px-button @click="showTopRight">Top Right</px-button>
    <px-button @click="showTopLeft">Top Left</px-button>
    <px-button @click="showBottomRight">Bottom Right</px-button>
    <px-button @click="showBottomLeft">Bottom Left</px-button>
  </div>
</template>

<script setup lang="ts">
import { PxNotification } from 'sakana-element';

const showTopRight = () => {
  PxNotification({
    title: 'Top Right',
    message: 'This is a notification',
    position: 'top-right',
  });
};
const showTopLeft = () => {
  PxNotification({
    title: 'Top Left',
    message: 'This is a notification',
    position: 'top-left',
  });
};
const showBottomRight = () => {
  PxNotification({
    title: 'Bottom Right',
    message: 'This is a notification',
    position: 'bottom-right',
  });
};
const showBottomLeft = () => {
  PxNotification({
    title: 'Bottom Left',
    message: 'This is a notification',
    position: 'bottom-left',
  });
};
</script>

<style scoped>
.demo-notification {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}
</style>

自定义时长

设置 duration0 时通知不会自动关闭。

<template>
  <px-button @click="showNotification">Non-auto close</px-button>
</template>

<script setup lang="ts">
import { PxNotification } from 'sakana-element';

const showNotification = () => {
  PxNotification({
    title: 'Notice',
    message: 'This notification will not auto close',
    duration: 0,
  });
};
</script>

计时进度条

底部的细进度条显示剩余时间,通过 showTimer(默认 true)控制。

<template>
  <div class="demo-notification">
    <px-button @click="showTimer">With Timer Bar (default)</px-button>
    <px-button @click="showNoTimer">Without Timer Bar</px-button>
  </div>
</template>

<script setup lang="ts">
import { PxNotification } from 'sakana-element';

const showTimer = () => {
  PxNotification({
    title: 'Timer Bar',
    message: 'Watch the progress bar at the bottom.',
    type: 'info',
    duration: 5000,
  });
};

const showNoTimer = () => {
  PxNotification({
    title: 'No Timer Bar',
    message: 'Timer bar is hidden.',
    type: 'info',
    duration: 5000,
    showTimer: false,
  });
};
</script>

<style scoped>
.demo-notification {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}
</style>

最大显示数量

设置 max 限制同一位置的最大可见通知数量,超出时自动关闭最早的通知。

<template>
  <div class="demo-notification">
    <px-button @click="showNotification">Show Notification (max 3)</px-button>
  </div>
</template>

<script setup lang="ts">
import { PxNotification } from 'sakana-element';

let count = 0;
const showNotification = () => {
  count++;
  PxNotification({
    title: `Notification #${count}`,
    message: 'Only 3 notifications are visible at once.',
    duration: 5000,
    max: 3,
  });
};
</script>

<style scoped>
.demo-notification {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}
</style>