primevue-mirror/components/toast/Toast.vue

303 lines
7.4 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<Portal>
<div ref="container" :class="containerClass" v-bind="$attrs">
<transition-group name="p-toast-message" tag="div" @enter="onEnter" @leave="onLeave">
2022-12-08 11:04:25 +00:00
<ToastMessage
v-for="msg of messages"
:key="msg.id"
:message="msg"
:template="$slots.message"
:closeIcon="closeIcon"
:infoIcon="infoIcon"
:warnIcon="warnIcon"
:errorIcon="errorIcon"
:successIcon="successIcon"
:closeButtonProps="closeButtonProps"
@close="remove($event)"
/>
2022-09-06 12:03:37 +00:00
</transition-group>
</div>
</Portal>
</template>
<script>
2022-12-08 11:04:25 +00:00
import Portal from 'primevue/portal';
2022-09-06 12:03:37 +00:00
import ToastEventBus from 'primevue/toasteventbus';
2022-12-08 11:04:25 +00:00
import { ObjectUtils, UniqueComponentId, ZIndexUtils } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
import ToastMessage from './ToastMessage.vue';
var messageIdx = 0;
export default {
name: 'Toast',
inheritAttrs: false,
props: {
group: {
type: String,
default: null
},
position: {
type: String,
default: 'top-right'
},
autoZIndex: {
type: Boolean,
default: true
},
baseZIndex: {
type: Number,
default: 0
},
breakpoints: {
type: Object,
default: null
2022-12-08 11:04:25 +00:00
},
closeIcon: {
type: String,
default: 'pi pi-times'
},
infoIcon: {
type: String,
default: 'pi pi-info-circle'
},
warnIcon: {
type: String,
default: 'pi pi-exclamation-triangle'
},
errorIcon: {
type: String,
default: 'pi pi-times'
},
successIcon: {
type: String,
default: 'pi pi-check'
},
closeButtonProps: {
type: null,
default: null
2022-09-06 12:03:37 +00:00
}
},
data() {
return {
messages: []
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
styleElement: null,
mounted() {
ToastEventBus.on('add', this.onAdd);
ToastEventBus.on('remove-group', this.onRemoveGroup);
ToastEventBus.on('remove-all-groups', this.onRemoveAllGroups);
if (this.breakpoints) {
this.createStyle();
}
},
beforeUnmount() {
this.destroyStyle();
if (this.$refs.container && this.autoZIndex) {
ZIndexUtils.clear(this.$refs.container);
}
ToastEventBus.off('add', this.onAdd);
ToastEventBus.off('remove-group', this.onRemoveGroup);
ToastEventBus.off('remove-all-groups', this.onRemoveAllGroups);
},
methods: {
add(message) {
if (message.id == null) {
message.id = messageIdx++;
}
this.messages = [...this.messages, message];
},
remove(message) {
let index = -1;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
for (let i = 0; i < this.messages.length; i++) {
if (this.messages[i] === message) {
index = i;
break;
}
}
this.messages.splice(index, 1);
},
onAdd(message) {
if (this.group == message.group) {
this.add(message);
}
},
onRemoveGroup(group) {
if (this.group === group) {
this.messages = [];
}
},
onRemoveAllGroups() {
this.messages = [];
},
onEnter() {
this.$refs.container.setAttribute(this.attributeSelector, '');
if (this.autoZIndex) {
ZIndexUtils.set('modal', this.$refs.container, this.baseZIndex || this.$primevue.config.zIndex.modal);
}
},
onLeave() {
if (this.$refs.container && this.autoZIndex && ObjectUtils.isEmpty(this.messages)) {
2022-12-08 11:04:25 +00:00
setTimeout(() => {
ZIndexUtils.clear(this.$refs.container);
}, 200);
2022-09-06 12:03:37 +00:00
}
},
createStyle() {
if (!this.styleElement) {
this.styleElement = document.createElement('style');
this.styleElement.type = 'text/css';
document.head.appendChild(this.styleElement);
let innerHTML = '';
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
for (let breakpoint in this.breakpoints) {
let breakpointStyle = '';
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
for (let styleProp in this.breakpoints[breakpoint]) {
breakpointStyle += styleProp + ':' + this.breakpoints[breakpoint][styleProp] + '!important;';
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
innerHTML += `
@media screen and (max-width: ${breakpoint}) {
.p-toast[${this.attributeSelector}] {
${breakpointStyle}
}
}
`;
}
this.styleElement.innerHTML = innerHTML;
}
},
destroyStyle() {
if (this.styleElement) {
document.head.removeChild(this.styleElement);
this.styleElement = null;
}
}
},
computed: {
containerClass() {
2022-09-14 11:26:01 +00:00
return [
'p-toast p-component p-toast-' + this.position,
{
'p-input-filled': this.$primevue.config.inputStyle === 'filled',
'p-ripple-disabled': this.$primevue.config.ripple === false
}
];
2022-09-06 12:03:37 +00:00
},
attributeSelector() {
return UniqueComponentId();
}
2022-09-14 11:26:01 +00:00
},
components: {
ToastMessage: ToastMessage,
Portal: Portal
2022-09-06 12:03:37 +00:00
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>
<style>
.p-toast {
position: fixed;
width: 25rem;
}
.p-toast-message-content {
display: flex;
align-items: flex-start;
}
.p-toast-message-text {
flex: 1 1 auto;
}
.p-toast-top-right {
2022-09-14 11:26:01 +00:00
top: 20px;
right: 20px;
2022-09-06 12:03:37 +00:00
}
.p-toast-top-left {
2022-09-14 11:26:01 +00:00
top: 20px;
left: 20px;
2022-09-06 12:03:37 +00:00
}
.p-toast-bottom-left {
2022-09-14 11:26:01 +00:00
bottom: 20px;
left: 20px;
2022-09-06 12:03:37 +00:00
}
.p-toast-bottom-right {
2022-09-14 11:26:01 +00:00
bottom: 20px;
right: 20px;
2022-09-06 12:03:37 +00:00
}
.p-toast-top-center {
2022-09-14 11:26:01 +00:00
top: 20px;
2022-09-06 12:03:37 +00:00
left: 50%;
transform: translateX(-50%);
}
.p-toast-bottom-center {
2022-09-14 11:26:01 +00:00
bottom: 20px;
2022-09-06 12:03:37 +00:00
left: 50%;
transform: translateX(-50%);
}
.p-toast-center {
2022-09-14 11:26:01 +00:00
left: 50%;
top: 50%;
2022-09-06 12:03:37 +00:00
min-width: 20vw;
transform: translate(-50%, -50%);
}
.p-toast-icon-close {
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
position: relative;
}
.p-toast-icon-close.p-link {
2022-09-14 11:26:01 +00:00
cursor: pointer;
2022-09-06 12:03:37 +00:00
}
/* Animations */
.p-toast-message-enter-from {
opacity: 0;
-webkit-transform: translateY(50%);
-ms-transform: translateY(50%);
transform: translateY(50%);
}
.p-toast-message-leave-from {
max-height: 1000px;
}
.p-toast .p-toast-message.p-toast-message-leave-to {
max-height: 0;
opacity: 0;
margin-bottom: 0;
overflow: hidden;
}
.p-toast-message-enter-active {
2022-09-14 11:26:01 +00:00
-webkit-transition: transform 0.3s, opacity 0.3s;
transition: transform 0.3s, opacity 0.3s;
2022-09-06 12:03:37 +00:00
}
.p-toast-message-leave-active {
2022-09-14 11:26:01 +00:00
-webkit-transition: max-height 0.45s cubic-bezier(0, 1, 0, 1), opacity 0.3s, margin-bottom 0.3s;
transition: max-height 0.45s cubic-bezier(0, 1, 0, 1), opacity 0.3s, margin-bottom 0.3s;
2022-09-06 12:03:37 +00:00
}
</style>