2020-10-03 20:55:23 +08:00
|
|
|
<template>
|
2021-09-04 19:29:28 +08:00
|
|
|
<transition
|
2022-02-09 00:25:19 +08:00
|
|
|
:name="ns.b('fade')"
|
2021-09-04 19:29:28 +08:00
|
|
|
@before-leave="onClose"
|
|
|
|
@after-leave="$emit('destroy')"
|
|
|
|
>
|
2020-10-03 20:55:23 +08:00
|
|
|
<div
|
|
|
|
v-show="visible"
|
|
|
|
:id="id"
|
2022-06-22 09:01:23 +08:00
|
|
|
ref="messageRef"
|
2020-10-03 20:55:23 +08:00
|
|
|
:class="[
|
2022-02-09 00:25:19 +08:00
|
|
|
ns.b(),
|
|
|
|
{ [ns.m(type)]: type && !icon },
|
|
|
|
ns.is('center', center),
|
|
|
|
ns.is('closable', showClose),
|
2020-10-03 20:55:23 +08:00
|
|
|
customClass,
|
|
|
|
]"
|
|
|
|
:style="customStyle"
|
|
|
|
role="alert"
|
|
|
|
@mouseenter="clearTimer"
|
|
|
|
@mouseleave="startTimer"
|
|
|
|
>
|
2021-11-05 18:55:40 +08:00
|
|
|
<el-badge
|
|
|
|
v-if="repeatNum > 1"
|
|
|
|
:value="repeatNum"
|
2021-12-04 22:16:33 +08:00
|
|
|
:type="badgeType"
|
2022-02-09 00:25:19 +08:00
|
|
|
:class="ns.e('badge')"
|
2022-03-14 04:16:11 +08:00
|
|
|
/>
|
2022-02-09 00:25:19 +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>
|
2020-10-03 20:55:23 +08:00
|
|
|
<slot>
|
2022-02-09 00:25:19 +08:00
|
|
|
<p v-if="!dangerouslyUseHTMLString" :class="ns.e('content')">
|
2021-09-04 19:29:28 +08:00
|
|
|
{{ message }}
|
|
|
|
</p>
|
2020-10-03 20:55:23 +08:00
|
|
|
<!-- Caution here, message could've been compromised, never use user's input as message -->
|
2022-03-14 04:16:11 +08:00
|
|
|
<p v-else :class="ns.e('content')" v-html="message" />
|
2020-10-03 20:55:23 +08:00
|
|
|
</slot>
|
2022-02-09 00:25:19 +08:00
|
|
|
<el-icon v-if="showClose" :class="ns.e('closeBtn')" @click.stop="close">
|
2022-06-21 19:50:16 +08:00
|
|
|
<Close />
|
2021-10-27 23:17:13 +08:00
|
|
|
</el-icon>
|
2020-10-03 20:55:23 +08:00
|
|
|
</div>
|
|
|
|
</transition>
|
|
|
|
</template>
|
2022-06-21 19:50:16 +08:00
|
|
|
|
|
|
|
<script lang="ts" setup>
|
|
|
|
import { computed, onMounted, ref, watch } from 'vue'
|
2022-06-22 09:01:23 +08:00
|
|
|
import { useEventListener, useResizeObserver, useTimeoutFn } from '@vueuse/core'
|
2022-02-11 11:03:15 +08:00
|
|
|
import { TypeComponents, TypeComponentsMap } from '@element-plus/utils'
|
|
|
|
import { EVENT_CODE } from '@element-plus/constants'
|
2021-11-05 18:55:40 +08:00
|
|
|
import ElBadge from '@element-plus/components/badge'
|
2021-10-27 23:17:13 +08:00
|
|
|
import { ElIcon } from '@element-plus/components/icon'
|
2022-02-09 00:25:19 +08:00
|
|
|
import { useNamespace } from '@element-plus/hooks'
|
2021-09-22 01:40:18 +08:00
|
|
|
import { messageEmits, messageProps } from './message'
|
2022-06-22 09:01:23 +08:00
|
|
|
import { getLastOffset } from './instance'
|
2021-12-04 22:16:33 +08:00
|
|
|
import type { BadgeProps } from '@element-plus/components/badge'
|
2021-09-22 01:40:18 +08:00
|
|
|
import type { CSSProperties } from 'vue'
|
|
|
|
|
2022-06-21 19:50:16 +08:00
|
|
|
const { Close } = TypeComponents
|
|
|
|
|
|
|
|
defineOptions({
|
2020-10-03 20:55:23 +08:00
|
|
|
name: 'ElMessage',
|
2022-06-21 19:50:16 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
const props = defineProps(messageProps)
|
|
|
|
defineEmits(messageEmits)
|
|
|
|
|
|
|
|
const ns = useNamespace('message')
|
2022-06-22 09:01:23 +08:00
|
|
|
|
|
|
|
const messageRef = ref<HTMLDivElement>()
|
2022-06-21 19:50:16 +08:00
|
|
|
const visible = ref(false)
|
2022-06-22 09:01:23 +08:00
|
|
|
const height = ref(0)
|
|
|
|
|
2022-06-21 19:50:16 +08:00
|
|
|
let stopTimer: (() => void) | undefined = undefined
|
|
|
|
|
2022-06-22 09:01:23 +08:00
|
|
|
const badgeType = computed<BadgeProps['type']>(() =>
|
|
|
|
props.type ? (props.type === 'error' ? 'danger' : props.type) : 'info'
|
|
|
|
)
|
2022-06-21 19:50:16 +08:00
|
|
|
const typeClass = computed(() => {
|
|
|
|
const type = props.type
|
|
|
|
return { [ns.bm('icon', type)]: type && TypeComponentsMap[type] }
|
|
|
|
})
|
|
|
|
const iconComponent = computed(
|
|
|
|
() => props.icon || TypeComponentsMap[props.type] || ''
|
|
|
|
)
|
|
|
|
|
2022-06-22 09:01:23 +08:00
|
|
|
const lastOffset = computed(() => getLastOffset(props.id))
|
|
|
|
const offset = computed(() => props.offset + lastOffset.value)
|
|
|
|
const bottom = computed((): number => height.value + offset.value)
|
2022-06-21 19:50:16 +08:00
|
|
|
const customStyle = computed<CSSProperties>(() => ({
|
2022-06-22 09:01:23 +08:00
|
|
|
top: `${offset.value}px`,
|
2022-06-21 19:50:16 +08:00
|
|
|
zIndex: props.zIndex,
|
|
|
|
}))
|
|
|
|
|
|
|
|
function startTimer() {
|
|
|
|
if (props.duration === 0) return
|
|
|
|
;({ stop: stopTimer } = useTimeoutFn(() => {
|
|
|
|
close()
|
|
|
|
}, props.duration))
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearTimer() {
|
|
|
|
stopTimer?.()
|
|
|
|
}
|
|
|
|
|
|
|
|
function close() {
|
|
|
|
visible.value = false
|
|
|
|
}
|
|
|
|
|
|
|
|
function keydown({ code }: KeyboardEvent) {
|
|
|
|
if (code === EVENT_CODE.esc) {
|
|
|
|
// press esc to close the message
|
|
|
|
close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
startTimer()
|
|
|
|
visible.value = true
|
|
|
|
})
|
|
|
|
|
|
|
|
watch(
|
|
|
|
() => props.repeatNum,
|
|
|
|
() => {
|
|
|
|
clearTimer()
|
|
|
|
startTimer()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
useEventListener(document, 'keydown', keydown)
|
2021-01-31 18:43:33 +08:00
|
|
|
|
2022-06-22 09:01:23 +08:00
|
|
|
useResizeObserver(messageRef, () => {
|
|
|
|
height.value = messageRef.value!.getBoundingClientRect().height
|
|
|
|
})
|
|
|
|
|
2022-06-21 19:50:16 +08:00
|
|
|
defineExpose({
|
|
|
|
visible,
|
2022-06-22 09:01:23 +08:00
|
|
|
bottom,
|
2022-06-21 19:50:16 +08:00
|
|
|
close,
|
2020-10-03 20:55:23 +08:00
|
|
|
})
|
|
|
|
</script>
|