koel/resources/assets/js/components/ui/message-toaster/MessageToaster.vue

72 lines
1.9 KiB
Vue
Raw Normal View History

<template>
2024-04-26 14:34:28 +00:00
<div ref="root" class="popover">
<TransitionGroup class="flex flex-col items-end gap-2" name="toast" tag="ul">
<li v-for="message in messages" :key="message.id">
<MessageToast :message="message" @dismiss="removeMessage(message)" />
</li>
</TransitionGroup>
</div>
</template>
<script lang="ts" setup>
2024-04-26 14:34:28 +00:00
import { onMounted, ref } from 'vue'
2023-04-17 20:13:11 +00:00
import { uuid } from '@/utils'
2024-04-04 22:20:42 +00:00
import MessageToast from '@/components/ui/message-toaster/MessageToast.vue'
2024-04-26 14:34:28 +00:00
const root = ref<HTMLDivElement & {
popover?: 'manual' | 'auto'
2024-04-26 14:34:28 +00:00
showPopover?: () => void
}>()
const messages = ref<ToastMessage[]>([])
const addMessage = (type: 'info' | 'success' | 'warning' | 'danger', content: string, timeout = 5) => {
messages.value.push({
type,
content,
2023-04-17 20:13:11 +00:00
timeout,
id: uuid(),
})
}
2024-01-24 22:39:47 +00:00
const removeMessage = (message: ToastMessage) => (messages.value = messages.value.filter(({ id }) => id !== message.id))
const info = (content: string, timeout?: number) => addMessage('info', content, timeout)
const success = (content: string, timeout?: number) => addMessage('success', content, timeout)
const warning = (content: string, timeout?: number) => addMessage('warning', content, timeout)
const error = (content: string, timeout?: number) => addMessage('danger', content, timeout)
2024-04-26 14:34:28 +00:00
onMounted(() => {
if (!root.value) {
return
}
2024-04-26 14:34:28 +00:00
root.value.popover = 'manual'
root.value.showPopover?.()
})
defineExpose({ info, success, warning, error })
</script>
2024-04-04 20:13:35 +00:00
<style lang="postcss" scoped>
.popover,
.popover:popover-open {
2024-04-26 14:34:28 +00:00
inset: unset;
@apply fixed top-3 right-0 p-0 bg-transparent m-0 overflow-visible border-0 backdrop:bg-transparent;
}
.toast-enter-active {
2024-04-04 22:20:42 +00:00
@apply opacity-100 transition-all duration-200 ease-in;
}
.toast-leave-active {
2024-04-04 22:20:42 +00:00
@apply opacity-0 transition-all duration-200 ease-out;
}
.toast-enter-from,
.toast-leave-to {
2024-04-04 22:20:42 +00:00
@apply opacity-0;
transform: translateX(100px);
}
</style>