优化通知组件

master
yutent 2023-04-12 14:22:37 +08:00
parent 3c4f740232
commit 2f4bff1e85
1 changed files with 30 additions and 7 deletions

View File

@ -12,7 +12,13 @@ const ANIMATION = [
{ transform: 'translateX(0)', opacity: 1 } { transform: 'translateX(0)', opacity: 1 }
] ]
const DEFAULT_OPT = { icon: '', image: '', body: '', progress: 0 } const DEFAULT_OPT = {
icon: '',
image: '',
body: '',
progress: 0,
duration: 4500
}
let notifyIns = null let notifyIns = null
@ -38,12 +44,21 @@ class NotifyGroup extends Component {
} }
` `
] ]
mounted() {
this.$on('removed', ev => {
if (this.children.length < 1) {
notifyIns = null
this.remove()
}
})
}
} }
class Notify extends Component { class Notify extends Component {
static props = { static props = {
title: '', title: '',
opt: { icon: '', image: '', body: '', progress: 0 } opt: { ...DEFAULT_OPT }
} }
static styles = [ static styles = [
@ -128,10 +143,13 @@ class Notify extends Component {
` `
] ]
#up = null
close() { close() {
clearTimeout(this.timer) clearTimeout(this.timer)
this.$refs.box.$animate(true).then(_ => { this.$refs.box.$animate(true).then(_ => {
this.remove() this.remove()
this.#up.$emit('removed')
}) })
} }
@ -143,14 +161,19 @@ class Notify extends Component {
n = 100 n = 100
} }
this.opt.progress = n this.opt.progress = n
this.$requestUpdate()
} }
mounted() { mounted() {
this.$refs.box.$animate() this.$refs.box.$animate()
this.#up = this.parentNode
if (this.opt.duration > 0) {
this.timer = setTimeout(() => { this.timer = setTimeout(() => {
this.close() this.close()
}, 3000) }, this.opt.duration)
}
} }
render() { render() {
@ -190,7 +213,7 @@ Notify.reg('notify')
export default function notify( export default function notify(
title = '通知', title = '通知',
{ icon, body, image, progress } { icon, body, image, progress, duration }
) { ) {
// //
let container = notifyIns || document.createElement('wc-notify-group') let container = notifyIns || document.createElement('wc-notify-group')
@ -198,7 +221,7 @@ export default function notify(
elem.title = title elem.title = title
elem.opt = merge({ icon, body, image, progress }) elem.opt = merge({ icon, body, image, progress, duration })
container.append(elem) container.append(elem)