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

79 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>
2025-10-18 03:12:27 +00:00
import { computed, watch } from 'vue'
import { useAlert } from '@/composables/useAlert'
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 03:12:27 +00:00
const { current, clear, scheduleAutoDismiss } = useAlert()
2025-10-18 02:57:40 +00:00
2025-10-18 03:12:27 +00:00
const showAlert = computed(() => !!current.value)
const heading = computed(() => current.value?.heading ?? '')
const message = computed(() => current.value?.message ?? '')
const type = computed(() => current.value?.type ?? '')
2025-10-18 02:57:40 +00:00
const icon = computed(() => (type.value && alertIcons[type.value] ? alertIcons[type.value] : null))
function dismiss() {
2025-10-18 03:12:27 +00:00
clear()
2025-10-18 02:57:40 +00:00
}
2025-10-18 03:12:27 +00:00
watch(
() => current.value?._ts,
(ts) => {
if (ts) scheduleAutoDismiss(5000)
}
)
2025-10-18 02:08:22 +00:00
</script>