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

feat(noti): close on click #12

Merged
merged 2 commits into from
Sep 16, 2023
Merged
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
33 changes: 24 additions & 9 deletions packages/core/src/components/Noti.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { ref } from 'vue'

import { useCounter } from '@vueuse/core'

import useEventBus from '../composables/useEventBus'

import { useNotiContext } from '../composables/useNotiContext'
Expand All @@ -10,10 +11,11 @@ import type { NotiOptions } from '../types'
import AtomicProgress from './AtomicProgress.vue'

export interface NotiTimer {

/**
* Whether the notification timer is active
* interval ID
*/
isActive: boolean
intervalID: NodeJS.Timeout | undefined

/**
* Time left (in milliseconds)
Expand Down Expand Up @@ -45,9 +47,6 @@ const MS_PER_FRAME = 1000 / 60

function triggerCountDown(target: Notification) {
const interval = setInterval(() => {
if (!target.timer.isActive)
clearInterval(interval)

const lastTime = target.timer.endCountDownTimestamp - Date.now()
if (lastTime > 0) {
target.timer.lastTime = lastTime
Expand All @@ -58,6 +57,12 @@ function triggerCountDown(target: Notification) {
clearInterval(interval)
}
}, MS_PER_FRAME)

target.timer.intervalID = interval
}

function clearCountDown(timer: NodeJS.Timeout) {
clearInterval(timer)
}

function publishEvent(options: NotiOptions) {
Expand All @@ -66,8 +71,8 @@ function publishEvent(options: NotiOptions) {
...options,
id: countIncrease(),
timer: {
isActive: true,
lastTime: 0,
intervalID: undefined,
endCountDownTimestamp: 0,
},
}
Expand All @@ -86,18 +91,27 @@ function publishEvent(options: NotiOptions) {
}

function onMouseEnter(val: Notification) {
if (!val.hoverPause)
if (!val.hoverPause || val.timer.intervalID === undefined)
return
val.timer.isActive = false

clearCountDown(val.timer.intervalID)
}
function onMouseLeave(val: Notification) {
if (!val.hoverPause)
return
val.timer.isActive = true

val.timer.endCountDownTimestamp = val.timer.lastTime + Date.now()
triggerCountDown(val)
}

function onClick(val: Notification) {
if (!val.closeOnClick || val.timer.intervalID === undefined)
return

clearCountDown(val.timer.intervalID)
queue.value = queue.value.filter(item => item.id !== val.id)
}

const bus = useEventBus()
bus.on(publishEvent)
</script>
Expand All @@ -111,6 +125,7 @@ bus.on(publishEvent)
class="vue3-noti-group"
@mouseenter="onMouseEnter(item)"
@mouseleave="onMouseLeave(item)"
@click="onClick(item)"
>
{{ item.message }}
<AtomicProgress
Expand Down
6 changes: 6 additions & 0 deletions playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ const options = ref<NotiOptions>({
<label for="hoverPause">hoverPause </label>
<input id="hoverPause" v-model="options.hoverPause" type="checkbox">
</div>

<div class="field-group">
<!-- closeOnClick -->
<label for="closeOnClick">closeOnClick </label>
<input id="closeOnClick" v-model="options.closeOnClick" type="checkbox">
</div>
</div>
</div>
</template>
Expand Down