munch-ease-frontend/src/components/AlertToast.vue

85 lines
1.7 KiB
Vue
Raw Normal View History

2024-09-21 22:02:21 +00:00
<template>
2025-10-18 02:08:22 +00:00
<div v-if="showAlert" :class="['alert', type]" @click="dismiss">
2024-09-21 22:02:21 +00:00
<img v-if="icon" :src="icon" alt="Notification icon" />
<div class="message-container">
2025-10-18 02:08:22 +00:00
<h4 class="heading">{{ heading }}</h4>
<p class="message">{{ message }}</p>
2024-09-21 22:02:21 +00:00
</div>
2025-10-18 02:08:22 +00:00
</div>
2024-09-21 22:02:21 +00:00
</template>
<style scoped>
/* Display as a toast, in the bottom right corner */
/* Place the icon to the left for the full height, then have the heading and message stacked to the right */
.alert {
2025-10-18 02:08:22 +00:00
position: fixed;
bottom: 1em;
right: 1em;
display: flex;
align-items: center;
padding: 1em;
border-radius: 5px;
color: white;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
text-align: left;
2024-09-21 22:02:21 +00:00
}
.alert img {
2025-10-18 02:08:22 +00:00
width: 2em;
height: 2em;
margin-right: 1em;
2024-09-21 22:02:21 +00:00
2025-10-18 02:08:22 +00:00
/* Invert svg colors */
filter: invert(1);
2024-09-21 22:02:21 +00:00
}
.alert.error {
2025-10-18 02:08:22 +00:00
background-color: #f44336;
2024-09-21 22:02:21 +00:00
}
.alert.success {
2025-10-18 02:08:22 +00:00
background-color: #4caf50;
2024-09-21 22:02:21 +00:00
}
.alert.info {
2025-10-18 02:08:22 +00:00
background-color: #2196f3;
2024-09-21 22:02:21 +00:00
}
</style>
2025-10-18 02:57:40 +00:00
<script setup>
import { ref, computed, onMounted } from 'vue'
2025-10-18 02:08:22 +00:00
import alert from '@/alert'
2024-09-21 22:02:21 +00:00
const alertIcons = {
2025-10-18 02:08:22 +00:00
error: require('@/assets/notification-error.svg'),
success: require('@/assets/notification-success.svg'),
info: require('@/assets/notification-info.svg'),
}
2024-09-21 22:02:21 +00:00
2025-10-18 02:57:40 +00:00
const showAlert = ref(false)
const heading = ref('')
const message = ref('')
const type = ref('')
const icon = computed(() => (type.value && alertIcons[type.value] ? alertIcons[type.value] : null))
function show({ heading: h, message: m, type: t }) {
heading.value = h
message.value = m
type.value = t
showAlert.value = true
setTimeout(() => {
showAlert.value = false
}, 5000)
2024-09-21 22:02:21 +00:00
}
2025-10-18 02:57:40 +00:00
function dismiss() {
showAlert.value = false
}
onMounted(() => {
alert.subscribe(show)
})
2025-10-18 02:08:22 +00:00
</script>