2020-08-04 19:03:20 +08:00
|
|
|
<template>
|
2021-09-04 19:29:28 +08:00
|
|
|
<transition
|
2022-02-08 05:51:03 +08:00
|
|
|
:name="ns.b('fade')"
|
2021-09-04 19:29:28 +08:00
|
|
|
@before-leave="onClose"
|
|
|
|
@after-leave="$emit('destroy')"
|
|
|
|
>
|
2020-08-04 19:03:20 +08:00
|
|
|
<div
|
2020-08-06 17:53:58 +08:00
|
|
|
v-show="visible"
|
2020-08-04 19:03:20 +08:00
|
|
|
:id="id"
|
2022-02-08 05:51:03 +08:00
|
|
|
:class="[ns.b(), customClass, horizontalClass]"
|
2020-08-04 19:03:20 +08:00
|
|
|
:style="positionStyle"
|
|
|
|
role="alert"
|
2021-01-31 18:43:33 +08:00
|
|
|
@mouseenter="clearTimer"
|
|
|
|
@mouseleave="startTimer"
|
|
|
|
@click="onClick"
|
2020-08-04 19:03:20 +08:00
|
|
|
>
|
2022-02-08 05:51:03 +08:00
|
|
|
<el-icon v-if="iconComponent" :class="[ns.e('icon'), typeClass]">
|
2021-10-27 23:17:13 +08:00
|
|
|
<component :is="iconComponent" />
|
|
|
|
</el-icon>
|
2022-02-08 05:51:03 +08:00
|
|
|
<div :class="ns.e('group')">
|
2022-03-14 04:16:11 +08:00
|
|
|
<h2 :class="ns.e('title')" v-text="title" />
|
2021-09-04 19:29:28 +08:00
|
|
|
<div
|
|
|
|
v-show="message"
|
2022-02-08 05:51:03 +08:00
|
|
|
:class="ns.e('content')"
|
2021-09-22 01:19:35 +08:00
|
|
|
:style="!!title ? undefined : { margin: 0 }"
|
2021-09-04 19:29:28 +08:00
|
|
|
>
|
2020-08-04 19:03:20 +08:00
|
|
|
<slot>
|
|
|
|
<p v-if="!dangerouslyUseHTMLString">{{ message }}</p>
|
2022-08-06 15:12:06 +08:00
|
|
|
<!-- Caution here, message could've been compromised, never use user's input as message -->
|
|
|
|
<p v-else v-html="message" />
|
2020-08-04 19:03:20 +08:00
|
|
|
</slot>
|
|
|
|
</div>
|
2022-02-08 05:51:03 +08:00
|
|
|
<el-icon v-if="showClose" :class="ns.e('closeBtn')" @click.stop="close">
|
2022-08-06 15:12:06 +08:00
|
|
|
<Close />
|
2021-10-27 23:17:13 +08:00
|
|
|
</el-icon>
|
2020-08-04 19:03:20 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</transition>
|
|
|
|
</template>
|
2022-08-06 15:12:06 +08:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { computed, onMounted, ref } from 'vue'
|
2021-09-22 01:19:35 +08:00
|
|
|
import { useEventListener, useTimeoutFn } from '@vueuse/core'
|
2022-08-06 15:12:06 +08:00
|
|
|
import { CloseComponents, TypeComponentsMap } from '@element-plus/utils'
|
2022-02-11 11:03:15 +08:00
|
|
|
import { EVENT_CODE } from '@element-plus/constants'
|
2021-10-27 23:17:13 +08:00
|
|
|
import { ElIcon } from '@element-plus/components/icon'
|
2022-02-08 05:51:03 +08:00
|
|
|
import { useNamespace } from '@element-plus/hooks'
|
2022-03-25 15:35:56 +08:00
|
|
|
import { notificationEmits, notificationProps } from './notification'
|
2021-01-31 18:43:33 +08:00
|
|
|
|
2021-09-22 01:19:35 +08:00
|
|
|
import type { CSSProperties } from 'vue'
|
2020-08-04 19:03:20 +08:00
|
|
|
|
2022-08-06 15:12:06 +08:00
|
|
|
defineOptions({
|
2020-08-04 19:03:20 +08:00
|
|
|
name: 'ElNotification',
|
2022-08-06 15:12:06 +08:00
|
|
|
})
|
2021-09-22 01:19:35 +08:00
|
|
|
|
2022-08-06 15:12:06 +08:00
|
|
|
const props = defineProps(notificationProps)
|
|
|
|
defineEmits(notificationEmits)
|
2020-08-04 19:03:20 +08:00
|
|
|
|
2022-08-06 15:12:06 +08:00
|
|
|
const ns = useNamespace('notification')
|
|
|
|
const { Close } = CloseComponents
|
2021-01-31 18:43:33 +08:00
|
|
|
|
2022-08-06 15:12:06 +08:00
|
|
|
const visible = ref(false)
|
|
|
|
let timer: (() => void) | undefined = undefined
|
2021-01-31 18:43:33 +08:00
|
|
|
|
2022-08-06 15:12:06 +08:00
|
|
|
const typeClass = computed(() => {
|
|
|
|
const type = props.type
|
|
|
|
return type && TypeComponentsMap[props.type] ? ns.m(type) : ''
|
|
|
|
})
|
|
|
|
|
|
|
|
const iconComponent = computed(() => {
|
|
|
|
if (!props.type) return ''
|
|
|
|
return TypeComponentsMap[props.type] || props.icon
|
|
|
|
})
|
2021-01-31 18:43:33 +08:00
|
|
|
|
2022-08-06 15:12:06 +08:00
|
|
|
const horizontalClass = computed(() =>
|
|
|
|
props.position.endsWith('right') ? 'right' : 'left'
|
|
|
|
)
|
|
|
|
|
|
|
|
const verticalProperty = computed(() =>
|
|
|
|
props.position.startsWith('top') ? 'top' : 'bottom'
|
|
|
|
)
|
|
|
|
|
|
|
|
const positionStyle = computed<CSSProperties>(() => {
|
|
|
|
return {
|
|
|
|
[verticalProperty.value]: `${props.offset}px`,
|
|
|
|
zIndex: props.zIndex,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
function startTimer() {
|
|
|
|
if (props.duration > 0) {
|
|
|
|
;({ stop: timer } = useTimeoutFn(() => {
|
|
|
|
if (visible.value) close()
|
|
|
|
}, props.duration))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearTimer() {
|
|
|
|
timer?.()
|
|
|
|
}
|
|
|
|
|
|
|
|
function close() {
|
|
|
|
visible.value = false
|
|
|
|
}
|
|
|
|
|
|
|
|
function onKeydown({ code }: KeyboardEvent) {
|
|
|
|
if (code === EVENT_CODE.delete || code === EVENT_CODE.backspace) {
|
|
|
|
clearTimer() // press delete/backspace clear timer
|
|
|
|
} else if (code === EVENT_CODE.esc) {
|
|
|
|
// press esc to close the notification
|
|
|
|
if (visible.value) {
|
|
|
|
close()
|
2021-01-31 18:43:33 +08:00
|
|
|
}
|
2022-08-06 15:12:06 +08:00
|
|
|
} else {
|
|
|
|
startTimer() // resume timer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lifecycle
|
|
|
|
onMounted(() => {
|
|
|
|
startTimer()
|
|
|
|
visible.value = true
|
|
|
|
})
|
|
|
|
|
|
|
|
useEventListener(document, 'keydown', onKeydown)
|
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
visible,
|
|
|
|
/** @description close notification */
|
|
|
|
close,
|
2020-08-04 19:03:20 +08:00
|
|
|
})
|
|
|
|
</script>
|