Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements "pause on hover" Toast feature & exposes onMouseEnter and onMouseLeave callback props #6844

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/primevue/src/toast/BaseToast.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ export default {
closeButtonProps: {
type: null,
default: null
},
onMouseEnter: {
type: Function,
default: undefined
},
onMouseLeave: {
type: Function,
default: undefined
}
},
style: ToastStyle,
Expand Down
8 changes: 8 additions & 0 deletions packages/primevue/src/toast/Toast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@ export interface ToastProps {
* @defaultValue false
*/
unstyled?: boolean;
/**
* Used to specify a callback function to be run when the @mouseenter event is fired on the message component.
*/
onMouseEnter?: Function | undefined;
/**
* Used to specify a callback function to be run when the @mouseleave event is fired on the message component.
*/
onMouseLeave?: Function | undefined;
}

/**
Expand Down
35 changes: 31 additions & 4 deletions packages/primevue/src/toast/ToastMessage.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div :class="[cx('message'), message.styleClass]" role="alert" aria-live="assertive" aria-atomic="true" v-bind="ptm('message')">
<div :class="[cx('message'), message.styleClass]" role="alert" aria-live="assertive" aria-atomic="true" v-bind="ptm('message')" @mouseenter="onMouseEnter" @mouseleave="onMouseLeave">
<component v-if="templates.container" :is="templates.container" :message="message" :closeCallback="onCloseClick" />
<div v-else :class="[cx('messageContent'), message.contentStyleClass]" v-bind="ptm('messageContent')">
<template v-if="!templates.message">
Expand Down Expand Up @@ -34,6 +34,8 @@ export default {
extends: BaseComponent,
emits: ['close'],
closeTimeout: null,
createdAt: null,
lifeRemaining: null,
props: {
message: {
type: null,
Expand Down Expand Up @@ -70,15 +72,20 @@ export default {
},
mounted() {
if (this.message.life) {
this.closeTimeout = setTimeout(() => {
this.close({ message: this.message, type: 'life-end' });
}, this.message.life);
this.lifeRemaining = this.message.life;
this.startTimeout();
}
},
beforeUnmount() {
this.clearCloseTimeout();
},
methods: {
startTimeout() {
this.createdAt = new Date().valueOf();
this.closeTimeout = setTimeout(() => {
this.close({ message: this.message, type: 'life-end' });
}, this.lifeRemaining);
},
close(params) {
this.$emit('close', params);
},
Expand All @@ -91,6 +98,26 @@ export default {
clearTimeout(this.closeTimeout);
this.closeTimeout = null;
}
},
onMouseEnter(event) {
this.props.onMouseEnter && this.props.onMouseEnter(event);
if (event.defaultPrevented) {
return;
}
if (this.message.life) {
this.lifeRemaining = this.createdAt + this.lifeRemaining - Date().valueOf();
this.createdAt = null;
this.clearCloseTimeout();
}
},
onMouseLeave(event) {
this.props.onMouseLeave && this.props.onMouseLeave(event);
if (event.defaultPrevented) {
return;
}
if (this.message.life) {
this.startTimeout();
}
}
},
computed: {
Expand Down
Loading