mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-22 07:50:36 +01:00
chore(Notification): transform ticker and timer plugins to useTimer composable
This commit is contained in:
@@ -11,7 +11,7 @@
|
|||||||
<div
|
<div
|
||||||
class="z-50 w-full bg-white rounded-lg shadow-lg pointer-events-auto dark:bg-gray-800"
|
class="z-50 w-full bg-white rounded-lg shadow-lg pointer-events-auto dark:bg-gray-800"
|
||||||
@mouseover="onMouseover"
|
@mouseover="onMouseover"
|
||||||
@mouseout="onMouseout"
|
@mouseleave="onMouseleave"
|
||||||
>
|
>
|
||||||
<div class="relative overflow-hidden rounded-lg ring-1 u-ring-gray-200">
|
<div class="relative overflow-hidden rounded-lg ring-1 u-ring-gray-200">
|
||||||
<div class="p-4">
|
<div class="p-4">
|
||||||
@@ -31,7 +31,7 @@
|
|||||||
variant="white"
|
variant="white"
|
||||||
size="xs"
|
size="xs"
|
||||||
class="mt-2"
|
class="mt-2"
|
||||||
@click.stop="cancel"
|
@click.stop="onUndo"
|
||||||
>
|
>
|
||||||
Undo
|
Undo
|
||||||
</Button>
|
</Button>
|
||||||
@@ -39,7 +39,7 @@
|
|||||||
<div class="flex-shrink-0 ml-4">
|
<div class="flex-shrink-0 ml-4">
|
||||||
<button
|
<button
|
||||||
class="transition duration-150 ease-in-out u-text-gray-400 focus:outline-none hover:u-text-gray-500 focus:u-text-gray-500"
|
class="transition duration-150 ease-in-out u-text-gray-400 focus:outline-none hover:u-text-gray-500 focus:u-text-gray-500"
|
||||||
@click.stop="close"
|
@click.stop="onClose"
|
||||||
>
|
>
|
||||||
<span class="sr-only">Close</span>
|
<span class="sr-only">Close</span>
|
||||||
<Icon name="heroicons-solid:x" class="w-5 h-5" />
|
<Icon name="heroicons-solid:x" class="w-5 h-5" />
|
||||||
@@ -56,8 +56,11 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import { ref, computed, onMounted, onUnmounted } from 'vue'
|
||||||
|
|
||||||
import Icon from '../elements/Icon'
|
import Icon from '../elements/Icon'
|
||||||
import Button from '../elements/Button'
|
import Button from '../elements/Button'
|
||||||
|
import { useTimer } from '../../composables/useTimer'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
components: {
|
components: {
|
||||||
@@ -102,84 +105,97 @@ export default {
|
|||||||
default: null
|
default: null
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
data () {
|
emits: ['close'],
|
||||||
return {
|
setup (props, { emit }) {
|
||||||
timer: null,
|
let timer = null
|
||||||
ticker: null,
|
const remaining = ref(props.timeout)
|
||||||
remainingTime: this.timeout
|
|
||||||
}
|
const iconName = computed(() => {
|
||||||
},
|
return props.icon || ({
|
||||||
computed: {
|
|
||||||
iconName () {
|
|
||||||
return this.icon || ({
|
|
||||||
warning: 'heroicons-outline:exclamation-circle',
|
warning: 'heroicons-outline:exclamation-circle',
|
||||||
info: 'heroicons-outline:information-circle',
|
info: 'heroicons-outline:information-circle',
|
||||||
success: 'heroicons-outline:check-circle',
|
success: 'heroicons-outline:check-circle',
|
||||||
error: 'heroicons-outline:x-circle'
|
error: 'heroicons-outline:x-circle'
|
||||||
})[this.type]
|
})[props.type]
|
||||||
},
|
})
|
||||||
iconClass () {
|
|
||||||
|
const iconClass = computed(() => {
|
||||||
return ({
|
return ({
|
||||||
warning: 'text-orange-400',
|
warning: 'text-orange-400',
|
||||||
info: 'text-blue-400',
|
info: 'text-blue-400',
|
||||||
success: 'text-green-400',
|
success: 'text-green-400',
|
||||||
error: 'text-red-400'
|
error: 'text-red-400'
|
||||||
})[this.type] || 'u-text-gray-400'
|
})[props.type] || 'u-text-gray-400'
|
||||||
},
|
})
|
||||||
progressBarStyle () {
|
|
||||||
const remainingPercent = this.remainingTime / this.timeout * 100
|
const progressBarStyle = computed(() => {
|
||||||
return { width: `${remainingPercent}%` }
|
const remainingPercent = remaining.value / props.timeout * 100
|
||||||
}
|
return { width: `${remainingPercent || 0}%` }
|
||||||
},
|
})
|
||||||
mounted () {
|
|
||||||
if (!this.$timer) {
|
function onMouseover () {
|
||||||
return
|
if (timer) {
|
||||||
|
timer.pause()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.timeout) {
|
function onMouseleave () {
|
||||||
this.timer = new this.$timer(() => {
|
if (timer) {
|
||||||
this.close()
|
timer.resume()
|
||||||
this.ticker?.stop()
|
}
|
||||||
}, this.timeout)
|
|
||||||
this.ticker = new this.$ticker(() => {
|
|
||||||
this.remainingTime -= 10
|
|
||||||
}, 10)
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
beforeDestroy () {
|
function onClose () {
|
||||||
if (this.timer) {
|
if (timer) {
|
||||||
this.timer.stop()
|
timer.stop()
|
||||||
this.ticker.stop()
|
}
|
||||||
|
|
||||||
|
if (props.callback) {
|
||||||
|
props.callback()
|
||||||
|
}
|
||||||
|
|
||||||
|
emit('close')
|
||||||
}
|
}
|
||||||
},
|
|
||||||
methods: {
|
function onUndo () {
|
||||||
onMouseover () {
|
if (timer) {
|
||||||
if (this.timer) {
|
timer.stop()
|
||||||
this.timer.pause()
|
|
||||||
this.ticker.stop()
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
onMouseout () {
|
if (props.undo) {
|
||||||
if (this.timer) {
|
props.undo()
|
||||||
this.timer.resume()
|
|
||||||
this.ticker.start()
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
cancel () {
|
emit('close')
|
||||||
if (this.timer) {
|
}
|
||||||
this.timer.stop()
|
|
||||||
this.ticker.stop()
|
onMounted(() => {
|
||||||
|
if (!props.timeout) {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if (this.undo) {
|
|
||||||
this.undo()
|
timer = useTimer(() => {
|
||||||
}
|
onClose()
|
||||||
this.$emit('close')
|
}, props.timeout)
|
||||||
},
|
|
||||||
close () {
|
watchEffect(() => {
|
||||||
if (this.callback) {
|
remaining.value = timer.remaining.value
|
||||||
this.callback()
|
})
|
||||||
}
|
})
|
||||||
this.$emit('close')
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
timer.stop()
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
timer,
|
||||||
|
iconName,
|
||||||
|
iconClass,
|
||||||
|
progressBarStyle,
|
||||||
|
onMouseover,
|
||||||
|
onMouseleave,
|
||||||
|
onClose,
|
||||||
|
onUndo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
64
src/composables/useTimer.ts
Normal file
64
src/composables/useTimer.ts
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
import { Ref, ref, computed } from 'vue-demi'
|
||||||
|
import { useTimestamp } from '@vueuse/core'
|
||||||
|
|
||||||
|
export function useTimer (cb: (...args: unknown[]) => any, interval: number) {
|
||||||
|
let timer: number | null = null
|
||||||
|
const timestamp = useTimestamp({ controls: true })
|
||||||
|
const startTime: Ref<number | null> = ref(null)
|
||||||
|
|
||||||
|
const remaining = computed(() => {
|
||||||
|
if (!startTime.value) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return interval - (timestamp.timestamp.value - startTime.value)
|
||||||
|
})
|
||||||
|
|
||||||
|
function set (...args: unknown[]) {
|
||||||
|
timer = setTimeout(() => {
|
||||||
|
timer = null
|
||||||
|
startTime.value = null
|
||||||
|
// eslint-disable-next-line node/no-callback-literal
|
||||||
|
cb(...args)
|
||||||
|
}, remaining.value) as unknown as number
|
||||||
|
}
|
||||||
|
|
||||||
|
function clear () {
|
||||||
|
if (timer) {
|
||||||
|
clearTimeout(timer)
|
||||||
|
timer = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function start () {
|
||||||
|
startTime.value = Date.now()
|
||||||
|
|
||||||
|
set()
|
||||||
|
}
|
||||||
|
|
||||||
|
function stop () {
|
||||||
|
clear()
|
||||||
|
timestamp.pause()
|
||||||
|
}
|
||||||
|
|
||||||
|
function pause () {
|
||||||
|
clear()
|
||||||
|
timestamp.pause()
|
||||||
|
}
|
||||||
|
|
||||||
|
function resume () {
|
||||||
|
console.log('resume')
|
||||||
|
startTime.value += (Date.now() - timestamp.timestamp.value)
|
||||||
|
timestamp.resume()
|
||||||
|
set()
|
||||||
|
}
|
||||||
|
|
||||||
|
start()
|
||||||
|
|
||||||
|
return {
|
||||||
|
start,
|
||||||
|
stop,
|
||||||
|
pause,
|
||||||
|
resume,
|
||||||
|
remaining
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -176,8 +176,6 @@ export default defineNuxtModule<UiOptions>({
|
|||||||
getContents: () => `/* @unocss-include */ export default ${JSON.stringify(ui)}`
|
getContents: () => `/* @unocss-include */ export default ${JSON.stringify(ui)}`
|
||||||
})
|
})
|
||||||
|
|
||||||
addPlugin(resolve(__dirname, './plugins/ticker.client'))
|
|
||||||
addPlugin(resolve(__dirname, './plugins/timer.client'))
|
|
||||||
addPlugin(resolve(__dirname, './plugins/toast.client'))
|
addPlugin(resolve(__dirname, './plugins/toast.client'))
|
||||||
|
|
||||||
addComponentsDir({
|
addComponentsDir({
|
||||||
@@ -211,6 +209,11 @@ export default defineNuxtModule<UiOptions>({
|
|||||||
watch: false
|
watch: false
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Add composables
|
||||||
|
nuxt.hook('autoImports:dirs', (dirs) => {
|
||||||
|
dirs.push(resolve(__dirname, './composables'))
|
||||||
|
})
|
||||||
|
|
||||||
nuxt.options.css.push(resolve(__dirname, './css/forms.css'))
|
nuxt.options.css.push(resolve(__dirname, './css/forms.css'))
|
||||||
|
|
||||||
nuxt.options.build.transpile.push('@popperjs/core', '@headlessui/vue', '@nuxthq/ui')
|
nuxt.options.build.transpile.push('@popperjs/core', '@headlessui/vue', '@nuxthq/ui')
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
import { defineNuxtPlugin } from '#app'
|
|
||||||
|
|
||||||
function Ticker (callback, interval) {
|
|
||||||
let id
|
|
||||||
|
|
||||||
this.start = function () {
|
|
||||||
id = window.setInterval(callback, interval)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.stop = function () {
|
|
||||||
window.clearInterval(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.start()
|
|
||||||
}
|
|
||||||
|
|
||||||
export default defineNuxtPlugin((nuxtApp) => {
|
|
||||||
nuxtApp.provide('ticker', Ticker)
|
|
||||||
})
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
import { defineNuxtPlugin } from '#app'
|
|
||||||
|
|
||||||
function Timer (callback, delay) {
|
|
||||||
let timerId
|
|
||||||
let start
|
|
||||||
let remaining = delay
|
|
||||||
|
|
||||||
this.pause = function () {
|
|
||||||
window.clearTimeout(timerId)
|
|
||||||
remaining -= new Date() - start
|
|
||||||
}
|
|
||||||
|
|
||||||
this.resume = function () {
|
|
||||||
start = new Date()
|
|
||||||
window.clearTimeout(timerId)
|
|
||||||
timerId = window.setTimeout(callback, remaining)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.reset = function () {
|
|
||||||
start = new Date()
|
|
||||||
window.clearTimeout(timerId)
|
|
||||||
timerId = window.setTimeout(callback, delay)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.stop = function () {
|
|
||||||
window.clearTimeout(timerId)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.resume()
|
|
||||||
}
|
|
||||||
|
|
||||||
export default defineNuxtPlugin((nuxtApp) => {
|
|
||||||
nuxtApp.provide('timer', Timer)
|
|
||||||
})
|
|
||||||
Reference in New Issue
Block a user