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

93 lines
1.8 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>
<script>
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
export default {
2025-10-18 02:08:22 +00:00
name: 'AlertToast',
data() {
return {
showAlert: false,
heading: '',
message: '',
type: '',
}
},
computed: {
icon() {
return this.type && alertIcons[this.type] ? alertIcons[this.type] : null
2024-09-21 22:02:21 +00:00
},
2025-10-18 02:08:22 +00:00
},
mounted() {
alert.subscribe(this.show)
},
methods: {
show({ heading, message, type }) {
this.heading = heading
this.message = message
this.type = type
this.showAlert = true
setTimeout(() => {
this.showAlert = false
}, 5000)
2024-09-21 22:02:21 +00:00
},
2025-10-18 02:08:22 +00:00
dismiss() {
this.showAlert = false
2024-09-21 22:02:21 +00:00
},
2025-10-18 02:08:22 +00:00
},
2024-09-21 22:02:21 +00:00
}
2025-10-18 02:08:22 +00:00
</script>